Refactoring pour plus de clarté (séparation en classes distinctes pour l'édition/clonage)
This commit is contained in:
		@@ -61,10 +61,7 @@
 | 
			
		||||
{% endblock %}
 | 
			
		||||
{% block content %}
 | 
			
		||||
  {% load static_content_extra %}
 | 
			
		||||
  {% if form.is_clone_from_url or form.is_simple_clone_from_url %}
 | 
			
		||||
    {% url "add_event_details" as urlparam %}
 | 
			
		||||
  {% endif %}
 | 
			
		||||
  <form method="post" action="{{ urlparam }}" enctype="multipart/form-data">
 | 
			
		||||
  <form method="post" enctype="multipart/form-data">
 | 
			
		||||
    {% if object %}
 | 
			
		||||
      <article>
 | 
			
		||||
        <header>
 | 
			
		||||
 
 | 
			
		||||
@@ -35,6 +35,9 @@ from .views import (
 | 
			
		||||
    recent,
 | 
			
		||||
    EventDetailView,
 | 
			
		||||
    EventUpdateView,
 | 
			
		||||
    EventCreateIndependantView,
 | 
			
		||||
    EventCreateLocalView,
 | 
			
		||||
    EventUpdateForceView,
 | 
			
		||||
    EventCreateView,
 | 
			
		||||
    update_from_source,
 | 
			
		||||
    change_status_event,
 | 
			
		||||
@@ -244,16 +247,18 @@ urlpatterns = [
 | 
			
		||||
    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-force", EventUpdateView.as_view(), name="edit_event_force"
 | 
			
		||||
        "event/<int:pk>/edit-force",
 | 
			
		||||
        EventUpdateForceView.as_view(),
 | 
			
		||||
        name="edit_event_force",
 | 
			
		||||
    ),
 | 
			
		||||
    path(
 | 
			
		||||
        "event/<int:pk>/simple-clone/edit",
 | 
			
		||||
        EventUpdateView.as_view(),
 | 
			
		||||
        EventCreateIndependantView.as_view(),
 | 
			
		||||
        name="simple_clone_edit",
 | 
			
		||||
    ),
 | 
			
		||||
    path(
 | 
			
		||||
        "event/<int:pk>/clone/edit",
 | 
			
		||||
        EventUpdateView.as_view(),
 | 
			
		||||
        EventCreateLocalView.as_view(),
 | 
			
		||||
        name="clone_edit",
 | 
			
		||||
    ),
 | 
			
		||||
    path(
 | 
			
		||||
 
 | 
			
		||||
@@ -98,9 +98,6 @@ class EventUpdateView(
 | 
			
		||||
        kwargs["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
 | 
			
		||||
 | 
			
		||||
    def get_success_message(self, cleaned_data):
 | 
			
		||||
@@ -111,15 +108,10 @@ class EventUpdateView(
 | 
			
		||||
        )
 | 
			
		||||
        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):
 | 
			
		||||
        event = super().get_object(queryset)
 | 
			
		||||
        if event.status == Event.STATUS.DRAFT:
 | 
			
		||||
            event.status = Event.STATUS.PUBLISHED
 | 
			
		||||
        if self.is_edit_force():
 | 
			
		||||
            event.free_modification_lock(self.request.user)
 | 
			
		||||
        return event
 | 
			
		||||
 | 
			
		||||
    def form_valid(self, form):
 | 
			
		||||
@@ -127,41 +119,6 @@ class EventUpdateView(
 | 
			
		||||
        self.with_message = form.instance.notify_if_required(self.request)
 | 
			
		||||
        return super().form_valid(form)
 | 
			
		||||
 | 
			
		||||
    def get_initial(self):
 | 
			
		||||
        self.is_cloning = "clone" in self.request.path.split("/")
 | 
			
		||||
        if self.is_cloning:
 | 
			
		||||
            messages.info(
 | 
			
		||||
                self.request,
 | 
			
		||||
                _(
 | 
			
		||||
                    "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()
 | 
			
		||||
 | 
			
		||||
        if self.is_cloning and "other_versions" not in result:
 | 
			
		||||
            obj = self.get_object()
 | 
			
		||||
            # if no DuplicatedEvents is associated, create one
 | 
			
		||||
            obj.other_versions = DuplicatedEvents.objects.create()
 | 
			
		||||
            obj.other_versions.save()
 | 
			
		||||
            # save them without updating modified date
 | 
			
		||||
            obj.set_no_modification_date_changed()
 | 
			
		||||
            obj.save()
 | 
			
		||||
            result["other_versions"] = obj.other_versions
 | 
			
		||||
            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
 | 
			
		||||
 | 
			
		||||
    def get_success_url(self):
 | 
			
		||||
        if "save_and_next" in self.request.POST:
 | 
			
		||||
            return reverse_lazy("moderate_event_next", args=[self.object.pk])
 | 
			
		||||
@@ -169,6 +126,109 @@ class EventUpdateView(
 | 
			
		||||
            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):
 | 
			
		||||
        messages.info(
 | 
			
		||||
            self.request,
 | 
			
		||||
            _(
 | 
			
		||||
                "Changes will be visible on a local copy of the event. The version identical to the imported source will be hidden."
 | 
			
		||||
            ),
 | 
			
		||||
        )
 | 
			
		||||
        result = super().get_initial()
 | 
			
		||||
 | 
			
		||||
        if "other_versions" not in result:
 | 
			
		||||
            obj = self.get_object()
 | 
			
		||||
            # if no DuplicatedEvents is associated, create one
 | 
			
		||||
            if obj.other_versions is None:
 | 
			
		||||
                obj.other_versions = DuplicatedEvents.objects.create()
 | 
			
		||||
                obj.other_versions.save()
 | 
			
		||||
                # save them without updating modified date
 | 
			
		||||
                obj.set_no_modification_date_changed()
 | 
			
		||||
                obj.save()
 | 
			
		||||
            result["other_versions"] = obj.other_versions
 | 
			
		||||
 | 
			
		||||
        result["status"] = Event.STATUS.PUBLISHED
 | 
			
		||||
 | 
			
		||||
        return result
 | 
			
		||||
 | 
			
		||||
    def form_valid(self, form):
 | 
			
		||||
        form.instance.set_in_moderation_process()
 | 
			
		||||
        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 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(
 | 
			
		||||
    SuccessMessageMixin,
 | 
			
		||||
    PermissionRequiredMixin,
 | 
			
		||||
@@ -412,11 +472,6 @@ class EventCreateView(SuccessMessageMixin, CreateView):
 | 
			
		||||
            )
 | 
			
		||||
 | 
			
		||||
    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"):
 | 
			
		||||
            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.set_processing_user(self.request.user)
 | 
			
		||||
 | 
			
		||||
        result = 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
 | 
			
		||||
        return super().form_valid(form)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
# A class to evaluate the URL according to the existing events and the authentification
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user