Scott Baker | 286ca5d | 2015-04-20 16:37:35 -0700 | [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 |
Scott Baker | 99f3b17 | 2015-06-05 12:44:22 -0700 | [diff] [blame] | 6 | from rest_framework import status |
Scott Baker | 286ca5d | 2015-04-20 16:37:35 -0700 | [diff] [blame] | 7 | from core.models import * |
| 8 | from django.forms import widgets |
Scott Baker | a72b454 | 2016-03-08 11:37:54 -0800 | [diff] [blame] | 9 | from services.cord.models import VOLTTenant, VOLTService, CordSubscriberRoot |
Scott Baker | 286ca5d | 2015-04-20 16:37:35 -0700 | [diff] [blame] | 10 | from plus import PlusSerializerMixin |
| 11 | from xos.apibase import XOSListCreateAPIView, XOSRetrieveUpdateDestroyAPIView, XOSPermissionDenied |
| 12 | |
| 13 | if hasattr(serializers, "ReadOnlyField"): |
| 14 | # rest_framework 3.x |
| 15 | ReadOnlyField = serializers.ReadOnlyField |
| 16 | else: |
| 17 | # rest_framework 2.x |
| 18 | ReadOnlyField = serializers.Field |
| 19 | |
| 20 | def get_default_volt_service(): |
| 21 | volt_services = VOLTService.get_service_objects().all() |
| 22 | if volt_services: |
| 23 | return volt_services[0].id |
| 24 | return None |
| 25 | |
| 26 | class VOLTTenantIdSerializer(serializers.ModelSerializer, PlusSerializerMixin): |
| 27 | id = ReadOnlyField() |
| 28 | service_specific_id = serializers.CharField() |
Scott Baker | f1527cd | 2015-11-09 20:25:21 -0800 | [diff] [blame] | 29 | s_tag = serializers.CharField() |
| 30 | c_tag = serializers.CharField() |
Scott Baker | a2f9ecc | 2015-04-20 16:57:53 -0700 | [diff] [blame] | 31 | provider_service = serializers.PrimaryKeyRelatedField(queryset=VOLTService.get_service_objects().all(), default=get_default_volt_service) |
Scott Baker | a72b454 | 2016-03-08 11:37:54 -0800 | [diff] [blame] | 32 | subscriber_root = serializers.PrimaryKeyRelatedField(queryset=CordSubscriberRoot.get_tenant_objects().all(), required=False) |
Scott Baker | 286ca5d | 2015-04-20 16:37:35 -0700 | [diff] [blame] | 33 | |
| 34 | humanReadableName = serializers.SerializerMethodField("getHumanReadableName") |
| 35 | |
Scott Baker | 12075a7 | 2015-06-01 14:11:48 -0700 | [diff] [blame] | 36 | computeNodeName = serializers.SerializerMethodField("getComputeNodeName") |
| 37 | |
Scott Baker | 286ca5d | 2015-04-20 16:37:35 -0700 | [diff] [blame] | 38 | class Meta: |
| 39 | model = VOLTTenant |
Scott Baker | a72b454 | 2016-03-08 11:37:54 -0800 | [diff] [blame] | 40 | fields = ('humanReadableName', 'id', 'provider_service', 'service_specific_id', 's_tag', 'c_tag', 'computeNodeName', 'subscriber_root' ) |
Scott Baker | 286ca5d | 2015-04-20 16:37:35 -0700 | [diff] [blame] | 41 | |
| 42 | def getHumanReadableName(self, obj): |
| 43 | return obj.__unicode__() |
| 44 | |
Scott Baker | 12075a7 | 2015-06-01 14:11:48 -0700 | [diff] [blame] | 45 | def getComputeNodeName(self, obj): |
| 46 | vcpe = obj.vcpe |
| 47 | if not vcpe: |
| 48 | return None |
Tony Mack | d851547 | 2015-08-19 11:58:18 -0400 | [diff] [blame] | 49 | instance = vcpe.instance |
| 50 | if not instance: |
Scott Baker | 12075a7 | 2015-06-01 14:11:48 -0700 | [diff] [blame] | 51 | return None |
Tony Mack | d851547 | 2015-08-19 11:58:18 -0400 | [diff] [blame] | 52 | return instance.node.name |
Scott Baker | 12075a7 | 2015-06-01 14:11:48 -0700 | [diff] [blame] | 53 | |
Scott Baker | 286ca5d | 2015-04-20 16:37:35 -0700 | [diff] [blame] | 54 | class VOLTTenantList(XOSListCreateAPIView): |
Scott Baker | 286ca5d | 2015-04-20 16:37:35 -0700 | [diff] [blame] | 55 | serializer_class = VOLTTenantIdSerializer |
| 56 | |
| 57 | method_kind = "list" |
| 58 | method_name = "volttenant" |
| 59 | |
Scott Baker | 8d29e6b | 2015-04-20 20:22:50 -0700 | [diff] [blame] | 60 | def get_queryset(self): |
| 61 | queryset = VOLTTenant.get_tenant_objects().select_related().all() |
| 62 | |
Scott Baker | 52c578a | 2016-03-30 09:37:52 -0700 | [diff] [blame] | 63 | service_specific_id = self.request.query_params.get('service_specific_id', None) |
Scott Baker | 8d29e6b | 2015-04-20 20:22:50 -0700 | [diff] [blame] | 64 | if service_specific_id is not None: |
| 65 | queryset = queryset.filter(service_specific_id=service_specific_id) |
| 66 | |
Scott Baker | 52c578a | 2016-03-30 09:37:52 -0700 | [diff] [blame] | 67 | c_tag = self.request.query_params.get('c_tag', None) |
Scott Baker | f1527cd | 2015-11-09 20:25:21 -0800 | [diff] [blame] | 68 | if c_tag is not None: |
| 69 | ids = [x.id for x in queryset if x.get_attribute("c_tag", None)==c_tag] |
| 70 | queryset = queryset.filter(id__in=ids) |
| 71 | |
Scott Baker | 52c578a | 2016-03-30 09:37:52 -0700 | [diff] [blame] | 72 | s_tag = self.request.query_params.get('s_tag', None) |
Scott Baker | f1527cd | 2015-11-09 20:25:21 -0800 | [diff] [blame] | 73 | if s_tag is not None: |
| 74 | ids = [x.id for x in queryset if x.get_attribute("s_tag", None)==s_tag] |
Scott Baker | 8d29e6b | 2015-04-20 20:22:50 -0700 | [diff] [blame] | 75 | queryset = queryset.filter(id__in=ids) |
| 76 | |
| 77 | return queryset |
| 78 | |
Scott Baker | 99f3b17 | 2015-06-05 12:44:22 -0700 | [diff] [blame] | 79 | def post(self, request, format=None): |
| 80 | data = request.DATA |
| 81 | |
| 82 | existing_obj = None |
| 83 | for obj in VOLTTenant.get_tenant_objects().all(): |
Scott Baker | f1527cd | 2015-11-09 20:25:21 -0800 | [diff] [blame] | 84 | if (obj.c_tag == data.get("c_tag", None)) and (obj.s_tag == data.get("s_tag", None)) and (obj.service_specific_id == data.get("service_specific_id",None)): |
Scott Baker | 99f3b17 | 2015-06-05 12:44:22 -0700 | [diff] [blame] | 85 | existing_obj = obj |
| 86 | |
| 87 | if existing_obj: |
| 88 | serializer = VOLTTenantIdSerializer(existing_obj) |
| 89 | headers = self.get_success_headers(serializer.data) |
| 90 | return Response( serializer.data, status=status.HTTP_200_OK ) |
| 91 | |
| 92 | return super(VOLTTenantList, self).post(request, format) |
| 93 | |
Scott Baker | 286ca5d | 2015-04-20 16:37:35 -0700 | [diff] [blame] | 94 | class VOLTTenantDetail(XOSRetrieveUpdateDestroyAPIView): |
Scott Baker | 286ca5d | 2015-04-20 16:37:35 -0700 | [diff] [blame] | 95 | serializer_class = VOLTTenantIdSerializer |
Scott Baker | 8d29e6b | 2015-04-20 20:22:50 -0700 | [diff] [blame] | 96 | queryset = VOLTTenant.get_tenant_objects().select_related().all() |
Scott Baker | 286ca5d | 2015-04-20 16:37:35 -0700 | [diff] [blame] | 97 | |
| 98 | method_kind = "detail" |
| 99 | method_name = "volttenant" |
| 100 | |
| 101 | |
Scott Baker | 8d29e6b | 2015-04-20 20:22:50 -0700 | [diff] [blame] | 102 | |
| 103 | |
| 104 | |