1 | #from zope.component import queryMultiAdapter |
---|
2 | from zope.interface import implements, alsoProvides, noLongerProvides, Interface |
---|
3 | from zope.component import adapts, getMultiAdapter |
---|
4 | |
---|
5 | |
---|
6 | |
---|
7 | from zope.formlib import form |
---|
8 | from zope.app.form.browser import MultiSelectWidget, TextAreaWidget |
---|
9 | from Products.Five.formlib import formbase |
---|
10 | |
---|
11 | from Products.Five import BrowserView |
---|
12 | from Products.Five.browser.pagetemplatefile import ViewPageTemplateFile |
---|
13 | from Products.CMFCore.utils import getToolByName |
---|
14 | |
---|
15 | from quintagroup.mobileextender import mobileextenderMessageFactory as _ |
---|
16 | from quintagroup.mobileextender.interfaces import IMobile |
---|
17 | from interfaces import IMobileConfiglet |
---|
18 | |
---|
19 | def SelectWidgetFactory( field, request ): |
---|
20 | vocabulary = field.value_type.vocabulary |
---|
21 | return MultiSelectWidget( field, vocabulary, request ) |
---|
22 | |
---|
23 | def ListTextAreaWidgetFactory( field, request ): |
---|
24 | taw = TextAreaWidget( field, request ) |
---|
25 | taw.width = 20 |
---|
26 | taw.height = 5 |
---|
27 | return taw |
---|
28 | |
---|
29 | class MobileControl(object): |
---|
30 | implements(IMobileConfiglet) |
---|
31 | adapts(Interface) |
---|
32 | |
---|
33 | ptypes = [] |
---|
34 | excludeids = [] |
---|
35 | wfstates = [] |
---|
36 | excludepaths = [] |
---|
37 | |
---|
38 | def __init__(self, context): |
---|
39 | self.context = context |
---|
40 | self.path = getToolByName(context, 'portal_url').getPortalPath() |
---|
41 | |
---|
42 | |
---|
43 | class MobileControlView(formbase.FormBase): |
---|
44 | """ |
---|
45 | Configlet settings browser view |
---|
46 | """ |
---|
47 | template = ViewPageTemplateFile('mobilecontrol.pt') |
---|
48 | |
---|
49 | form_fields = form.Fields(IMobileConfiglet) |
---|
50 | |
---|
51 | label = _(u"Define mobile content") |
---|
52 | form_name = _(u"Define mobile content") |
---|
53 | description = _(u"This configlet allows you to define content, " \ |
---|
54 | u"which should be identified as mobile content") |
---|
55 | |
---|
56 | form_fields["ptypes"].custom_widget = SelectWidgetFactory |
---|
57 | form_fields["wfstates"].custom_widget = SelectWidgetFactory |
---|
58 | form_fields["excludeids"].custom_widget = ListTextAreaWidgetFactory |
---|
59 | form_fields["excludepaths"].custom_widget = ListTextAreaWidgetFactory |
---|
60 | form_fields["path"].render_context = True |
---|
61 | |
---|
62 | def setUpWidgets(self, ignore_request=False): |
---|
63 | self.adapters = {} |
---|
64 | self.adapters[IMobileConfiglet] = MobileControl(self.context) |
---|
65 | |
---|
66 | self.widgets = form.setUpWidgets(self.form_fields, self.prefix, |
---|
67 | self.context, self.request, form=self, adapters=self.adapters, |
---|
68 | ignore_request=ignore_request) |
---|
69 | |
---|
70 | def is_fieldsets(self): |
---|
71 | # We need to be able to test for non-fieldsets in templates. |
---|
72 | return False |
---|
73 | |
---|
74 | @form.action(_(u"label_mark", default=u"Mark"), name=u'Mark') |
---|
75 | def handle_mark(self, action, data): |
---|
76 | res = self.getFilteredContent(data) |
---|
77 | if res: |
---|
78 | exclids = data.get('excludeids', '') |
---|
79 | if exclids: |
---|
80 | exclids = filter(None, [i.strip() for i in exclids.split('\n')]) |
---|
81 | res = filter(lambda b:not b.getId in exclids, res) |
---|
82 | |
---|
83 | exclpaths = data.get('excludepaths', '') |
---|
84 | if exclpaths: |
---|
85 | exclpaths = filter(None, [i.strip() for i in exclpaths.split('\n')]) |
---|
86 | res = filter(lambda b:not [1 for ep in exclpaths if b.getPath().startswith(ep)], res) |
---|
87 | |
---|
88 | # Mark objects with interface |
---|
89 | marked = 0 |
---|
90 | for b in res: |
---|
91 | ob = b.getObject() |
---|
92 | if not IMobile.providedBy(ob): |
---|
93 | alsoProvides(ob, IMobile) |
---|
94 | marked += 1 |
---|
95 | |
---|
96 | if marked: |
---|
97 | catalog = getToolByName(self.context, 'portal_catalog') |
---|
98 | catalog.manage_reindexIndex(ids=['object_provides']) |
---|
99 | |
---|
100 | self.status = "Marked %d objects" % marked |
---|
101 | else: |
---|
102 | self.status = "No objects found for given criterion" |
---|
103 | |
---|
104 | @form.action(_(u"label_demark", default=u"DeMark"), name=u'DeMark') |
---|
105 | def handle_demark(self, action, data): |
---|
106 | res = self.getFilteredContent(data) |
---|
107 | if res: |
---|
108 | exclids = data.get('excludeids', '') |
---|
109 | if exclids: |
---|
110 | exclids = filter(None, [i.strip() for i in exclids.split('\n')]) |
---|
111 | res = filter(lambda b:not b.getId in exclids, res) |
---|
112 | |
---|
113 | exclpaths = data.get('excludepaths', '') |
---|
114 | if exclpaths: |
---|
115 | exclpaths = filter(None, [i.strip() for i in exclpaths.split('\n')]) |
---|
116 | res = filter(lambda b:not [1 for ep in exclpaths if b.getPath().startswith(ep)], res) |
---|
117 | |
---|
118 | # DeMark objects with interface |
---|
119 | demarked = 0 |
---|
120 | for b in res: |
---|
121 | ob = b.getObject() |
---|
122 | if IMobile.providedBy(ob): |
---|
123 | noLongerProvides(ob, IMobile) |
---|
124 | demarked += 1 |
---|
125 | |
---|
126 | if demarked: |
---|
127 | catalog = getToolByName(self.context, 'portal_catalog') |
---|
128 | catalog.manage_reindexIndex(ids=['object_provides']) |
---|
129 | |
---|
130 | self.status = "DeMarked %d objects" % demarked |
---|
131 | else: |
---|
132 | self.status = "No objects found for given criterion" |
---|
133 | |
---|
134 | |
---|
135 | def getFilteredContent(self, data): |
---|
136 | purl = getToolByName(self.context, 'portal_url') |
---|
137 | catalog = getToolByName(self.context, 'portal_catalog') |
---|
138 | |
---|
139 | ptypes = data.get('ptypes',[]) |
---|
140 | sorton = data.get('sorton',None) |
---|
141 | wfstates = data.get('wfstates', []) |
---|
142 | |
---|
143 | query = {'path' : data.get('path', purl.getPortalPath()),} |
---|
144 | if ptypes: query.update({'portal_type':ptypes}) |
---|
145 | if wfstates: query.update({'review_state':wfstates}) |
---|
146 | |
---|
147 | return catalog(**query) |
---|
148 | |
---|
149 | def currentlyMarked(self): |
---|
150 | catalog = getToolByName(self.context, 'portal_catalog') |
---|
151 | return len(catalog(object_provides='quintagroup.mobileextender.interfaces.IMobile')) |
---|