forms.py 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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. from .validators import validate_future_date
  8. User = get_user_model()
  9. class LoanDeclareForm(forms.Form):
  10. loan_date_end = forms.DateField(
  11. label='Date de retour prévue',
  12. required=False,
  13. validators=[validate_future_date],
  14. input_formats=['%d/%m/%Y'],
  15. help_text='laisser vide si non planifié',
  16. widget=forms.TextInput(
  17. attrs={'type': 'date', 'placeholder': 'JJ/MM/AAAA'}))
  18. class LoanReturnForm(forms.Form):
  19. storage = forms.ModelChoiceField(
  20. label='Dans quel lieu de stockage ai-je remis le matériel ?',
  21. required=False,
  22. queryset=Storage.objects.all(), empty_label='Je ne sais pas')
  23. class LoanTransferForm(forms.Form):
  24. target_user = forms.CharField(
  25. max_length=100,
  26. label='Adhérent',
  27. help_text='email, pseudonyme ou numéro de l\'adhérent',
  28. )
  29. def clean_target_user(self):
  30. value = self.cleaned_data['target_user']
  31. result = User.objects.filter(
  32. Q(email__iexact=value)
  33. | Q(pk__iexact=value)
  34. | Q(nickname__iexact=value)
  35. | Q(username__iexact=value)
  36. )
  37. if result.count() > 1:
  38. raise ValidationError(
  39. "La recherche retourne plus d'un adhérent")
  40. elif result.count() < 1:
  41. raise ValidationError(
  42. "Aucun adhérent ne correspond à cette recherche")
  43. return result.first()