mamweb/personalni/admin.py

62 lines
2.1 KiB
Python
Raw Normal View History

2021-10-08 18:06:02 +02:00
from django.contrib import admin
from django.contrib.auth.models import Group
from django_reverse_admin import ReverseModelAdmin
2023-06-21 15:51:08 +02:00
from django.contrib.messages import WARNING, ERROR, SUCCESS
2021-10-08 18:06:02 +02:00
import seminar.models as m
from datetime import datetime
2021-10-08 18:06:02 +02:00
@admin.register(m.Osoba)
class OsobaAdmin(admin.ModelAdmin):
actions = ['synchronizuj_maily', 'udelej_orgem']
2021-12-13 23:46:53 +01:00
search_fields = ['jmeno', 'prijmeni', 'prezdivka']
2021-10-08 18:06:02 +02:00
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ů"
def udelej_orgem(self,request,queryset):
org_group = Group.objects.get(name='org')
2023-06-21 15:51:08 +02:00
uspesne_vytvoreni_orgove = 0
2021-10-08 18:06:02 +02:00
for o in queryset:
2023-06-21 15:51:08 +02:00
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
self.message_user(request, f"Osoba {o} už je org, přeskakuji.", level=WARNING)
continue
2021-10-08 18:06:02 +02:00
user = o.user
2023-06-21 15:51:08 +02:00
if user is None:
self.message_user(request, f"Osoba {o} nemá uživatele! Přeskakuji.", level=ERROR)
continue
2021-10-08 18:06:02 +02:00
user.groups.add(org_group)
user.is_staff = True
user.save()
org = m.Organizator.objects.create(osoba=o, organizuje_od=datetime.now())
2021-10-08 18:06:02 +02:00
org.save()
2023-06-21 15:51:08 +02:00
uspesne_vytvoreni_orgove += 1
self.message_user(request, f'Úspěšně vytvořeno {uspesne_vytvoreni_orgove} orgů.', level=SUCCESS)
2023-06-21 15:41:17 +02:00
udelej_orgem.short_description = "Udělej z vybraných osob organizátory"
2021-10-08 18:06:02 +02:00
class OsobaInline(admin.TabularInline):
model = m.Osoba
2021-11-22 20:11:50 +01:00
@admin.register(m.Organizator)
class OrganizatorAdmin(ReverseModelAdmin):
search_fields = ['osoba__jmeno', 'osoba__prijmeni', 'osoba__prezdivka']
inline_type = 'stacked'
inline_reverse = ['osoba']
2021-10-08 18:06:02 +02:00
@admin.register(m.Resitel)
class ResitelAdmin(ReverseModelAdmin):
search_fields = ['osoba__jmeno', 'osoba__prijmeni', 'osoba__prezdivka']
ordering = ('osoba__prijmeni', 'osoba__jmeno')
2021-10-08 18:06:02 +02:00
inline_type = 'stacked'
inline_reverse = ['osoba']
admin.site.register(m.Skola)
admin.site.register(m.Prijemce)