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
12 def create_subscription_if_not_exists(question, user):
\r
14 subscription = QuestionSubscription.objects.get(question=question, user=user)
\r
16 subscription = QuestionSubscription(question=question, user=user)
\r
19 def filter_subscribers(subscribers):
\r
20 subscribers = subscribers.exclude(is_active=False)
\r
22 if settings.DONT_NOTIFY_UNVALIDATED:
\r
23 return subscribers.exclude(email_isvalid=False)
\r
27 def question_posted(action, new):
\r
28 question = action.node
\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
37 subscribers = filter_subscribers(subscribers)
\r
39 send_template_email(subscribers, "notifications/newquestion.html", {'question': question})
\r
41 if question.author.subscription_settings.questions_asked:
\r
42 subscription = QuestionSubscription(question=question, user=question.author)
\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
51 for user in new_subscribers:
\r
52 create_subscription_if_not_exists(question, user)
\r
54 AskAction.hook(question_posted)
\r
57 def answer_posted(action, new):
\r
58 answer = action.node
\r
59 question = answer.question
\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
67 subscribers = filter_subscribers(subscribers)
\r
69 send_template_email(subscribers, "notifications/newanswer.html", {'answer': answer})
\r
71 if answer.author.subscription_settings.questions_answered:
\r
72 create_subscription_if_not_exists(question, answer.author)
\r
74 AnswerAction.hook(answer_posted)
\r
77 def comment_posted(action, new):
\r
78 comment = action.node
\r
79 post = comment.parent
\r
81 if post.__class__ == Question:
\r
84 question = post.question
\r
86 q_filter = Q(subscription_settings__notify_comments=True) | Q(subscription_settings__notify_comments_own_post=True, id=post.author.id)
\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
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
98 subscribers = filter_subscribers(subscribers)
\r
101 send_template_email(subscribers, "notifications/newcomment.html", {'comment': comment})
\r
103 if comment.user.subscription_settings.questions_commented:
\r
104 create_subscription_if_not_exists(question, comment.user)
\r
106 CommentAction.hook(comment_posted)
\r
109 def answer_accepted(action, new):
\r
110 question = action.node.question
\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
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)
\r