Automatically translate files uploaded by PloneFlashUpload
Project description
Full readme with doctests in slc/autotranslate/README.txt
Introduction
This product adds a boolean ‘autoTranslateUploadedFiles’ on all BaseFolder based folders, which enables any uploaded files to be automatically translated.
‘Translation’ here merely refers to a procedure whereby the file’s language is identified and its language code set accordingly (via setLanguage).
The language that the file will be translated into, is determined form the file’s name. The file name must be prefixed or suffixed with a language code, followed (or preceded) with an underscore.
The following are valid file names:
de_factsheet.pdf
factsheet_de.pdf
How To Use (Doc Tests):
First, we import everything we need:
>>> from z3c.widget.flashupload.interfaces import FlashUploadedEvent >>> from Products.Archetypes.event import ObjectInitializedEvent >>> from zope import event
We will create the folder in which we upload our test files inside the test user’s Member folder.
>>> self.folder.invokeFactory('Folder', 'publications') 'publications' >>> folder = self.folder._getOb('publications')
To enable the autotranslation feature, we must set the ‘autoTranslateUploadedFiles’ field on the parent folder.
This is a field added via schema-extension, so we cannot use an Archetypes generated mutator.
>>> folder.Schema().get('autoTranslateUploadedFiles', True).set(folder, True)
Now we simulate the uploading of 2 files via PloneFlashUpload. Each time we create a file inside our folder, and then call the FlashUploadedEvent.
>>> for fn in ['en_file.txt', 'de_file.txt']: ... fid = folder.invokeFactory('File', fn) ... file = getattr(folder, fid) ... f = open('src/slc.autotranslate/slc/autotranslate/tests/files/%s' % fn) ... file.setFile(f) ... event.notify(FlashUploadedEvent(file))
Let’s see if our uploaded files were set correctly to their indicated languages, and also that the canonical was set properly:
>>> file = getattr(folder, 'en_file.txt') >>> file.getLanguage() 'en'>>> file.getCanonical() <ATFile at /plone/Members/test_user_1_/publications/en_file.txt>>>> file = getattr(folder, 'de_file.txt')>>> file.getLanguage() 'de'>>> file.getCanonical() <ATFile at /plone/Members/test_user_1_/publications/en_file.txt>
File names that end with the language code (as apposed to it being prefixed) are also valid. We’ll test now that this is indeed the case.
First we again upload a file, this time file_es.txt (Spanish):
>>> fid = folder.invokeFactory('File', 'file_es.txt') >>> file = getattr(folder, fid) >>> f = open('src/slc.autotranslate/slc/autotranslate/tests/files/file_es.txt') >>> file.setFile(f)
Now we fire the event. This time, we use Archetypes’ ObjectInitializedEvent, to show that normally added (as opposed to PloneFlashUpload) files are also translated.
>>> event.notify(ObjectInitializedEvent(file))
Let’s test that the file’s language and the canonical was set correctly:
>>> file = getattr(folder, 'file_es.txt') >>> file.getLanguage() 'es'>>> file.getCanonical() <ATFile at /plone/Members/test_user_1_/publications/en_file.txt>
For additional doctests, see the tests/doctests.txt folder.
Change history
1.5.4 (2010-04-01)
Bugix: Images don’t have the getFile method, using getFilename instead as this works for both files and images (deroiste)
1.5.3 (2010-02-10)
Bugfix for my recently added clause: As cononical might be None, we have to consider this possibility in the clause (thomasw)
If for whatever reason translate_file is called twice on the same file, it can happen that the file is no longer present in the parent, but was already moved to a translation of the parent. Therefore make sure the file is still there before trying to delete it (thomasw)
Prevent calling translate_file twice on the same file by setting a flag. This can happen if a file is flash-uploaded. First, the IObjectInitializedEvent is triggered, then FlashUploadedEvent. (thomasw)
1.5.2 (2010-01-28)
Added a clause to translate_file, that prevents trying to add a translation reference if it exists already (causing an AlreadyTranslated err) (thomasw)
1.5.1 (2009-12-10)
Small change in default view configuration (jcbrand)
1.5 (2009-11-20)
Now works with Products.PloneFlashUpload = 1.3b1; also backwards compatible (thomasw)
1.4.3 (2009-10-22)
call unmarkCreationFlag on an uploaded file; otherwise a PFU uploaded file might trigger an IObjectInitializedEvent upon first editing, leading to an AlreadyTranslated error (thomasw)
1.4.2 (2009-10-22)
Add more detailed logging output (jcbrand)
Fix a name in split_filename where files with base names of 2 characters weren’t processed correctly (jcbrand)
slc.autotranslate 1.4.1 (2009-09-10)
Call the LinguaPlone events before and after setting the language for an uploaded file. (jcbrand)
Use addTranslationReference instead of addReference (jcbrand)
Copy over the language independent values from the canonical (jcbrand)
slc.autotranslate 1.4 (2009-09-09)
Search for existing translations also in translated parent folders (jcbrand)
Refactored events and moved ‘translate_file’ method to new module utils.py (jcbrand)
Added with new utility methods to utils.py (jcbrand)
Extend doctests for the new use-cases. (jcbrand)
slc.autotranslate 1.3 (2009-08-26)
In case of CopyError, manually set the file’s language and copy and paste it to the appropriate language version parent. (jcbrand)
Extend the doctests to test for more use-cases. (jcbrand)
slc.autotranslate 1.2 (2009-08-24)
Add interlude as package dependency
slc.autotranslate 1.1 (2009-08-21)
Use setLanguage instead of addTranslation (jcbrand)
Properly set the canonical and translation references (jcbrand)
Add more extensive tests (jcbrnad)
slc.autotranslate 1.0 (2009-08-13)
Initial release. (jcbrand)
Contributors
Jan-Carel Brand (Syslab.com GmbH)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
File details
Details for the file slc.autotranslate-1.5.4.tar.gz
.
File metadata
- Download URL: slc.autotranslate-1.5.4.tar.gz
- Upload date:
- Size: 12.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | fd63d36ad10b538d83a57e9907b0cecda83171dc06d84dde817160b9edf058d7 |
|
MD5 | 66cf8f23fe1fdfda5295cbced3e5cffd |
|
BLAKE2b-256 | 055d9fbae02f0b4c3621fdbe763ea62625620368b4f97c1d746f4a63071156d8 |