forms.py 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. from django import forms
  2. from .models import Person
  3. from django.contrib.auth.forms import UserCreationForm
  4. from django.contrib.auth.models import User
  5. class UploadImage(forms.ModelForm):
  6. class Meta:
  7. model = Person
  8. fields = ['image']
  9. class SignUpForm(UserCreationForm):
  10. class Meta:
  11. model = User
  12. fields = ('first_name', 'last_name', 'email', 'username', 'password1', 'password2', )
  13. # Form to filter parents and partners by birth date
  14. class PersonAdminForm(forms.ModelForm):
  15. class Meta:
  16. model = Person
  17. fields = '__all__'
  18. widgets = {
  19. 'parent': forms.Select(attrs={'class': 'select2'}),
  20. 'partners': forms.SelectMultiple(attrs={'class': 'select2'}),
  21. }
  22. def __init__(self, *args, **kwargs):
  23. super(PersonAdminForm, self).__init__(*args, **kwargs)
  24. if self.instance and self.instance.pk:
  25. print(self.instance.birth_date)
  26. # Parents that were born before the person and didn't die before the person or don't have any information
  27. if self.instance.birth_date:
  28. possible_parents = Person.objects.filter(birth_date__lt=self.instance.birth_date, death_date__gt=self.instance.birth_date) | \
  29. Person.objects.filter(birth_date__lt=self.instance.birth_date, death_date__isnull=True, group=self.instance.group) | \
  30. Person.objects.filter(birth_date__isnull=True, death_date__gt=self.instance.birth_date, group=self.instance.group) | \
  31. Person.objects.filter(birth_date__isnull=True, death_date__isnull=True, group=self.instance.group)
  32. else:
  33. possible_parents = Person.objects.filter(group=self.instance.group)
  34. # return all persons if none were found
  35. if len(possible_parents) == 0:
  36. possible_parents = Person.objects.all()
  37. # add the current parent to the list of possible parents
  38. if self.instance.parent:
  39. possible_parents = possible_parents | Person.objects.filter(pk=self.instance.parent.pk)
  40. self.fields['parent'].queryset = possible_parents
  41. # Partners that were born before the person and didn't die before the person or don't have any information
  42. if self.instance.death_date and self.instance.birth_date:
  43. possible_partners = Person.objects.filter(birth_date__lt=self.instance.death_date, death_date__gt=self.instance.birth_date) | \
  44. Person.objects.filter(birth_date__lt=self.instance.death_date, death_date__isnull=True, group=self.instance.group) | \
  45. Person.objects.filter(birth_date__isnull=True, death_date__gt=self.instance.birth_date, group=self.instance.group) | \
  46. Person.objects.filter(birth_date__isnull=True, death_date__isnull=True, group=self.instance.group)
  47. elif self.instance.birth_date:
  48. possible_partners = Person.objects.filter(death_date__gt=self.instance.birth_date, group=self.instance.group) | \
  49. Person.objects.filter(death_date__isnull=True, group=self.instance.group)
  50. elif self.instance.death_date:
  51. possible_partners = Person.objects.filter(birth_date__lt=self.instance.death_date, group=self.instance.group) | \
  52. Person.objects.filter(birth_date__isnull=True, group=self.instance.group)
  53. else:
  54. possible_partners = Person.objects.filter(group=self.instance.group)
  55. # return all persons if none were found
  56. if len(possible_partners) == 0:
  57. possible_partners = Person.objects.all()
  58. # add the current partners to the list of possible partners
  59. if self.instance.partners:
  60. possible_partners = possible_partners | Person.objects.filter(pk__in=self.instance.partners.all())
  61. self.fields['partners'].queryset = possible_partners