Browse Source

improve admin view

subDesTagesMitExtraKaese 2 years ago
parent
commit
af5c50fb7c
2 changed files with 66 additions and 13 deletions
  1. 11 7
      admin.py
  2. 55 6
      forms.py

+ 11 - 7
admin.py

@@ -1,11 +1,12 @@
 import random
+from django import forms
 from django.urls import reverse
 from django.utils.html import format_html, format_html_join
 
 from django.contrib import admin
 
-from .models import *
-from .forms import PersonChangeForm
+from .models import Group, Person
+from .forms import PersonAdminForm
 
 def random_color():
   r = lambda: random.randint(0,255)
@@ -15,14 +16,14 @@ def random_color():
 class PersonAdmin(admin.ModelAdmin):
   list_select_related = ('parent', 'group')
   list_display = ['name', 'get_partners', 'get_parent', 'birth_date', 'death_date', 'comment', 'color', 'group']
-  list_editable = ['color']#, 'group']
+  list_editable = ['color']
   list_filter = ('group', 'birth_date')
-  search_fields = ['name', 'parent__name', 'partners__name']
+  search_fields = ['name', 'birth_date', 'birth_town', 'death_date', 'death_town', 'comment']
   ordering = ('group','name')
   filter_horizontal = ('partners',)
 
-  fields = ('name', 'parent', 'partners', 'birth_date', 'birth_town', 'death_date', 'death_town', 'comment', 'image', 'image_tag', 'color', 'group', )
-  readonly_fields = ('image_tag',)
+  fields = ('name', 'parent', 'partners', 'children', 'birth_date', 'birth_town', 'death_date', 'death_town', 'comment', 'image', 'image_tag', 'color', 'group')
+  readonly_fields = ('image_tag', 'children')
 
   def get_queryset(self, request):
     qs = super().get_queryset(request)
@@ -37,6 +38,9 @@ class PersonAdmin(admin.ModelAdmin):
       return format_html('<a href="{}">{}</a>', reverse("admin:stammbaum_person_change", args=[obj.parent.id]), obj.parent)
   get_parent.short_description = "Eltern"
 
+  def children(self, obj):
+    return format_html_join(', ', '<a href="{}">{}</a>', [(reverse("admin:stammbaum_person_change", args=[p.id]), p) for p in Person.objects.filter(parent=obj)])
+
   def save_related(self, request, form, formsets, change):
     super(PersonAdmin, self).save_related(request, form, formsets, change)
     partners = form.instance.partners.all()
@@ -54,7 +58,7 @@ class PersonAdmin(admin.ModelAdmin):
       partner.color = form.instance.color
       partner.save()
 
-  #form = PersonChangeForm
+  form = PersonAdminForm
 
 @admin.register(Group)
 class GroupAdmin(admin.ModelAdmin):

+ 55 - 6
forms.py

@@ -15,11 +15,60 @@ class SignUpForm(UserCreationForm):
     model = User
     fields = ('first_name', 'last_name', 'email', 'username', 'password1', 'password2', )
 
-class PersonChangeForm(forms.ModelForm):
-  def __init__(self, instance) -> None:
-    print(instance)
-    super().__init__(instance)
-    #self.fields['partner'].queryset = Person.objects.complex_filter()
+# Form to filter parents and partners by birth date
+class PersonAdminForm(forms.ModelForm):
   class Meta:
     model = Person
-    fields = '__all__'
+    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