Add some comments

This commit is contained in:
Yohann D'ANELLO 2020-02-17 11:36:46 +01:00
parent 3ce5f31411
commit c884cbb0ad
2 changed files with 9 additions and 9 deletions

View File

@ -4,11 +4,8 @@
# SPDX-License-Identifier: GPL-3.0-or-later # SPDX-License-Identifier: GPL-3.0-or-later
from dal import autocomplete from dal import autocomplete
from django.contrib.auth.mixins import LoginRequiredMixin from django.contrib.auth.mixins import LoginRequiredMixin
from django.core.exceptions import ValidationError
from django.utils.translation import gettext_lazy as _ from django.utils.translation import gettext_lazy as _
from django.views.generic import CreateView, ListView, DetailView, UpdateView from django.views.generic import CreateView, ListView, DetailView, UpdateView
from django.http import HttpResponseRedirect
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth.models import User from django.contrib.auth.models import User
from django.urls import reverse_lazy from django.urls import reverse_lazy
from django.db.models import Q from django.db.models import Q
@ -65,13 +62,13 @@ class UserUpdateView(LoginRequiredMixin,UpdateView):
return context return context
def get_form(self, form_class=None): def get_form(self, form_class=None):
from django.forms import forms form = super().get_form(form_class)
form: forms.Form = super().get_form(form_class)
if 'username' not in form.data: if 'username' not in form.data:
return form return form
new_username = form.data['username'] new_username = form.data['username']
# Si l'utilisateur cherche à modifier son pseudo, le nouveau pseudo ne doit pas être proche d'un alias existant
note = NoteUser.objects.filter(alias__normalized_name=Alias.normalize(new_username)) note = NoteUser.objects.filter(alias__normalized_name=Alias.normalize(new_username))
if note.exists() and note.get().user != self.request.user: if note.exists() and note.get().user != self.request.user:
form.add_error('username', _("An alias with a similar name already exists.")) form.add_error('username', _("An alias with a similar name already exists."))
@ -84,16 +81,17 @@ class UserUpdateView(LoginRequiredMixin,UpdateView):
if form.is_valid() and profile_form.is_valid(): if form.is_valid() and profile_form.is_valid():
new_username = form.data['username'] new_username = form.data['username']
alias = Alias.objects.filter(name=new_username) alias = Alias.objects.filter(name=new_username)
# Si le nouveau pseudo n'est pas un de nos alias, on supprime éventuellement un alias similaire pour le remplacer
if not alias.exists(): if not alias.exists():
similar = Alias.objects.filter(normalized_name=Alias.normalize(new_username)) similar = Alias.objects.filter(normalized_name=Alias.normalize(new_username))
if similar.exists(): if similar.exists():
similar.delete() similar.delete()
note = NoteUser.objects.filter(alias__normalized_name=Alias.normalize(self.request.user.username)).get()
Alias(name=new_username, note=note).save() user = form.save(commit=False)
user = form.save()
profile = profile_form.save(commit=False) profile = profile_form.save(commit=False)
profile.user = user profile.user = user
profile.save() profile.save()
user.save()
return super().form_valid(form) return super().form_valid(form)
def get_success_url(self, **kwargs): def get_success_url(self, **kwargs):

View File

@ -60,7 +60,7 @@ class NoteAutocomplete(autocomplete.Select2QuerySetView):
def get_queryset(self): def get_queryset(self):
""" """
Quand une personne cherche un alias, une requête est envoyée sur l'API dédiée à l'auto-complétion. Quand une personne cherche un alias, une requête est envoyée sur l'API dédiée à l'auto-complétion.
Cette fonction récupère la requête, et renvoie la liste filtrée des notes par aliases. Cette fonction récupère la requête, et renvoie la liste filtrée des aliases.
""" """
# Un utilisateur non connecté n'a accès à aucune information # Un utilisateur non connecté n'a accès à aucune information
if not self.request.user.is_authenticated: if not self.request.user.is_authenticated:
@ -89,6 +89,7 @@ class NoteAutocomplete(autocomplete.Select2QuerySetView):
return qs return qs
def get_result_label(self, result): def get_result_label(self, result):
# Gère l'affichage de l'alias dans la recherche
res = result.name res = result.name
note_name = str(result.note) note_name = str(result.note)
if res != note_name: if res != note_name:
@ -96,6 +97,7 @@ class NoteAutocomplete(autocomplete.Select2QuerySetView):
return res return res
def get_result_value(self, result): def get_result_value(self, result):
# Le résultat renvoyé doit être l'identifiant de la note, et non de l'alias
return str(result.note.pk) return str(result.note.pk)