]> git.openstreetmap.org Git - osqa.git/blob - forum/subscriptions.py
Don't print anything.
[osqa.git] / forum / subscriptions.py
1 import os\r
2 import re\r
3 import datetime\r
4 from forum.models import User, Question, Comment, QuestionSubscription, SubscriptionSettings, Answer\r
5 from forum.utils.mail import send_email\r
6 from django.utils.translation import ugettext as _\r
7 from forum.actions import AskAction, AnswerAction, CommentAction, AcceptAnswerAction, UserJoinsAction, QuestionViewAction\r
8 from forum import settings\r
9 from django.db.models import Q, F\r
10 \r
11 def create_subscription_if_not_exists(question, user):\r
12     try:\r
13         subscription = QuestionSubscription.objects.get(question=question, user=user)\r
14     except:\r
15         subscription = QuestionSubscription(question=question, user=user)\r
16         subscription.save()\r
17 \r
18 def apply_default_filters(queryset, excluded_id):\r
19     return queryset.values('email', 'username').exclude(id=excluded_id)\r
20 \r
21 def create_recipients_dict(usr_list):\r
22     return [(s['username'], s['email'], {'username': s['username']}) for s in usr_list]\r
23 \r
24 def question_posted(action, new):\r
25     question = action.node\r
26 \r
27     subscribers = User.objects.values('email', 'username').filter(\r
28             Q(subscription_settings__enable_notifications=True, subscription_settings__new_question='i') |\r
29             (Q(subscription_settings__new_question_watched_tags='i') &\r
30               Q(marked_tags__name__in=question.tagnames.split(' ')) &\r
31               Q(tag_selections__reason='good'))\r
32     ).exclude(id=question.author.id).distinct()\r
33 \r
34     recipients = create_recipients_dict(subscribers)\r
35 \r
36     send_email(settings.EMAIL_SUBJECT_PREFIX + _("New question on %(app_name)s") % dict(app_name=settings.APP_SHORT_NAME),\r
37                recipients, "notifications/newquestion.html", {\r
38         'question': question,\r
39     })\r
40 \r
41     if question.author.subscription_settings.questions_asked:\r
42         subscription = QuestionSubscription(question=question, user=question.author)\r
43         subscription.save()\r
44 \r
45     new_subscribers = User.objects.filter(\r
46             Q(subscription_settings__all_questions=True) |\r
47             Q(subscription_settings__all_questions_watched_tags=True,\r
48                     marked_tags__name__in=question.tagnames.split(' '),\r
49                     tag_selections__reason='good'))\r
50 \r
51     for user in new_subscribers:\r
52         create_subscription_if_not_exists(question, user)\r
53 \r
54 AskAction.hook(question_posted)\r
55 \r
56 \r
57 def answer_posted(action, new):\r
58     answer = action.node\r
59     question = answer.question\r
60 \r
61     subscribers = question.subscribers.values('email', 'username').filter(\r
62             subscription_settings__enable_notifications=True,\r
63             subscription_settings__notify_answers=True,\r
64             subscription_settings__subscribed_questions='i'\r
65     ).exclude(id=answer.author.id).distinct()\r
66     recipients = create_recipients_dict(subscribers)\r
67 \r
68     send_email(settings.EMAIL_SUBJECT_PREFIX + _("New answer to '%(question_title)s'") % dict(question_title=question.title),\r
69                recipients, "notifications/newanswer.html", {\r
70         'question': question,\r
71         'answer': answer\r
72     }, threaded=False)\r
73 \r
74     if answer.author.subscription_settings.questions_answered:\r
75         create_subscription_if_not_exists(question, answer.author)\r
76 \r
77 AnswerAction.hook(answer_posted)\r
78 \r
79 \r
80 def comment_posted(action, new):\r
81     comment = action.node\r
82     post = comment.content_object\r
83 \r
84     if post.__class__ == Question:\r
85         question = post\r
86     else:\r
87         question = post.question\r
88 \r
89     subscribers = question.subscribers.values('email', 'username')\r
90 \r
91     q_filter = Q(subscription_settings__notify_comments=True) | Q(subscription_settings__notify_comments_own_post=True, id=post.author.id)\r
92 \r
93     #inreply = re.search('@\w+', comment.comment)\r
94     #if inreply is not None:\r
95     #    q_filter = q_filter | Q(subscription_settings__notify_reply_to_comments=True,\r
96     #                            username__istartswith=inreply.group(0)[1:],\r
97     ##                            comments__object_id=post.id,\r
98     #                            comments__content_type=ContentType.objects.get_for_model(post.__class__)\r
99     #                            )\r
100 \r
101     subscribers = subscribers.filter(\r
102             q_filter, subscription_settings__subscribed_questions='i', subscription_settings__enable_notifications=True\r
103     ).exclude(id=comment.user.id).distinct()\r
104 \r
105     recipients = create_recipients_dict(subscribers)\r
106 \r
107     send_email(settings.EMAIL_SUBJECT_PREFIX + _("New comment on %(question_title)s") % dict(question_title=question.title),\r
108                recipients, "notifications/newcomment.html", {\r
109                 'comment': comment,\r
110                 'post': post,\r
111                 'question': question,\r
112     }, threaded=False)\r
113 \r
114     if comment.user.subscription_settings.questions_commented:\r
115         create_subscription_if_not_exists(question, comment.user)\r
116 \r
117 CommentAction.hook(comment_posted)\r
118 \r
119 \r
120 def answer_accepted(action, new):\r
121     question = action.node.question\r
122 \r
123     subscribers = question.subscribers.values('email', 'username').filter(\r
124             subscription_settings__enable_notifications=True,\r
125             subscription_settings__notify_accepted=True,\r
126             subscription_settings__subscribed_questions='i'\r
127     ).exclude(id=action.node.accepted.by.id).distinct()\r
128     recipients = create_recipients_dict(subscribers)\r
129 \r
130     send_email(settings.EMAIL_SUBJECT_PREFIX + _("An answer to '%(question_title)s' was accepted") % dict(question_title=question.title),\r
131                recipients, "notifications/answeraccepted.html", {\r
132         'question': question,\r
133         'answer': action.node\r
134     }, threaded=False)\r
135 \r
136 AcceptAnswerAction.hook(answer_accepted)\r
137 \r
138 \r
139 def member_joined(action, new):\r
140     subscribers = User.objects.values('email', 'username').filter(\r
141             subscription_settings__enable_notifications=True,\r
142             subscription_settings__member_joins='i'\r
143     ).exclude(id=action.user.id).distinct()\r
144 \r
145     recipients = create_recipients_dict(subscribers)\r
146 \r
147     send_email(settings.EMAIL_SUBJECT_PREFIX + _("%(username)s is a new member on %(app_name)s") % dict(username=action.user.username, app_name=settings.APP_SHORT_NAME),\r
148                recipients, "notifications/newmember.html", {\r
149         'newmember': action.user,\r
150     }, threaded=False)\r
151 \r
152 UserJoinsAction.hook(member_joined)\r
153 \r
154 def question_viewed(action, new):\r
155     if not action.viewuser.is_authenticated():\r
156         return\r
157 \r
158     try:\r
159         subscription = QuestionSubscription.objects.get(question=action.node, user=action.viewuser)\r
160         subscription.last_view = datetime.datetime.now()\r
161         subscription.save()\r
162     except:\r
163         if action.viewuser.subscription_settings.questions_viewed:\r
164             subscription = QuestionSubscription(question=action.node, user=action.viewuser)\r
165             subscription.save()\r
166 \r
167 QuestionViewAction.hook(question_viewed)\r
168 \r
169 \r
170 #todo: translate this\r
171 #record_answer_event_re = re.compile("You have received (a|\d+) .*new response.*")\r
172 #def record_answer_event(instance, created, **kwargs):\r
173 #    if created:\r
174 #        q_author = instance.question.author\r
175 #        found_match = False\r
176 #        #print 'going through %d messages' % q_author.message_set.all().count()\r
177 #        for m in q_author.message_set.all():\r
178 ##            #print m.message\r
179 # #           match = record_answer_event_re.search(m.message)\r
180 #            if match:\r
181 #                found_match = True\r
182 #                try:\r
183 #                    cnt = int(match.group(1))\r
184 #                except:\r
185 #                    cnt = 1\r
186 ##                m.message = u"You have received %d <a href=\"%s?sort=responses\">new responses</a>."\\r
187 # #                           % (cnt+1, q_author.get_profile_url())\r
188 #\r
189 #                m.save()\r
190 #                break\r
191 #        if not found_match:\r
192 #            msg = u"You have received a <a href=\"%s?sort=responses\">new response</a>."\\r
193 #                    % q_author.get_profile_url()\r
194 #\r
195 #            q_author.message_set.create(message=msg)\r
196 #\r
197 #post_save.connect(record_answer_event, sender=Answer)