From a2e49cca18f3df41a96e9ef3ad07bc363e437cc0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mika=C3=ABl=20Ates?= Date: Fri, 26 Sep 2014 18:39:14 +0200 Subject: [PATCH] agenda: prevent periodic modification and deletion if one act is not new. --- calebasse/agenda/forms.py | 40 +++++++++---------- calebasse/agenda/models.py | 19 +++++++++ .../agenda/templates/agenda/appointment.html | 19 +++++---- .../agenda/templates/agenda/periodicity.html | 2 +- calebasse/static/js/calebasse.agenda.js | 4 +- 5 files changed, 53 insertions(+), 31 deletions(-) diff --git a/calebasse/agenda/forms.py b/calebasse/agenda/forms.py index 82de1873..280d6042 100644 --- a/calebasse/agenda/forms.py +++ b/calebasse/agenda/forms.py @@ -156,31 +156,29 @@ class UpdatePeriodicAppointmentForm(UpdateAppointmentForm): choices=Event.PERIODICITIES, required=True) def clean(self): - ''' - Check that reccurrency bound dates - won't exclude already_billed acts. - ''' cleaned_data = super(UpdatePeriodicAppointmentForm, self).clean() + '''If one act not new in the reccurrence, we prevent to modify the + start date or the recurrence_periodicity since this could trigger act + deletion''' start_datetime = cleaned_data.get('start_datetime') - if start_datetime: - acts = Act.objects.filter( - Q(parent_event=self.instance, - already_billed=True, date__lt=start_datetime) | \ - Q(parent_event__exception_to=self.instance, - already_billed=True, date__lt=start_datetime)) - if acts: - self._errors['start_datetime'] = self.error_class([ - u"La date de début doit être antérieure au premier acte déja facturé de la récurrence"]) + if start_datetime and start_datetime != self.instance.start_datetime \ + and self.instance.one_act_not_new(): + self._errors['start_datetime'] = self.error_class([ + u"La date de début ne peut-être modifiée car un acte de la récurrence est pointé"]) +# FIXME +# recurrence_periodicity = cleaned_data.get('recurrence_periodicity') +# if recurrence_periodicity and recurrence_periodicity != str(self.instance.recurrence_periodicity) \ +# and self.instance.one_act_not_new(): +# self._errors['recurrence_periodicity'] = self.error_class([ +# u"La récurrence ne peut-être modifiée car un acte de la récurrence est pointé"]) + '''We check that the end date is posterior to the last act not new''' recurrence_end_date = cleaned_data.get('recurrence_end_date') - if recurrence_end_date: - acts = Act.objects.filter( - Q(parent_event=self.instance, - already_billed=True, date__gt=recurrence_end_date) | \ - Q(parent_event__exception_to=self.instance, - already_billed=True, date__gt=recurrence_end_date)) - if acts: + if recurrence_end_date and recurrence_end_date != self.instance.recurrence_end_date \ + and self.instance.one_act_not_new(): + last = self.instance.last_act_not_new() + if last and last.date > recurrence_end_date: self._errors['recurrence_end_date'] = self.error_class([ - u"La date de fin doit être postérieure au dernier acte déja facturé de la récurrence"]) + u"La date de fin doit être postérieure au dernier acte pointé de la récurrence (%s)" % last]) return cleaned_data class DisablePatientAppointmentForm(UpdateAppointmentForm): diff --git a/calebasse/agenda/models.py b/calebasse/agenda/models.py index 6c8ed55e..c0e5f6ee 100644 --- a/calebasse/agenda/models.py +++ b/calebasse/agenda/models.py @@ -395,6 +395,25 @@ class Event(models.Model): models.Q(parent_event=self) | \ models.Q(parent_event__exception_to=self)) + def one_act_not_new(self): + ''' + Return True if at least one act of the present event or an act + of one of its exceptions is not new. + ''' + return True if [a for a in self.acts if not a.is_new()] else False + + def last_act_not_new(self): + ''' + Return True if at least one act of the present event or an act + of one of its exceptions is not new. + ''' + act = None + for a in self.acts: + if not a.is_new(): + if not act or a.date > act.date: + act = a + return act + def one_act_already_billed(self): ''' Return True if at least one act of the present event or an act diff --git a/calebasse/agenda/templates/agenda/appointment.html b/calebasse/agenda/templates/agenda/appointment.html index 93052de7..7d2ccd9e 100644 --- a/calebasse/agenda/templates/agenda/appointment.html +++ b/calebasse/agenda/templates/agenda/appointment.html @@ -3,14 +3,14 @@ {{ form.non_field_errors }} {{ form.start_datetime }} {{ form.start_datetime.errors }} - {% if object.is_recurring and object.one_act_is_billed %}

Le rendez-vous périodique a un acte qui est facturé ce qui empêche de le modifier à l'exception de la description, de la ressource et des dates de début et de fin.

{% endif %} - {% if object.is_recurring and object.one_act_already_billed %}

Le rendez-vous périodique a un acte qui a déjà été facturé ce qui empêche de le supprimer.

{% endif %} + {% if object.is_recurring and object.one_act_not_new %}

Le rendez-vous périodique a un acte qui est pointé ce qui empêche de le modifier à l'exception de la description, de la ressource et de la date de fin.

{% endif %} + {% if object.is_recurring and object.one_act_not_new %}

Le rendez-vous périodique a un acte qui est pointé ce qui empêche de le supprimer.

{% endif %} @@ -52,7 +57,7 @@

{{ form.time.label_tag }} - {% if object.is_recurring and object.one_act_is_billed %} + {% if object.is_recurring and object.one_act_not_new %} {{ object.time }} {{ form.time.as_hidden }} {% else %} @@ -22,7 +22,7 @@

{{ form.duration.label_tag }} - {% if object.is_recurring and object.one_act_is_billed %} + {% if object.is_recurring and object.one_act_not_new %} {{ object.duration }} {{ form.duration.as_hidden }} {% else %} @@ -34,7 +34,12 @@

{{ form.date.label_tag }} + {% if object.is_recurring and object.one_act_not_new %} + {{ object.date }} + {{ form.date.as_hidden }} + {% else %} {{ form.date|add_class:"datepicker-date" }} + {% endif %} {{ form.date.errors }}

{{ form.participants.label_tag }}

- {% if object.is_recurring and object.one_act_is_billed %} + {% if object.is_recurring and object.one_act_not_new %}
    {% for p in object.participants.all %}
  • {{ p }}
  • {% endfor %}
{% else %} @@ -75,7 +80,7 @@

{{ form.act_type.label_tag }}

- {% if object.is_recurring and object.one_act_is_billed %} + {% if object.is_recurring and object.one_act_not_new %} {{ object.act_type }} {{ form.act_type.as_hidden }} {% else %} @@ -108,7 +113,7 @@ {{ object.exception_to.recurrence_description|lower }}

{% if not object.exception_to.canceled %} -

+

{% endif %} {% endif %} diff --git a/calebasse/agenda/templates/agenda/periodicity.html b/calebasse/agenda/templates/agenda/periodicity.html index 013042d8..0ee215fd 100644 --- a/calebasse/agenda/templates/agenda/periodicity.html +++ b/calebasse/agenda/templates/agenda/periodicity.html @@ -4,7 +4,7 @@

{{ form.recurrence_periodicity.label_tag }} - {% if object.one_act_already_billed %} + {% if object.one_act_not_new %} {{ object.recurrence_description }} {{ form.recurrence_periodicity.as_hidden }} {% else %} diff --git a/calebasse/static/js/calebasse.agenda.js b/calebasse/static/js/calebasse.agenda.js index ecc84e5d..3197fac4 100644 --- a/calebasse/static/js/calebasse.agenda.js +++ b/calebasse/static/js/calebasse.agenda.js @@ -280,9 +280,9 @@ function event_dialog(url, title, width, btn_text) { $(base).on('click', '.update-periodic-rdv', function (event) { $('.ui-icon-closethick').click(); var id = $(this).data('id'); - var one_act_already_billed = $(this).data('one_act_already_billed'); + var one_act_not_new = $(this).data('one_act_not_new'); var delete_button = null; - if (one_act_already_billed == 'False') { + if (one_act_not_new == 'False') { var delete_url = $(this).data('delete-url'); var delete_button = { text: "Supprimer",