From cd4fe6792975f683c197bc823696cd93a0902427 Mon Sep 17 00:00:00 2001 From: Evarin Date: Wed, 21 Mar 2018 13:14:14 +0100 Subject: [PATCH] Sauvegarde locale plus robuste --- avisstage/static/js/editstage.js | 24 ++++++++++++++----- .../avisstage/formulaires/stage.html | 2 +- avisstage/views.py | 8 ++++++- 3 files changed, 26 insertions(+), 8 deletions(-) diff --git a/avisstage/static/js/editstage.js b/avisstage/static/js/editstage.js index db8bd70..195f195 100644 --- a/avisstage/static/js/editstage.js +++ b/avisstage/static/js/editstage.js @@ -121,7 +121,7 @@ function initEditStage(STATIC_URL, API_URL) { // SAUVEGARDE LOCALE EN CAS DE PÉPIN - function localSave () { + function localSave (original) { cleanForm(); var formvals = {}; var els = $("#stageform").get(0).elements; @@ -137,9 +137,16 @@ function initEditStage(STATIC_URL, API_URL) { extvals["lieux-n"+i] = $("#change-lieux-"+i).text(); } extvals.date = new Date().getTime(); - var locsave = {"form": formvals, "misc": extvals}; - window.localStorage.setItem("stage_save_"+stage_object_id, JSON.stringify(locsave)); - console.log(formvals); + var locfvals = JSON.stringify(formvals); + var locsave = JSON.stringify({"form": formvals, "misc": extvals}); + var origfvals = window.localStorage.getItem("stage_origsave_"+stage_object_id); + if (origfvals === null) // sauvegarde de l'original + window.localStorage.setItem("stage_origsave_"+stage_object_id, + locfvals) + else if(origfvals != locfvals) // modifications depuis l'original + window.localStorage.setItem("stage_save_"+stage_object_id, + locsave); + console.log(origfvals == locfvals, formvals); } function restoreSave () { @@ -180,8 +187,8 @@ function initEditStage(STATIC_URL, API_URL) { var locsave = window.localStorage.getItem("stage_save_"+stage_object_id); if (locsave === null) return; locsave = JSON.parse(locsave); - var last_maj = $("#date_maj").val(); - console.log(last_maj, locsave.misc.date/1000); + var last_maj = Number($("#date_maj").val()); + console.log( locsave.misc.date/1000-last_maj); if (locsave.misc.date/1000 > last_maj+60) { var texte = ""; if(stage_object_id == "new") @@ -190,8 +197,13 @@ function initEditStage(STATIC_URL, API_URL) { texte = "Une sauvegarde automatique plus récente que la dernière modification de cette fiche de stage existe, voulez-vous la récupérer ?"; if(confirm(texte)) { restoreSave(); + return; } + } else { + console.log("clearing save"); + window.localStorage.removeItem("stage_origsave_"+stage_object_id); } + setTimeout(localSave, 1000); } function cleanForm() { diff --git a/avisstage/templates/avisstage/formulaires/stage.html b/avisstage/templates/avisstage/formulaires/stage.html index 909cfff..e4c04b7 100644 --- a/avisstage/templates/avisstage/formulaires/stage.html +++ b/avisstage/templates/avisstage/formulaires/stage.html @@ -29,7 +29,7 @@ {% block content %}

{% if creation %}Nouvelle expérience{% else %}Modification d'une expérience{% endif %}

- +
{% csrf_token %} {# Général #} diff --git a/avisstage/views.py b/avisstage/views.py index 4782226..a59bce5 100644 --- a/avisstage/views.py +++ b/avisstage/views.py @@ -92,15 +92,21 @@ class ProfilEdit(LoginRequiredMixin, UpdateView): @login_required def manage_stage(request, pk=None): # Objet de base + last_maj = None if pk is None: stage = Stage(auteur=request.user.profil) avis_stage = AvisStage(stage=stage) c_del = False + last_creation = Stage.objects.filter(auteur=request.user.profil)\ + .order_by("-date_creation")[:1] + if len(last_creation) != 0: + last_maj = last_creation[0].date_creation else: try: stage = Stage.objects.filter(auteur=request.user.profil).get(pk=pk) except Stage.DoesNotExist: return HttpResponseForbidden() + last_maj = stage.date_maj avis_stage, _ = AvisStage.objects.get_or_create(stage=stage) c_del = True @@ -140,7 +146,7 @@ def manage_stage(request, pk=None): return render(request, "avisstage/formulaires/stage.html", {'form': form, 'avis_stage_form': avis_stage_form, 'avis_lieu_formset': avis_lieu_formset, - 'creation': pk is None}) + 'creation': pk is None, "last_maj": last_maj}) # Ajout d'un lieu de stage #login_required