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 status |
| 7 | from core.models import * |
| 8 | from django.forms import widgets |
| 9 | from services.veg.models import VEGTenant, VEGService |
| 10 | from xos.apibase import XOSListCreateAPIView, XOSRetrieveUpdateDestroyAPIView, XOSPermissionDenied |
| 11 | from api.xosapi_helpers import PlusModelSerializer, XOSViewSet, ReadOnlyField |
| 12 | |
| 13 | def get_default_veg_service(): |
| 14 | VEG_services = VEGService.get_service_objects().all() |
| 15 | if VEG_services: |
| 16 | return VEG_services[0].id |
| 17 | return None |
| 18 | |
| 19 | class VEGTenantForAPI(VEGTenant): |
| 20 | class Meta: |
| 21 | proxy = True |
| 22 | app_label = "cord" |
| 23 | |
| 24 | @property |
| 25 | def related(self): |
| 26 | related = {} |
| 27 | if self.instance: |
| 28 | related["instance_id"] = self.instance.id |
| 29 | return related |
| 30 | |
| 31 | class VEGTenantSerializer(PlusModelSerializer): |
| 32 | id = ReadOnlyField() |
| 33 | wan_container_ip = serializers.CharField() |
| 34 | wan_container_mac = ReadOnlyField() |
| 35 | related = serializers.DictField(required=False) |
| 36 | |
| 37 | humanReadableName = serializers.SerializerMethodField("getHumanReadableName") |
| 38 | class Meta: |
| 39 | model = VEGTenantForAPI |
| 40 | fields = ('humanReadableName', 'id', 'wan_container_ip', 'wan_container_mac', 'related' ) |
| 41 | |
| 42 | def getHumanReadableName(self, obj): |
| 43 | return obj.__unicode__() |
| 44 | |
| 45 | class VEGTenantViewSet(XOSViewSet): |
| 46 | base_name = "veg" |
| 47 | method_name = "veg" |
| 48 | method_kind = "viewset" |
| 49 | queryset = VEGTenantForAPI.get_tenant_objects().all() |
| 50 | serializer_class = VEGTenantSerializer |
| 51 | |
| 52 | @classmethod |
| 53 | def get_urlpatterns(self, api_path="^"): |
| 54 | patterns = super(VEGTenantViewSet, self).get_urlpatterns(api_path=api_path) |
| 55 | |
| 56 | return patterns |
| 57 | |
| 58 | |
| 59 | |
| 60 | |
| 61 | |
| 62 | |