|
1 from cm.models import Activity, Text, Notification, Comment, ApplicationConfiguration |
|
2 from datetime import datetime, timedelta |
|
3 from django.contrib.auth.models import User |
|
4 from django.db.models import Q, signals |
|
5 from django.utils.translation import ugettext as _ |
|
6 from django.template.loader import render_to_string |
|
7 from time import mktime |
|
8 from django.conf import settings |
|
9 from cm.utils.mail import send_mail |
|
10 import django.dispatch |
|
11 import logging |
|
12 |
|
13 |
|
14 class FakeRequest(object): |
|
15 def __init__(self, user): |
|
16 self.user = user |
|
17 |
|
18 def notify(sender, **kwargs): |
|
19 from cm.security import get_viewable_comments, has_perm |
|
20 allready_notified = set() # avoid sending multiple notifications to same user |
|
21 |
|
22 activity = kwargs['instance'] |
|
23 if activity.type in Activity.VIEWABLE_ACTIVITIES.get('view_users'): # user activity: only viewed by managers |
|
24 notifications = Notification.objects.filter(text=None) |
|
25 for notification in notifications: |
|
26 if notification.user: |
|
27 from cm.security import user_has_perm # import here! |
|
28 if user_has_perm(notification.user, 'can_manage_workspace'): |
|
29 send_notification(activity, notification) |
|
30 allready_notified.add(notification.user) |
|
31 elif activity.type in Activity.VIEWABLE_ACTIVITIES.get('view_comments'): |
|
32 notifications = Notification.objects.filter(Q(text=activity.text) | Q(text=None)) |
|
33 for notification in notifications: |
|
34 viewable = get_viewable_comments(FakeRequest(notification.user), |
|
35 Comment.objects.filter(id__in = [activity.comment.id]), |
|
36 text=activity.text) |
|
37 if viewable and \ |
|
38 ((notification.type == 'own' and activity.comment.top_comment().user == notification.user) or |
|
39 (notification.type != 'own')): |
|
40 if not notification.user in allready_notified: |
|
41 send_notification(activity, notification) |
|
42 allready_notified.add(notification.user) |
|
43 elif activity.type in Activity.VIEWABLE_ACTIVITIES.get('view_texts'): |
|
44 notifications = Notification.objects.filter(Q(text=activity.text) | Q(text=None)) |
|
45 for notification in notifications: |
|
46 if notification.user: |
|
47 from cm.security import user_has_perm # import here! |
|
48 if user_has_perm(notification.user, 'can_view_text', text=activity.text) and not notification.user in allready_notified: |
|
49 send_notification(activity, notification) |
|
50 allready_notified.add(notification.user) |
|
51 else: |
|
52 if has_perm(None, 'can_view_text', text=activity.text) and not notification.email in allready_notified: |
|
53 send_notification(activity, notification) |
|
54 allready_notified.add(notification.email) |
|
55 |
|
56 signals.post_save.connect(notify, sender=Activity) |
|
57 |
|
58 def send_notification(activity, notification): |
|
59 email = notification.user.email if notification.user else notification.email |
|
60 subject = _('Notification:') + " " + activity.printable_data(html=False, link=False) |
|
61 message = render_to_string('email/activity_notification.txt', |
|
62 { |
|
63 'activity' : activity, |
|
64 'notification' : notification, |
|
65 'SITE_URL' : settings.SITE_URL, |
|
66 'CONF' : ApplicationConfiguration, |
|
67 }) |
|
68 |
|
69 send_mail(subject, message, ApplicationConfiguration['email_from'], [email], fail_silently=True) |
|
70 |
|
71 logging.debug("Notification sent [%s] => %s" %(activity,notification.user) if notification.user else "sending (email) %s => %s" %(activity,notification.email)) |