[1593] | 1 | from zope.component import queryMultiAdapter |
---|
| 2 | from zope.interface import implements, Interface, Attribute |
---|
| 3 | |
---|
[2516] | 4 | from OFS.Image import cookId |
---|
| 5 | from OFS.ObjectManager import BadRequestException |
---|
[1593] | 6 | from Products.Five import BrowserView |
---|
| 7 | from Products.CMFCore.utils import getToolByName |
---|
| 8 | |
---|
[2515] | 9 | from quintagroup.plonegooglesitemaps.interfaces import ISitemap |
---|
[1593] | 10 | from quintagroup.plonegooglesitemaps import qPloneGoogleSitemapsMessageFactory as _ |
---|
| 11 | |
---|
| 12 | def 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 | |
---|
| 22 | class 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 | sm_types = Attribute("List of sitemap typs") |
---|
| 32 | |
---|
| 33 | def sitemapsDict(): |
---|
| 34 | """ Return dictionary like object with data for table |
---|
| 35 | """ |
---|
| 36 | def sitemapsURLByType(): |
---|
| 37 | """ Return dictionary like object with sitemap_type as key |
---|
| 38 | and sitemap object(s) as value |
---|
| 39 | """ |
---|
| 40 | def getVerificationFiles(): |
---|
| 41 | """ Return list of existent verification files on site. |
---|
| 42 | Update googlesitemap_properties.verification_file |
---|
| 43 | property for only existent files |
---|
| 44 | """ |
---|
| 45 | |
---|
[2516] | 46 | def uploadVerificationFile(vfile): |
---|
| 47 | """ Upload passed site verification file to the site. |
---|
| 48 | On success - update googlesitemaps verification files list. |
---|
| 49 | Return tuple where : |
---|
| 50 | 1. boolean value - is verification file successfully created. |
---|
| 51 | 2. string value: |
---|
| 52 | 2.1. if successfull - id of created verification file |
---|
| 53 | 2.2. if failure - error descirption |
---|
| 54 | """ |
---|
[1593] | 55 | |
---|
| 56 | class ConfigletSettingsView(BrowserView): |
---|
| 57 | """ |
---|
| 58 | Configlet settings browser view |
---|
| 59 | """ |
---|
| 60 | implements(IConfigletSettingsView) |
---|
[2596] | 61 | sitemaps = [] |
---|
[1593] | 62 | |
---|
| 63 | def __init__(self, context, request): |
---|
| 64 | self.context = context |
---|
| 65 | self.request = request |
---|
| 66 | |
---|
[2515] | 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')] |
---|
[1593] | 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 = [] |
---|
[2515] | 142 | props = getattr(self.tools.properties(),'googlesitemap_properties') |
---|
[1593] | 143 | if props: |
---|
[2515] | 144 | portal_ids = self.pps.portal().objectIds() |
---|
[1593] | 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) |
---|
[2515] | 149 | return vfs |
---|
[2516] | 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 |
---|