Skip to content
Snippets Groups Projects
Commit 45a48df6 authored by HeJ's avatar HeJ
Browse files

Voucher: have .doAutoAssignment() return totals

parent 1e8a575c
Branches
Tags
No related merge requests found
......@@ -148,24 +148,28 @@ class Voucher(models.Model):
super().save(*args, **kwargs)
@classmethod
def do_auto_assignments(cls, conference: Conference = None, assemblies: List[Assembly] = None):
def do_auto_assignments(cls, conference: Conference = None, assemblies: List[Assembly] = None) -> int:
qs = cls.objects.filter(conference=conference) if conference is not None else cls.objects.all()
qs = qs.filter(enabled=True)
qs = qs.filter(target__in=Voucher.ASSEMBLY_TARGETS)
total = 0
for voucher in qs.iterator():
if conference is None and assemblies is not None:
v_assemblies = [a for a in assemblies if a.conference_id == conference.pk]
else:
v_assemblies = assemblies
voucher.do_auto_assignment(v_assemblies)
total += voucher.do_auto_assignment(v_assemblies)
def do_auto_assignment(self, assemblies: List[Assembly]):
return total
def do_auto_assignment(self, assemblies: List[Assembly]) -> int:
if self.target not in self.ASSEMBLY_TARGETS:
raise NotImplementedError('Auto-Assignment of non assemblies/channels not implemented yet.')
# nothing to do if we're set to manual assignment
if self.assignment == self.Assignment.MANUAL:
return
# nothing to do if we're not enabled or set to manual assignment
if (not self.enabled) or self.assignment == self.Assignment.MANUAL:
return 0
# work on all conference assemblies (and channels) if no list is provided
if assemblies is None:
......@@ -183,6 +187,8 @@ class Voucher(models.Model):
# fetch available entries
available_entries = list(self.entries.filter(assigned=None).all())
# iterate over all missing assignments
total = 0
for assembly in missing_assignments:
# skip non-public assembly/channel if auto-assignment is for public ones only
if self.assignment == self.Assignment.ON_PUBLIC:
......@@ -200,6 +206,10 @@ class Voucher(models.Model):
entry.assign(assembly)
self.logger.info('Auto-assigned voucher %s to assembly <%s> (%s, entry #%s).', self.id, assembly.slug, assembly.id, entry.id)
total += 1
return total
class VoucherEntry(models.Model):
voucher = models.ForeignKey(Voucher, on_delete=models.CASCADE, related_name='entries')
......
......@@ -98,9 +98,9 @@ class VoucherTests(TestCase):
def testManualAssignment(self):
# setup test objects
v = self.conference.vouchers.create(name='test', assignment=Voucher.Assignment.MANUAL, target=Voucher.Target.ASSEMBLY) # type: Voucher
v = self.conference.vouchers.create(name='test', assignment=Voucher.Assignment.MANUAL, target=Voucher.Target.ASSEMBLY, enabled=True) # type: Voucher
entry = v.entries.create(content='abc') # type: VoucherEntry
v2 = self.conference.vouchers.create(name='uv', assignment=Voucher.Assignment.MANUAL, target=Voucher.Target.USER) # type: Voucher
v2 = self.conference.vouchers.create(name='uv', assignment=Voucher.Assignment.MANUAL, target=Voucher.Target.USER, enabled=True) # type: Voucher
entry2 = v2.entries.create(content='abc') # type: VoucherEntry
# try assigning entries to wrong targets
......@@ -123,23 +123,25 @@ class VoucherTests(TestCase):
with self.assertRaises(ValidationError):
entry2.assign(self.user2)
def doAutoAssignment(self, expected_warnings=0, conference=None, assemblies=None):
def doAutoAssignment(self, expected_warnings=0, conference=None, assemblies=None) -> int:
if expected_warnings == 0:
with self.assertNoLogs(level=logging.WARNING):
Voucher.do_auto_assignments(conference=conference, assemblies=assemblies)
total = Voucher.do_auto_assignments(conference=conference, assemblies=assemblies)
else:
with self.assertLogs(level=logging.WARNING) as log:
Voucher.do_auto_assignments(conference=conference, assemblies=assemblies)
total = Voucher.do_auto_assignments(conference=conference, assemblies=assemblies)
self.assertEqual(expected_warnings, len(log.records))
return total
def testAutomaticAssignment(self):
# setup test objects
v = self.conference.vouchers.create(name='test', assignment=Voucher.Assignment.ON_PUBLIC, target=Voucher.Target.ASSEMBLY) # type: Voucher
v = self.conference.vouchers.create(name='test', assignment=Voucher.Assignment.ON_PUBLIC, target=Voucher.Target.ASSEMBLY, enabled=True) # type: Voucher
entry = v.entries.create(content='abc') # type: VoucherEntry
# trigger auto-assignment (would be run by e.g. cron)
self.doAutoAssignment()
self.assertEqual(1, self.doAutoAssignment())
# check that the entry has been assigned
entry.refresh_from_db()
......@@ -149,15 +151,15 @@ class VoucherTests(TestCase):
a2 = self.conference.assemblies.create(slug='second', name='test2', state_assembly=Assembly.State.REGISTERED)
# try auto-assignment again, should have no effect (assignment=ON_PUBLIC, only one public assembly which is already assigned)
self.doAutoAssignment()
self.assertEqual(0, self.doAutoAssignment())
# set assembly public, should trigger a warning as we don't have a free entry available
a2.state_assembly = Assembly.State.ACCEPTED
a2.save()
self.doAutoAssignment(expected_warnings=1)
self.assertEqual(0, self.doAutoAssignment(expected_warnings=1))
# add another entry and we should be fine
entry2 = v.entries.create(content='def') # type: VoucherEntry
self.doAutoAssignment()
self.assertEqual(1, self.doAutoAssignment())
entry2.refresh_from_db()
self.assertEqual(entry2.target, a2)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment