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