Ajout d'une fonction de copie d'événements

Fix #208
This commit is contained in:
Jean-Marie Favreau 2024-11-20 11:06:27 +01:00
parent 0924d5d36c
commit 1e278581ed
6 changed files with 42 additions and 15 deletions

View File

@ -137,6 +137,7 @@ class EventForm(ModelForm):
required_css_class = 'required'
old_local_image = CharField(widget=HiddenInput(), required=False)
simple_cloning = CharField(widget=HiddenInput(), required=False)
tags = MultipleChoiceField(
label=_("Tags"),
@ -181,6 +182,7 @@ class EventForm(ModelForm):
def __init__(self, *args, **kwargs):
is_authenticated = kwargs.pop("is_authenticated", False)
self.cloning = kwargs.pop("is_cloning", False)
self.simple_cloning = kwargs.pop("is_simple_cloning", False)
super().__init__(*args, **kwargs)
if not is_authenticated:
del self.fields["status"]
@ -191,6 +193,9 @@ class EventForm(ModelForm):
def is_clone_from_url(self):
return self.cloning
def is_simple_clone_from_url(self):
return self.simple_cloning
def clean_end_day(self):
start_day = self.cleaned_data.get("start_day")

View File

@ -41,3 +41,6 @@
<a href="{% url 'delete_event' event.id %}" role="button">supprimer définitivement {% picto_from_name "x-circle" %}</a>
{% endif %}
{% if with_clone %}
<a href="{% url 'simple_clone_edit' event.id %}" role="button">Dupliquer {% picto_from_name "copy" %}</a>
{% endif %}

View File

@ -4,7 +4,11 @@
{% block title %}{% block og_title %}
{% if object %}{% if form.is_clone_from_url %}
Création d'une copie de {% else %}
Création d'une copie locale de {% else %}
Édition de l'événement {{ object.title }} ({{ object.start_day }})
{% endif %}
{% if form.is_simple_clone_from_url %}
Duplication de {% else %}
Édition de l'événement {{ object.title }} ({{ object.start_day }})
{% endif %}
{% else %}
@ -48,9 +52,10 @@ Création d'une copie de {% else %}
<header>
{% if object %}
<h1>{% if form.is_clone_from_url %}
Création d'une copie de {% else %}
Édition de l'événement{% endif %} {{ object.title }} ({{ object.start_day }})</h1>
{% else %}
Création d'une copie locale de {% else %}
{% if form.is_simple_clone_from_url %}
Duplication de {% else %}
Édition de l'événement{% endif %}{% endif %} {{ object.title }} ({{ object.start_day }})</h1>
{% if from_import %}
<h1>Ajuster l'événement importé</h1>
{% else %}
@ -61,7 +66,7 @@ Création d'une copie de {% else %}
</header>
{% if form.is_clone_from_url %}
{% 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">{% csrf_token %}

View File

@ -98,7 +98,7 @@
<div class="buttons">
<a href="{% url 'export_event_ical' event.start_day.year event.start_day.month event.start_day.day event.id %}" role="button">Exporter ical {% picto_from_name "calendar" %}</a>
{% if perms.agenda_culturel.change_event and not noedit %}
{% include "agenda_culturel/edit-buttons-inc.html" with event=event %}
{% include "agenda_culturel/edit-buttons-inc.html" with event=event with_clone=1 %}
{% endif %}
</div>
</footer>

View File

@ -38,6 +38,7 @@ urlpatterns = [
path("event/<int:pk>/moderate-next", EventModerateView.as_view(), name="moderate_event_next"),
path("event/<int:pk>/moderate-next/error", error_next_event, name="error_next_event"),
path("moderate", EventModerateView.as_view(), name="moderate"),
path("event/<int:pk>/simple-clone/edit", EventUpdateView.as_view(), name="simple_clone_edit"),
path("event/<int:pk>/clone/edit", EventUpdateView.as_view(), name="clone_edit"),
path("event/<int:pk>/update-from-source", update_from_source, name="update_from_source"),
path(

View File

@ -304,14 +304,18 @@ class EventUpdateView(
success_message = _("The event has been successfully modified.")
def get_form_kwargs(self):
logger.warning("start kwargs")
kwargs = super().get_form_kwargs()
kwargs["is_authenticated"] = self.request.user.is_authenticated
kwargs["is_cloning"] = self.is_cloning
kwargs["is_simple_cloning"] = self.is_simple_cloning
return kwargs
def get_initial(self):
self.is_cloning = "clone" in self.request.path.split('/')
self.is_simple_cloning = "simple-clone" in self.request.path.split('/')
result = super().get_initial()
if self.is_cloning and not "other_versions" in result:
obj = self.get_object()
# if no DuplicatedEvents is associated, create one
@ -322,20 +326,18 @@ class EventUpdateView(
obj.save()
result["other_versions"] = obj.other_versions
result["status"] = Event.STATUS.PUBLISHED
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 form_valid(self, form):
original = self.get_object()
# if an image is uploaded, it removes the url stored
if form.cleaned_data['local_image'] != original.local_image:
form.instance.image = None
form.instance.import_sources = None
return super().form_valid(form)
class EventModerateView(
@ -504,6 +506,17 @@ class EventCreateView(SuccessMessageMixin, CreateView):
return _("The event has been submitted and will be published as soon as it has been validated by the moderation team.")
def form_valid(self, form):
logger.warning(str(form.cleaned_data))
if form.cleaned_data['simple_cloning']:
form.instance.set_skip_duplicate_check()
form.instance.import_sources = None
return super().form_valid(form)
def import_from_details(request):
form = EventForm(request.POST, is_authenticated=request.user.is_authenticated)
if form.is_valid():