add application Preferences
This commit is contained in:
parent
4dca5cea43
commit
53786a7a4a
2 changed files with 35 additions and 5 deletions
|
|
@ -7,13 +7,15 @@ from rest_framework.viewsets import GenericViewSet
|
|||
|
||||
from apps.crud.models import Data, Plan
|
||||
from apps.crud.serializers import DataSerializer
|
||||
from apps.gooyal_oauth2.models import Application
|
||||
from apps.gooyal_oauth2.rest_framework import IsAuthenticatedOrTokenMatchesOASRequirements
|
||||
from apps.gooyal_oauth2.utils import get_application
|
||||
|
||||
|
||||
class DataViewSet(mixins.RetrieveModelMixin,
|
||||
mixins.UpdateModelMixin,
|
||||
mixins.DestroyModelMixin,
|
||||
GenericViewSet):
|
||||
class UserDataViewSet(mixins.RetrieveModelMixin,
|
||||
mixins.UpdateModelMixin,
|
||||
mixins.DestroyModelMixin,
|
||||
GenericViewSet):
|
||||
queryset = Data.objects.all()
|
||||
serializer_class = DataSerializer
|
||||
|
||||
|
|
@ -28,7 +30,7 @@ class DataViewSet(mixins.RetrieveModelMixin,
|
|||
def get_object(self):
|
||||
user = self.request.user
|
||||
auth = self.request.auth
|
||||
if auth:
|
||||
if auth and user:
|
||||
# plan = Plan.objects.filter(application_id=auth.application_id).first()
|
||||
plan, created = Plan.objects.get_or_create(application_id=auth.application_id)
|
||||
# data, created = Data.objects.get_or_create(plan_id=plan.pk, user=user)
|
||||
|
|
@ -36,3 +38,25 @@ class DataViewSet(mixins.RetrieveModelMixin,
|
|||
return data
|
||||
|
||||
raise Http404
|
||||
|
||||
|
||||
class ApplicationDataViewSet(mixins.RetrieveModelMixin,
|
||||
GenericViewSet):
|
||||
queryset = Application.objects.all()
|
||||
serializer_class = DataSerializer
|
||||
|
||||
permission_classes = [IsAuthenticatedOrTokenMatchesOASRequirements]
|
||||
required_alternate_scopes = {
|
||||
"GET": [["data_crud.data:retrieve"]],
|
||||
}
|
||||
|
||||
def get_object(self):
|
||||
application = get_application(self.request)
|
||||
if application:
|
||||
# plan = Plan.objects.filter(application_id=auth.application_id).first()
|
||||
plan, created = Plan.objects.get_or_create(application=application)
|
||||
# data, created = Data.objects.get_or_create(plan_id=plan.pk, user=user)
|
||||
data, created = Data.objects.get_or_create(plan=plan, user=None)
|
||||
return data
|
||||
|
||||
raise Http404
|
||||
|
|
|
|||
6
apps/gooyal_oauth2/utils.py
Normal file
6
apps/gooyal_oauth2/utils.py
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
def get_application(request):
|
||||
try:
|
||||
application = request.auth.application
|
||||
except:
|
||||
application = None
|
||||
return application
|
||||
Loading…
Add table
Reference in a new issue