forked from geosolutions-it/django-mapstore-adapter
-
Notifications
You must be signed in to change notification settings - Fork 2
/
views.py
66 lines (53 loc) · 2.45 KB
/
views.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# -*- coding: utf-8 -*-
#########################################################################
#
# Copyright 2018, GeoSolutions Sas.
# All rights reserved.
#
# This source code is licensed under the BSD-style license found in the
# LICENSE file in the root directory of this source tree.
#
#########################################################################
from django.contrib.auth import get_user_model
from rest_framework import viewsets
from rest_framework.authentication import SessionAuthentication, BasicAuthentication
from rest_framework.permissions import IsAdminUser, IsAuthenticated, IsAuthenticatedOrReadOnly # noqa
from oauth2_provider.contrib.rest_framework import OAuth2Authentication
from geonode.base.api.permissions import IsOwnerOrReadOnly
from .models import MapStoreResource
from .serializers import (UserSerializer,
MapStoreResourceSerializer,)
from ..hooks import hookset
import logging
logger = logging.getLogger(__name__)
class UserViewSet(viewsets.ModelViewSet):
"""
API endpoint that allows users to be viewed or edited.
"""
authentication_classes = (SessionAuthentication, BasicAuthentication, OAuth2Authentication)
permission_classes = (IsAdminUser,)
queryset = get_user_model().objects.all()
serializer_class = UserSerializer
class MapStoreResourceViewSet(viewsets.ModelViewSet):
""" Only Authenticate User perform CRUD Operations on Respective Data
"""
authentication_classes = [SessionAuthentication, BasicAuthentication, OAuth2Authentication]
permission_classes = [IsAuthenticatedOrReadOnly, IsOwnerOrReadOnly]
model = MapStoreResource
serializer_class = MapStoreResourceSerializer
def get_queryset(self):
""" Return datasets belonging to the current user """
queryset = self.model.objects.all()
# filter to tasks owned by user making request
queryset = hookset.get_queryset(self, queryset)
return queryset
def perform_create(self, serializer):
""" Associate current user as task owner """
if serializer.is_valid():
hookset.perform_create(self, serializer)
return serializer.save(user=self.request.user)
def perform_update(self, serializer):
""" Associate current user as task owner """
if serializer.is_valid():
hookset.perform_update(self, serializer)
return serializer.save()