1 | from DateTime import DateTime |
---|
2 | from zope.interface import implements, Interface |
---|
3 | from zope.component import getUtility, queryUtility |
---|
4 | from zope.component import getMultiAdapter |
---|
5 | from zope.app.pagetemplate.viewpagetemplatefile import ViewPageTemplateFile |
---|
6 | from plone.i18n.normalizer.interfaces import IURLNormalizer |
---|
7 | from plone.registry.interfaces import IRegistry |
---|
8 | |
---|
9 | from Products.Five import BrowserView |
---|
10 | from Products.CMFCore.utils import getToolByName |
---|
11 | from Products.Archetypes.config import RENAME_AFTER_CREATION_ATTEMPTS |
---|
12 | |
---|
13 | from ploneorg.kudobounty import logger |
---|
14 | from ploneorg.kudobounty.config import * |
---|
15 | from ploneorg.kudobounty import kudobountyMessageFactory as _ |
---|
16 | from ploneorg.kudobounty.content.bountyprogramsubmission import calcTitle |
---|
17 | |
---|
18 | from collective.portlet.collectionmultiview.renderers.base import ( |
---|
19 | CollectionMultiViewBaseRenderer) |
---|
20 | |
---|
21 | class BountyCollectionRenderer(CollectionMultiViewBaseRenderer): |
---|
22 | __name__ = 'Bounty Collection View' |
---|
23 | template = ViewPageTemplateFile('bounty_collection_view.pt') |
---|
24 | |
---|
25 | @property |
---|
26 | def available(self): |
---|
27 | return len(self.results()) |
---|
28 | |
---|
29 | def bounty_form_url(self): |
---|
30 | # Prepare bounty-form url based on plone.registry record |
---|
31 | portal_url = getMultiAdapter((self.context, self.request), |
---|
32 | name='plone_portal_state').portal_url() |
---|
33 | form_path = getUtility(IRegistry)['ploneorg.kudobounty.bountySubmissionForm'] |
---|
34 | return "%s/%s" % (portal_url, form_path) |
---|
35 | |
---|
36 | |
---|
37 | class BountyFormProcessorView(BrowserView): |
---|
38 | """ |
---|
39 | Browser page view for automation of 'Bounty Program Submission' |
---|
40 | content object creation. |
---|
41 | """ |
---|
42 | |
---|
43 | @property |
---|
44 | def portal(self): |
---|
45 | return getMultiAdapter((self.context, self.request), |
---|
46 | name='plone_portal_state').portal() |
---|
47 | |
---|
48 | @property |
---|
49 | def wftool(self): |
---|
50 | return getMultiAdapter((self.context, self.request), |
---|
51 | name='plone_tools').workflow() |
---|
52 | |
---|
53 | def __call__(self): |
---|
54 | """ |
---|
55 | Perform following steps: |
---|
56 | * Fill it with data from the PFG form, |
---|
57 | * Set effective and expiration date to nearest month |
---|
58 | * Change the workflow state into pending state |
---|
59 | """ |
---|
60 | try: |
---|
61 | container = self.portal.restrictedTraverse(CONTAINER_ID) |
---|
62 | except: |
---|
63 | msg = "Can't find bounty submission container" |
---|
64 | logger.warn(msg + " with '%s' path" % CONTAINER_ID) |
---|
65 | raise RuntimeError(msg) |
---|
66 | else: |
---|
67 | # Create Bounty Program Submission object |
---|
68 | form = self.request.form |
---|
69 | title = calcTitle(form['firstName'], form['lastName'], form['organization']) |
---|
70 | id = self.getUniqueId(container, title) |
---|
71 | container.invokeFactory("Bounty Program Submission", id) |
---|
72 | bps = getattr(container, id) |
---|
73 | # Update Submission with data from the PFG form |
---|
74 | form['image'] = form['image_file'] |
---|
75 | form['description'] = form['altText'] |
---|
76 | effd, expd = self.getEffExpDates() |
---|
77 | form['effectiveDate'] = effd |
---|
78 | form['expirationDate'] = expd |
---|
79 | |
---|
80 | bps.update(**form) |
---|
81 | bps.unmarkCreationFlag() |
---|
82 | bps.reindexObject() |
---|
83 | # Change wf state |
---|
84 | self.wftool.doActionFor(bps, "submit") |
---|
85 | |
---|
86 | return {} |
---|
87 | |
---|
88 | def getEffExpDates(self): |
---|
89 | now = DateTime() |
---|
90 | month = now.month() |
---|
91 | year = now.year() |
---|
92 | if month == 12: |
---|
93 | month = 1 |
---|
94 | year = year + 1 |
---|
95 | else: |
---|
96 | month = month + 1 |
---|
97 | effd = DateTime(year, month, 1, 0, 0) |
---|
98 | expd = DateTime(year, month + 1, 1, 23, 55) - 1 |
---|
99 | return effd, expd |
---|
100 | |
---|
101 | |
---|
102 | def getUniqueId(self, container, title): |
---|
103 | # NOTE: |
---|
104 | # Mixed and little refactored functions of |
---|
105 | # Products.Archetypes.BaseObject.BaseObject class: |
---|
106 | # * _findUniqueId (check uniqueness of id in the container) |
---|
107 | # * generateNewId (used url noralizer utility) |
---|
108 | id = queryUtility(IURLNormalizer).normalize(title) |
---|
109 | container_ids = container.objectIds() |
---|
110 | check_id = lambda id, required: id in container_ids |
---|
111 | |
---|
112 | invalid_id = check_id(id, required=1) |
---|
113 | if not invalid_id: |
---|
114 | return id |
---|
115 | |
---|
116 | idx = 1 |
---|
117 | while idx <= RENAME_AFTER_CREATION_ATTEMPTS: |
---|
118 | new_id = "%s-%d" % (id, idx) |
---|
119 | if not check_id(new_id, required=1): |
---|
120 | return new_id |
---|
121 | idx += 1 |
---|
122 | |
---|
123 | return None |
---|