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 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 create_subscription_if_not_exists(question, answer.author)
\r
72 AnswerAction.hook(answer_posted)
\r
75 def comment_posted(action, new):
\r
76 comment = action.node
\r
77 post = comment.parent
\r
79 if post.__class__ == Question:
\r
82 question = post.question
\r
84 q_filter = Q(subscription_settings__notify_comments=True) | Q(subscription_settings__notify_comments_own_post=True, id=post.author.id)
\r
86 inreply = re.search('@\w+', comment.comment)
\r
87 if inreply is not None:
\r
88 q_filter = q_filter | Q(subscription_settings__notify_reply_to_comments=True,
\r
89 username__istartswith=inreply.group(0)[1:],
\r
90 nodes__parent=post, nodes__node_type="comment")
\r
92 subscribers = question.subscribers.filter(
\r
93 q_filter, subscription_settings__subscribed_questions='i', subscription_settings__enable_notifications=True
\r
94 ).exclude(id=comment.user.id).distinct()
\r
96 subscribers = filter_subscribers(subscribers)
\r
99 send_template_email(subscribers, "notifications/newcomment.html", {'comment': comment})
\r
101 create_subscription_if_not_exists(question, comment.user)
\r
103 CommentAction.hook(comment_posted)
\r
106 def answer_accepted(action, new):
\r
107 question = action.node.question
\r
109 subscribers = question.subscribers.filter(
\r
110 subscription_settings__enable_notifications=True,
\r
111 subscription_settings__subscribed_questions='i'
\r
112 ).exclude(id=action.node.nstate.accepted.by.id).distinct()
\r
114 subscribers = filter_subscribers(subscribers)
\r
116 send_template_email(subscribers, "notifications/answeraccepted.html", {'answer': action.node})
\r
118 AcceptAnswerAction.hook(answer_accepted)
\r
121 def member_joined(action, new):
\r
122 subscribers = User.objects.filter(
\r
123 subscription_settings__enable_notifications=True,
\r
124 subscription_settings__member_joins='i'
\r
125 ).exclude(id=action.user.id).distinct()
\r
127 subscribers = filter_subscribers(subscribers)
\r
129 send_template_email(subscribers, "notifications/newmember.html", {'newmember': action.user})
\r
131 UserJoinsAction.hook(member_joined)
\r
133 def question_viewed(action, new):
\r
134 if not action.viewuser.is_authenticated():
\r
138 subscription = QuestionSubscription.objects.get(question=action.node, user=action.viewuser)
\r
139 subscription.last_view = datetime.datetime.now()
\r
140 subscription.save()
\r
142 if action.viewuser.subscription_settings.questions_viewed:
\r
143 subscription = QuestionSubscription(question=action.node, user=action.viewuser)
\r
144 subscription.save()
\r
146 QuestionViewAction.hook(question_viewed)
\r
149 #todo: translate this
\r
150 #record_answer_event_re = re.compile("You have received (a|\d+) .*new response.*")
\r
151 #def record_answer_event(instance, created, **kwargs):
\r
153 # q_author = instance.question.author
\r
154 # found_match = False
\r
155 # #print 'going through %d messages' % q_author.message_set.all().count()
\r
156 # for m in q_author.message_set.all():
\r
157 ## #print m.message
\r
158 # # match = record_answer_event_re.search(m.message)
\r
160 # found_match = True
\r
162 # cnt = int(match.group(1))
\r
165 ## m.message = u"You have received %d <a href=\"%s?sort=responses\">new responses</a>."\
\r
166 # # % (cnt+1, q_author.get_profile_url())
\r
170 # if not found_match:
\r
171 # msg = u"You have received a <a href=\"%s?sort=responses\">new response</a>."\
\r
172 # % q_author.get_profile_url()
\r
174 # q_author.message_set.create(message=msg)
\r
176 #post_save.connect(record_answer_event, sender=Answer)
\r