forms.py 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. # -*- coding: utf-8 -*-
  2. from django.core.exceptions import ValidationError
  3. from django.contrib.auth import get_user_model
  4. from django.db.models import Q
  5. from django import forms
  6. from .models import Storage
  7. User = get_user_model()
  8. class LoanReturnForm(forms.Form):
  9. storage = forms.ModelChoiceField(
  10. label='Dans quel lieu de stockage ai-je remis le matériel ?',
  11. required=False,
  12. queryset=Storage.objects.all(), empty_label='Je ne sais pas')
  13. class LoanTransferForm(forms.Form):
  14. target_user = forms.CharField(
  15. max_length=100,
  16. label='Adhérent',
  17. help_text='email, pseudonyme ou numéro de l\'adhérent',
  18. )
  19. def clean_target_user(self):
  20. value = self.cleaned_data['target_user']
  21. result = User.objects.filter(
  22. Q(email__iexact=value)
  23. | Q(pk__iexact=value)
  24. | Q(nickname__iexact=value)
  25. | Q(username__iexact=value)
  26. )
  27. if result.count() > 1:
  28. raise ValidationError(
  29. "La recherche retourne plus d'un adhérent")
  30. elif result.count() < 1:
  31. raise ValidationError(
  32. "Aucun adhérent ne correspond à cette recherche")
  33. return result.first()