mirror of
				https://gitlab.crans.org/bde/nk20
				synced 2025-10-31 15:50:03 +01:00 
			
		
		
		
	Préparation du code pour le système de droits & filtrage par type de note pour l'auto-complétion
This commit is contained in:
		| @@ -125,6 +125,10 @@ class UserAutocomplete(autocomplete.Select2QuerySetView): | ||||
|         Quand une personne cherche un utilisateur par pseudo, 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 utilisateurs par pseudos. | ||||
|         """ | ||||
|         #  Un utilisateur non connecté n'a accès à aucune information | ||||
|         if not self.request.user.is_authenticated: | ||||
|             return User.objects.none() | ||||
|  | ||||
|         qs = User.objects.all() | ||||
|  | ||||
|         if self.q: | ||||
|   | ||||
| @@ -1,6 +1,6 @@ | ||||
| #!/usr/bin/env python | ||||
|  | ||||
| from dal import autocomplete | ||||
| from dal import autocomplete, forward | ||||
| from django import forms | ||||
| from .models import Transaction, TransactionTemplate | ||||
|  | ||||
| @@ -12,6 +12,8 @@ class TransactionTemplateForm(forms.ModelForm): | ||||
|         # Le champ de destination est remplacé par un champ d'auto-complétion. | ||||
|         # Quand des lettres sont tapées, une requête est envoyée sur l'API d'auto-complétion | ||||
|         # et récupère les aliases valides | ||||
|         # Pour force le type d'une note, il faut rajouter le paramètre : | ||||
|         # forward=(forward.Const('TYPE', 'note_type') où TYPE est dans {user, club, special} | ||||
|         widgets = { | ||||
|             'destination': autocomplete.ModelSelect2(url='note:note_autocomplete', | ||||
|                                                      attrs={ | ||||
| @@ -22,9 +24,14 @@ class TransactionTemplateForm(forms.ModelForm): | ||||
|  | ||||
|  | ||||
| class TransactionForm(forms.ModelForm): | ||||
|     def save(self, commit=True): | ||||
|         self.instance.transaction_type = 'transfert' | ||||
|  | ||||
|         super().save(commit) | ||||
|  | ||||
|     class Meta: | ||||
|         model = Transaction | ||||
|         fields = ('destination', 'reason', 'amount',) | ||||
|         fields = ('source', 'destination', 'reason', 'amount',) | ||||
|  | ||||
|         # Voir ci-dessus | ||||
|         widgets = { | ||||
| @@ -32,11 +39,10 @@ class TransactionForm(forms.ModelForm): | ||||
|                                                      attrs={ | ||||
|                                                          'data-placeholder': 'Note ...', | ||||
|                                                          'data-minimum-input-length': 1, | ||||
|                                                      }), | ||||
|                                                      },), | ||||
|             'destination': autocomplete.ModelSelect2(url='note:note_autocomplete', | ||||
|                                                      attrs={ | ||||
|                                                          'data-placeholder': 'Note ...', | ||||
|                                                          'data-minimum-input-length': 1, | ||||
|                                                      }), | ||||
|                                                      },), | ||||
|         } | ||||
|  | ||||
|   | ||||
| @@ -30,6 +30,27 @@ class TransactionCreate(LoginRequiredMixin, CreateView): | ||||
|         return context | ||||
|  | ||||
|  | ||||
|     def get_form(self, form_class=None): | ||||
|         """ | ||||
|         If the user has no right to transfer funds, then it won't have the choice of the source of the transfer. | ||||
|         """ | ||||
|         form = super().get_form(form_class) | ||||
|  | ||||
|         if False: # TODO: fix it with "if %user has no right to transfer funds" | ||||
|             del form.fields['source'] | ||||
|  | ||||
|         return form | ||||
|  | ||||
|     def form_valid(self, form): | ||||
|         """ | ||||
|         If the user has no right to transfer funds, then it will be the source of the transfer by default. | ||||
|         """ | ||||
|         if False: # TODO: fix it with "if %user has no right to transfer funds" | ||||
|             form.instance.source = self.request.user.note | ||||
|  | ||||
|         return super().form_valid(form) | ||||
|  | ||||
|  | ||||
| class NoteAutocomplete(autocomplete.Select2QuerySetView): | ||||
|     """ | ||||
|     Auto complete note by aliases | ||||
| @@ -40,11 +61,29 @@ class NoteAutocomplete(autocomplete.Select2QuerySetView): | ||||
|         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. | ||||
|         """ | ||||
|         #  Un utilisateur non connecté n'a accès à aucune information | ||||
|         if not self.request.user.is_authenticated: | ||||
|             return Note.objects.none() | ||||
|  | ||||
|         qs = Note.objects.all() | ||||
|  | ||||
|         # self.q est le paramètre de la recherche | ||||
|         if self.q: | ||||
|             qs = qs.filter(Q(alias__name__regex=self.q) | Q(alias__normalized_name__regex=self.q)) | ||||
|  | ||||
|         # Filtrage par type de note (user, club, special) | ||||
|         note_type = self.forwarded.get("note_type", None) | ||||
|         if note_type: | ||||
|             l = str(note_type).lower() | ||||
|             if "user" in l: | ||||
|                 qs = qs.filter(polymorphic_ctype__model="noteuser") | ||||
|             elif "club" in l: | ||||
|                 qs = qs.filter(polymorphic_ctype__model="noteclub") | ||||
|             elif "special" in l: | ||||
|                 qs = qs.filter(polymorphic_ctype__model="notespecial") | ||||
|             else: | ||||
|                 qs = qs.none() | ||||
|  | ||||
|         return qs | ||||
|  | ||||
|  | ||||
|   | ||||
		Reference in New Issue
	
	Block a user