1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374 |
- from django import forms
- from .models import Person
- from django.contrib.auth.forms import UserCreationForm
- from django.contrib.auth.models import User
- class UploadImage(forms.ModelForm):
- class Meta:
- model = Person
- fields = ['image']
-
- class SignUpForm(UserCreationForm):
- class Meta:
- model = User
- fields = ('first_name', 'last_name', 'email', 'username', 'password1', 'password2', )
- # Form to filter parents and partners by birth date
- class PersonAdminForm(forms.ModelForm):
- class Meta:
- model = Person
- fields = '__all__'
- widgets = {
- 'parent': forms.Select(attrs={'class': 'select2'}),
- 'partners': forms.SelectMultiple(attrs={'class': 'select2'}),
- }
- def __init__(self, *args, **kwargs):
- super(PersonAdminForm, self).__init__(*args, **kwargs)
- if self.instance and self.instance.pk:
- print(self.instance.birth_date)
- # Parents that were born before the person and didn't die before the person or don't have any information
- if self.instance.birth_date:
- possible_parents = Person.objects.filter(birth_date__lt=self.instance.birth_date, death_date__gt=self.instance.birth_date) | \
- Person.objects.filter(birth_date__lt=self.instance.birth_date, death_date__isnull=True, group=self.instance.group) | \
- Person.objects.filter(birth_date__isnull=True, death_date__gt=self.instance.birth_date, group=self.instance.group) | \
- Person.objects.filter(birth_date__isnull=True, death_date__isnull=True, group=self.instance.group)
- else:
- possible_parents = Person.objects.filter(group=self.instance.group)
- # return all persons if none were found
- if len(possible_parents) == 0:
- possible_parents = Person.objects.all()
-
- # add the current parent to the list of possible parents
- if self.instance.parent:
- possible_parents = possible_parents | Person.objects.filter(pk=self.instance.parent.pk)
- self.fields['parent'].queryset = possible_parents
- # Partners that were born before the person and didn't die before the person or don't have any information
- if self.instance.death_date and self.instance.birth_date:
- possible_partners = Person.objects.filter(birth_date__lt=self.instance.death_date, death_date__gt=self.instance.birth_date) | \
- Person.objects.filter(birth_date__lt=self.instance.death_date, death_date__isnull=True, group=self.instance.group) | \
- Person.objects.filter(birth_date__isnull=True, death_date__gt=self.instance.birth_date, group=self.instance.group) | \
- Person.objects.filter(birth_date__isnull=True, death_date__isnull=True, group=self.instance.group)
- elif self.instance.birth_date:
- possible_partners = Person.objects.filter(death_date__gt=self.instance.birth_date, group=self.instance.group) | \
- Person.objects.filter(death_date__isnull=True, group=self.instance.group)
- elif self.instance.death_date:
- possible_partners = Person.objects.filter(birth_date__lt=self.instance.death_date, group=self.instance.group) | \
- Person.objects.filter(birth_date__isnull=True, group=self.instance.group)
- else:
- possible_partners = Person.objects.filter(group=self.instance.group)
- # return all persons if none were found
- if len(possible_partners) == 0:
- possible_partners = Person.objects.all()
- # add the current partners to the list of possible partners
- if self.instance.partners:
- possible_partners = possible_partners | Person.objects.filter(pk__in=self.instance.partners.all())
- self.fields['partners'].queryset = possible_partners
|