source: products/ploneorg.kudobounty/trunk/ploneorg/kudobounty/browser/views.py @ 3129

Last change on this file since 3129 was 3129, checked in by mylan, 13 years ago

Initial import of the package

  • Property svn:eol-style set to native
File size: 2.9 KB
RevLine 
[3129]1from DateTime import DateTime
2from zope.interface import implements, Interface
3from zope.component import queryUtility
4from zope.component import getMultiAdapter
5from plone.i18n.normalizer.interfaces import IURLNormalizer
6
7from Products.Five import BrowserView
8from Products.CMFCore.utils import getToolByName
9from Products.Archetypes.config import RENAME_AFTER_CREATION_ATTEMPTS
10
11
12from ploneorg.kudobounty import kudobountyMessageFactory as _
13from ploneorg.kudobounty.config import *
14
15
16class BountyFormProcessorView(BrowserView):
17    """
18    test_view browser view
19    """
20
21    @property
22    def portal(self):
23        return getMultiAdapter((self.context, self.request),
24                               name='plone_portal_state').portal()
25
26    @property
27    def wftool(self):
28        return getMultiAdapter((self.context, self.request),
29                               name='plone_tools').workflow()
30
31    def __call__(self):
32        """
33        test method
34        """
35        try:
36            container = self.portal.restrictedTraverse(SUBMISSION_CONTAINER_ID)
37        except:
38            logger.warn("Can't find bounty submission container " \
39               "with '%s' path" % SUBMISSION_CONTAINER_ID)
40        else:
41            # Create Bounty Program Submission object
42            form = self.request.form
43            title = ' '.join(filter(None,
44                        [form['firstName'], form['lastName'], form['organization']]))
45            id = self.getUniqueId(container, title)
46            container.invokeFactory("Bounty Program Submission", id)
47            bps = getattr(container, id)
48            # Update Submission with data from the PFG form
49            form['image'] = form['image_file']
50            form['description'] = form['altText']
51            effd, expd = self.getEffExpDates()
52            form['effectiveDate'] = effd
53            form['expirationDate'] = expd
54           
55            bps.update(**form)
56            bps.unmarkCreationFlag()
57            bps.reindexObject()
58            # Change wf state
59            self.wftool.doActionFor(bps, "submit")
60
61        return {}
62
63    def getEffExpDates(self):
64        now = DateTime()
65        month = now.month()
66        year = now.year()
67        if month == 12:
68            month = 1
69            year = year + 1
70        else:
71            month = month + 1
72        effd = DateTime(year, month, 1, 0, 0)
73        expd = DateTime(year, month + 1, 1, 23, 55) - 1
74        return effd, expd
75       
76
77    def getUniqueId(self, container, title):
78        id = queryUtility(IURLNormalizer).normalize(title)
79        container_ids = container.objectIds()
80        check_id = lambda id, required: id in container_ids
81
82        invalid_id = check_id(id, required=1)
83        if not invalid_id:
84            return id
85
86        idx = 1
87        while idx <= RENAME_AFTER_CREATION_ATTEMPTS:
88            new_id = "%s-%d" % (id, idx)
89            if not check_id(new_id, required=1):
90                return new_id
91            idx += 1
92
93        return None
Note: See TracBrowser for help on using the repository browser.