Skip to content

Commit

Permalink
Merge pull request #275 from BoPeng/create_post
Browse files Browse the repository at this point in the history
factor out create_post and create_topic
  • Loading branch information
ellmetha authored May 14, 2022
2 parents 5ff449d + d211dba commit f3aa5ee
Showing 1 changed file with 45 additions and 33 deletions.
78 changes: 45 additions & 33 deletions machina/apps/forum_conversation/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,25 +86,32 @@ 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 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 = 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)
Expand Down Expand Up @@ -194,31 +201,36 @@ 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 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:
# 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']:
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 f3aa5ee

Please sign in to comment.