from django.contrib import admin from django.contrib.auth.models import Group from django_reverse_admin import ReverseModelAdmin from django.contrib.messages import WARNING, ERROR, SUCCESS import seminar.models as m from datetime import datetime @admin.action(description="Sjednoť telefony") def sjednot_telefony(admin, request, queryset): for o in queryset: if o.telefon == '': continue try: telefon = int(o.telefon.replace(" ", "").replace("+", "")) # 6 míst if len(str(telefon)) == 9: o.telefon = "+420 " + str(telefon)[0:3] + " " + str(telefon)[3:6] + " " + str(telefon)[6:9] o.save() # 12 míst elif len(str(telefon)) == 12: o.telefon = "+" + str(telefon)[0:3] + " " + str(telefon)[3:6] + " " + str(telefon)[6:9] + " " + str(telefon)[9:12] o.save() else: raise ValueError except: admin.message_user(request, f"{o.jmeno} {o.prijmeni} (id: {o.id}) má divný telefon: {o.telefon}", level=ERROR) admin.message_user(request, "Telefony sjednoceny.", level=SUCCESS) # Tohle chceme umět použít i z ResitelAdmin @admin.action(description="Udělej z vybraných osob organizátory") def udelej_orgem(admin, request, queryset): org_group = Group.objects.get(name='org') uspesne_vytvoreni_orgove = 0 for o in queryset: if m.Organizator.objects.filter(osoba=o).exists(): # Ref: https://docs.djangoproject.com/en/3.2/ref/contrib/admin/#django.contrib.admin.ModelAdmin.message_user admin.message_user(request, f"Osoba {o} už je org, přeskakuji.", level=WARNING) continue user = o.user if user is None: admin.message_user(request, f"Osoba {o} nemá uživatele! Přeskakuji.", level=ERROR) continue user.groups.add(org_group) user.is_staff = True user.save() org = m.Organizator.objects.create(osoba=o, organizuje_od=datetime.now()) org.save() uspesne_vytvoreni_orgove += 1 admin.message_user(request, f'Úspěšně vytvořeno {uspesne_vytvoreni_orgove} orgů.', level=SUCCESS) @admin.register(m.Osoba) class OsobaAdmin(admin.ModelAdmin): actions = ['synchronizuj_maily', udelej_orgem, sjednot_telefony] search_fields = ['jmeno', 'prijmeni', 'prezdivka'] def synchronizuj_maily(self, request, queryset): for o in queryset: if o.user is not None: u = o.user u.email = o.email u.save() self.message_user(request, "E-maily synchronizovány.") synchronizuj_maily.short_description = "Synchronizuj vybraným osobám e-maily do uživatelů" class OsobaInline(admin.TabularInline): model = m.Osoba @admin.register(m.Organizator) class OrganizatorAdmin(ReverseModelAdmin): search_fields = ['osoba__jmeno', 'osoba__prijmeni', 'osoba__prezdivka'] inline_type = 'stacked' inline_reverse = ['osoba'] @admin.register(m.Resitel) class ResitelAdmin(ReverseModelAdmin): search_fields = ['osoba__jmeno', 'osoba__prijmeni', 'osoba__prezdivka'] list_filter = ['zasilat', 'zasilat_cislo_papirove', 'zasilat_cislo_emailem', 'rok_maturity'] ordering = ('osoba__prijmeni', 'osoba__jmeno') inline_type = 'stacked' inline_reverse = ['osoba'] actions = ['udelej_resitele_orgem'] @admin.action(description="Udělej z řešitelů organizátory") def udelej_resitele_orgem(self, req, qs): osoby = m.Osoba.objects.filter(resitel__in=qs) udelej_orgem(self, req, osoby) admin.site.register(m.Skola) admin.site.register(m.Prijemce)