source: products/quintagroup.mobileextender/trunk/quintagroup/mobileextender/browser/mobilecontrol.py @ 754

Last change on this file since 754 was 754, checked in by piv, 17 years ago

copied from trunk

  • Property svn:eol-style set to native
File size: 4.1 KB
Line 
1#from zope.component import queryMultiAdapter
2from zope.interface import implements, alsoProvides, Interface
3from zope.component import adapts, getMultiAdapter
4
5
6
7from zope.formlib import form
8from zope.app.form.browser import MultiSelectWidget, TextAreaWidget
9from Products.Five.formlib import formbase
10
11from Products.Five import BrowserView
12from Products.Five.browser.pagetemplatefile import ViewPageTemplateFile
13from Products.CMFCore.utils import getToolByName
14
15from quintagroup.mobileextender import mobileextenderMessageFactory as _
16from quintagroup.mobileextender.interfaces import IMobile
17from interfaces import IMobileConfiglet
18
19def SelectWidgetFactory( field, request ):
20    vocabulary = field.value_type.vocabulary
21    return MultiSelectWidget( field, vocabulary, request )
22
23def ListTextAreaWidgetFactory( field, request ):
24    taw = TextAreaWidget( field, request )
25    taw.width = 20
26    taw.height = 5
27    return taw
28
29class 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
43class 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(
67                self.form_fields, self.prefix, self.context, self.request,
68                form=self, adapters=self.adapters, 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_search", default=u"Search"), name=u'Search')
75    def handle_search(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            for b in res:
90                ob = b.getObject()
91                if not IMobile.providedBy(ob):
92                    alsoProvides(ob, IMobile)
93
94            #self.status = "Marked %d objects: \n %s" % (len(res), map(lambda b:b.getPath(), res))
95            self.status = "Marked %d objects" % len(res)
96        else:
97            self.status = "No objects found for given criterion"
98
99        #raise Exception(self.status)
100
101        return ''
102
103
104    def getFilteredContent(self, data):
105        purl = getToolByName(self.context, 'portal_url')
106        catalog = getToolByName(self.context, 'portal_catalog')
107
108        ptypes = data.get('ptypes',[])
109        sorton = data.get('sorton',None)
110        wfstates = data.get('wfstates', [])
111
112        query = {'path' : data.get('path', purl.getPortalPath()),}
113        if ptypes: query.update({'portal_type':ptypes})
114        if wfstates: query.update({'review_state':wfstates})
115
116        #print ">>>>>>> query: ", query
117
118        return catalog(**query)
Note: See TracBrowser for help on using the repository browser.