Automatically translate files uploaded by PloneFlashUpload
Full readme with doctests in slc/autotranslate/README.txt
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:
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.
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.
- Bugix: Images don’t have the getFile method, using getFilename instead as this works for both files and images (deroiste)
- 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)
- Added a clause to translate_file, that prevents trying to add a translation reference if it exists already (causing an AlreadyTranslated err) (thomasw)
- Small change in default view configuration (jcbrand)
- Now works with Products.PloneFlashUpload = 1.3b1; also backwards compatible (thomasw)
- call unmarkCreationFlag on an uploaded file; otherwise a PFU uploaded file might trigger an IObjectInitializedEvent upon first editing, leading to an AlreadyTranslated error (thomasw)
- 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)
- Jan-Carel Brand (Syslab.com GmbH)