示例#1
0
def translateExhibitions(language="en"):
    import plone.api
    import transaction
    from plone.app.multilingual.interfaces import ITranslationManager
    from zope.event import notify
    from zope.lifecycleevent import ObjectModifiedEvent

    container_path = "/nl/nu-en-verwacht/activiteiten"

    with plone.api.env.adopt_user(username="******"):
        container = plone.api.content.get(path=container_path)

        total = len(list(container))
        curr = 0

        for _id in list(container):
            curr += 1
            print "Translating Exhibition %s / %s to '%s'" %(curr, total, language)
            exhibition = container[_id]
            if exhibition.portal_type == "Event":
                if not ITranslationManager(exhibition).has_translation(language):
                    ITranslationManager(exhibition).add_translation(language)
                    exhibition_translated = ITranslationManager(exhibition).get_translation(language)
                    exhibition_translated.title = exhibition.title
                    exhibition_translated.start = exhibition.start
                    exhibition_translated.end = exhibition.end
                    exhibition_translated.description = exhibition.description
                    exhibition_translated.text = exhibition.text
                    exhibition_translated.whole_day = exhibition.whole_day
                    exhibition_translated.portal_workflow.doActionFor(exhibition_translated, "publish", comment="published")
                    exhibition_translated.reindexObject()
                    transaction.get().commit()
                    if hasattr(exhibition, 'slideshow'):
                        slideshow = exhibition['slideshow']
                        ITranslationManager(slideshow).add_translation(language)
                        slideshow_trans = ITranslationManager(slideshow).get_translation(language)
                        slideshow_trans.title = slideshow.title
                        slideshow_trans.portal_workflow.doActionFor(slideshow_trans, "publish", comment="Slideshow published")
                        
                        for sitem in slideshow:
                            if slideshow[sitem].portal_type == "Image":
                                if not ITranslationManager(slideshow[sitem]).has_translation(language):
                                    ITranslationManager(slideshow[sitem]).add_translation(language)
                                    trans = ITranslationManager(slideshow[sitem]).get_translation(language)
                                    trans.image = slideshow[sitem].image
                        exhibition_translated.reindexObject()
                        exhibition_translated.reindexObject(idxs=["hasMedia"])
                        exhibition_translated.reindexObject(idxs=["leadMedia"])
                        notify(ObjectModifiedEvent(exhibition_translated))

                    print "Translation added for Exhibition '%s'" %(exhibition.title)
                else:
                    print "Exhibition '%s' already has a translation to '%s'" %(exhibition.title, language)
                    print "- Translating slideshow"
                    exhibition_translated = ITranslationManager(exhibition).get_translation(language)
                    if hasattr(exhibition, 'slideshow'):
                        slideshow = exhibition['slideshow']
                        if ITranslationManager(slideshow).has_translation(language):
                            print "-- Slideshow alredy has a translation."
                            slideshow_trans = ITranslationManager(slideshow).get_translation(language)
                            slideshow_trans.title = slideshow.title
                            try:
                                slideshow_trans.portal_workflow.doActionFor(slideshow_trans, "publish", comment="Slideshow published")
                            except:
                                pass

                            print "--- Translating images"
                            for sitem in slideshow:
                                if slideshow[sitem].portal_type == "Image":
                                    if not ITranslationManager(slideshow[sitem]).has_translation(language):
                                        ITranslationManager(slideshow[sitem]).add_translation(language)
                                        trans = ITranslationManager(slideshow[sitem]).get_translation(language)
                                        trans.image = slideshow[sitem].image
                            exhibition_translated.reindexObject()
                            exhibition_translated.reindexObject(idxs=["hasMedia"])
                            exhibition_translated.reindexObject(idxs=["leadMedia"])
                            notify(ObjectModifiedEvent(exhibition_translated))
                            transaction.get().commit()
                        else:
                            print "-- Slideshow doesn't have a translation. Translating slideshow folder."
                            ITranslationManager(slideshow).add_translation(language)
                            slideshow_trans = ITranslationManager(slideshow).get_translation(language)
                            slideshow_trans.title = slideshow.title
                            try:
                                slideshow_trans.portal_workflow.doActionFor(slideshow_trans, "publish", comment="Slideshow published")
                            except:
                                pass

                            print "--- Translating images."
                            for sitem in slideshow:
                                if slideshow[sitem].portal_type == "Image":
                                    if not ITranslationManager(slideshow[sitem]).has_translation(language):
                                        ITranslationManager(slideshow[sitem]).add_translation(language)
                                        trans = ITranslationManager(slideshow[sitem]).get_translation(language)
                                        trans.image = slideshow[sitem].image
                            exhibition_translated.reindexObject()
                            exhibition_translated.reindexObject(idxs=["hasMedia"])
                            exhibition_translated.reindexObject(idxs=["leadMedia"])
                            notify(ObjectModifiedEvent(exhibition_translated))
                            transaction.get().commit()
                    print "Slideshow and images for Exhibition '%s' are translated" %(exhibition.title)
    return True