Zprávy do admina
This commit is contained in:
parent
36a7a5af5d
commit
3eb9c7998d
1 changed files with 11 additions and 1 deletions
|
@ -1,6 +1,7 @@
|
||||||
from django.contrib import admin
|
from django.contrib import admin
|
||||||
from django.contrib.auth.models import Group
|
from django.contrib.auth.models import Group
|
||||||
from django_reverse_admin import ReverseModelAdmin
|
from django_reverse_admin import ReverseModelAdmin
|
||||||
|
from django.contrib.messages import WARNING, ERROR, SUCCESS
|
||||||
import seminar.models as m
|
import seminar.models as m
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
|
||||||
|
@ -21,14 +22,23 @@ class OsobaAdmin(admin.ModelAdmin):
|
||||||
|
|
||||||
def udelej_orgem(self,request,queryset):
|
def udelej_orgem(self,request,queryset):
|
||||||
org_group = Group.objects.get(name='org')
|
org_group = Group.objects.get(name='org')
|
||||||
|
uspesne_vytvoreni_orgove = 0
|
||||||
for o in queryset:
|
for o in queryset:
|
||||||
if m.Organizator.objects.filter(osoba=o).exists(): continue
|
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
|
||||||
user = o.user
|
user = o.user
|
||||||
|
if user is None:
|
||||||
|
self.message_user(request, f"Osoba {o} nemá uživatele! Přeskakuji.", level=ERROR)
|
||||||
|
continue
|
||||||
user.groups.add(org_group)
|
user.groups.add(org_group)
|
||||||
user.is_staff = True
|
user.is_staff = True
|
||||||
user.save()
|
user.save()
|
||||||
org = m.Organizator.objects.create(osoba=o, organizuje_od=datetime.now())
|
org = m.Organizator.objects.create(osoba=o, organizuje_od=datetime.now())
|
||||||
org.save()
|
org.save()
|
||||||
|
uspesne_vytvoreni_orgove += 1
|
||||||
|
self.message_user(request, f'Úspěšně vytvořeno {uspesne_vytvoreni_orgove} orgů.', level=SUCCESS)
|
||||||
udelej_orgem.short_description = "Udělej z vybraných osob organizátory"
|
udelej_orgem.short_description = "Udělej z vybraných osob organizátory"
|
||||||
|
|
||||||
class OsobaInline(admin.TabularInline):
|
class OsobaInline(admin.TabularInline):
|
||||||
|
|
Loading…
Reference in a new issue