5 from forum.models import User, Question, Comment, QuestionSubscription, SubscriptionSettings, Answer
6 from forum.utils.mail import send_template_email
7 from django.utils.translation import ugettext as _
8 from forum.actions import AskAction, AnswerAction, CommentAction, AcceptAnswerAction, UserJoinsAction, QuestionViewAction
9 from forum import settings
10 from django.db.models import Q, F
12 def create_subscription_if_not_exists(question, user):
14 subscription = QuestionSubscription.objects.get(question=question, user=user)
16 subscription = QuestionSubscription(question=question, user=user)
19 def filter_subscribers(subscribers):
20 subscribers = subscribers.exclude(is_active=False)
22 if settings.DONT_NOTIFY_UNVALIDATED:
23 return subscribers.exclude(email_isvalid=False)
27 def question_posted(action, new):
28 question = action.node
30 subscribers = User.objects.filter(
31 Q(subscription_settings__enable_notifications=True, subscription_settings__new_question='i') |
32 (Q(subscription_settings__new_question_watched_tags='i') &
33 Q(marked_tags__name__in=question.tagnames.split(' ')) &
34 Q(tag_selections__reason='good'))
35 ).exclude(id=question.author.id).distinct()
37 subscribers = filter_subscribers(subscribers)
39 send_template_email(subscribers, "notifications/newquestion.html", {'question': question})
41 subscription = QuestionSubscription(question=question, user=question.author)
44 new_subscribers = User.objects.filter(
45 Q(subscription_settings__all_questions=True) |
46 Q(subscription_settings__all_questions_watched_tags=True,
47 marked_tags__name__in=question.tagnames.split(' '),
48 tag_selections__reason='good'))
50 for user in new_subscribers:
51 create_subscription_if_not_exists(question, user)
53 AskAction.hook(question_posted)
56 def answer_posted(action, new):
58 question = answer.question
60 subscribers = question.subscribers.filter(
61 subscription_settings__enable_notifications=True,
62 subscription_settings__notify_answers=True,
63 subscription_settings__subscribed_questions='i'
64 ).exclude(id=answer.author.id).distinct()
66 subscribers = filter_subscribers(subscribers)
68 send_template_email(subscribers, "notifications/newanswer.html", {'answer': answer})
70 create_subscription_if_not_exists(question, answer.author)
72 AnswerAction.hook(answer_posted)
75 def comment_posted(action, new):
79 if post.__class__ == Question:
82 question = post.question
84 q_filter = Q(subscription_settings__notify_comments=True) | Q(subscription_settings__notify_comments_own_post=True, id=post.author.id)
86 inreply = re.search('@\w+', comment.comment)
87 if inreply is not None:
88 q_filter = q_filter | Q(subscription_settings__notify_reply_to_comments=True,
89 username__istartswith=inreply.group(0)[1:],
90 nodes__parent=post, nodes__node_type="comment")
92 subscribers = question.subscribers.filter(
93 q_filter, subscription_settings__subscribed_questions='i', subscription_settings__enable_notifications=True
94 ).exclude(id=comment.user.id).distinct()
96 subscribers = filter_subscribers(subscribers)
99 send_template_email(subscribers, "notifications/newcomment.html", {'comment': comment})
101 create_subscription_if_not_exists(question, comment.user)
103 CommentAction.hook(comment_posted)
106 def answer_accepted(action, new):
107 question = action.node.question
109 subscribers = question.subscribers.filter(
110 subscription_settings__enable_notifications=True,
111 subscription_settings__subscribed_questions='i'
112 ).exclude(id=action.node.nstate.accepted.by.id).distinct()
114 subscribers = filter_subscribers(subscribers)
116 send_template_email(subscribers, "notifications/answeraccepted.html", {'answer': action.node})
118 AcceptAnswerAction.hook(answer_accepted)
121 def member_joined(action, new):
122 subscribers = User.objects.filter(
123 subscription_settings__enable_notifications=True,
124 subscription_settings__member_joins='i'
125 ).exclude(id=action.user.id).distinct()
127 subscribers = filter_subscribers(subscribers)
129 send_template_email(subscribers, "notifications/newmember.html", {'newmember': action.user})
131 UserJoinsAction.hook(member_joined)
133 def question_viewed(action, new):
134 if not action.viewuser.is_authenticated():
138 subscription = QuestionSubscription.objects.get(question=action.node, user=action.viewuser)
139 subscription.last_view = datetime.datetime.now()
142 if action.viewuser.subscription_settings.questions_viewed:
143 subscription = QuestionSubscription(question=action.node, user=action.viewuser)
146 QuestionViewAction.hook(question_viewed)
149 #todo: translate this
150 #record_answer_event_re = re.compile("You have received (a|\d+) .*new response.*")
151 #def record_answer_event(instance, created, **kwargs):
153 # q_author = instance.question.author
154 # found_match = False
155 # #print 'going through %d messages' % q_author.message_set.all().count()
156 # for m in q_author.message_set.all():
158 # # match = record_answer_event_re.search(m.message)
162 # cnt = int(match.group(1))
165 ## m.message = u"You have received %d <a href=\"%s?sort=responses\">new responses</a>."\
166 # # % (cnt+1, q_author.get_profile_url())
170 # if not found_match:
171 # msg = u"You have received a <a href=\"%s?sort=responses\">new response</a>."\
172 # % q_author.get_profile_url()
174 # q_author.message_set.create(message=msg)
176 #post_save.connect(record_answer_event, sender=Answer)