Vyrábění orgů z řešitelů
This commit is contained in:
parent
8dbb92d3a4
commit
e431e23ad4
1 changed files with 28 additions and 22 deletions
|
@ -5,10 +5,31 @@ from django.contrib.messages import WARNING, ERROR, SUCCESS
|
|||
import seminar.models as m
|
||||
from datetime import datetime
|
||||
|
||||
# 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']
|
||||
actions = ['synchronizuj_maily', udelej_orgem]
|
||||
search_fields = ['jmeno', 'prijmeni', 'prezdivka']
|
||||
|
||||
def synchronizuj_maily(self, request, queryset):
|
||||
|
@ -20,27 +41,6 @@ class OsobaAdmin(admin.ModelAdmin):
|
|||
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')
|
||||
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
|
||||
self.message_user(request, f"Osoba {o} už je org, přeskakuji.", level=WARNING)
|
||||
continue
|
||||
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.is_staff = True
|
||||
user.save()
|
||||
org = m.Organizator.objects.create(osoba=o, organizuje_od=datetime.now())
|
||||
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"
|
||||
|
||||
class OsobaInline(admin.TabularInline):
|
||||
model = m.Osoba
|
||||
|
||||
|
@ -58,5 +58,11 @@ class ResitelAdmin(ReverseModelAdmin):
|
|||
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)
|
||||
|
|
Loading…
Reference in a new issue