]> git.openstreetmap.org Git - osqa.git/blob - forum/subscriptions.py
disabling the submit buttons for question and answers.
[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_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 filter_subscribers(subscribers):\r
19     subscribers = subscribers.exclude(is_active=False)\r
20 \r
21     if settings.DONT_NOTIFY_UNVALIDATED:\r
22         return subscribers.exclude(email_isvalid=False)\r
23     else:\r
24         return subscribers\r
25 \r
26 def question_posted(action, new):\r
27     question = action.node\r
28 \r
29     subscribers = User.objects.filter(\r
30             Q(subscription_settings__enable_notifications=True, subscription_settings__new_question='i') |\r
31             (Q(subscription_settings__new_question_watched_tags='i') &\r
32               Q(marked_tags__name__in=question.tagnames.split(' ')) &\r
33               Q(tag_selections__reason='good'))\r
34     ).exclude(id=question.author.id).distinct()\r
35 \r
36     subscribers = filter_subscribers(subscribers)\r
37 \r
38     send_template_email(subscribers, "notifications/newquestion.html", {'question': question})\r
39 \r
40     if question.author.subscription_settings.questions_asked:\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     if answer.author.subscription_settings.questions_answered:\r
71         create_subscription_if_not_exists(question, answer.author)\r
72 \r
73 AnswerAction.hook(answer_posted)\r
74 \r
75 \r
76 def comment_posted(action, new):\r
77     comment = action.node\r
78     post = comment.parent\r
79 \r
80     if post.__class__ == Question:\r
81         question = post\r
82     else:\r
83         question = post.question\r
84 \r
85     q_filter = Q(subscription_settings__notify_comments=True) | Q(subscription_settings__notify_comments_own_post=True, id=post.author.id)\r
86 \r
87     inreply = re.search('@\w+', comment.comment)\r
88     if inreply is not None:\r
89         q_filter = q_filter | Q(subscription_settings__notify_reply_to_comments=True,\r
90                                 username__istartswith=inreply.group(0)[1:],\r
91                                 nodes__parent=post, nodes__node_type="comment")\r
92 \r
93     subscribers = question.subscribers.filter(\r
94             q_filter, subscription_settings__subscribed_questions='i', subscription_settings__enable_notifications=True\r
95     ).exclude(id=comment.user.id).distinct()\r
96 \r
97     subscribers = filter_subscribers(subscribers)\r
98 \r
99 \r
100     send_template_email(subscribers, "notifications/newcomment.html", {'comment': comment})\r
101 \r
102     if comment.user.subscription_settings.questions_commented:\r
103         create_subscription_if_not_exists(question, comment.user)\r
104 \r
105 CommentAction.hook(comment_posted)\r
106 \r
107 \r
108 def answer_accepted(action, new):\r
109     question = action.node.question\r
110 \r
111     subscribers = question.subscribers.filter(\r
112             subscription_settings__enable_notifications=True,\r
113             subscription_settings__notify_accepted=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)