From a64ee655809b6b2431d85e78e9a34ffc6d2c847a Mon Sep 17 00:00:00 2001 From: vincent porte Date: Thu, 8 Aug 2024 10:09:06 +0200 Subject: [PATCH] pythonic --- lacommunaute/forum_conversation/forms.py | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) diff --git a/lacommunaute/forum_conversation/forms.py b/lacommunaute/forum_conversation/forms.py index 122fe685..728ce8d0 100644 --- a/lacommunaute/forum_conversation/forms.py +++ b/lacommunaute/forum_conversation/forms.py @@ -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 @@ -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