Skip to content

Commit

Permalink
pythonic
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentporte committed Aug 8, 2024
1 parent 94bbcd3 commit a64ee65
Showing 1 changed file with 5 additions and 12 deletions.
17 changes: 5 additions & 12 deletions lacommunaute/forum_conversation/forms.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from django.core.exceptions import ObjectDoesNotExist
from django.db.models import F
from django.forms import CharField, CheckboxSelectMultiple, HiddenInput, ModelMultipleChoiceField
from machina.apps.forum_conversation.forms import PostForm as AbstractPostForm, TopicForm as AbstractTopicForm
Expand Down Expand Up @@ -59,21 +58,15 @@ class TopicForm(CreateUpdatePostMixin, AbstractTopicForm):

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
try:
if hasattr(self.instance, "topic"):
self.fields["tags"].initial = self.instance.topic.tags.all()
except ObjectDoesNotExist:
pass
if self.instance.pk:
self.fields["tags"].initial = self.instance.topic.tags.all()

def save(self):
post = super().save()
post.topic.tags.set(self.cleaned_data["tags"])
if self.cleaned_data.get("new_tag"):
post.topic.tags.add(self.cleaned_data["new_tag"])

if self.user.is_anonymous:
post.username = self.cleaned_data["username"]
post.topic.tags.set(self.cleaned_data["tags"])
post.topic.tags.add(self.cleaned_data["new_tag"]) if self.cleaned_data.get("new_tag") else None
post.username = self.cleaned_data["username"] if self.user.is_anonymous else None

post.save()

return post

0 comments on commit a64ee65

Please sign in to comment.