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 | |
---|
50 | if req.get('form.button.Delete', False): |
---|
51 | portal.manage_delObjects(ids=smselected[:]) |
---|
52 | message = "Succesfully deleted: %s" % smselected |
---|
53 | |
---|
54 | elif req.get('form.button.Ping', False): |
---|
55 | pinged = [] |
---|
56 | message = "Google pinged. It will review your sitemap as soon as it will be able to. Processed: %s" |
---|
57 | for sm_id in smselected: |
---|
58 | try: |
---|
59 | ping_google(portalURL, sm_id) |
---|
60 | except: |
---|
61 | message = "Cannot contact Google. Try again in a while. But pinged for: %s" |
---|
62 | break |
---|
63 | else: |
---|
64 | pinged.append(sm_id) |
---|
65 | message = message % pinged |
---|
66 | |
---|
67 | return state.set(next_action='traverse_to:string:prefs_gsm_settings', |
---|
68 | portal_status_message = message) |
---|