Exemple #1
0
    def handle_request(self, target=None, fields=None):
        #XXX all of the errors that are reported back here are not going
        # through the translation machinery
        putils = getToolByName(self.context, 'plone_utils')
        self.request.set('__initialize_project__', True)

        self.errors = {}
        title = self.request.form.get('project_title')
        title = strip_extra_whitespace(title)
        if not isinstance(title, unicode):
            title = unicode(title, 'utf-8')
        self.request.form['project_title'] = title

        if not valid_title(title):
            self.errors['project_title'] = 'The name must contain 2 or more characters.'

        id_ = self.request.form.get('projid')
        if not valid_id(id_):
            self.errors['id'] = 'The url must contain 2 or more characters; ' + \
                'only A-Z, 0-9 and "-" are valid characters.'
        else:
            id_ = putils.normalizeString(id_)
            if self.context.has_key(id_):
                self.errors['id'] = 'The requested url is already taken.'

        # Give plugin viewlets a chance to validate. We don't have a
        # project yet, so they'll have to tolerate validating with the
        # project container as the context.
        viewlet_mgr = getMultiAdapter((self.context, self.request, self),
                                      name='opencore.proj_prefs')
        if not hasattr(viewlet_mgr, 'viewlets'):
            viewlet_mgr.update()
        viewlets = viewlet_mgr.viewlets
        for viewlet in viewlets:
            if hasattr(viewlet, 'validate'):
                self.errors.update(viewlet.validate())

        # XXX TO DO: handle featurelets, just like in preferences.py

        if self.errors:
            self.add_status_message(_(u'psm_correct_errors_below', u'Please correct the errors indicated below.'))
            return

        self.request.form['featurelets'] = [f['id'] for f in self.featurelets()]

        # Aarrgghh!! #*!&% plone snoops into the request, and reads the form variables directly,
        # so we have to set the form variables with the same names as the schema
        self.request.form['title'] = title

        proj = self.context.restrictedTraverse('portal_factory/OpenProject/%s' %id_)
        # not calling validate because it explodes on "'" for project titles
        # XXX is no validation better than an occasional ugly error?
        #proj.validate(REQUEST=self.request, errors=self.errors, data=1, metadata=0)
        if self.errors:
            self.add_status_message(_(u'psm_correct_errors_below', u'Please correct the errors indicated below.'))
            return 
        if id_ in self.reserved_names():
            self.errors['id'] = 'Name reserved'
            self.add_status_message(_(u'psm_project_name_reserved', u'The name "${project_name}" is reserved. Please try a different name.',
                                      mapping={u'project_name':id_}))
            return

        self.context.portal_factory.doCreate(proj, id_)
        proj = aq_inner(self.context)._getOb(id_)
        self.notify(proj)

        logo = self.request.form.get('logo')
        if logo:
            if not self.check_logo(proj, logo):
                return
            del self.request.form['logo']

        hpcontext = IHomePage(proj)
        hpcontext.home_page = 'summary'

        # We have to look up the viewlets again, now that we have
        # a project for them to use as the context to save to.
        viewlet_mgr = getMultiAdapter((proj, self.request, self),
                                      name='opencore.proj_prefs')
        if not hasattr(viewlet_mgr, 'viewlets'):
            viewlet_mgr.update()
        for viewlet in viewlet_mgr.viewlets:
            if hasattr(viewlet, 'save'):
                viewlet.save()

        self.template = None  # Don't render anything before redirect.
        site_url = getToolByName(self.context, 'portal_url')()
        proj_edit_url = '%s/projects/%s/project-home/edit' % (site_url, id_)

        s_message_mapping = {'title': title, 'proj_edit_url': proj_edit_url,
                             'project_noun': self.project_noun,}


        s_message = _(u'project_created',
                      u'"${title}" has been created. Create a team by searching for other members to invite to your ${project_noun}, then <a href="${proj_edit_url}">edit your ${project_noun} home page</a>.',
                      mapping=s_message_mapping)
        
#        self.add_status_message(s_message)

        self.redirect('%s/tour' % proj.absolute_url())
    def handle_request(self):
        # First do validation. We don't treat validation problems as
        # exceptions, because we want to warn user of as many problems
        # as possible, not just the first one that fails.  But this
        # also means this method needs to manually bail out after
        # validation failure, to avoid saving bad data.
        title = self.request.form.get('project_title', self.request.form.get('title'))
        title = text.strip_extra_whitespace(title)
        self.request.form['project_title'] = title
        if not text.valid_title(title):
            self.errors['project_title'] = _(u'err_project_name', u'The name must contain at least 2 characters with at least 1 letter or number.')

        # We're inventing a convention by which viewlets can extend
        # forms with more form data to validate: just provide a
        # validate() method.  (And then later we'll call a save()
        # method.)
        # XXX I'd prefer to just implicitly use viewlet.update(), and
        # not explicitly iterate over them at all; but this view's
        # need to validate *everything* first prevents me from doing
        # that. Maybe this view should be re-architected.
        viewlet_mgr = getMultiAdapter((self.context, self.request, self),
                                      name='opencore.proj_prefs')
        if not hasattr(viewlet_mgr, 'viewlets'):
            # This means it hasn't had update() called yet. only do that once.
            viewlet_mgr.update()
        for viewlet in viewlet_mgr.viewlets:
            if hasattr(viewlet, 'validate'):
                self.errors.update(viewlet.validate())

        if self.errors:
            self.add_status_message(_(u'psm_correct_errors_below', u'Please correct the errors indicated below.'))
            return

        # Validation passed, so we save the data and set status PSMs.

        # start w/ the viewlets, so they can munge the form if need be
        for viewlet in viewlet_mgr.viewlets:
            if hasattr(viewlet, 'save'):
                viewlet.save()

        allowed_params = set(['__initialize_project__', 'update', 'set_flets',
                              'project_title', 'description', 'logo', 'workflow_policy',
                              'featurelets', 'home-page',
                              'location',])
        new_form = {}
        for k in allowed_params:
            if k in self.request.form:
                if 'project_title' == k:
                    # Aarrgghh!! #*!&% plone snoops into the request, and reads the form variables directly,
                    # so we have to set the form variables with the same names as the schema
                    new_form['title'] = self.request.form[k]
                else:
                    new_form[k] = self.request.form[k]

        reader = IReadWorkflowPolicySupport(self.context)
        old_workflow_policy = reader.getCurrentPolicyId()

        logo = self.request.form.get('logo')
        logochanged = False
        if logo:
            try:
                self.set_logo(logo)
                logochanged = True
            except ValueError:
                pass
            del self.request.form['logo']

        #store change status of flet, security, title, description, logo...
        try:
            title_changed = self.context.title != self.request.form.get('project_title', self.context.title)
        except UnicodeDecodeError:
            title_changed = self.context.title != self.request.form.get('project_title', self.context.title).decode('utf-8','ignore')
 
        try:
            desc_changed = self.context.Description() != self.request.form.get('description', self.context.Description())
        except UnicodeDecodeError:
            desc_changed = self.context.Description() != self.request.form.get('description', self.context.Description()).decode('utf-8','ignore')
 
        changed = {
            _(u'psm_project_title_changed') : title_changed,
            _(u'psm_project_desc_changed') : desc_changed,
            _(u'psm_project_logo_changed') : logochanged,
            _(u'psm_security_changed') : old_workflow_policy != self.request.form.get('workflow_policy'),
            #_(u'psm_location_changed'): bool(locationchanged),
            }
        
        supporter = IFeatureletSupporter(self.context)
        flets = [f for n, f in getAdapters((supporter,), IFeaturelet)]

        old_featurelets = set([(f.id, f.title) for f in flets if f.installed])

        old_form = self.request.form
        self.request.form = new_form
        self.context.processForm(REQUEST=self.request, metadata=1)
        self.request.form = old_form
        
        featurelets = set([(f.id, f.title) for f in flets if f.installed])

        for flet in featurelets:
            if flet not in old_featurelets:
                changed[_(u'psm_featurelet_added', u'${flet} feature has been added.',
                          mapping={u'flet':flet[1].capitalize()})] = 1
        
        for flet in old_featurelets:
            if flet not in featurelets:
                changed[_(u'psm_featurelet_removed', u'${flet} feature has been removed.',
                          mapping={u'flet':flet[1].capitalize()})] = 1
        
        for field, changed in changed.items():
            if changed:
                self.add_status_message(field)
        #self.add_status_message('Your changes have been saved.')

        home_page = self.request.form.get('home-page', None)
        hpcontext = IHomePage(self.context)
        if home_page is not None:
            if hpcontext.home_page != home_page:
                hp_url = '%s/%s' % (self.context.absolute_url(), home_page)
                self.add_status_message(_(u'psm_proj_homepage_change', u'${project_noun} home page set to: <a href="${hp_url}">${homepage}</a>',
                                        mapping={u'homepage':home_page, u'hp_url':hp_url,
                                                 u'project_noun':self.project_noun.title(),
                                                 }))
                hpcontext.home_page = home_page


        self.redirect(self.context.absolute_url())