From f5f2985e193bb3b1705a79c19e20da9dec268202 Mon Sep 17 00:00:00 2001 From: Bo Peng Date: Sat, 23 Apr 2022 19:57:51 -0500 Subject: [PATCH 1/2] factor out create_post and create_topic --- machina/apps/forum_conversation/forms.py | 60 +++++++++++++----------- 1 file changed, 33 insertions(+), 27 deletions(-) diff --git a/machina/apps/forum_conversation/forms.py b/machina/apps/forum_conversation/forms.py index 366c11f5..15b52cce 100644 --- a/machina/apps/forum_conversation/forms.py +++ b/machina/apps/forum_conversation/forms.py @@ -86,6 +86,20 @@ def clean(self): self.instance.anonymous_key = get_anonymous_user_forum_key(self.user) return super().clean() + def create_post(self): + post = Post( + topic=self.topic, + subject=self.cleaned_data['subject'], + approved=self.perm_handler.can_post_without_approval(self.forum, self.user), + content=self.cleaned_data['content'], + enable_signature=self.cleaned_data['enable_signature']) + if not self.user.is_anonymous: + post.poster = self.user + else: + post.username = self.cleaned_data['username'] + post.anonymous_key = get_anonymous_user_forum_key(self.user) + return post + def save(self, commit=True): """ Saves the instance. """ if self.instance.pk: @@ -94,17 +108,7 @@ def save(self, commit=True): post.updated_by = self.user post.updates_count = F('updates_count') + 1 else: - post = Post( - topic=self.topic, - subject=self.cleaned_data['subject'], - approved=self.perm_handler.can_post_without_approval(self.forum, self.user), - content=self.cleaned_data['content'], - enable_signature=self.cleaned_data['enable_signature']) - if not self.user.is_anonymous: - post.poster = self.user - else: - post.username = self.cleaned_data['username'] - post.anonymous_key = get_anonymous_user_forum_key(self.user) + post = self.create_post() # Locks the topic if appropriate. lock_topic = self.cleaned_data.get('lock_topic', False) @@ -194,27 +198,29 @@ def clean(self): return super().clean() + def create_topic(self): + if 'topic_type' in self.cleaned_data and len(self.cleaned_data['topic_type']): + topic_type = self.cleaned_data['topic_type'] + else: + topic_type = Topic.TOPIC_POST + topic = Topic( + forum=self.forum, + subject=self.cleaned_data['subject'], # The topic's name is the post's name + type=topic_type, + status=Topic.TOPIC_UNLOCKED, + approved=self.perm_handler.can_post_without_approval(self.forum, self.user), + ) + if not self.user.is_anonymous: + topic.poster = self.user + return topic + def save(self, commit=True): """ Saves the instance. """ if not self.instance.pk: # First, handle topic creation - if 'topic_type' in self.cleaned_data and len(self.cleaned_data['topic_type']): - topic_type = self.cleaned_data['topic_type'] - else: - topic_type = Topic.TOPIC_POST - - topic = Topic( - forum=self.forum, - subject=self.cleaned_data['subject'], # The topic's name is the post's name - type=topic_type, - status=Topic.TOPIC_UNLOCKED, - approved=self.perm_handler.can_post_without_approval(self.forum, self.user), - ) - if not self.user.is_anonymous: - topic.poster = self.user - self.topic = topic + self.topic = self.create_topic() if commit: - topic.save() + self.topic.save() else: if 'topic_type' in self.cleaned_data and len(self.cleaned_data['topic_type']): if self.instance.topic.type != self.cleaned_data['topic_type']: From d211dba9c365d1a445bb94dff77aa1491de187f2 Mon Sep 17 00:00:00 2001 From: Bo Peng Date: Sat, 23 Apr 2022 21:05:15 -0500 Subject: [PATCH 2/2] Add update_post and update_topic --- machina/apps/forum_conversation/forms.py | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/machina/apps/forum_conversation/forms.py b/machina/apps/forum_conversation/forms.py index 15b52cce..4c7c4271 100644 --- a/machina/apps/forum_conversation/forms.py +++ b/machina/apps/forum_conversation/forms.py @@ -100,13 +100,16 @@ def create_post(self): post.anonymous_key = get_anonymous_user_forum_key(self.user) return post + def update_post(self, post): + post.updated_by = self.user + post.updates_count = F('updates_count') + 1 + def save(self, commit=True): """ Saves the instance. """ if self.instance.pk: # First handle updates post = super().save(commit=False) - post.updated_by = self.user - post.updates_count = F('updates_count') + 1 + self.update_post(post) else: post = self.create_post() @@ -214,6 +217,12 @@ def create_topic(self): topic.poster = self.user return topic + def update_topic(self, topic): + if 'topic_type' in self.cleaned_data and len(self.cleaned_data['topic_type']): + if topic.type != self.cleaned_data['topic_type']: + topic.type = self.cleaned_data['topic_type'] + topic._simple_save() + def save(self, commit=True): """ Saves the instance. """ if not self.instance.pk: @@ -222,9 +231,6 @@ def save(self, commit=True): if commit: self.topic.save() else: - if 'topic_type' in self.cleaned_data and len(self.cleaned_data['topic_type']): - if self.instance.topic.type != self.cleaned_data['topic_type']: - self.instance.topic.type = self.cleaned_data['topic_type'] - self.instance.topic._simple_save() + self.update_topic(self.instance.topic) return super().save(commit)