[1593] | 1 | ## Script (Python) "gsm_edit_settings" |
---|
| 2 | ##bind container=container |
---|
| 3 | ##bind context=context |
---|
| 4 | ##bind namespace= |
---|
| 5 | ##bind script=script |
---|
| 6 | ##bind subpath=traverse_subpath |
---|
| 7 | ##bind state=state |
---|
| 8 | ##parameters= |
---|
| 9 | ##title=Configure Plone Google Sitemap |
---|
| 10 | ## |
---|
| 11 | |
---|
| 12 | from Products.CMFCore.utils import getToolByName |
---|
| 13 | from quintagroup.plonegooglesitemaps.utils import ping_google |
---|
| 14 | |
---|
| 15 | portal_url = getToolByName(context,'portal_url') |
---|
| 16 | portalURL = portal_url() |
---|
| 17 | portal = portal_url.getPortalObject() |
---|
| 18 | req = context.REQUEST |
---|
| 19 | message = "" |
---|
| 20 | |
---|
| 21 | def addSMByType(parent, sm_type, sm_id): |
---|
| 22 | new_id = portal.invokeFactory(id=sm_id, type_name="Sitemap", sitemapType=sm_type) |
---|
| 23 | sm = getattr(portal, new_id) |
---|
| 24 | if sm: |
---|
| 25 | sm.markCreationFlag() |
---|
| 26 | return new_id |
---|
| 27 | return None |
---|
| 28 | |
---|
| 29 | if req.get('form.button.AddContent', False): |
---|
| 30 | new_id = addSMByType(portal, 'content', 'sitemap.xml') |
---|
| 31 | if new_id: |
---|
| 32 | return req.RESPONSE.redirect("%s/%s/edit" % (portalURL, new_id)) |
---|
| 33 | else: |
---|
| 34 | message = "Can't create content sitemap" |
---|
| 35 | elif req.get('form.button.AddMobile', False): |
---|
| 36 | new_id = addSMByType(portal, 'mobile', 'mobile-sitemap.xml') |
---|
| 37 | if new_id: |
---|
| 38 | return req.RESPONSE.redirect("%s/%s/edit" % (portalURL, new_id)) |
---|
| 39 | else: |
---|
| 40 | message = "Can't create mobile sitemap" |
---|
| 41 | elif req.get('form.button.AddNews', False): |
---|
| 42 | new_id = addSMByType(portal, 'news', 'news-sitemap.xml') |
---|
| 43 | if new_id: |
---|
| 44 | return req.RESPONSE.redirect("%s/%s/edit" % (portalURL, new_id)) |
---|
| 45 | else: |
---|
| 46 | message = "Can't create news sitemap" |
---|
| 47 | else: |
---|
| 48 | smselected = req.get('smselected', []) |
---|
| 49 | if req.get('form.button.Delete', False): |
---|
[3568] | 50 | for sm_path in smselected: |
---|
| 51 | sitemap = portal.restrictedTraverse(sm_path) |
---|
| 52 | sitemap.aq_parent.manage_delObjects(sitemap.id) |
---|
[1593] | 53 | message = "Succesfully deleted: %s" % smselected |
---|
| 54 | |
---|
| 55 | elif req.get('form.button.Ping', False): |
---|
| 56 | pinged = [] |
---|
| 57 | message = "Google pinged. It will review your sitemap as soon as it will be able to. Processed: %s" |
---|
[3568] | 58 | for sm_path in smselected: |
---|
[1593] | 59 | try: |
---|
[3568] | 60 | ping_google(portalURL, sm_path) |
---|
[1593] | 61 | except: |
---|
| 62 | message = "Cannot contact Google. Try again in a while. But pinged for: %s" |
---|
| 63 | break |
---|
| 64 | else: |
---|
[3568] | 65 | pinged.append(sm_path) |
---|
[1593] | 66 | message = message % pinged |
---|
| 67 | |
---|
| 68 | return state.set(next_action='traverse_to:string:prefs_gsm_settings', |
---|
| 69 | portal_status_message = message) |
---|