source: products/qPloneComments/tags/2.3/utils.py @ 1591

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

Building directory structure

File size: 6.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
51def getMsg(context, template, args):
52    return getattr(context, template)(**args)
53
54def allowEmail(context, reply, state, creator):
55    condition = getattr(context, 'emailCommentNotification', True)
56    if callable(condition):
57        condition = condition(reply=reply, state=state, creator=creator)
58    return condition
59
60def send_email(reply, context, state):
61    def getEmail(obj, context):
62        email = obj.getProperty('email', None)
63        if email is None:
64            userid = obj.getOwnerTuple()[1]
65            creator = getToolByName(context, 'portal_membership').getMemberById(userid)
66            if creator and allowEmail(context, reply, state, creator):
67                return creator.getProperty('email', '')
68        else:
69            return email
70        return ''
71
72    def getParent(reply):
73        reply = reply.inReplyTo()
74        return reply.meta_type == 'Discussion Item' and getParent(reply) or reply
75
76    def getDIParent(reply):
77        r = reply.inReplyTo()
78        return r.meta_type == 'Discussion Item' and r or None
79
80    def getParentOwnerEmail(reply, context):
81        creator_id = getParent(reply).getOwnerTuple()[1]
82        creator = getToolByName(context, 'portal_membership').getMemberById(creator_id)
83        if creator and allowEmail(context, reply, state, creator):
84            return creator.getProperty('email', '')
85        return ''
86
87    args = {}
88    if reply:
89        user_email = getEmail(reply, context)
90        reply_parent = getParent(reply)
91
92    organization_name = getProp(context, 'email_subject_prefix', '')
93    creator_name = reply.getOwnerTuple()[1]
94    admin_email = context.portal_url.getPortalObject().getProperty('email_from_address')
95
96    if state == 'enable_approve_user_notification':
97        if user_email:
98            template = 'notify_comment_template'
99            args={'mto': user_email,
100                  'mfrom': admin_email,
101                  'obj': reply_parent,
102                  'organization_name': organization_name,
103                  'name': creator_name}
104        else:
105            args = {}
106
107    elif state == 'enable_rejected_user_notification':
108        if user_email:
109            template = 'rejected_comment_template'
110            args={'mto': user_email,
111                  'mfrom': admin_email,
112                  'obj': reply_parent,
113                  'organization_name': organization_name,
114                  'name': creator_name}
115        else:
116            args = {}
117
118    elif state == 'enable_reply_user_notification':
119        template = 'reply_notify_template'
120        di_parrent = getDIParent(reply)
121        if di_parrent:
122            user_email = getEmail(di_parrent, context)
123            if user_email:
124                args={'mto': user_email,
125                      'mfrom': admin_email,
126                      'obj': reply_parent,
127                      'organization_name': organization_name,
128                      'name': di_parrent.getOwnerTuple()[1]}
129            else:
130                args = {}
131        else:
132            args = {}
133
134    elif state == 'enable_published_notification':
135        template = 'published_comment_template'
136        user_email = getParentOwnerEmail(reply, context)
137        if user_email:
138            args={'mto':user_email,
139                  'mfrom':admin_email,
140                  'obj':reply_parent,
141                  'organization_name':organization_name}
142        else:
143            args = {}
144
145    elif state == 'enable_approve_notification':
146        template = 'approve_comment_template'
147        user_email = getProp(context, "email_discussion_manager", None)
148        if user_email:
149            args={'mto':user_email,
150                  'mfrom':admin_email,
151                  'obj':reply_parent,
152                  'organization_name':organization_name}
153        else:
154            args = {}
155
156    if args:
157        msg = getMsg(context, template, args)
158        context.MailHost.send(msg)
159
160
161HAS_MESSAGEFACTORY = True
162try:
163    from Products.CMFPlone import MessageFactory
164except ImportError:
165    HAS_MESSAGEFACTORY = False
166
167def getTranslFunction(context):
168    if HAS_MESSAGEFACTORY:
169        func = MessageFactory('plonecomments')
170    else:
171        func = lambda x:context.translate(x, domain='plonecomments')
172    return func
173
174def setStatusMsg(state, context, msg):
175    transl = getTranslFunction(context)
176    if HAS_MESSAGEFACTORY:
177        context.plone_utils.addPortalMessage(transl(msg))
178    else:
179        state.set(portal_status_message=transl(msg))
Note: See TracBrowser for help on using the repository browser.