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 question_posted(action, new):
\r
19 question = action.node
\r
21 subscribers = User.objects.filter(
\r
22 Q(subscription_settings__enable_notifications=True, subscription_settings__new_question='i') |
\r
23 (Q(subscription_settings__new_question_watched_tags='i') &
\r
24 Q(marked_tags__name__in=question.tagnames.split(' ')) &
\r
25 Q(tag_selections__reason='good'))
\r
26 ).exclude(id=question.author.id).distinct()
\r
28 send_template_email(subscribers, "notifications/newquestion.html", {'question': question})
\r
30 if question.author.subscription_settings.questions_asked:
\r
31 subscription = QuestionSubscription(question=question, user=question.author)
\r
34 new_subscribers = User.objects.filter(
\r
35 Q(subscription_settings__all_questions=True) |
\r
36 Q(subscription_settings__all_questions_watched_tags=True,
\r
37 marked_tags__name__in=question.tagnames.split(' '),
\r
38 tag_selections__reason='good'))
\r
40 for user in new_subscribers:
\r
41 create_subscription_if_not_exists(question, user)
\r
43 AskAction.hook(question_posted)
\r
46 def answer_posted(action, new):
\r
47 answer = action.node
\r
48 question = answer.question
\r
50 subscribers = question.subscribers.filter(
\r
51 subscription_settings__enable_notifications=True,
\r
52 subscription_settings__notify_answers=True,
\r
53 subscription_settings__subscribed_questions='i'
\r
54 ).exclude(id=answer.author.id).distinct()
\r
56 send_template_email(subscribers, "notifications/newanswer.html", {'answer': answer})
\r
58 if answer.author.subscription_settings.questions_answered:
\r
59 create_subscription_if_not_exists(question, answer.author)
\r
61 AnswerAction.hook(answer_posted)
\r
64 def comment_posted(action, new):
\r
65 comment = action.node
\r
66 post = comment.parent
\r
68 if post.__class__ == Question:
\r
71 question = post.question
\r
73 q_filter = Q(subscription_settings__notify_comments=True) | Q(subscription_settings__notify_comments_own_post=True, id=post.author.id)
\r
75 inreply = re.search('@\w+', comment.comment)
\r
76 if inreply is not None:
\r
77 q_filter = q_filter | Q(subscription_settings__notify_reply_to_comments=True,
\r
78 username__istartswith=inreply.group(0)[1:],
\r
79 nodes__parent=post, nodes__node_type="comment")
\r
81 subscribers = question.subscribers.filter(
\r
82 q_filter, subscription_settings__subscribed_questions='i', subscription_settings__enable_notifications=True
\r
83 ).exclude(id=comment.user.id).distinct()
\r
86 send_template_email(subscribers, "notifications/newcomment.html", {'comment': comment})
\r
88 if comment.user.subscription_settings.questions_commented:
\r
89 create_subscription_if_not_exists(question, comment.user)
\r
91 CommentAction.hook(comment_posted)
\r
94 def answer_accepted(action, new):
\r
95 question = action.node.question
\r
97 subscribers = question.subscribers.filter(
\r
98 subscription_settings__enable_notifications=True,
\r
99 subscription_settings__notify_accepted=True,
\r
100 subscription_settings__subscribed_questions='i'
\r
101 ).exclude(id=action.node.nstate.accepted.by.id).distinct()
\r
103 send_template_email(subscribers, "notifications/answeraccepted.html", {'answer': action.node})
\r
105 AcceptAnswerAction.hook(answer_accepted)
\r
108 def member_joined(action, new):
\r
109 subscribers = User.objects.filter(
\r
110 subscription_settings__enable_notifications=True,
\r
111 subscription_settings__member_joins='i'
\r
112 ).exclude(id=action.user.id).distinct()
\r
114 send_template_email(subscribers, "notifications/newmember.html", {'newmember': action.user})
\r
116 UserJoinsAction.hook(member_joined)
\r
118 def question_viewed(action, new):
\r
119 if not action.viewuser.is_authenticated():
\r
123 subscription = QuestionSubscription.objects.get(question=action.node, user=action.viewuser)
\r
124 subscription.last_view = datetime.datetime.now()
\r
125 subscription.save()
\r
127 if action.viewuser.subscription_settings.questions_viewed:
\r
128 subscription = QuestionSubscription(question=action.node, user=action.viewuser)
\r
129 subscription.save()
\r
131 QuestionViewAction.hook(question_viewed)
\r
134 #todo: translate this
\r
135 #record_answer_event_re = re.compile("You have received (a|\d+) .*new response.*")
\r
136 #def record_answer_event(instance, created, **kwargs):
\r
138 # q_author = instance.question.author
\r
139 # found_match = False
\r
140 # #print 'going through %d messages' % q_author.message_set.all().count()
\r
141 # for m in q_author.message_set.all():
\r
142 ## #print m.message
\r
143 # # match = record_answer_event_re.search(m.message)
\r
145 # found_match = True
\r
147 # cnt = int(match.group(1))
\r
150 ## m.message = u"You have received %d <a href=\"%s?sort=responses\">new responses</a>."\
\r
151 # # % (cnt+1, q_author.get_profile_url())
\r
155 # if not found_match:
\r
156 # msg = u"You have received a <a href=\"%s?sort=responses\">new response</a>."\
\r
157 # % q_author.get_profile_url()
\r
159 # q_author.message_set.create(message=msg)
\r
161 #post_save.connect(record_answer_event, sender=Answer)