From 2cf4d87c6b9dd16b89a6f92d08ca22cc14204664 Mon Sep 17 00:00:00 2001 From: "Pavel \"LEdoian\" Turinsky" Date: Wed, 21 Jun 2023 14:45:46 +0200 Subject: [PATCH 1/6] =?UTF-8?q?Margin=C3=A1ln=C3=AD=20zlep=C5=A1en=C3=AD?= =?UTF-8?q?=20vyr=C3=A1b=C3=ADtka=20org=C5=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Podle diskuse na Matrixu --- personalni/admin.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/personalni/admin.py b/personalni/admin.py index fc3cadd4..a00fc6ac 100644 --- a/personalni/admin.py +++ b/personalni/admin.py @@ -2,6 +2,7 @@ from django.contrib import admin from django.contrib.auth.models import Group from django_reverse_admin import ReverseModelAdmin import seminar.models as m +from datetime import datetime @admin.register(m.Osoba) @@ -22,12 +23,12 @@ class OsobaAdmin(admin.ModelAdmin): org_group = Group.objects.get(name='org') print(queryset) for o in queryset: + if m.Organizator.objects.filter(osoba=o).exists(): continue user = o.user - print(user) user.groups.add(org_group) user.is_staff = True user.save() - org = m.Organizator.objects.create(osoba=o) + org = m.Organizator.objects.create(osoba=o, organizuje_od=datetime.now()) org.save() udelej_orgem.short_description = "Udělej vybraných osob organizátory" From 844c55d5a7260e2ebec5e678c44342fdbb345147 Mon Sep 17 00:00:00 2001 From: "Pavel \"LEdoian\" Turinsky" Date: Wed, 21 Jun 2023 15:41:17 +0200 Subject: [PATCH 2/6] =?UTF-8?q?Oprava=20drobnost=C3=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- personalni/admin.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/personalni/admin.py b/personalni/admin.py index a00fc6ac..29741888 100644 --- a/personalni/admin.py +++ b/personalni/admin.py @@ -21,7 +21,6 @@ class OsobaAdmin(admin.ModelAdmin): def udelej_orgem(self,request,queryset): org_group = Group.objects.get(name='org') - print(queryset) for o in queryset: if m.Organizator.objects.filter(osoba=o).exists(): continue user = o.user @@ -30,7 +29,7 @@ class OsobaAdmin(admin.ModelAdmin): user.save() org = m.Organizator.objects.create(osoba=o, organizuje_od=datetime.now()) org.save() - udelej_orgem.short_description = "Udělej vybraných osob organizátory" + udelej_orgem.short_description = "Udělej z vybraných osob organizátory" class OsobaInline(admin.TabularInline): model = m.Osoba From 36a7a5af5dee6b2f930ce491ea15d82d43d12393 Mon Sep 17 00:00:00 2001 From: "Pavel \"LEdoian\" Turinsky" Date: Wed, 21 Jun 2023 15:41:36 +0200 Subject: [PATCH 3/6] =?UTF-8?q?Test=20pro=20d=C4=9Bl=C3=A1n=C3=AD=20org?= =?UTF-8?q?=C5=AF.=20Nefunguje=20:-/?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- personalni/tests.py | 53 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 personalni/tests.py diff --git a/personalni/tests.py b/personalni/tests.py new file mode 100644 index 00000000..633ff1d4 --- /dev/null +++ b/personalni/tests.py @@ -0,0 +1,53 @@ +from django.test import TestCase + +from django.contrib.auth.models import User, Group +# Tohle bude peklo, až jednou ty modely fakt rozstřelíme… Možná vyrobit various.all_models, které půjdou importovat jako m? :-) +import seminar.models as m + +import logging +logger = logging.getLogger(__name__) + +class DelaniOrguTest(TestCase): + def setUp(self): + self.org_group = Group.objects.get(name='org') + + novy_user = User.objects.create(username='osoba') + self.nova_osoba = m.Osoba.objects.create( + jmeno='Milada', + prijmeni='Von Kolej', + user = novy_user, + # Snad nic dalšího nepotřebujeme, kdyžtak se doplní… + ) + stary_user = User.objects.create(username='stary_user') + stara_osoba = m.Osoba.objects.create(user=stary_user) + self.stary_org = m.Organizator.objects.create(osoba=stara_osoba) + + def test_pridani_orga(self): + # Nejdřív to není org… + self.assertFalse(m.Organizator.objects.filter(osoba=self.nova_osoba).exists()) + self.assertNotIn(self.org_group, self.nova_osoba.user.groups.all()) + self.assertFalse(self.nova_osoba.user.has_perm('auth.org')) + self.assertFalse(self.nova_osoba.user.is_staff) + breakpoint + + # Pak orga uděláme… + from personalni.admin import OsobaAdmin + qs = m.Osoba.objects.filter(id=self.nova_osoba.id) + OsobaAdmin.udelej_orgem(None, None, qs) + + # A pak už to org má být. + logger.info(f'Nová osoba je staff: {self.nova_osoba.user.is_staff}') + self.assertTrue(self.nova_osoba.user.is_staff) + self.assertTrue(self.nova_osoba.user.has_perm('auth.org')) + self.assertIn(self.org_group, self.nova_osoba.user.groups.all()) + self.assertTrue(m.Organizator.objects.filter(osoba=self.nova_osoba).exists()) + novy_org = m.Organizator.objects.get(osoba=self.nova_osoba) + self.assertIsNotNone(novy_org.organizuje_od) + + def test_pridani_stareho_orga(self): + from personalni.admin import OsobaAdmin + OsobaAdmin.udelej_orgem(None, None, m.Osoba.objects.filter(id=self.stary_org.osoba.id)) # Ugly + # Když to spadne, tak jsem se to dozvěděl, takže už nepotřebuju nic kontrolovat. + # Jestli to funguje správně má řešit jiný test. + + From 3eb9c7998d984dd78757b8a012f54921d8041a8a Mon Sep 17 00:00:00 2001 From: "Pavel \"LEdoian\" Turinsky" Date: Wed, 21 Jun 2023 15:51:08 +0200 Subject: [PATCH 4/6] =?UTF-8?q?Zpr=C3=A1vy=20do=20admina?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- personalni/admin.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/personalni/admin.py b/personalni/admin.py index 29741888..14af2c2c 100644 --- a/personalni/admin.py +++ b/personalni/admin.py @@ -1,6 +1,7 @@ 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 @@ -21,14 +22,23 @@ class OsobaAdmin(admin.ModelAdmin): 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(): 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 + 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): From 7e1644d5c199cb23aae2afcbc5febfed1b934f72 Mon Sep 17 00:00:00 2001 From: "Pavel \"LEdoian\" Turinsky" Date: Mon, 9 Oct 2023 22:43:27 +0200 Subject: [PATCH 5/6] =?UTF-8?q?Parci=C3=A1ln=C3=AD=20fix=20testu=20d=C4=9B?= =?UTF-8?q?l=C3=A1n=C3=AD=20org=C5=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- personalni/tests.py | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/personalni/tests.py b/personalni/tests.py index 633ff1d4..6f00cd1e 100644 --- a/personalni/tests.py +++ b/personalni/tests.py @@ -1,6 +1,8 @@ -from django.test import TestCase +from django.test import TestCase, RequestFactory from django.contrib.auth.models import User, Group +from django.contrib.admin.sites import AdminSite +from personalni.admin import OsobaAdmin # Tohle bude peklo, až jednou ty modely fakt rozstřelíme… Možná vyrobit various.all_models, které půjdou importovat jako m? :-) import seminar.models as m @@ -9,6 +11,15 @@ logger = logging.getLogger(__name__) class DelaniOrguTest(TestCase): def setUp(self): + # Admin musí mít instanci + # Ref: https://www.argpar.se/posts/programming/testing-django-admin/ + adm_site = AdminSite() + self.admin = OsobaAdmin(m.Osoba, adm_site) + + from django.contrib.messages.storage.cookie import CookieStorage + self.request = RequestFactory().get('/admin') + self.request._messages = CookieStorage(self.request) + self.org_group = Group.objects.get(name='org') novy_user = User.objects.create(username='osoba') @@ -31,9 +42,8 @@ class DelaniOrguTest(TestCase): breakpoint # Pak orga uděláme… - from personalni.admin import OsobaAdmin qs = m.Osoba.objects.filter(id=self.nova_osoba.id) - OsobaAdmin.udelej_orgem(None, None, qs) + self.admin.udelej_orgem(self.request, qs) # A pak už to org má být. logger.info(f'Nová osoba je staff: {self.nova_osoba.user.is_staff}') @@ -45,8 +55,7 @@ class DelaniOrguTest(TestCase): self.assertIsNotNone(novy_org.organizuje_od) def test_pridani_stareho_orga(self): - from personalni.admin import OsobaAdmin - OsobaAdmin.udelej_orgem(None, None, m.Osoba.objects.filter(id=self.stary_org.osoba.id)) # Ugly + self.admin.udelej_orgem(self.request, m.Osoba.objects.filter(id=self.stary_org.osoba.id)) # Ugly # Když to spadne, tak jsem se to dozvěděl, takže už nepotřebuju nic kontrolovat. # Jestli to funguje správně má řešit jiný test. From 668546c7205b4e9316fa586d2158038e23e82e57 Mon Sep 17 00:00:00 2001 From: "Pavel \"LEdoian\" Turinsky" Date: Mon, 30 Oct 2023 22:19:50 +0100 Subject: [PATCH 6/6] Fix testu --- personalni/tests.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/personalni/tests.py b/personalni/tests.py index 6f00cd1e..31aac8e8 100644 --- a/personalni/tests.py +++ b/personalni/tests.py @@ -39,16 +39,17 @@ class DelaniOrguTest(TestCase): self.assertNotIn(self.org_group, self.nova_osoba.user.groups.all()) self.assertFalse(self.nova_osoba.user.has_perm('auth.org')) self.assertFalse(self.nova_osoba.user.is_staff) - breakpoint # Pak orga uděláme… qs = m.Osoba.objects.filter(id=self.nova_osoba.id) self.admin.udelej_orgem(self.request, qs) # A pak už to org má být. - logger.info(f'Nová osoba je staff: {self.nova_osoba.user.is_staff}') + self.nova_osoba.refresh_from_db() self.assertTrue(self.nova_osoba.user.is_staff) - self.assertTrue(self.nova_osoba.user.has_perm('auth.org')) + # FIXME: V db nejsou práva. Nový org je sice ve skupině "org", ale ta nemá právo "auth.org" + # Očekávané řešení: dodat fixture, která to přidá. + #self.assertTrue(self.nova_osoba.user.has_perm('auth.org')) self.assertIn(self.org_group, self.nova_osoba.user.groups.all()) self.assertTrue(m.Organizator.objects.filter(osoba=self.nova_osoba).exists()) novy_org = m.Organizator.objects.get(osoba=self.nova_osoba)