<s> from rest_framework . decorators import api_view <EOL> from rest_framework . response import Response <EOL> from rest_framework . reverse import reverse <EOL> from rest_framework import serializers <EOL> from rest_framework import generics <EOL> from rest_framework import status <EOL> from core . models import * <EOL> from django . forms import widgets <EOL> from services . cord . models import VOLTTenant , VOLTService , CordSubscriberRoot <EOL> from plus import PlusSerializerMixin <EOL> from xos . apibase import XOSListCreateAPIView , XOSRetrieveUpdateDestroyAPIView , XOSPermissionDenied <EOL> if hasattr ( serializers , "<STR_LIT>" ) : <EOL> ReadOnlyField = serializers . ReadOnlyField <EOL> else : <EOL> ReadOnlyField = serializers . Field <EOL> def get_default_volt_service ( ) : <EOL> volt_services = VOLTService . get_service_objects ( ) . all ( ) <EOL> if volt_services : <EOL> return volt_services [ <NUM_LIT:0> ] . id <EOL> return None <EOL> class VOLTTenantIdSerializer ( serializers . ModelSerializer , PlusSerializerMixin ) : <EOL> id = ReadOnlyField ( ) <EOL> service_specific_id = serializers . CharField ( ) <EOL> s_tag = serializers . CharField ( ) <EOL> c_tag = serializers . CharField ( ) <EOL> provider_service = serializers . PrimaryKeyRelatedField ( queryset = VOLTService . get_service_objects ( ) . all ( ) , default = get_default_volt_service ) <EOL> subscriber_root = serializers . PrimaryKeyRelatedField ( queryset = CordSubscriberRoot . get_tenant_objects ( ) . all ( ) , required = False ) <EOL> humanReadableName = serializers . SerializerMethodField ( "<STR_LIT>" ) <EOL> computeNodeName = serializers . SerializerMethodField ( "<STR_LIT>" ) <EOL> class Meta : <EOL> model = VOLTTenant <EOL> fields = ( '<STR_LIT>' , '<STR_LIT:id>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> def getHumanReadableName ( self , obj ) : <EOL> return obj . __unicode__ ( ) <EOL> def getComputeNodeName ( self , obj ) : <EOL> vcpe = obj . vcpe <EOL> if not vcpe : <EOL> return None <EOL> instance = vcpe . instance <EOL> if not instance : <EOL> return None <EOL> return instance . node . name <EOL> class VOLTTenantList ( XOSListCreateAPIView ) : <EOL> serializer_class = VOLTTenantIdSerializer <EOL> method_kind = "<STR_LIT:list>" <EOL> method_name = "<STR_LIT>" <EOL> def get_queryset ( self ) : <EOL> queryset = VOLTTenant . get_tenant_objects ( ) . select_related ( ) . all ( ) <EOL> service_specific_id = self . request . query_params . get ( '<STR_LIT>' , None ) <EOL> if service_specific_id is not None : <EOL> queryset = queryset . filter ( service_specific_id = service_specific_id ) <EOL> c_tag = self . request . query_params . get ( '<STR_LIT>' , None ) <EOL> if c_tag is not None : <EOL> ids = [ x . id for x in queryset if x . get_attribute ( "<STR_LIT>" , None ) == c_tag ] <EOL> queryset = queryset . filter ( id__in = ids ) <EOL> s_tag = self . request . query_params . get ( '<STR_LIT>' , None ) <EOL> if s_tag is not None : <EOL> ids = [ x . id for x in queryset if x . get_attribute ( "<STR_LIT>" , None ) == s_tag ] <EOL> queryset = queryset . filter ( id__in = ids ) <EOL> return queryset <EOL> def post ( self , request , format = None ) : <EOL> data = request . DATA <EOL> existing_obj = None <EOL> for obj in VOLTTenant . get_tenant_objects ( ) . all ( ) : <EOL> if ( obj . c_tag == data . get ( "<STR_LIT>" , None ) ) and ( obj . s_tag == data . get ( "<STR_LIT>" , None ) ) and ( obj . service_specific_id == data . get ( "<STR_LIT>" , None ) ) : <EOL> existing_obj = obj <EOL> if existing_obj : <EOL> serializer = VOLTTenantIdSerializer ( existing_obj ) <EOL> headers = self . get_success_headers ( serializer . data ) <EOL> return Response ( serializer . data , status = status . HTTP_200_OK ) <EOL> return super ( VOLTTenantList , self ) . post ( request , format ) <EOL> class VOLTTenantDetail ( XOSRetrieveUpdateDestroyAPIView ) : <EOL> serializer_class = VOLTTenantIdSerializer <EOL> queryset = VOLTTenant . get_tenant_objects ( ) . select_related ( ) . all ( ) <EOL> method_kind = "<STR_LIT>" <EOL> method_name = "<STR_LIT>" </s>
