summaryrefslogtreecommitdiffstats
path: root/forum/models/content.py
diff options
context:
space:
mode:
Diffstat (limited to 'forum/models/content.py')
-rw-r--r--forum/models/content.py124
1 files changed, 122 insertions, 2 deletions
diff --git a/forum/models/content.py b/forum/models/content.py
index 5f144f1e..441f7133 100644
--- a/forum/models/content.py
+++ b/forum/models/content.py
@@ -5,6 +5,7 @@ from django.contrib.contenttypes import generic
from django.contrib.sitemaps import ping_google
from django.db import models
from forum.models.meta import Comment, Vote, FlaggedItem
+from forum.models.user import EmailFeedSetting
class Content(models.Model):
"""
@@ -75,6 +76,115 @@ class Content(models.Model):
self.comment_count = self.comment_count + 1
self.save()
+ def get_instant_notification_subscribers(
+ self,
+ potential_subscribers = None,
+ mentioned_users = None,
+ exclude_list = None,
+ ):
+ """get list of users who have subscribed to
+ receive instant notifications for a given post
+ this method works for questions and answers
+
+ parameter "potential_subscribers" is not used here,
+ but left for the uniformity of the interface (Comment method does use it)
+
+ comment class has it's own variant which does have quite a bit
+ of duplicated code at the moment
+ """
+ subscriber_set = set()
+
+ #1) mention subscribers - common to questions and answers
+ if mentioned_users:
+ mention_subscribers = EmailFeedSetting.objects.filter(
+ subscriber__in = mentioned_users,
+ feed_type = 'm_and_c',
+ frequency = 'i'
+ ).values_list(
+ 'subscriber',
+ flat=True
+ )
+ subscriber_set.update(mention_subscribers)
+
+ origin_post = self.get_origin_post()#handy to make generic method
+
+ #2) individually selected - make sure that users
+ #are individual subscribers to this question
+ selective_subscribers = origin_post.followed_by.all()
+ if selective_subscribers:
+ selective_subscribers = EmailFeedSetting.objects.filter(
+ subscriber__in = selective_subscribers,
+ feed_type = 'q_sel',
+ frequency = 'i'
+ ).values_list(
+ 'subscriber',
+ flat=True
+ )
+ for subscriber in selective_subscribers:
+ if origin_post.passes_tag_filter_for_user(subscriber):
+ subscriber_set.add(subscriber)
+
+ subscriber_set.update(selective_subscribers)
+
+ #3) whole forum subscibers
+ global_subscribers = EmailFeedSetting.objects.filter(
+ feed_type = 'q_all',
+ frequency = 'i'
+ ).values_list(
+ 'subscriber',
+ flat=True
+ )
+ #todo: apply tag filters here
+ subscriber_set.update(global_subscribers)
+
+ #4) question asked by me
+ question_author = origin_post.author
+ if EmailFeedSetting.objects.filter(
+ subscriber = question_author,
+ frequency = 'i',
+ feed_type = 'q_ask'
+ ):
+ subscriber_set.add(question_author)
+
+ #4) questions answered by me -make sure is that people
+ #are authors of the answers to this question
+ #todo: replace this with a query set method
+ answer_authors = set()
+ for answer in origin_post.answers.all():
+ authors = answer.get_author_list()
+ answer_authors.update(authors)
+
+ if answer_authors:
+ answer_authors = EmailFeedSetting.objects.filter(
+ subscriber__in = answer_authors,
+ frequency = 'i',
+ feed_type = 'q_ans',
+ ).values_list(
+ 'subscriber',
+ flat=True
+ )
+ subscriber_set.update(answer_authors)
+ subscriber_set -= set(exclude_list)
+
+ return list(subscriber_set)
+
+ def passes_tag_filter_for_user(user):
+
+ post_tags = self.get_origin_post().tags.all()
+
+ if user.tag_filter_setting == 'ignored':
+ ignored_tags = user.tag_selections.filter(reason = 'bad')
+ if set(post_tags) & set(ignored_tags):
+ return False
+ else:
+ return True
+ else:
+ interesting_tags = user.tag_selections.filter(reason = 'good')
+ if set(post_tags) & set(interesting_tags):
+ return True
+ else:
+ return False
+
def get_latest_revision(self):
return self.revisions.all().order_by('-revised_at')[0]
@@ -82,7 +192,11 @@ class Content(models.Model):
return self.get_latest_revision().revision
def get_last_author(self):
- return self.last_edited_by
+ #todo: fix this issue
+ if self.last_edited_by:
+ return self.last_edited_by
+ else:
+ return self.author
def get_time_of_last_edit(self):
if self.last_edited_at:
@@ -90,7 +204,13 @@ class Content(models.Model):
else:
return self.added_at
- def get_author_list(self, include_comments = False, recursive = False, exclude_list = None):
+ def get_author_list(
+ self,
+ include_comments = False,
+ recursive = False,
+ exclude_list = None):
+
+ #todo: there may be a better way to do these queries
authors = set()
authors.update([r.author for r in self.revisions.all()])
if include_comments: