def mark_read(privatetopic): """Mark a private topic as read for the user.""" PrivateTopicRead.objects.filter( privatetopic=privatetopic, user=get_current_user()).delete() t = PrivateTopicRead( privatepost=privatetopic.last_message, privatetopic=privatetopic, user=get_current_user()) t.save()
def mark_read(privatetopic): """Mark a private topic as read for the user.""" PrivateTopicRead.objects.filter(privatetopic=privatetopic, user=get_current_user()).delete() t = PrivateTopicRead(privatepost=privatetopic.last_message, privatetopic=privatetopic, user=get_current_user()) t.save()
def antispam(self, user=None): """Check if the user is allowed to post in a topic. This method uses the SPAM_LIMIT_SECONDS value. If user shouldn't be able to post, then antispam is activated and this method returns True. Otherwise time elapsed between user's last post and now is enough, and the method will return False. Returns: boolean """ if user is None: user = get_current_user() last_user_posts = Post.objects\ .filter(topic=self)\ .filter(author=user)\ .order_by('-pubdate') if last_user_posts and last_user_posts[0] == self.get_last_answer(): last_user_post = last_user_posts[0] t = timezone.now() - last_user_post.pubdate if t.total_seconds() < settings.SPAM_LIMIT_SECONDS: return True return False
def mark_read(topic, user=None): """Mark a topic as read for an user. If no user is provided, this will use the current session user. Args: topic: Topic to be marked as read user: User who as read the topic """ if user is None: user = get_current_user() # We update existing TopicRead or create a new one req = TopicRead.objects.filter(topic=topic, user=user) if req: t = req[0] else: t = TopicRead(topic=topic, user=user) t.post = topic.last_message t.save() # If the topic is followed, we want to update some cached values if topic.is_followed(user): template_cache_delete('topbar-topics', [user.username]) if topic in get_last_topics(): template_cache_delete('home-forums', [user.username])
def never_read(topic, user=None): """Check if a topic has been read by an user since it last post was added. For convenience for new users, topics older than the user's joined datetime will be marked as read. If no user is provided, this will use the current session user. Args: topic: Topic to check if has been read user: User who may have read the topic Returns: boolean """ if user is None: user = get_current_user() if topic.last_message.pubdate < user.date_joined: return False return TopicRead.objects\ .filter(post_id=topic.last_message_id, topic=topic, user=user)\ .count() == 0
def never_privateread(privatetopic, user=None): """Check if a private topic has been read by an user. Returns: boolean """ if user is None: user = get_current_user() return PrivateTopicRead.objects\ .filter(privatepost=privatetopic.last_message, privatetopic=privatetopic, user=user)\ .count() == 0
def is_followed(self, user=None): """Check if the topic is currently followed by the user. This method uses the TopicFollowed objects. Returns: boolean """ if user is None: user = get_current_user() try: TopicFollowed.objects.get(topic=self, user=user) except TopicFollowed.DoesNotExist: return False return True
def last_read_post(self): """Return the last private post the user has read. Returns: PrivatePost object """ try: post = PrivateTopicRead.objects\ .select_related()\ .filter(privatetopic=self, user=get_current_user()) if len(post) == 0: return self.first_post() else: return post.latest('privatepost__pubdate').privatepost except PrivatePost.DoesNotExist: return self.first_post()
def follow(topic, user=None): """Toggle following of a topic for an user. If no user is provided, this will use the current session user. Args: topic: Topic to be (un)marked as followed user: User to toogle the following state for Returns: New status as boolean : is the user following the topic now? """ # TODO: create follow, unfollow and toggle_follow functions instead of this # big one. if user is None: user = get_current_user() ret = None try: existing = TopicFollowed.objects.get( topic=topic, user=user ) except TopicFollowed.DoesNotExist: existing = None if not existing: # Make the user follow the topic t = TopicFollowed( topic=topic, user=user ) t.save() ret = True else: # If user is already following the topic, we make him don't anymore existing.delete() ret = False return ret
def last_read_post(self): """Return the last post the user has read. Returns: Post object or None """ user = get_current_user() if user is not None: # Logged-in user, so he may have a TopicRead instance try: return TopicRead.objects\ .select_related()\ .filter(topic=self, user=user)\ .latest('post__pubdate').post except TopicRead.DoesNotExist: return self.first_post() # Anonymous user, we return the last post since the first one is # available using the topic title link so it would have been redundant. resp = self.get_last_answer() return resp if resp is not None else self.first_post()
def is_read(self): """Check if this forum was read by current user. For convenience for new users, we only mark the forum as unread if the user has not read messages posted after the datetime he joined. Returns: boolean """ user = get_current_user() # Though the never_read func will return false for old topics, better # filter these old topics here so our topic set is way more compact # than a Topic.objects.all() on which we call never_read in for loop. topics = Topic.objects.all() \ .filter(forum=self) \ .filter(last_message__pubdate__gt=user.date_joined) for t in topics: if never_read(t): return False return True
def follow(topic, user=None): """Toggle following of a topic for an user. If no user is provided, this will use the current session user. Args: topic: Topic to be (un)marked as followed user: User to toogle the following state for Returns: New status as boolean : is the user following the topic now? """ # TODO: create follow, unfollow and toggle_follow functions instead of this # big one. if user is None: user = get_current_user() ret = None try: existing = TopicFollowed.objects.get(topic=topic, user=user) except TopicFollowed.DoesNotExist: existing = None if not existing: # Make the user follow the topic t = TopicFollowed(topic=topic, user=user) t.save() ret = True else: # If user is already following the topic, we make him don't anymore existing.delete() ret = False return ret