Skip to content
GitLab
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
2c203cca
Commit
2c203cca
authored
Jan 17, 2018
by
Luko van der Maas
Browse files
added an api for setting the categories and listing them in the devices path
parent
36790af2
Changes
4
Hide whitespace changes
Inline
Side-by-side
website/pushnotifications/api/permissions.py
View file @
2c203cca
from
django.db.models
import
QuerySet
from
rest_framework
import
permissions
class
IsOwner
(
permissions
.
BasePermission
):
def
has_object_permission
(
self
,
request
,
view
,
obj
):
if
isinstance
(
obj
,
QuerySet
):
return
True
# must be the owner to view the object
return
obj
.
user
==
request
.
user
website/pushnotifications/api/serializers.py
View file @
2c203cca
...
...
@@ -2,14 +2,28 @@ from __future__ import absolute_import
from
rest_framework.serializers
import
ModelSerializer
from
pushnotifications.models
import
Device
from
pushnotifications.models
import
Device
,
Category
class
DeviceSerializer
(
ModelSerializer
):
class
Meta
:
model
=
Device
fields
=
(
'pk'
,
'registration_id'
,
'active'
,
'date_created'
,
'type'
)
fields
=
(
'pk'
,
'registration_id'
,
'active'
,
'date_created'
,
'type'
,
'receive_category'
)
read_only_fields
=
(
'date_created'
,)
extra_kwargs
=
{
'active'
:
{
'default'
:
True
}}
class
CategorySerializer
(
ModelSerializer
):
class
Meta
:
model
=
Category
fields
=
(
'key'
,
'name'
)
website/pushnotifications/api/viewsets.py
View file @
2c203cca
from
rest_framework
import
permissions
from
rest_framework.decorators
import
list_route
from
rest_framework.response
import
Response
from
rest_framework.viewsets
import
ModelViewSet
from
pushnotifications.api.permissions
import
IsOwner
from
pushnotifications.api.serializers
import
DeviceSerializer
from
pushnotifications.models
import
Device
from
pushnotifications.api.serializers
import
DeviceSerializer
,
\
CategorySerializer
from
pushnotifications.models
import
Device
,
Category
class
DeviceViewSet
(
ModelViewSet
):
...
...
@@ -27,3 +30,9 @@ class DeviceViewSet(ModelViewSet):
def
perform_update
(
self
,
serializer
):
serializer
.
save
(
user
=
self
.
request
.
user
)
@
list_route
()
def
categories
(
self
,
request
):
categories
=
Category
.
objects
.
all
()
serializer
=
CategorySerializer
(
categories
,
many
=
True
)
return
Response
(
serializer
.
data
)
website/pushnotifications/models.py
View file @
2c203cca
...
...
@@ -70,6 +70,7 @@ class Message(models.Model):
)
category
=
models
.
ForeignKey
(
Category
,
on_delete
=
models
.
CASCADE
,
verbose_name
=
_
(
'category'
),
default
=
"general"
)
...
...
Write
Preview
Supports
Markdown
0%
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!
Cancel
Please
register
or
sign in
to comment