From e455859e13c0c26a9e0f992932dfb8c03aeae703 Mon Sep 17 00:00:00 2001 From: Nanoy Date: Tue, 19 Feb 2019 08:26:54 +0100 Subject: [PATCH] Vue verify --- preferences/views.py | 2 +- users/models.py | 10 +++++++++- users/urls.py | 1 + users/views.py | 22 +++++++++++++++++++--- 4 files changed, 30 insertions(+), 5 deletions(-) diff --git a/preferences/views.py b/preferences/views.py index e5a3f7a..5fd61a5 100644 --- a/preferences/views.py +++ b/preferences/views.py @@ -263,7 +263,7 @@ def get_config(request): """ Load the config and return it in a json format """ - gp,_ = GeneralPreferences.objects.get_or_create(pk=1) + gp,_ = GeneralPreferences.objects.get_or_create(pk=1).defer("statutes", "rules", "menu") data = json.dumps(model_to_dict(gp)) return HttpResponse(data, content_type='application/json') \ No newline at end of file diff --git a/users/models.py b/users/models.py index b586df7..2ae5c2c 100644 --- a/users/models.py +++ b/users/models.py @@ -75,14 +75,22 @@ class Profile(models.Model): debit = models.DecimalField(max_digits=7, decimal_places=2, default=0) school = models.ForeignKey(School, on_delete=models.PROTECT, blank=True, null=True) cotisationEnd = models.DateTimeField(blank=True, null=True) + date_verified = models.DateTimeField(blank=True, null=True) history = HistoricalRecords() + @property + def is_verified(self): + """ + Test if a user is verified + """ + return self.date_verified is not None + @property def is_adherent(self): """ Test if a user is adherent """ - if(self.cotisationEnd and self.cotisationEnd > timezone.now()): + if(self.is_verified and self.cotisationEnd and self.cotisationEnd > timezone.now()): return True else: return False diff --git a/users/urls.py b/users/urls.py index 2201cae..03f64e9 100644 --- a/users/urls.py +++ b/users/urls.py @@ -45,4 +45,5 @@ urlpatterns = [ path('allMenus//', views.all_menus, name="allMenus"), path('exportCSV', views.export_csv, name="exportCSV"), path('switchActivateUser/', views.switch_activate_user, name="switchActivateUser"), + path('verify/', views.verify, name="verify") ] diff --git a/users/views.py b/users/views.py index c2ce219..43e0644 100644 --- a/users/views.py +++ b/users/views.py @@ -20,6 +20,10 @@ from .models import CotisationHistory, WhiteListHistory, School from .forms import CreateUserForm, LoginForm, CreateGroupForm, EditGroupForm, SelectUserForm, GroupsEditForm, EditPasswordForm, addCotisationHistoryForm, addCotisationHistoryForm, addWhiteListHistoryForm, SelectNonAdminUserForm, SelectNonSuperUserForm, SchoolForm, ExportForm from gestion.models import Reload, Consumption, ConsumptionHistory, MenuHistory + +from django.contrib.auth.forms import SetPasswordForm + + @active_required def loginView(request): """ @@ -177,8 +181,6 @@ def profile(request, pk): if quantities_pre[k]/totQ >= 0.01: products.append(products_pre[k]) quantities.append(quantities_pre[k]) - print(products) - print(quantities) lastConsumptions = ConsumptionHistory.objects.filter(customer=user).order_by('-date')[:10] lastMenus = MenuHistory.objects.filter(customer=user).order_by('-date')[:10] return render(request, "users/profile.html", @@ -219,7 +221,6 @@ def createUser(request): form = CreateUserForm(request.POST or None) if(form.is_valid()): user = form.save(commit=False) - user.set_password(user.username) user.save() user.profile.school = form.cleaned_data['school'] user.save() @@ -512,6 +513,21 @@ def switch_activate_user(request, pk): user.save() messages.success(request, "Le statut de l'utilisateur a bien été changé") return HttpResponseRedirect(request.META.get('HTTP_REFERER', '/')) + +@active_required +def verify(request, pk): + user = get_object_or_404(User, pk=pk) + if(user.profile.is_verified): + messages.error(request, "L'utilisateur est déjà vérifié") + return redirect(reverse('users:login')) + form = SetPasswordForm(user, request.POST or None) + if(form.is_valid()): + form.save() + user.profile.date_verified = datetime.now() + user.save() + messages.success(request, "Le compte a bien été vérifié") + return redirect(reverse('users:login')) + return render(request, "users/verify.html", {"user": user, "form": form}) ########## Groups ##########