]> git.openstreetmap.org Git - osqa.git/blob - forum/subscriptions.py
merge jambazov -> trunk
[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     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.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 \r
67     subscribers = filter_subscribers(subscribers)\r
68 \r
69     send_template_email(subscribers, "notifications/newanswer.html", {'answer': answer})\r
70 \r
71     if answer.author.subscription_settings.questions_answered:\r
72         create_subscription_if_not_exists(question, answer.author)\r
73 \r
74 AnswerAction.hook(answer_posted)\r
75 \r
76 \r
77 def comment_posted(action, new):\r
78     comment = action.node\r
79     post = comment.parent\r
80 \r
81     if post.__class__ == Question:\r
82         question = post\r
83     else:\r
84         question = post.question\r
85 \r
86     q_filter = Q(subscription_settings__notify_comments=True) | Q(subscription_settings__notify_comments_own_post=True, id=post.author.id)\r
87 \r
88     inreply = re.search('@\w+', comment.comment)\r
89     if inreply is not None:\r
90         q_filter = q_filter | Q(subscription_settings__notify_reply_to_comments=True,\r
91                                 username__istartswith=inreply.group(0)[1:],\r
92                                 nodes__parent=post, nodes__node_type="comment")\r
93 \r
94     subscribers = question.subscribers.filter(\r
95             q_filter, subscription_settings__subscribed_questions='i', subscription_settings__enable_notifications=True\r
96     ).exclude(id=comment.user.id).distinct()\r
97 \r
98     subscribers = filter_subscribers(subscribers)\r
99 \r
100 \r
101     send_template_email(subscribers, "notifications/newcomment.html", {'comment': comment})\r
102 \r
103     if comment.user.subscription_settings.questions_commented:\r
104         create_subscription_if_not_exists(question, comment.user)\r
105 \r
106 CommentAction.hook(comment_posted)\r
107 \r
108 \r
109 def answer_accepted(action, new):\r
110     question = action.node.question\r
111 \r
112     subscribers = question.subscribers.filter(\r
113             subscription_settings__enable_notifications=True,\r
114             subscription_settings__subscribed_questions='i'\r
115     ).exclude(id=action.node.nstate.accepted.by.id).distinct()\r
116     \r
117     subscribers = filter_subscribers(subscribers)\r
118 \r
119     send_template_email(subscribers, "notifications/answeraccepted.html", {'answer': action.node})\r
120 \r
121 AcceptAnswerAction.hook(answer_accepted)\r
122 \r
123 \r
124 def member_joined(action, new):\r
125     subscribers = User.objects.filter(\r
126             subscription_settings__enable_notifications=True,\r
127             subscription_settings__member_joins='i'\r
128     ).exclude(id=action.user.id).distinct()\r
129 \r
130     subscribers = filter_subscribers(subscribers)\r
131 \r
132     send_template_email(subscribers, "notifications/newmember.html", {'newmember': action.user})\r
133 \r
134 UserJoinsAction.hook(member_joined)\r
135 \r
136 def question_viewed(action, new):\r
137     if not action.viewuser.is_authenticated():\r
138         return\r
139 \r
140     try:\r
141         subscription = QuestionSubscription.objects.get(question=action.node, user=action.viewuser)\r
142         subscription.last_view = datetime.datetime.now()\r
143         subscription.save()\r
144     except:\r
145         if action.viewuser.subscription_settings.questions_viewed:\r
146             subscription = QuestionSubscription(question=action.node, user=action.viewuser)\r
147             subscription.save()\r
148 \r
149 QuestionViewAction.hook(question_viewed)\r
150 \r
151 \r
152 #todo: translate this\r
153 #record_answer_event_re = re.compile("You have received (a|\d+) .*new response.*")\r
154 #def record_answer_event(instance, created, **kwargs):\r
155 #    if created:\r
156 #        q_author = instance.question.author\r
157 #        found_match = False\r
158 #        #print 'going through %d messages' % q_author.message_set.all().count()\r
159 #        for m in q_author.message_set.all():\r
160 ##            #print m.message\r
161 # #           match = record_answer_event_re.search(m.message)\r
162 #            if match:\r
163 #                found_match = True\r
164 #                try:\r
165 #                    cnt = int(match.group(1))\r
166 #                except:\r
167 #                    cnt = 1\r
168 ##                m.message = u"You have received %d <a href=\"%s?sort=responses\">new responses</a>."\\r
169 # #                           % (cnt+1, q_author.get_profile_url())\r
170 #\r
171 #                m.save()\r
172 #                break\r
173 #        if not found_match:\r
174 #            msg = u"You have received a <a href=\"%s?sort=responses\">new response</a>."\\r
175 #                    % q_author.get_profile_url()\r
176 #\r
177 #            q_author.message_set.create(message=msg)\r
178 #\r
179 #post_save.connect(record_answer_event, sender=Answer)\r