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