setup.py

Plain Text icon setup.py — Plain Text, 5 KB (5234 bytes)

Dateiinhalt

import os
import urllib2
import random
import loremipsum

from zope.component import getMultiAdapter
from Acquisition import aq_inner
from Products.Five.browser import BrowserView
from Products.CMFPlone.factory import addPloneSite
from DateTime.DateTime import DateTime
from Products.CMFCore.utils import getToolByName
from plone.i18n.normalizer.de import Normalizer

level2pt = {
    0: 'Subsidiary',
    1: 'Folder',
    2: 'SubsidiarySection',
}

def gen_paragraphs(num=3):
    return u'/'.join([p[2] for p in loremipsum.Generator().generate_paragraphs(num)])

def gen_sentence():
    return loremipsum.Generator().generate_sentence()[-1]

def gen_sentences(length=80):
    return u'/'.join([s[2] for s in loremipsum.Generator().generate_sentences(length)])

def random_image(width, height):
    url = 'http://lorempixel.com/%d/%d/' % (width, height)
    return urllib2.urlopen(url).read()

def invokeFactory(folder, portal_type, title=None):
    pt = getToolByName(folder, 'plone_utils')
    obj_title = title
    if not obj_title:
        obj_title = gen_sentence()
    obj_id = pt.normalizeString(obj_title)
    if obj_id in folder.objectIds():
        obj_id += str(random.randint(0,100))
    folder.invokeFactory(portal_type, id=obj_id, title=obj_title)
    folder[obj_id].reindexObject()
    folder[obj_id].setDescription(gen_paragraphs(1))
    return folder[obj_id]

class Setup(BrowserView):

    def setupSite(self):
        portal_id = 'plone-%s' % DateTime().strftime('%d.%m.%y-%H%M%S')
        profiles = ['plonetheme.sunburst:default', 'vs.policy:default']
        addPloneSite(self.context, portal_id, create_userfolder=True, extension_ids=profiles)
        site = self.context[portal_id]
        if 'content' in self.request.form:
            self.setupContent(site)
        self.request.response.redirect(self.context.getId() + '/' + portal_id)

    def setupSubsidiary(self, subsidiary_name, languages):
        pt = getToolByName(self.context, 'plone_utils')
        subsidiary_id = pt.normalizeString(subsidiary_name)
        subsidiary = invokeFactory(self.context, 'Subsidiary', subsidiary_name)
        subsidiary.setLanguage('')
        subsidiary.indexObject()
        subsidiary.restrictedTraverse('@@language-setup-folders')()
        return subsidiary

    def setupContent(self, site):
        self.installStructure(site)
        self.installGlossary(site)
        self.installDownloads(site)
        self.installAssets(site)

    def installStructure(self, site):

        site.restrictedTraverse('setup-subsidiary')('de')

        fname = os.path.join(os.path.dirname(__file__), '..', 'data', 'structure.txt')        
        for line in file(fname):
            line = line.strip()
            if not line:
                continue

            if ':' in line:
                path, portal_type = line.split(':')
            else:
                path = line
                portal_type = 'Document'

            language = 'de'
            if line.startswith('en/'):
                language = 'en'

            current = site
            level = 0
            for c in path.split('/')[:-1]:
                if not c in current.objectIds():
                    current.invokeFactory(level2pt.get(level, 'Folder'), id=c, title=c.capitalize())
                current = current[c]
                level += 1
                current.setLanguage(language)
                current.reindexObject()

            id = path.split('/')[-1]
            if id in current.objectIds():
                current.manage_delObjects(id)
            current.invokeFactory(portal_type, id=id, title=id.capitalize())
            content = current[id]
            content.reindexObject()
            content.setLanguage(language)

    def installGlossary(self, site):
        service = site.restrictedTraverse('de/service')
        glossary = invokeFactory(service, 'PloneGlossary', 'Glossar')
        for i in range(100):
            term = invokeFactory(glossary, 'PloneGlossaryDefinition')
            term.setDefinition(gen_paragraphs(2))
            term.reindexObject()

    def installDownloads(self, site):
        pdf_filename = os.path.join(os.path.dirname(__file__), '..', 'data', 'prospekt.pdf')        
        pdf_data = file(pdf_filename, 'rb').read()
        service = site.restrictedTraverse('de/service')
        dl = invokeFactory(service, 'Folder', 'Downloads')
        for i in range(10):
            title = gen_sentence()
            id = Normalizer().normalize(unicode(title, 'utf-8').lower())
            download = invokeFactory(dl, 'File')
            download.setFile(pdf_data)
            download.reindexObject()


    def installAssets(self, site):

        service = site.restrictedTraverse('de/service')
        assets = invokeFactory(service, 'Folder', 'Assets')
        for width,height in ((200,200), (400,400), (600, 400), (800, 600), (800,800), (1024, 768)):
            imagefolder_id = '%sx%s' % (width, height)
            images = invokeFactory(assets, 'Folder', imagefolder_id)
            for i in range(20):
                img = invokeFactory(images, 'Image')
                img.setImage(random_image(width, height))
                img.reindexObject()