1 | from Products.Archetypes.public import listTypes |
---|
2 | from Products.Archetypes.Extensions.utils import installTypes, install_subskin |
---|
3 | from Products.CMFCore.WorkflowTool import addWorkflowFactory |
---|
4 | from Products.CMFCore import CMFCorePermissions |
---|
5 | from Products.CMFCore.utils import getToolByName |
---|
6 | from Products.CMFDefault.Link import Link |
---|
7 | from StringIO import StringIO |
---|
8 | |
---|
9 | from Products.qClickTrackingTool.ClickTracker import ClickTracker |
---|
10 | from Products.qClickTrackingTool.Workflow import createWorkflow |
---|
11 | from Products.qClickTrackingTool.ClickTracker import ClickTracker |
---|
12 | from Products.qClickTrackingTool.config import * |
---|
13 | |
---|
14 | |
---|
15 | def isSwitchedToATCT(self): |
---|
16 | pt = getToolByName(self, 'portal_types') |
---|
17 | doc_fti = pt.getTypeInfo('Document') |
---|
18 | if doc_fti.Metatype() == 'ATDocument': |
---|
19 | return 1 |
---|
20 | else: |
---|
21 | return 0 |
---|
22 | |
---|
23 | def write_object_properties(self, obj): |
---|
24 | properties = {} |
---|
25 | if not isSwitchedToATCT(self): |
---|
26 | properties['id'] = obj.id |
---|
27 | properties['title'] = obj.title |
---|
28 | properties['description'] = obj.description |
---|
29 | properties['remote_url'] = obj.getRemoteUrl() |
---|
30 | else: |
---|
31 | properties['id'] = obj.id |
---|
32 | properties['title'] = obj.Title() |
---|
33 | properties['description'] = obj.Description() |
---|
34 | properties['remote_url'] = obj.getRemoteUrl() |
---|
35 | |
---|
36 | return properties |
---|
37 | |
---|
38 | def update_object(self, obj, properties): |
---|
39 | if isSwitchedToATCT(self): |
---|
40 | obj.update(title = properties['title'], |
---|
41 | description = properties['description'], |
---|
42 | remoteUrl = properties['remote_url']) |
---|
43 | else: |
---|
44 | setattr(obj, 'title', properties['title']) |
---|
45 | setattr(obj, 'description', properties['description']) |
---|
46 | setattr(obj, 'remote_url', properties['remote_url']) |
---|
47 | |
---|
48 | def convert_objects(self, container, new_type, out): |
---|
49 | pt = getToolByName(self, 'portal_types') |
---|
50 | if getObjectsType(self, container, out): |
---|
51 | for o in listFolderItems(container)[:]: |
---|
52 | prop = write_object_properties(self, o) |
---|
53 | container._delObject(o.id) |
---|
54 | fti = pt.getTypeInfo('ClickTracker') |
---|
55 | fti.manage_changeProperties(allowed_content_types = (new_type,)) |
---|
56 | container.invokeFactory(id = prop['id'], type_name=new_type) |
---|
57 | campaign=getattr(container, prop['id']) |
---|
58 | update_object(self, campaign, prop) |
---|
59 | else: |
---|
60 | create_repType(self, out) |
---|
61 | |
---|
62 | def listFolderItems(container): |
---|
63 | res = [] |
---|
64 | ids = container.objectIds() |
---|
65 | for ob_id in ids: |
---|
66 | res.append(container._getOb(ob_id)) |
---|
67 | return res |
---|
68 | |
---|
69 | def getObjectsType(self, container, out): |
---|
70 | pt = getToolByName(self, 'portal_types') |
---|
71 | ids = container.objectIds() |
---|
72 | if ids: |
---|
73 | obj = container._getOb(ids[0]) |
---|
74 | type_obj = obj.meta_type |
---|
75 | else: |
---|
76 | print >> out, "Portal click tracker is empty..." |
---|
77 | type_obj = None |
---|
78 | |
---|
79 | return type_obj |
---|
80 | |
---|
81 | def install_tool(self, out, tool='ClickTracker'): |
---|
82 | portal=getToolByName(self, 'portal_url').getPortalObject() |
---|
83 | pt = getToolByName(self, 'portal_types') |
---|
84 | create_repType(self, out) |
---|
85 | if not TOOLID in portal.objectIds(): |
---|
86 | addPloneTool=portal.manage_addProduct[PROJECTNAME].manage_addTool(tool) |
---|
87 | else: |
---|
88 | qi=getToolByName(self, 'portal_quickinstaller') |
---|
89 | |
---|
90 | p=qi._getOb(PROJECTNAME, None) |
---|
91 | oldVersion=p.getInstalledVersion() |
---|
92 | newVersion= qi.getProductVersion(PROJECTNAME) |
---|
93 | is_migrated = isSwitchedToATCT(self) |
---|
94 | pc = getToolByName(self, 'portal_clicktracker') |
---|
95 | if oldVersion<newVersion: |
---|
96 | convert_objects(self, pc, REP_TYPE, out) |
---|
97 | |
---|
98 | if is_migrated: |
---|
99 | if getObjectsType(self, pc, out) != 'ATLink': |
---|
100 | convert_objects(self, pc, REP_TYPE, out) |
---|
101 | |
---|
102 | else: |
---|
103 | print >> out, "Repurpouse type already migrated to ATCT..." |
---|
104 | else: |
---|
105 | if getObjectsType(self, pc, out) == 'ATLink': |
---|
106 | convert_objects(self, pc, 'Link', out) |
---|
107 | |
---|
108 | else: |
---|
109 | print >> out, "Repurpouse type already migrated to CMF..." |
---|
110 | |
---|
111 | print >> out, "Installed %s tool..." % tool |
---|
112 | |
---|
113 | def repurpose_content_type(self, old_type, new_type): |
---|
114 | pt = getToolByName(self, 'portal_types') |
---|
115 | copy = pt.manage_copyObjects(old_type) |
---|
116 | pt.manage_pasteObjects(cb_copy_data=copy) |
---|
117 | copy_name = 'copy_of_%s' %old_type |
---|
118 | copy_obj = pt._getOb(copy_name) |
---|
119 | copy_obj.manage_renameObject(copy_name, new_type) |
---|
120 | setattr(copy_obj, 'title', new_type) |
---|
121 | |
---|
122 | def create_repType(self, out): |
---|
123 | pt = getToolByName(self, 'portal_types') |
---|
124 | if REP_TYPE in pt.objectIds(): |
---|
125 | print >> out, "%s already installed ..." %REP_TYPE |
---|
126 | pt._delObject(REP_TYPE) |
---|
127 | repurpose_content_type(self, 'Link', 'Campaign') |
---|
128 | print >> out, "Repurpose content successfully" |
---|
129 | |
---|
130 | def install_configlet(self, out): |
---|
131 | control_panel=getToolByName(self,'portal_controlpanel') |
---|
132 | control_panel.registerConfiglet(PROJECTNAME, |
---|
133 | 'Click Tracking Tool', |
---|
134 | 'string:${portal_url}/%s/portal_clicktracker_view' % TOOLID, |
---|
135 | permission=MANAGE_CLICKTRACKINGTOOL_PERMISSION, |
---|
136 | imageUrl='link_icon.gif', |
---|
137 | category='Products', |
---|
138 | ) |
---|
139 | print >> out, "Installed configlet.,," |
---|
140 | |
---|
141 | def install_workflow(self, out): |
---|
142 | portal_workflow=getToolByName(self, 'portal_workflow') |
---|
143 | |
---|
144 | #register the workflow in the system |
---|
145 | addWorkflowFactory(createWorkflow, |
---|
146 | id=C_WORKFLOWID, |
---|
147 | title='Campaign Workflow') |
---|
148 | |
---|
149 | portal_workflow.manage_addWorkflow(id=C_WORKFLOWID, workflow_type=C_WORKFLOWID +' (Campaign Workflow)') |
---|
150 | #set workflow for Campaign |
---|
151 | portal_workflow.setChainForPortalTypes( ('Campaign'), C_WORKFLOWID) |
---|
152 | print >> out, "Installed workflow..." |
---|
153 | |
---|
154 | def fix_permissions(self, out): |
---|
155 | portal_workflow=getToolByName(self, 'portal_workflow') |
---|
156 | #remove worklow from tool |
---|
157 | portal_workflow.setChainForPortalTypes( ('ClickTracker (qClickTrackingTool)'), '') |
---|
158 | #set permission for portal_clicktracker |
---|
159 | pc=getToolByName(self, 'portal_clicktracker') |
---|
160 | pc.manage_permission(CMFCorePermissions.ListFolderContents, ('Manager', 'Owner', 'Anonymous'), 1) |
---|
161 | |
---|
162 | print >> out, "Click Tracking Tool Permissions fixed..." |
---|
163 | |
---|
164 | def remove_configlet(self, out): |
---|
165 | # unregister Configlet |
---|
166 | control_panel=getToolByName(self,'portal_controlpanel') |
---|
167 | control_panel.unregisterConfiglet(PROJECTNAME) |
---|
168 | |
---|
169 | def install(self): |
---|
170 | |
---|
171 | out=StringIO(); |
---|
172 | |
---|
173 | pt = getToolByName(self, 'portal_types') |
---|
174 | installTypes(self, out, listTypes(PROJECTNAME), PROJECTNAME) |
---|
175 | install_subskin(self, out, GLOBALS) |
---|
176 | install_workflow(self, out) |
---|
177 | install_tool(self, out) |
---|
178 | install_configlet(self, out) |
---|
179 | fix_permissions(self, out) |
---|
180 | |
---|
181 | print >> out, "Successfully installed %s." % PROJECTNAME |
---|
182 | return out.getvalue() |
---|
183 | |
---|
184 | def uninstall(self): |
---|
185 | out = StringIO() |
---|
186 | |
---|
187 | remove_configlet(self, out) |
---|
188 | |
---|
189 | print >> out, "Successfully uninstalled %s." % PROJECTNAME |
---|
190 | return out.getvalue() |
---|