source: products/qPloneComments/tags/2.2.0/utils.py @ 1

Last change on this file since 1 was 1, checked in by myroslav, 18 years ago

Building directory structure

File size: 5.1 KB
Line 
1from Products.CMFCore.utils import getToolByName
2
3# Get apropriate property from (propery_sheeet) configlet
4def getProp(self, prop_name, marker=None):
5    result = marker
6    pp = getToolByName(self, 'portal_properties')
7    config_ps = getattr(pp, 'qPloneComments', None)
8    if config_ps:
9        result =  getattr(config_ps, prop_name, marker)
10    return result
11
12def publishDiscussion(self):
13    roles = ['Anonymous']
14    self.review_state = "published"
15    self.manage_permission('View', roles, acquire=1)
16    self._p_changed = 1
17    self.reindexObject()
18
19
20def setAnonymCommenting(context, allow=False):
21    portal = getToolByName(context, 'portal_url').getPortalObject()
22    if allow:
23        portal.manage_permission('Reply to item', ['Anonymous','Manager','Member'], 1)
24    else:
25        portal.manage_permission('Reply to item', ['Manager','Member'], 1)
26
27def manage_mails(reply, context, action):
28    def sendMails(props, actions, key):
29        for p in props:
30            if p in actions[key]:
31                send_email(reply, context, p)
32
33    prop_sheet = context.portal_properties['qPloneComments']
34    props = filter(lambda x: prop_sheet.getProperty(x), prop_sheet.propertyIds())
35
36    actions = { 'onPublish': ('enable_approve_user_notification',
37                              'enable_reply_user_notification',
38                              'enable_published_notification'),
39                'onDelete' :  ('enable_rejected_user_notification',),
40                'onApprove': ('enable_approve_notification',)}
41
42    if action == 'publishing':
43        sendMails(props, actions, 'onPublish')
44
45    elif action == 'deleting':
46        sendMails(props, actions, 'onDelete')
47
48    elif action == 'aproving':
49        sendMails(props, actions, 'onApprove')
50
51
52def send_email(reply, context, state):
53    def send(context, template, args):
54        msg = getattr(context, template)(**args)
55        return context.MailHost.send(msg)
56
57    def getEmail(obj, context):
58        userid = obj.getProperty('userid', '')
59        if userid == 'anonym':
60            return obj.getProperty('email', '')
61        else:
62            return context.portal_membership.getMemberById(userid).getProperty('email', '')
63
64    def getParent(reply):
65        reply = reply.inReplyTo()
66        return reply.meta_type == 'Discussion Item' and getParent(reply) or reply
67
68    def getDIParent(reply):
69        r = reply.inReplyTo()
70        return r.meta_type == 'Discussion Item' and r or None
71
72    def getParentOwnerEmail(reply, context):
73        creator_id = getParent(reply).Creator()
74        creator = getToolByName(context, 'portal_membership').getMemberById(creator_id)
75        if creator:
76            return creator.getProperty('email', '')
77        return ''
78
79    args = {}
80    if reply:
81        user_email = getEmail(reply, context)
82        reply_parent = getParent(reply)
83
84    organization_name = getProp(context, 'email_subject_prefix', '')
85    creator_name = reply.Creator()
86    admin_email = context.portal_url.getPortalObject().getProperty('email_from_address')
87
88    if state == 'enable_approve_user_notification':
89        if user_email:
90            template = 'notify_comment_template'
91            args={'mto': user_email,
92                  'mfrom': admin_email,
93                  'obj': reply_parent,
94                  'organization_name': organization_name,
95                  'name': creator_name}
96        else:
97            args = {}
98
99    elif state == 'enable_rejected_user_notification':
100        if user_email:
101            template = 'rejected_comment_template'
102            args={'mto': user_email,
103                  'mfrom': admin_email,
104                  'obj': reply_parent,
105                  'organization_name': organization_name,
106                  'name': creator_name}
107        else:
108            args = {}
109
110    elif state == 'enable_reply_user_notification':
111        template = 'reply_notify_template'
112        di_parrent = getDIParent(reply)
113        if di_parrent:
114            user_email = getEmail(di_parrent, context)
115            if user_email:
116                args={'mto': user_email,
117                      'mfrom': admin_email,
118                      'obj': reply_parent,
119                      'organization_name': organization_name,
120                      'name': creator_name}
121            else:
122                args = {}
123        else:
124            args = {}
125
126    elif state == 'enable_published_notification':
127        template = 'published_comment_template'
128        user_email = getParentOwnerEmail(reply, context)
129        if user_email:
130            args={'mto':user_email,
131                  'mfrom':admin_email,
132                  'obj':reply_parent,
133                  'organization_name':organization_name}
134        else:
135            args = {}
136
137    elif state == 'enable_approve_notification':
138        template = 'approve_comment_template'
139        user_email = getProp(context, "email_discussion_manager", None)
140        if user_email:
141            args={'mto':user_email,
142                  'mfrom':admin_email,
143                  'obj':reply_parent,
144                  'organization_name':organization_name}
145        else:
146            args = {}
147
148    if args:
149        return send(context, template, args)
150    else:
151        return 1
Note: See TracBrowser for help on using the repository browser.