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