Andrea Campanella | edfdbca | 2017-02-01 17:33:47 -0800 | [diff] [blame] | 1 | from rest_framework.decorators import api_view |
| 2 | from rest_framework.response import Response |
| 3 | from rest_framework.reverse import reverse |
| 4 | from rest_framework import serializers |
| 5 | from rest_framework import generics |
| 6 | from rest_framework import viewsets |
| 7 | from rest_framework import status |
| 8 | from rest_framework.decorators import detail_route, list_route |
| 9 | from rest_framework.views import APIView |
| 10 | from core.models import * |
| 11 | from django.forms import widgets |
| 12 | from django.conf.urls import patterns, url |
| 13 | from services.veg.models import VEGService |
| 14 | from api.xosapi_helpers import PlusModelSerializer, XOSViewSet, ReadOnlyField |
| 15 | from django.shortcuts import get_object_or_404 |
| 16 | from xos.apibase import XOSListCreateAPIView, XOSRetrieveUpdateDestroyAPIView, XOSPermissionDenied |
| 17 | from xos.exceptions import * |
| 18 | import json |
| 19 | import subprocess |
| 20 | from django.views.decorators.csrf import ensure_csrf_cookie |
| 21 | |
| 22 | class VEGServiceForApi(VEGService): |
| 23 | class Meta: |
| 24 | proxy = True |
| 25 | app_label = "cord" |
| 26 | |
| 27 | def __init__(self, *args, **kwargs): |
| 28 | super(VEGServiceForApi, self).__init__(*args, **kwargs) |
| 29 | |
| 30 | def save(self, *args, **kwargs): |
| 31 | super(VEGServiceForApi, self).save(*args, **kwargs) |
| 32 | |
| 33 | def __init__(self, *args, **kwargs): |
| 34 | super(VEGService, self).__init__(*args, **kwargs) |
| 35 | |
| 36 | class VEGServiceSerializer(PlusModelSerializer): |
| 37 | id = ReadOnlyField() |
| 38 | humanReadableName = serializers.SerializerMethodField("getHumanReadableName") |
| 39 | wan_container_gateway_ip = serializers.CharField(required=False) |
| 40 | wan_container_gateway_mac = serializers.CharField(required=False) |
| 41 | dns_servers = serializers.CharField(required=False) |
| 42 | url_filter_kind = serializers.CharField(required=False) |
| 43 | node_label = serializers.CharField(required=False) |
| 44 | |
| 45 | class Meta: |
| 46 | model = VEGServiceForApi |
| 47 | fields = ('humanReadableName', |
| 48 | 'id', |
| 49 | 'wan_container_gateway_ip', |
| 50 | 'wan_container_gateway_mac', |
| 51 | 'dns_servers', |
| 52 | 'url_filter_kind', |
| 53 | 'node_label') |
| 54 | |
| 55 | def getHumanReadableName(self, obj): |
| 56 | return obj.__unicode__() |
| 57 | |
| 58 | # @ensure_csrf_cookie |
| 59 | class VEGServiceViewSet(XOSViewSet): |
| 60 | base_name = "VEGservice" |
| 61 | method_name = None # use the api endpoint /api/service/veg/ |
| 62 | method_kind = "viewset" |
| 63 | queryset = VEGService.get_service_objects().select_related().all() |
| 64 | serializer_class = VEGServiceSerializer |
| 65 | |
| 66 | @classmethod |
| 67 | def get_urlpatterns(self, api_path="^"): |
| 68 | patterns = super(VEGServiceViewSet, self).get_urlpatterns(api_path=api_path) |
| 69 | |
| 70 | return patterns |
| 71 | |
| 72 | def list(self, request): |
| 73 | object_list = self.filter_queryset(self.get_queryset()) |
| 74 | |
| 75 | serializer = self.get_serializer(object_list, many=True) |
| 76 | |
| 77 | return Response(serializer.data) |
| 78 | |