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