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
11 def create_subscription_if_not_exists(question, user):
\r
13 subscription = QuestionSubscription.objects.get(question=question, user=user)
\r
15 subscription = QuestionSubscription(question=question, user=user)
\r
18 def filter_subscribers(subscribers):
\r
19 subscribers = subscribers.exclude(is_active=False)
\r
21 if settings.DONT_NOTIFY_UNVALIDATED:
\r
22 return subscribers.exclude(email_isvalid=False)
\r
26 def question_posted(action, new):
\r
27 question = action.node
\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
36 subscribers = filter_subscribers(subscribers)
\r
38 send_template_email(subscribers, "notifications/newquestion.html", {'question': question})
\r
40 if question.author.subscription_settings.questions_asked:
\r
41 subscription = QuestionSubscription(question=question, user=question.author)
\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
50 for user in new_subscribers:
\r
51 create_subscription_if_not_exists(question, user)
\r
53 AskAction.hook(question_posted)
\r
56 def answer_posted(action, new):
\r
57 answer = action.node
\r
58 question = answer.question
\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
66 subscribers = filter_subscribers(subscribers)
\r
68 send_template_email(subscribers, "notifications/newanswer.html", {'answer': answer})
\r
70 if answer.author.subscription_settings.questions_answered:
\r
71 create_subscription_if_not_exists(question, answer.author)
\r
73 AnswerAction.hook(answer_posted)
\r
76 def comment_posted(action, new):
\r
77 comment = action.node
\r
78 post = comment.parent
\r
80 if post.__class__ == Question:
\r
83 question = post.question
\r
85 q_filter = Q(subscription_settings__notify_comments=True) | Q(subscription_settings__notify_comments_own_post=True, id=post.author.id)
\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
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
97 subscribers = filter_subscribers(subscribers)
\r
100 send_template_email(subscribers, "notifications/newcomment.html", {'comment': comment})
\r
102 if comment.user.subscription_settings.questions_commented:
\r
103 create_subscription_if_not_exists(question, comment.user)
\r
105 CommentAction.hook(comment_posted)
\r
108 def answer_accepted(action, new):
\r
109 question = action.node.question
\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
117 subscribers = filter_subscribers(subscribers)
\r
119 send_template_email(subscribers, "notifications/answeraccepted.html", {'answer': action.node})
\r
121 AcceptAnswerAction.hook(answer_accepted)
\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
130 subscribers = filter_subscribers(subscribers)
\r
132 send_template_email(subscribers, "notifications/newmember.html", {'newmember': action.user})
\r
134 UserJoinsAction.hook(member_joined)
\r
136 def question_viewed(action, new):
\r
137 if not action.viewuser.is_authenticated():
\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
145 if action.viewuser.subscription_settings.questions_viewed:
\r
146 subscription = QuestionSubscription(question=action.node, user=action.viewuser)
\r
147 subscription.save()
\r
149 QuestionViewAction.hook(question_viewed)
\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
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
163 # found_match = True
\r
165 # cnt = int(match.group(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
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
177 # q_author.message_set.create(message=msg)
\r
179 #post_save.connect(record_answer_event, sender=Answer)