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(article): """Mark a article as read for the user.""" if article.last_reaction is not None: ArticleRead.objects.filter( article=article, user=get_current_user()).delete() a = ArticleRead( reaction=article.last_reaction, article=article, user=get_current_user()) a.save()
def mark_read(tutorial): ''' Mark a tutorial as read for the user ''' if tutorial.last_note != None : TutorialRead.objects.filter(tutorial=tutorial, user=get_current_user()).delete() a = TutorialRead( note=tutorial.last_note, tutorial=tutorial, user=get_current_user()) a.save()
def never_read(topic, user=None): """Check if a topic has been read by an user since it last post was added.""" if user is None: user = get_current_user() return not TopicRead.objects.filter(post=topic.last_message, topic=topic, user=user).exists()
def toggle_follow(self, content_object, user=None, by_email=False): """ Toggle following of a resource notifiable for a user. :param content_object: A resource notifiable. :param user: A user. If undefined, the current user is used. :param by_email: Get subscription by email or not. :return: subscription of the user for the content. """ if not user: user = get_current_user() if by_email: existing = self.get_existing(user, content_object, is_active=True, by_email=True) else: existing = self.get_existing(user, content_object, is_active=True) if not existing: subscription = self.get_or_create_active(user, content_object) if by_email: subscription.activate_email() return subscription signals.content_read.send(sender=content_object.__class__, instance=content_object, user=user, target=content_object.__class__) if by_email: existing.deactivate_email() else: existing.deactivate() return existing
def mark_read(privatetopic, user=None): """ Mark a private topic as read for the user. :param privatetopic: a PrivateTopic to check :type privatetopic: PrivateTopic object :param user: a user as Django User object. If None, the current user is used :type user: User object :return: nothing is returned :rtype: None """ # If user param is not defined, we get the current user if user is None: user = get_current_user() # Fetch the privateTopicRead concerning the given privateTopic and given (or current) user # Set the last read post as the current last post and save try: topic = PrivateTopicRead.objects.filter(privatetopic=privatetopic, user=user).get() topic.privatepost = privatetopic.last_message # Or create it if it does not exists yet except PrivateTopicRead.DoesNotExist: topic = PrivateTopicRead(privatepost=privatetopic.last_message, privatetopic=privatetopic, user=user) topic.save() signals.content_read.send(sender=privatetopic.__class__, instance=privatetopic, user=user)
def first_unread_post(self, user=None): """ Get the first PrivatePost the user has unread. :param user: The user is reading the PrivateTopic. If None, the current user is used. :type user: User object :return: first PrivatePost unread :rtype: PrivatePost object or None """ # If user param is not defined, we get the current user if user is None: user = get_current_user() try: last_post = PrivateTopicRead.objects \ .select_related() \ .filter(privatetopic=self, user=user) \ .latest('privatepost__position_in_topic').privatepost next_post = PrivatePost.objects.filter( privatetopic__pk=self.pk, position_in_topic__gt=last_post.position_in_topic).first() return next_post except (PrivatePost.DoesNotExist, PrivateTopicRead.DoesNotExist): return self.first_post()
def never_read(tutorial, user=None): """Check if a topic has been read by an user since it last post was added.""" if user is None: user = get_current_user() return TutorialRead.objects.filter(note=tutorial.last_note, tutorial=tutorial, user=user).count() == 0
def antispam(self, user=None): """Check if the user is allowed to post in a topic according to 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. """ if user is None: user = get_current_user() last_user_post = Post.objects\ .filter(topic=self)\ .filter(author=user.pk)\ .order_by('pubdate')\ .last() if last_user_post and last_user_post == self.get_last_post(): t = timezone.now() - last_user_post.pubdate if t.total_seconds() < settings.SPAM_LIMIT_SECONDS: return True return False
def first_unread_note(self, user=None): """ :return: Return the first note the user has unread. :rtype: ContentReaction """ if user is None: user = get_current_user() if user and user.is_authenticated(): try: read = ContentRead.objects\ .filter(content=self, user__pk=user.pk)\ .latest('note__pubdate') if read and read.note: last_note = read.note next_note = ContentReaction.objects\ .select_related('related_content')\ .select_related('related_content__public_version')\ .filter( related_content__pk=self.pk, pk__gt=last_note.pk)\ .select_related('author').first() if next_note: return next_note else: return last_note except ContentRead.DoesNotExist: pass return self.first_note()
def get_commit_author(): """get a dictionary that represent the commit author with ``author`` and ``comitter`` key. If there is no users, bot account pk is used. :return: correctly formatted commit author for ``repo.index.commit()`` :rtype: dict """ user = get_current_user() if user and user.is_authenticated(): aut_user = str(user.pk) aut_email = None if hasattr(user, 'email'): aut_email = user.email else: try: aut_user = str(User.objects.filter(username=settings.ZDS_APP['member']['bot_account']).first().pk) except AttributeError: # if nothing is found, `first` returns None, which does not have attribute pk aut_user = '******' aut_email = None if aut_email is None or not aut_email.strip(): aut_email = _('inconnu@{}').format(settings.ZDS_APP['site']['dns']) return {'author': Actor(aut_user, aut_email), 'committer': Actor(aut_user, aut_email)}
def antispam(self, user=None): """Check if the user is allowed to post in an article according to the SPAM_LIMIT_SECONDS value. If user shouldn't be able to reaction, then antispam is activated and this method returns True. Otherwise time elapsed between user's last reaction and now is enough, and the method will return False. """ if user is None: user = get_current_user() last_user_reactions = Reaction.objects\ .filter(article=self)\ .filter(author=user.pk)\ .order_by('-pubdate') if last_user_reactions \ and last_user_reactions[0] == self.last_reaction: last_user_reaction = last_user_reactions[0] t = timezone.now() - last_user_reaction.pubdate if t.total_seconds() < settings.SPAM_LIMIT_SECONDS: return True return False
def follow(topic, user=None): """Toggle following of a topic for an user.""" ret = None if user is None: user=get_current_user() 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 follow_by_email(topic, user=None): """Toggle following of a topic for an user.""" ret = None if user is None: user=get_current_user() 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, email = True ) t.save() ret = True else: existing.email = not existing.email existing.save() ret = existing.email return ret
def last_read_reaction(self): """Return the last post the user has read.""" try: return ArticleRead.objects\ .select_related()\ .filter(article=self, user=get_current_user())\ .latest('reaction__pubdate').reaction except Reaction.DoesNotExist: return self.first_post()
def never_read(article, user=None): """Check if a topic has been read by an user since it last post was added.""" if user is None: user = get_current_user() return ArticleRead.objects\ .filter(reaction=article.last_reaction, article=article, user=user)\ .count() == 0
def mark_read(topic): """Mark a topic as read for the user.""" u = get_current_user() t = TopicRead.objects.filter(topic=topic, user=u).first() if t is None: t = TopicRead(post=topic.last_message, topic=topic, user=u) else: t.post = topic.last_message t.save()
def last_read_post(self): """Return the last post the user has read.""" try: return TopicRead.objects\ .select_related()\ .filter(topic=self, user=get_current_user())\ .latest('post__pubdate').post except: return self.first_post()
def last_read_note(self): """Return the last post the user has read.""" try: return TutorialRead.objects\ .select_related()\ .filter(tutorial=self, user=get_current_user())\ .latest('note__pubdate').note except Note.DoesNotExist: return self.first_post()
def is_followed(self, topic, user=None): """ Checks if the user follows this topic. :param user: A user. If undefined, the current user is used. :return: `True` if the user follows this topic, `False` otherwise. """ if user is None: user = get_current_user() return self.filter(topic=topic, user=user).exists()
def never_privateread(privatetopic, user=None): """Check if a private topic has been read by an user since it last post was added.""" 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. """ if user is None: user = get_current_user() return TopicFollowed.objects.filter(topic=self, user=user).exists()
def last_read_post(self): """Return the last post the user has read.""" try: return ( TopicRead.objects.select_related() .filter(topic=self, user=get_current_user()) .latest("post__pubdate") .post ) except: return self.first_post()
def first_unread_post(self): """Return the first post the user has unread.""" try: last_post = TopicRead.objects.filter(topic=self, user=get_current_user()).latest("post__pubdate").post next_post = ( Post.objects.filter(topic__pk=self.pk, pubdate__gt=last_post.pubdate).select_related("author").first() ) return next_post except: return self.first_post()
def last_read_post(self): """Return the last private post the user has read.""" 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 is_unread(self, user=None): """ Check if a user has never read the current PrivateTopic. :param user: a user as Django User object. If None, the current user is used. :type user: User object :return: True if the PrivateTopic was never read :rtype: bool """ # If user param is not defined, we get the current user if user is None: user = get_current_user() return is_privatetopic_unread(self, user)
def is_read(topic, user=None): """ Checks if the user has read the **last post** of the topic. Returns false if the user read the topic except its last post. Technically this is done by checking if the user has a `TopicRead` object for the last post of this topic. :param topic: A topic :param user: A user. If undefined, the current user is used. :return: """ if user is None: user = get_current_user() return TopicRead.objects.filter(post=topic.last_message, topic=topic, user=user).exists()
def is_email_followed(self, user=None): """Check if the topic is currently email followed by the user. This method uses the TopicFollowed objects. """ if user is None: user = get_current_user() try: TopicFollowed.objects.get(topic=self, user=user, email=True) except TopicFollowed.DoesNotExist: return False return True
def first_unread_note(self): """Return the first note the user has unread.""" try: last_note = TutorialRead.objects.filter(tutorial=self, user=get_current_user()).latest("note__pubdate").note next_note = ( Note.objects.filter(tutorial__pk=self.pk, pubdate__gt=last_note.pubdate) .select_related("author") .first() ) return next_note except: return self.first_note()
def last_read_post(self): """ Returns the last post the current user has read in this topic. If it has never read this topic, returns the first post. Used in "last read post" balloon (base.html line 91). :return: the last post the user has read. """ try: return TopicRead.objects \ .select_related() \ .filter(topic__pk=self.pk, user__pk=get_current_user().pk) \ .latest('post__position').post except TopicRead.DoesNotExist: return self.first_post()
def is_new_publication_email_followed(user_to_follow): user = get_current_user() return user.is_authenticated( ) and NewPublicationSubscription.objects.does_exist( user, user_to_follow, is_active=True, by_email=True)
def is_email_followed(topic): user = get_current_user() return TopicAnswerSubscription.objects.does_exist(user, topic, is_active=True, by_email=True)
def is_email_followed_for_new_topic(forum_or_tag): user = get_current_user() return NewTopicSubscription.objects.does_exist(user, forum_or_tag, is_active=True, by_email=True)
def is_content_followed(content): user = get_current_user() return user.is_authenticated( ) and ContentReactionAnswerSubscription.objects.does_exist( user, content, is_active=True)
def is_forum_email_followed(forum): user = get_current_user() return NewTopicSubscription.objects.does_exist(user, forum, is_active=True, by_email=True)