source: products/quintagroup.plonegooglesitemaps/trunk/quintagroup/plonegooglesitemaps/browser/configletview.py @ 2538

Last change on this file since 2538 was 2538, checked in by mylan, 14 years ago

Merged revisions 3566-3575 via svnmerge from
http://svn.quintagroup.com/products/quintagroup.plonegooglesitemaps/branches/test_refactoring

........

r3566 | mylan | 2010-06-14 12:24:52 +0300 (Mon, 14 Jun 2010) | 1 line


#206: Split single testqPloneGoogleSitemaps module into several specific one

........

r3567 | mylan | 2010-06-14 16:14:34 +0300 (Mon, 14 Jun 2010) | 1 line


#206: Improve code coverage - remove useless BBB code from interfaces.

........

r3568 | mylan | 2010-06-14 20:24:25 +0300 (Mon, 14 Jun 2010) | 1 line


#206: Improve code coverage - added test for MobileSitemap?, MobileSitemapView?.

........

r3569 | mylan | 2010-06-14 22:39:39 +0300 (Mon, 14 Jun 2010) | 1 line


#206: reorganize sitemap tests

........

r3570 | mylan | 2010-06-14 22:40:12 +0300 (Mon, 14 Jun 2010) | 1 line


#206: Added configlet tests

........

r3571 | mylan | 2010-06-14 23:05:19 +0300 (Mon, 14 Jun 2010) | 1 line


#206: remove BBB code for plone<3.0

........

r3572 | mylan | 2010-06-14 23:12:00 +0300 (Mon, 14 Jun 2010) | 1 line


#206: added workflow vocabularies tests for SitemapTypes?

........

r3573 | mylan | 2010-06-15 21:36:40 +0300 (Tue, 15 Jun 2010) | 1 line


#206: some cleanup, simplify tests

........

r3574 | mylan | 2010-06-16 16:35:31 +0300 (Wed, 16 Jun 2010) | 1 line


#206: move mobile sitemap code preparation into base module

........

r3575 | mylan | 2010-06-16 16:37:14 +0300 (Wed, 16 Jun 2010) | 1 line


#206: Added security tests

........

  • Property svn:eol-style set to native
File size: 6.0 KB
Line 
1from zope.component import queryMultiAdapter
2from zope.interface import implements, Interface, Attribute
3
4from OFS.Image import cookId
5from OFS.ObjectManager import BadRequestException
6from Products.Five import BrowserView
7from Products.CMFCore.utils import getToolByName
8
9from quintagroup.plonegooglesitemaps.interfaces import ISitemap
10from quintagroup.plonegooglesitemaps import qPloneGoogleSitemapsMessageFactory as _
11
12def splitNum(num):
13    res = []
14    prefn = 3
15    for c in str(num)[::-1]:
16        res.insert(0,c)
17        if not len(res)%prefn:
18            res.insert(0,',')
19            prefn += 4
20    return "".join(res[0]==',' and res[1:] or res)
21
22class IConfigletSettingsView(Interface):
23    """
24    Sitemap view interface
25    """
26
27    sitemaps = Attribute("return mapping of sitemap's type to list of appropriate objects")
28    hasContentSM = Attribute("Return boolean about existance content sitemap")
29    hasMobileSM = Attribute("Return boolean about existance mobile sitemap")
30    hasNewsSM = Attribute("Return boolean about existance news sitemap")
31    sitemaps = Attribute("List of sitemap typs")
32    sm_types = Attribute("List of sitemap typs")
33
34    def sitemapsDict():
35        """ Return dictionary like object with data for table
36        """
37    def sitemapsURLByType():
38        """ Return dictionary like object with sitemap_type as key
39            and sitemap object(s) as value
40        """
41    def getVerificationFiles():
42        """ Return list of existent verification files on site.
43            Update googlesitemap_properties.verification_file
44            property for only existent files
45        """
46
47    def uploadVerificationFile(vfile):
48        """ Upload passed site verification file to the site.
49            On success - update googlesitemaps verification files list.
50            Return tuple where :
51              1. boolean value - is verification file successfully created.
52              2. string value:
53                2.1. if successfull - id of created verification file
54                2.2. if failure - error descirption
55        """
56
57class ConfigletSettingsView(BrowserView):
58    """
59    Configlet settings browser view
60    """
61    implements(IConfigletSettingsView)
62
63    def __init__(self, context, request):
64        self.context = context
65        self.request = request
66
67        self.tools = queryMultiAdapter((self.context, self.request), name="plone_tools")
68        self.pps = queryMultiAdapter((self.context, self.request), name="plone_portal_state")
69        self.sitemaps = [i.getObject() for i in self.tools.catalog()(portal_type='Sitemap')]
70
71    @property
72    def sm_types(self):
73        return [i.getSitemapType() for i in self.sitemaps]
74
75    @property
76    def hasContentSM(self):
77        return 'content' in self.sm_types
78
79    @property
80    def hasMobileSM(self):
81        return 'mobile' in self.sm_types
82
83    @property
84    def hasNewsSM(self):
85        return 'news' in self.sm_types
86
87    def sitemapsURLByType(self):
88        sitemaps = {}
89        for sm in self.sitemaps:
90            smlist = sitemaps.setdefault(sm.getSitemapType(),[])
91            smlist.append({'url':sm.absolute_url(),'id':sm.id})
92        sitemaps['all'] = sitemaps.setdefault('content',[]) + \
93                          sitemaps.setdefault('mobile',[]) + \
94                          sitemaps.setdefault('news',[])
95        return sitemaps
96
97    def sitemapsURLs(self):
98        sitemaps = {}
99        for sm in self.sitemaps:
100            smlist = sitemaps.setdefault(sm.getSitemapType(),[])
101            smlist.append(sm.absolute_url())
102        return sitemaps
103
104    def sitemapsDict(self):
105        content, mobile, news = [],[],[]
106        for sm in self.sitemaps:
107            data = self.getSMData(sm)
108            if data['sm_type'] == 'Content':
109                content.append(data)
110            elif data['sm_type'] == 'Mobile':
111                mobile.append(data)
112            elif data['sm_type'] == 'News':
113                news.append(data)
114        return content + mobile + news
115
116    def getSMData(self, ob):
117        size, entries = self.getSitemapData(ob)
118        return {'sm_type'    : ob.getSitemapType().capitalize(),
119                'sm_id'      : ob.id,
120                'sm_url'     : ob.absolute_url(),
121                'sm_size'    : size and splitNum(size) or '',
122                'sm_entries' : entries and splitNum(entries) or '',
123               }
124
125    def getSitemapData(self, ob):
126        size, entries = (0, 0)
127        view = ob and ob.defaultView() or None
128        if view:
129            resp = self.request.RESPONSE
130            bview = queryMultiAdapter((ob,self.request), name=view)
131            if bview:
132                try:
133                    size = len(bview())
134                    entries = bview.numEntries
135                    self.request.RESPONSE.setHeader('Content-Type', 'text/html')
136                except:
137                    pass
138        return (size, entries)
139
140    def getVerificationFiles(self):
141        vfs = []
142        props = getattr(self.tools.properties(),'googlesitemap_properties')
143        if props:
144            portal_ids = self.pps.portal().objectIds()
145            props_vfs = list(props.getProperty('verification_filenames',[]))
146            vfs = [vf for vf in props_vfs if vf in portal_ids]
147            if not props_vfs==vfs:
148                props._updateProperty('verification_filenames', vfs)
149        return vfs
150
151    def uploadVerificationFile(self, request):
152        vfilename = ""
153        portal = self.pps.portal()
154        try:
155            vfile = request.get("verification_file")
156            vfilename, vftitle = cookId("", "", vfile)
157            portal.manage_addFile(id="", file=vfile)
158            portal[vfilename].manage_addProperty(
159                'CreatedBy', 'quintagroupt.plonegooglesitemaps','string')
160        except BadRequestException, e:
161            return False, str(e)
162        else:
163            props = self.tools.properties().googlesitemap_properties
164            vfilenames = list(props.getProperty('verification_filenames',[]))
165            vfilenames.append(vfilename)
166            props.manage_changeProperties(verification_filenames = vfilenames)
167        return True, vfilename
Note: See TracBrowser for help on using the repository browser.