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