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