4 from forum.models import User, Question, Comment, QuestionSubscription, SubscriptionSettings, Answer
\r
5 from forum.utils.mail import send_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 django.conf 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 apply_default_filters(queryset, excluded_id):
\r
19 return queryset.values('email', 'username').exclude(id=excluded_id)
\r
21 def create_recipients_dict(usr_list):
\r
22 return [(s['username'], s['email'], {'username': s['username']}) for s in usr_list]
\r
24 def question_posted(action, new):
\r
25 question = action.node
\r
27 subscribers = User.objects.values('email', 'username').filter(
\r
28 Q(subscription_settings__enable_notifications=True, subscription_settings__new_question='i') |
\r
29 (Q(subscription_settings__new_question_watched_tags='i') &
\r
30 Q(marked_tags__name__in=question.tagnames.split(' ')) &
\r
31 Q(tag_selections__reason='good'))
\r
32 ).exclude(id=question.author.id).distinct()
\r
34 recipients = create_recipients_dict(subscribers)
\r
36 send_email(settings.EMAIL_SUBJECT_PREFIX + _("New question on %(app_name)s") % dict(app_name=settings.APP_SHORT_NAME),
\r
37 recipients, "notifications/newquestion.html", {
\r
38 '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.values('email', 'username').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
66 recipients = create_recipients_dict(subscribers)
\r
68 send_email(settings.EMAIL_SUBJECT_PREFIX + _("New answer to '%(question_title)s'") % dict(question_title=question.title),
\r
69 recipients, "notifications/newanswer.html", {
\r
70 'question': question,
\r
74 if answer.author.subscription_settings.questions_answered:
\r
75 create_subscription_if_not_exists(question, answer.author)
\r
77 AnswerAction.hook(answer_posted)
\r
80 def comment_posted(action, new):
\r
81 comment = action.node
\r
82 post = comment.content_object
\r
84 if post.__class__ == Question:
\r
87 question = post.question
\r
89 subscribers = question.subscribers.values('email', 'username')
\r
91 q_filter = Q(subscription_settings__notify_comments=True) | Q(subscription_settings__notify_comments_own_post=True, id=post.author.id)
\r
93 #inreply = re.search('@\w+', comment.comment)
\r
94 #if inreply is not None:
\r
95 # q_filter = q_filter | Q(subscription_settings__notify_reply_to_comments=True,
\r
96 # username__istartswith=inreply.group(0)[1:],
\r
97 ## comments__object_id=post.id,
\r
98 # comments__content_type=ContentType.objects.get_for_model(post.__class__)
\r
101 subscribers = subscribers.filter(
\r
102 q_filter, subscription_settings__subscribed_questions='i', subscription_settings__enable_notifications=True
\r
103 ).exclude(id=comment.user.id).distinct()
\r
105 recipients = create_recipients_dict(subscribers)
\r
107 send_email(settings.EMAIL_SUBJECT_PREFIX + _("New comment on %(question_title)s") % dict(question_title=question.title),
\r
108 recipients, "notifications/newcomment.html", {
\r
109 'comment': comment,
\r
111 'question': question,
\r
114 if comment.user.subscription_settings.questions_commented:
\r
115 create_subscription_if_not_exists(question, comment.user)
\r
117 CommentAction.hook(comment_posted)
\r
120 def answer_accepted(action, new):
\r
121 question = action.node.question
\r
123 subscribers = question.subscribers.values('email', 'username').filter(
\r
124 subscription_settings__enable_notifications=True,
\r
125 subscription_settings__notify_accepted=True,
\r
126 subscription_settings__subscribed_questions='i'
\r
127 ).exclude(id=instance.accepted_by.id).distinct()
\r
128 recipients = create_recipients_dict(subscribers)
\r
130 send_email(settings.EMAIL_SUBJECT_PREFIX + _("An answer to '%(question_title)s' was accepted") % dict(question_title=question.title),
\r
131 recipients, "notifications/answeraccepted.html", {
\r
132 'question': question,
\r
133 'answer': action.node
\r
136 AcceptAnswerAction.hook(answer_accepted)
\r
139 def member_joined(action, new):
\r
140 subscribers = User.objects.values('email', 'username').filter(
\r
141 subscription_settings__enable_notifications=True,
\r
142 subscription_settings__member_joins='i'
\r
143 ).exclude(id=action.user.id).distinct()
\r
145 recipients = create_recipients_dict(subscribers)
\r
147 send_email(settings.EMAIL_SUBJECT_PREFIX + _("%(username)s is a new member on %(app_name)s") % dict(username=instance.username, app_name=settings.APP_SHORT_NAME),
\r
148 recipients, "notifications/newmember.html", {
\r
149 'newmember': action.user,
\r
152 UserJoinsAction.hook(member_joined)
\r
154 def question_viewed(action, new):
\r
155 if not action.viewuser.is_authenticated():
\r
159 subscription = QuestionSubscription.objects.get(question=action.node, user=action.viewuser)
\r
160 subscription.last_view = datetime.datetime.now()
\r
161 subscription.save()
\r
163 if action.viewuser.subscription_settings.questions_viewed:
\r
164 subscription = QuestionSubscription(question=action.node, user=action.viewuser)
\r
165 subscription.save()
\r
167 QuestionViewAction.hook(question_viewed)
\r
170 #todo: translate this
\r
171 #record_answer_event_re = re.compile("You have received (a|\d+) .*new response.*")
\r
172 #def record_answer_event(instance, created, **kwargs):
\r
174 # q_author = instance.question.author
\r
175 # found_match = False
\r
176 # #print 'going through %d messages' % q_author.message_set.all().count()
\r
177 # for m in q_author.message_set.all():
\r
178 ## #print m.message
\r
179 # # match = record_answer_event_re.search(m.message)
\r
181 # found_match = True
\r
183 # cnt = int(match.group(1))
\r
186 ## m.message = u"You have received %d <a href=\"%s?sort=responses\">new responses</a>."\
\r
187 # # % (cnt+1, q_author.get_profile_url())
\r
191 # if not found_match:
\r
192 # msg = u"You have received a <a href=\"%s?sort=responses\">new response</a>."\
\r
193 # % q_author.get_profile_url()
\r
195 # q_author.message_set.create(message=msg)
\r
197 #post_save.connect(record_answer_event, sender=Answer)