def mark_as_read(self, user): from pybb.models import TopicReadTracker, ForumReadTracker, Subscription, Topic Subscription.objects.filter(topic=self, user=user).update(sent=False) try: forum_mark = ForumReadTracker.objects.get(forum=self.forum, user=user) except ObjectDoesNotExist: forum_mark = None if self.updated and ((forum_mark is None) or (forum_mark.time_stamp < self.updated)): # Mark topic as readed count = TopicReadTracker.objects.filter(topic=self, user=user).update(time_stamp=tznow()) if not count: TopicReadTracker.objects.create(topic=self, user=user) # Check, if there are any unread topics in forum read = Topic.objects.filter(Q(forum=self.forum) & (Q(topicreadtracker__user=user, topicreadtracker__time_stamp__gt=F('updated'))) | Q(forum__forumreadtracker__user=user, forum__forumreadtracker__time_stamp__gt=F('updated'))) unread = Topic.objects.filter(forum=self.forum).exclude(id__in=read) if not unread.exists(): # Clear all topic marks for this forum, mark forum as readed TopicReadTracker.objects.filter( user=user, topic__forum=self.forum ).delete() if not forum_mark: ForumReadTracker.objects.create(forum=self.forum, user=user) else: forum_mark.time_stamp = tznow() update_fields(forum_mark, fields=('time_stamp', ))
def absorb(self, topic, redirection_type=None, expired=None): from pybb.models import TopicRedirection if not redirection_type: redirection_type = TopicRedirection.TYPE_PERMANENT_REDIRECT topic.posts.all().update(topic=self) topic.subscription_set.all().update(topic=self) if topic.poll: poll = topic.poll topic.poll = None self.poll = poll update_fields(self, fields=('poll', )) topic.topicreadtracker_set.all().update(topic=self) topic.redirect = True update_fields(topic, fields=('redirect', )) TopicRedirection.objects.create(from_topic=topic, to_topic=self, type=redirection_type, expired=expired) topic.update_counters() self.update_counters()
def mark_updated(self): now = tznow() self.updated = now update_fields(self, fields=('updated', )) for topic in self.topics.all(): topic.updated = now update_fields(topic, fields=('updated', ))
def render(self, commit=False): self.body_html = markup(self.body, obj=self) # Remove tags which was generated with the markup processor text = strip_tags(self.body_html) # Unescape entities which was generated with the markup processor self.body_text = unescape(text) if commit: update_fields(self, fields=('body_html', 'body_text', ))
def mark_as_deleted(self, commit=True, update=True): self.deleted = True self.posts.visible(join=False).update(deleted=True) if commit: update_fields(self, fields=('deleted', )) if update: self.forum.update_counters(commit=commit)
def mark_as_undeleted(self, commit=True, update=True): self.deleted = False post_ids = PostDeletion.objects.filter(post__topic=self).values_list('post', flat=True) self.posts.exclude(pk__in=post_ids).update(deleted=False) if commit: update_fields(self, fields=('deleted', )) if update: self.forum.update_counters(commit=commit)
def mark_as_undeleted(self, commit=True): self_id = self.id self.deleted = False PostDeletion.objects.filter(post=self).delete() if commit: update_fields(self, fields=('deleted', )) try: head_post_id = self.topic.posts.visible(join=False).order_by('created')[0].id except IndexError: head_post_id = None else: if self_id == head_post_id: self.topic.mark_as_undeleted()
def sync_cover(self, commit=True, force=False): if not self.first_post_id: return False if force is False and self.cover: return False for url in self.first_post.images: response = requests.get(url) if response.status_code == 200: file_ext = urlparse(url).path.split('.')[-1] self.cover.save("%s.%s" % (self.id, file_ext), ContentFile(response.content), save=True) if commit: update_fields(self, fields=('cover', )) return True return False
def compute(self, commit=True): self.user_count = self.votes() if commit: update_fields(self, fields=('user_count', ))