source: products/qPloneComments/tags/3.0.2/utils.py

Last change on this file was 81, checked in by chervol, 18 years ago

branch plone2.0.5 fill in

  • Property svn:eol-style set to native
File size: 6.9 KB
Line 
1from Products.CMFCore.utils import getToolByName
2# Get apropriate property from (propery_sheeet) configlet
3def getProp(self, prop_name, marker=None):
4    result = marker
5    pp = getToolByName(self, 'portal_properties')
6    config_ps = getattr(pp, 'qPloneComments', None)
7    if config_ps:
8        result =  getattr(config_ps, prop_name, marker)
9    return result
10
11def publishDiscussion(self):
12    roles = ['Anonymous']
13    self.review_state = "published"
14    self.manage_permission('View', roles, acquire=1)
15    self._p_changed = 1
16    self.reindexObject()
17
18
19def setAnonymCommenting(context, allow=False):
20    portal = getToolByName(context, 'portal_url').getPortalObject()
21    if allow:
22        portal.manage_permission('Reply to item', ['Anonymous','Manager','Member'], 1)
23    else:
24        portal.manage_permission('Reply to item', ['Manager','Member'], 1)
25
26def manage_mails(reply, context, action):
27    def sendMails(props, actions, key):
28        for p in props:
29            if p in actions[key]:
30                send_email(reply, context, p)
31
32    prop_sheet = context.portal_properties['qPloneComments']
33    props = filter(lambda x: prop_sheet.getProperty(x), prop_sheet.propertyIds())
34
35    actions = { 'onPublish': ('enable_approve_user_notification',
36                              'enable_reply_user_notification',
37                              'enable_published_notification'),
38                'onDelete' :  ('enable_rejected_user_notification',),
39                'onApprove': ('enable_approve_notification',)}
40
41    if action == 'publishing':
42        sendMails(props, actions, 'onPublish')
43
44    elif action == 'deleting':
45        sendMails(props, actions, 'onDelete')
46
47    elif action == 'aproving':
48        sendMails(props, actions, 'onApprove')
49
50def getMsg(context, template, args):
51    return getattr(context, template)(**args)
52
53def allowEmail(context, reply, state, creator):
54    condition = getattr(context, 'emailCommentNotification', True)
55    if callable(condition):
56        condition = condition(reply=reply, state=state, creator=creator)
57    return condition
58
59def send_email(reply, context, state):
60    def getEmail(obj, context):
61        email = obj.getProperty('email', None)
62        if email is None:
63            creators = hasattr(obj, 'listCreators') and obj.listCreators() or [obj.Creator(),]
64            userid = creators and creators[0] or ""
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    subject = ''
97    if state == 'enable_approve_user_notification':
98        subject = 'Your comment on %s is now published'%context.Title()
99        if user_email:
100            template = 'notify_comment_template'
101            args={'mto': user_email,
102                  'mfrom': admin_email,
103                  'obj': reply_parent,
104                  'organization_name': organization_name,
105                  'name': creator_name}
106        else:
107            args = {}
108
109    elif state == 'enable_rejected_user_notification':
110        subject = 'Your comment on %s was not approved.'%context.Title()
111        if user_email:
112            template = 'rejected_comment_template'
113            args={'mto': user_email,
114                  'mfrom': admin_email,
115                  'obj': reply_parent,
116                  'organization_name': organization_name,
117                  'name': creator_name}
118        else:
119            args = {}
120
121    elif state == 'enable_reply_user_notification':
122        template = 'reply_notify_template'
123        subject = 'Someone replied to your comment on %s'%context.Title()
124        di_parrent = getDIParent(reply)
125        if di_parrent:
126            user_email = getEmail(di_parrent, context)
127            if user_email:
128                args={'mto': user_email,
129                      'mfrom': admin_email,
130                      'obj': reply_parent,
131                      'organization_name': organization_name,
132                      'name': di_parrent.getOwnerTuple()[1]}
133            else:
134                args = {}
135        else:
136            args = {}
137
138    elif state == 'enable_published_notification':
139        template = 'published_comment_template'
140        user_email = getParentOwnerEmail(reply, context)
141        if user_email:
142            args={'mto':user_email,
143                  'mfrom':admin_email,
144                  'obj':reply_parent,
145                  'organization_name':organization_name}
146            subject = '[%s] New comment added'%organization_name
147        else:
148            args = {}
149
150    elif state == 'enable_approve_notification':
151        template = 'approve_comment_template'
152        user_email = getProp(context, "email_discussion_manager", None)
153        if user_email:
154            args={'mto':user_email,
155                  'mfrom':admin_email,
156                  'obj':reply_parent,
157                  'organization_name':organization_name}
158            subject = '[%s] New comment awaits moderation'%organization_name
159        else:
160            args = {}
161
162    if args:
163        msg = getMsg(context, template, args)
164        p_utils = context.plone_utils
165        site_props = context.portal_properties.site_properties
166        host = p_utils.getMailHost()
167        host.secureSend(msg, user_email, admin_email,
168                        subject = subject,
169                        subtype = 'plain',
170                        debug = False,
171                        charset = site_props.getProperty('default_charset', 'utf-8'),
172                        From = admin_email)
173
174
175HAS_MESSAGEFACTORY = True
176try:
177    from Products.CMFPlone import MessageFactory
178except ImportError:
179    HAS_MESSAGEFACTORY = False
180
181def getTranslFunction(context):
182    if HAS_MESSAGEFACTORY:
183        func = MessageFactory('plonecomments')
184    else:
185        func = lambda x:context.translate(x, domain='plonecomments')
186    return func
187
188def setStatusMsg(state, context, msg):
189    transl = getTranslFunction(context)
190    if HAS_MESSAGEFACTORY:
191        context.plone_utils.addPortalMessage(transl(msg))
192    else:
193        state.set(portal_status_message=transl(msg))
Note: See TracBrowser for help on using the repository browser.