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
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() {

View file

@ -29,7 +29,7 @@
{% block content %}
<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="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">
{% csrf_token %}
{# Général #}

View file

@ -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