forms.py 974 B

123456789101112131415161718192021222324252627282930313233
  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. User = get_user_model()
  7. class LoanTransferForm(forms.Form):
  8. target_user = forms.CharField(
  9. max_length=100,
  10. label='Adhérent',
  11. help_text='email, pseudonyme ou numéro de l\'adhérent',
  12. )
  13. def clean_target_user(self):
  14. value = self.cleaned_data['target_user']
  15. result = User.objects.filter(
  16. Q(email__iexact=value)
  17. | Q(pk__iexact=value)
  18. | Q(nickname__iexact=value)
  19. | Q(username__iexact=value)
  20. )
  21. if result.count() > 1:
  22. raise ValidationError(
  23. "La recherche retourne plus d'un adhérent")
  24. elif result.count() < 1:
  25. raise ValidationError(
  26. "Aucun adhérent ne correspond à cette recherche")
  27. return result.first()