Sauvegarde locale plus robuste

This commit is contained in:
Evarin 2018-03-21 13:14:14 +01:00
parent 3d659fab3c
commit cd4fe67929
3 changed files with 26 additions and 8 deletions

View file

@ -121,7 +121,7 @@ function initEditStage(STATIC_URL, API_URL) {
// SAUVEGARDE LOCALE EN CAS DE PÉPIN // SAUVEGARDE LOCALE EN CAS DE PÉPIN
function localSave () { function localSave (original) {
cleanForm(); cleanForm();
var formvals = {}; var formvals = {};
var els = $("#stageform").get(0).elements; 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["lieux-n"+i] = $("#change-lieux-"+i).text();
} }
extvals.date = new Date().getTime(); extvals.date = new Date().getTime();
var locsave = {"form": formvals, "misc": extvals}; var locfvals = JSON.stringify(formvals);
window.localStorage.setItem("stage_save_"+stage_object_id, JSON.stringify(locsave)); var locsave = JSON.stringify({"form": formvals, "misc": extvals});
console.log(formvals); 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 () { function restoreSave () {
@ -180,8 +187,8 @@ function initEditStage(STATIC_URL, API_URL) {
var locsave = window.localStorage.getItem("stage_save_"+stage_object_id); var locsave = window.localStorage.getItem("stage_save_"+stage_object_id);
if (locsave === null) return; if (locsave === null) return;
locsave = JSON.parse(locsave); locsave = JSON.parse(locsave);
var last_maj = $("#date_maj").val(); var last_maj = Number($("#date_maj").val());
console.log(last_maj, locsave.misc.date/1000); console.log( locsave.misc.date/1000-last_maj);
if (locsave.misc.date/1000 > last_maj+60) { if (locsave.misc.date/1000 > last_maj+60) {
var texte = ""; var texte = "";
if(stage_object_id == "new") 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 ?"; 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)) { if(confirm(texte)) {
restoreSave(); restoreSave();
return;
} }
} else {
console.log("clearing save");
window.localStorage.removeItem("stage_origsave_"+stage_object_id);
} }
setTimeout(localSave, 1000);
} }
function cleanForm() { function cleanForm() {

View file

@ -29,7 +29,7 @@
{% block content %} {% block content %}
<h1>{% if creation %}Nouvelle expérience{% else %}Modification d'une expérience{% endif %}</h1> <h1>{% if creation %}Nouvelle expérience{% else %}Modification d'une expérience{% endif %}</h1>
<input type="hidden" id="stage_object_id" value="{% if creation %}new{% else %}{{ form.instance.id }}{% endif %}"/> <input type="hidden" id="stage_object_id" value="{% if creation %}new{% else %}{{ form.instance.id }}{% endif %}"/>
<input type="hidden" id="date_maj" value="{% if creation %}{% else %}{{ form.instance.date_maj|date:"U" }}{% endif %}"/> <input type="hidden" id="date_maj" value="{% if last_maj %}{{ last_maj|date:"U" }}{% else %}0{% endif %}"/>
<form action="" method="post" id="stageform"> <form action="" method="post" id="stageform">
{% csrf_token %} {% csrf_token %}
{# Général #} {# Général #}

View file

@ -92,15 +92,21 @@ class ProfilEdit(LoginRequiredMixin, UpdateView):
@login_required @login_required
def manage_stage(request, pk=None): def manage_stage(request, pk=None):
# Objet de base # Objet de base
last_maj = None
if pk is None: if pk is None:
stage = Stage(auteur=request.user.profil) stage = Stage(auteur=request.user.profil)
avis_stage = AvisStage(stage=stage) avis_stage = AvisStage(stage=stage)
c_del = False 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: else:
try: try:
stage = Stage.objects.filter(auteur=request.user.profil).get(pk=pk) stage = Stage.objects.filter(auteur=request.user.profil).get(pk=pk)
except Stage.DoesNotExist: except Stage.DoesNotExist:
return HttpResponseForbidden() return HttpResponseForbidden()
last_maj = stage.date_maj
avis_stage, _ = AvisStage.objects.get_or_create(stage=stage) avis_stage, _ = AvisStage.objects.get_or_create(stage=stage)
c_del = True c_del = True
@ -140,7 +146,7 @@ def manage_stage(request, pk=None):
return render(request, "avisstage/formulaires/stage.html", return render(request, "avisstage/formulaires/stage.html",
{'form': form, 'avis_stage_form': avis_stage_form, {'form': form, 'avis_stage_form': avis_stage_form,
'avis_lieu_formset': avis_lieu_formset, 'avis_lieu_formset': avis_lieu_formset,
'creation': pk is None}) 'creation': pk is None, "last_maj": last_maj})
# Ajout d'un lieu de stage # Ajout d'un lieu de stage
#login_required #login_required