Skip to content

Commit

Permalink
Add update_post and update_topic
Browse files Browse the repository at this point in the history
  • Loading branch information
BoPeng committed Apr 24, 2022
1 parent f5f2985 commit d211dba
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions machina/apps/forum_conversation/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down Expand Up @@ -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:
Expand All @@ -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)

0 comments on commit d211dba

Please sign in to comment.