Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
H
hub
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Package registry
Operate
Terraform modules
Analyze
Contributor analytics
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
thomasDOTwtf
hub
Commits
45a48df6
Commit
45a48df6
authored
Jun 15, 2023
by
HeJ
Browse files
Options
Downloads
Patches
Plain Diff
Voucher: have .doAutoAssignment() return totals
parent
1e8a575c
Branches
Branches containing commit
Tags
Tags containing commit
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/core/models/voucher.py
+16
-6
16 additions, 6 deletions
src/core/models/voucher.py
src/core/tests/vouchers.py
+12
-10
12 additions, 10 deletions
src/core/tests/vouchers.py
with
28 additions
and
16 deletions
src/core/models/voucher.py
+
16
−
6
View file @
45a48df6
...
...
@@ -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
'
)
...
...
This diff is collapsed.
Click to expand it.
src/core/tests/vouchers.py
+
12
−
10
View file @
45a48df6
...
...
@@ -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
)
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment