Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
thalia
concrexit
Commits
6ace705e
Verified
Commit
6ace705e
authored
Mar 14, 2018
by
Sébastiaan Versteeg
Browse files
Add failsafes, if no receive_category values are set in the API: enable them all
parent
a1b2460d
Changes
3
Hide whitespace changes
Inline
Side-by-side
website/pushnotifications/api/serializers.py
View file @
6ace705e
from
__future__
import
absolute_import
from
rest_framework.relations
import
ManyRelatedField
,
PrimaryKeyRelatedField
from
rest_framework.serializers
import
ModelSerializer
from
pushnotifications.models
import
Device
,
Category
class
DeviceSerializer
(
ModelSerializer
):
receive_category
=
ManyRelatedField
(
allow_empty
=
True
,
child_relation
=
PrimaryKeyRelatedField
(
allow_empty
=
True
,
queryset
=
Category
.
objects
.
all
(),
required
=
False
)
)
class
Meta
:
model
=
Device
...
...
website/pushnotifications/api/viewsets.py
View file @
6ace705e
...
...
@@ -29,7 +29,12 @@ class DeviceViewSet(ModelViewSet):
)
except
Device
.
DoesNotExist
:
pass
serializer
.
save
(
user
=
self
.
request
.
user
,
language
=
language
)
if
len
(
serializer
.
validated_data
[
'receive_category'
])
>
0
:
serializer
.
save
(
user
=
self
.
request
.
user
,
language
=
language
)
else
:
categories
=
[
c
.
pk
for
c
in
Category
.
objects
.
all
()]
serializer
.
save
(
user
=
self
.
request
.
user
,
language
=
language
,
receive_category
=
categories
)
def
perform_update
(
self
,
serializer
):
serializer
.
save
(
user
=
self
.
request
.
user
)
...
...
website/pushnotifications/migrations/0005_populate_category.py
View file @
6ace705e
...
...
@@ -17,6 +17,11 @@ def forwards_func(apps, schema_editor):
Category
(
key
=
"board"
,
name_en
=
"board"
,
name_nl
=
"bestuur"
),
])
Device
=
apps
.
get_model
(
"pushnotifications"
,
"Device"
)
for
device
in
Device
.
objects
.
using
(
db_alias
).
all
():
for
category
in
Category
.
objects
.
using
(
db_alias
).
all
():
device
.
receive_category
.
add
(
category
)
def
reverse_func
(
apps
,
schema_editor
):
Category
=
apps
.
get_model
(
"pushnotifications"
,
"Category"
)
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment