Commit b9bf2a9e authored by Rafal's avatar Rafal

Now u can add api to user

parent 66d77566
...@@ -2,11 +2,12 @@ ...@@ -2,11 +2,12 @@
from django.http import HttpResponse, Http404 from django.http import HttpResponse, Http404
from rest_framework import viewsets, permissions from rest_framework import viewsets, permissions
import rest_framework import rest_framework
from rest_framework.decorators import detail_route
from rest_framework.response import Response from rest_framework.response import Response
from app.API.models import API, APIType from app.API.models import API, APIType
from app.API.serializers import APISerializer, APITypeSerializer from app.API.serializers import APISerializer, APITypeSerializer
from app.User.models import UserAPI from app.User.models import UserAPI
from django.shortcuts import get_object_or_404
class APIViewSet(viewsets.ModelViewSet): class APIViewSet(viewsets.ModelViewSet):
""" """
...@@ -16,8 +17,19 @@ class APIViewSet(viewsets.ModelViewSet): ...@@ -16,8 +17,19 @@ class APIViewSet(viewsets.ModelViewSet):
serializer_class = APISerializer serializer_class = APISerializer
permission_classes = (permissions.IsAuthenticatedOrReadOnly,) permission_classes = (permissions.IsAuthenticatedOrReadOnly,)
@staticmethod
def add(request, pk=None):
api = get_object_or_404(APIViewSet.queryset, pk=pk)
user = request.user
user_api = UserAPI(user=user, api=api)
user_api.save()
return Response(status=rest_framework.status.HTTP_200_OK)
@staticmethod @staticmethod
def test(request, id, function, slash): def test(request, id, function, slash):
if function == 'add':
APIViewSet.add(request, id)
try: try:
object = API.objects.get(pk=id) object = API.objects.get(pk=id)
except Exception as e: except Exception as e:
...@@ -29,7 +41,6 @@ class APIViewSet(viewsets.ModelViewSet): ...@@ -29,7 +41,6 @@ class APIViewSet(viewsets.ModelViewSet):
return HttpResponse(api.execute(function), content_type="application/json") return HttpResponse(api.execute(function), content_type="application/json")
def get_queryset(self): def get_queryset(self):
queryset = API.objects.all() queryset = API.objects.all()
username = self.request.query_params.get('only_new', None) username = self.request.query_params.get('only_new', None)
if username is not None: if username is not None:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment