Refactoring pour plus de clarté (séparation en classes distinctes pour l'édition/clonage)

This commit is contained in:
Jean-Marie Favreau 2025-05-02 23:30:53 +02:00
parent 3b09e08aed
commit cbed16ac42
3 changed files with 113 additions and 68 deletions

View File

@ -61,10 +61,7 @@
{% endblock %} {% endblock %}
{% block content %} {% block content %}
{% load static_content_extra %} {% load static_content_extra %}
{% if form.is_clone_from_url or form.is_simple_clone_from_url %} <form method="post" enctype="multipart/form-data">
{% url "add_event_details" as urlparam %}
{% endif %}
<form method="post" action="{{ urlparam }}" enctype="multipart/form-data">
{% if object %} {% if object %}
<article> <article>
<header> <header>

View File

@ -35,6 +35,9 @@ from .views import (
recent, recent,
EventDetailView, EventDetailView,
EventUpdateView, EventUpdateView,
EventCreateIndependantView,
EventCreateLocalView,
EventUpdateForceView,
EventCreateView, EventCreateView,
update_from_source, update_from_source,
change_status_event, change_status_event,
@ -244,16 +247,18 @@ urlpatterns = [
path("event/<int:pk>/", EventDetailView.as_view(), name="edit_event_pk"), path("event/<int:pk>/", EventDetailView.as_view(), name="edit_event_pk"),
path("event/<int:pk>/edit", EventUpdateView.as_view(), name="edit_event"), path("event/<int:pk>/edit", EventUpdateView.as_view(), name="edit_event"),
path( path(
"event/<int:pk>/edit-force", EventUpdateView.as_view(), name="edit_event_force" "event/<int:pk>/edit-force",
EventUpdateForceView.as_view(),
name="edit_event_force",
), ),
path( path(
"event/<int:pk>/simple-clone/edit", "event/<int:pk>/simple-clone/edit",
EventUpdateView.as_view(), EventCreateIndependantView.as_view(),
name="simple_clone_edit", name="simple_clone_edit",
), ),
path( path(
"event/<int:pk>/clone/edit", "event/<int:pk>/clone/edit",
EventUpdateView.as_view(), EventCreateLocalView.as_view(),
name="clone_edit", name="clone_edit",
), ),
path( path(

View File

@ -98,9 +98,6 @@ class EventUpdateView(
kwargs["is_moderation_expert"] = ( kwargs["is_moderation_expert"] = (
self.request.user.userprofile.is_moderation_expert self.request.user.userprofile.is_moderation_expert
) )
kwargs["is_cloning"] = self.is_cloning
kwargs["is_edit_from_moderation"] = self.is_edit_force()
kwargs["is_simple_cloning"] = self.is_simple_cloning
return kwargs return kwargs
def get_success_message(self, cleaned_data): def get_success_message(self, cleaned_data):
@ -111,15 +108,10 @@ class EventUpdateView(
) )
return mark_safe(_("The event has been successfully modified.") + txt) return mark_safe(_("The event has been successfully modified.") + txt)
def is_edit_force(self):
return "edit-force" in self.request.path.split("/")
def get_object(self, queryset=None): def get_object(self, queryset=None):
event = super().get_object(queryset) event = super().get_object(queryset)
if event.status == Event.STATUS.DRAFT: if event.status == Event.STATUS.DRAFT:
event.status = Event.STATUS.PUBLISHED event.status = Event.STATUS.PUBLISHED
if self.is_edit_force():
event.free_modification_lock(self.request.user)
return event return event
def form_valid(self, form): def form_valid(self, form):
@ -127,46 +119,114 @@ class EventUpdateView(
self.with_message = form.instance.notify_if_required(self.request) self.with_message = form.instance.notify_if_required(self.request)
return super().form_valid(form) return super().form_valid(form)
def get_success_url(self):
if "save_and_next" in self.request.POST:
return reverse_lazy("moderate_event_next", args=[self.object.pk])
else:
return self.object.get_absolute_url()
class EventUpdateForceView(EventUpdateView):
def get_form_kwargs(self):
kwargs = super().get_form_kwargs()
kwargs["is_edit_from_moderation"] = True
return kwargs
def get_object(self, queryset=None):
event = super().get_object(queryset)
event.free_modification_lock(self.request.user)
return event
class EventCloneView(EventUpdateView):
def form_valid(self, form):
# Clone the object removing the key
self.object = form.save(commit=False)
self.object.pk = None
self.object.save()
form.save_m2m()
form.instance.import_sources = None
form.instance.set_processing_user(self.request.user)
return super().form_valid(form)
def get_initial(self):
result = super().get_initial()
obj = self.get_object()
if obj.local_image:
result["old_local_image"] = obj.local_image.name
result["imported_date"] = None
return result
class EventCreateLocalView(EventCloneView):
def get_form_kwargs(self):
kwargs = super().get_form_kwargs()
kwargs["is_cloning"] = True
return kwargs
def get_initial(self): def get_initial(self):
self.is_cloning = "clone" in self.request.path.split("/")
if self.is_cloning:
messages.info( messages.info(
self.request, self.request,
_( _(
"Changes will be visible on a local copy of the event. The version identical to the imported source will be hidden." "Changes will be visible on a local copy of the event. The version identical to the imported source will be hidden."
), ),
) )
self.is_simple_cloning = "simple-clone" in self.request.path.split("/")
result = super().get_initial() result = super().get_initial()
if self.is_cloning and "other_versions" not in result: if "other_versions" not in result:
obj = self.get_object() obj = self.get_object()
# if no DuplicatedEvents is associated, create one # if no DuplicatedEvents is associated, create one
if obj.other_versions is None:
obj.other_versions = DuplicatedEvents.objects.create() obj.other_versions = DuplicatedEvents.objects.create()
obj.other_versions.save() obj.other_versions.save()
# save them without updating modified date # save them without updating modified date
obj.set_no_modification_date_changed() obj.set_no_modification_date_changed()
obj.save() obj.save()
result["other_versions"] = obj.other_versions result["other_versions"] = obj.other_versions
result["status"] = Event.STATUS.PUBLISHED result["status"] = Event.STATUS.PUBLISHED
result["cloning"] = True
if self.is_simple_cloning:
result["other_versions"] = None
result["simple_cloning"] = True
if self.is_cloning or self.is_simple_cloning:
obj = self.get_object()
if obj.local_image:
result["old_local_image"] = obj.local_image.name
return result return result
def get_success_url(self): def form_valid(self, form):
if "save_and_next" in self.request.POST: form.instance.set_in_moderation_process()
return reverse_lazy("moderate_event_next", args=[self.object.pk]) with_msg = form.instance.notify_if_required(self.request)
else: if with_msg:
return self.object.get_absolute_url() messages.success(
self.request,
_(
"A message has been sent to the person who proposed the initial event."
),
)
return super().form_valid(form)
class EventCreateIndependantView(EventCloneView):
def get_form_kwargs(self):
kwargs = super().get_form_kwargs()
kwargs["is_simple_cloning"] = True
return kwargs
def get_initial(self):
result = super().get_initial()
result["other_versions"] = None
result["simple_cloning"] = True
return result
def form_valid(self, form):
form.instance.set_skip_duplicate_check()
return super().form_valid(form)
class EventDeleteView( class EventDeleteView(
@ -412,11 +472,6 @@ class EventCreateView(SuccessMessageMixin, CreateView):
) )
def form_valid(self, form): def form_valid(self, form):
if form.cleaned_data["simple_cloning"]:
form.instance.set_skip_duplicate_check()
if form.cleaned_data["cloning"]:
form.instance.set_in_moderation_process()
if form.cleaned_data.get("email") or form.cleaned_data.get("comments"): if form.cleaned_data.get("email") or form.cleaned_data.get("comments"):
has_comments = form.cleaned_data.get("comments") not in ["", None] has_comments = form.cleaned_data.get("comments") not in ["", None]
@ -437,19 +492,7 @@ class EventCreateView(SuccessMessageMixin, CreateView):
form.instance.import_sources = None form.instance.import_sources = None
form.instance.set_processing_user(self.request.user) form.instance.set_processing_user(self.request.user)
result = super().form_valid(form) return super().form_valid(form)
if form.cleaned_data["cloning"]:
with_msg = form.instance.notify_if_required(self.request)
if with_msg:
messages.success(
self.request,
_(
"A message has been sent to the person who proposed the initial event."
),
)
return result
# A class to evaluate the URL according to the existing events and the authentification # A class to evaluate the URL according to the existing events and the authentification