<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 viewsets <EOL> from rest_framework import status <EOL> from rest_framework . decorators import detail_route , list_route <EOL> from rest_framework . views import APIView <EOL> from core . models import * <EOL> from django . forms import widgets <EOL> from django . conf . urls import patterns , url <EOL> from services . cord . models import VOLTTenant , VBNGTenant , CordSubscriberRoot <EOL> from api . xosapi_helpers import PlusModelSerializer , XOSViewSet , ReadOnlyField <EOL> from django . shortcuts import get_object_or_404 <EOL> from xos . apibase import XOSListCreateAPIView , XOSRetrieveUpdateDestroyAPIView , XOSPermissionDenied <EOL> from xos . exceptions import * <EOL> import json <EOL> import subprocess <EOL> from django . views . decorators . csrf import ensure_csrf_cookie <EOL> class CordSubscriberNew ( CordSubscriberRoot ) : <EOL> class Meta : <EOL> proxy = True <EOL> app_label = "<STR_LIT>" <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> super ( CordSubscriberNew , self ) . __init__ ( * args , ** kwargs ) <EOL> def __unicode__ ( self ) : <EOL> return u"<STR_LIT>" % str ( self . id ) <EOL> @ property <EOL> def features ( self ) : <EOL> return { "<STR_LIT>" : self . cdn_enable , <EOL> "<STR_LIT>" : self . uplink_speed , <EOL> "<STR_LIT>" : self . downlink_speed , <EOL> "<STR_LIT>" : self . enable_uverse , <EOL> "<STR_LIT:status>" : self . status } <EOL> @ features . setter <EOL> def features ( self , value ) : <EOL> self . cdn_enable = value . get ( "<STR_LIT>" , self . get_default_attribute ( "<STR_LIT>" ) ) <EOL> self . uplink_speed = value . get ( "<STR_LIT>" , self . get_default_attribute ( "<STR_LIT>" ) ) <EOL> self . downlink_speed = value . get ( "<STR_LIT>" , self . get_default_attribute ( "<STR_LIT>" ) ) <EOL> self . enable_uverse = value . get ( "<STR_LIT>" , self . get_default_attribute ( "<STR_LIT>" ) ) <EOL> self . status = value . get ( "<STR_LIT:status>" , self . get_default_attribute ( "<STR_LIT:status>" ) ) <EOL> def update_features ( self , value ) : <EOL> d = self . features <EOL> d . update ( value ) <EOL> self . features = d <EOL> @ property <EOL> def identity ( self ) : <EOL> return { "<STR_LIT>" : self . service_specific_id , <EOL> "<STR_LIT:name>" : self . name } <EOL> @ identity . setter <EOL> def identity ( self , value ) : <EOL> self . service_specific_id = value . get ( "<STR_LIT>" , self . service_specific_id ) <EOL> self . name = value . get ( "<STR_LIT:name>" , self . name ) <EOL> def update_identity ( self , value ) : <EOL> d = self . identity <EOL> d . update ( value ) <EOL> self . identity = d <EOL> @ property <EOL> def related ( self ) : <EOL> related = { } <EOL> if self . volt : <EOL> related [ "<STR_LIT>" ] = self . volt . id <EOL> related [ "<STR_LIT>" ] = self . volt . s_tag <EOL> related [ "<STR_LIT>" ] = self . volt . c_tag <EOL> if self . volt . vcpe : <EOL> related [ "<STR_LIT>" ] = self . volt . vcpe . id <EOL> if self . volt . vcpe . instance : <EOL> related [ "<STR_LIT>" ] = self . volt . vcpe . instance . id <EOL> related [ "<STR_LIT>" ] = self . volt . vcpe . instance . name <EOL> related [ "<STR_LIT>" ] = self . volt . vcpe . wan_container_ip <EOL> if self . volt . vcpe . instance . node : <EOL> related [ "<STR_LIT>" ] = self . volt . vcpe . instance . node . name <EOL> return related <EOL> def save ( self , * args , ** kwargs ) : <EOL> super ( CordSubscriberNew , self ) . save ( * args , ** kwargs ) <EOL> class FeatureSerializer ( serializers . Serializer ) : <EOL> cdn = serializers . BooleanField ( required = False ) <EOL> uplink_speed = serializers . IntegerField ( required = False ) <EOL> downlink_speed = serializers . IntegerField ( required = False ) <EOL> uverse = serializers . BooleanField ( required = False ) <EOL> status = serializers . CharField ( required = False ) <EOL> class IdentitySerializer ( serializers . Serializer ) : <EOL> account_num = serializers . CharField ( required = False ) <EOL> name = serializers . CharField ( required = False ) <EOL> class CordSubscriberSerializer ( PlusModelSerializer ) : <EOL> id = ReadOnlyField ( ) <EOL> humanReadableName = serializers . SerializerMethodField ( "<STR_LIT>" ) <EOL> features = FeatureSerializer ( required = False ) <EOL> identity = IdentitySerializer ( required = False ) <EOL> related = serializers . DictField ( required = False ) <EOL> nested_fields = [ "<STR_LIT>" , "<STR_LIT>" ] <EOL> class Meta : <EOL> model = CordSubscriberNew <EOL> fields = ( '<STR_LIT>' , <EOL> '<STR_LIT:id>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' ) <EOL> def getHumanReadableName ( self , obj ) : <EOL> return obj . __unicode__ ( ) <EOL> class CordSubscriberViewSet ( XOSViewSet ) : <EOL> base_name = "<STR_LIT>" <EOL> method_name = "<STR_LIT>" <EOL> method_kind = "<STR_LIT>" <EOL> queryset = CordSubscriberNew . get_tenant_objects ( ) . select_related ( ) . all ( ) <EOL> serializer_class = CordSubscriberSerializer <EOL> custom_serializers = { "<STR_LIT>" : FeatureSerializer , <EOL> "<STR_LIT>" : FeatureSerializer , <EOL> "<STR_LIT>" : IdentitySerializer , <EOL> "<STR_LIT>" : IdentitySerializer } <EOL> @ classmethod <EOL> def get_urlpatterns ( self , api_path = "<STR_LIT>" ) : <EOL> patterns = super ( CordSubscriberViewSet , self ) . get_urlpatterns ( api_path = api_path ) <EOL> patterns . append ( self . detail_url ( "<STR_LIT>" , { "<STR_LIT>" : "<STR_LIT>" , "<STR_LIT>" : "<STR_LIT>" } , "<STR_LIT>" ) ) <EOL> patterns . append ( self . detail_url ( "<STR_LIT>" , { "<STR_LIT>" : "<STR_LIT>" , "<STR_LIT>" : "<STR_LIT>" } , "<STR_LIT>" ) ) <EOL> patterns . append ( self . detail_url ( "<STR_LIT>" , { "<STR_LIT>" : "<STR_LIT>" , "<STR_LIT>" : "<STR_LIT>" } , "<STR_LIT>" ) ) <EOL> patterns . append ( self . detail_url ( "<STR_LIT>" , { "<STR_LIT>" : "<STR_LIT>" , "<STR_LIT>" : "<STR_LIT>" } , "<STR_LIT>" ) ) <EOL> patterns . append ( url ( self . api_path + "<STR_LIT>" , self . as_view ( { "<STR_LIT>" : "<STR_LIT>" } ) , name = "<STR_LIT>" ) ) <EOL> patterns . append ( url ( self . api_path + "<STR_LIT>" , self . as_view ( { "<STR_LIT>" : "<STR_LIT>" } ) , name = "<STR_LIT>" ) ) <EOL> patterns . append ( url ( self . api_path + "<STR_LIT>" , self . as_view ( { "<STR_LIT>" : "<STR_LIT>" } ) , name = "<STR_LIT>" ) ) <EOL> return patterns <EOL> def list ( self , request ) : <EOL> object_list = self . filter_queryset ( self . get_queryset ( ) ) <EOL> serializer = self . get_serializer ( object_list , many = True ) <EOL> return Response ( serializer . data ) <EOL> def get_features ( self , request , pk = None ) : <EOL> subscriber = self . get_object ( ) <EOL> return Response ( FeatureSerializer ( subscriber . features ) . data ) <EOL> def set_features ( self , request , pk = None ) : <EOL> subscriber = self . get_object ( ) <EOL> ser = FeatureSerializer ( subscriber . features , data = request . data ) <EOL> ser . is_valid ( raise_exception = True ) <EOL> subscriber . update_features ( ser . validated_data ) <EOL> subscriber . save ( ) <EOL> return Response ( FeatureSerializer ( subscriber . features ) . data ) <EOL> def get_feature ( self , request , pk = None , feature = None ) : <EOL> subscriber = self . get_object ( ) <EOL> return Response ( { feature : FeatureSerializer ( subscriber . features ) . data [ feature ] } ) <EOL> def set_feature ( self , request , pk = None , feature = None ) : <EOL> subscriber = self . get_object ( ) <EOL> if [ feature ] != request . data . keys ( ) : <EOL> raise serializers . ValidationError ( "<STR_LIT>" % ( feature , "<STR_LIT:U+002C>" . join ( request . data . keys ( ) ) ) ) <EOL> ser = FeatureSerializer ( subscriber . features , data = request . data ) <EOL> ser . is_valid ( raise_exception = True ) <EOL> subscriber . update_features ( ser . validated_data ) <EOL> subscriber . save ( ) <EOL> return Response ( { feature : FeatureSerializer ( subscriber . features ) . data [ feature ] } ) <EOL> def get_identities ( self , request , pk = None ) : <EOL> subscriber = self . get_object ( ) <EOL> return Response ( IdentitySerializer ( subscriber . identity ) . data ) <EOL> def set_identities ( self , request , pk = None ) : <EOL> subscriber = self . get_object ( ) <EOL> ser = IdentitySerializer ( subscriber . identity , data = request . data ) <EOL> ser . is_valid ( raise_exception = True ) <EOL> subscriber . update_identity ( ser . validated_data ) <EOL> subscriber . save ( ) <EOL> return Response ( IdentitySerializer ( subscriber . identity ) . data ) <EOL> def get_identity ( self , request , pk = None , identity = None ) : <EOL> subscriber = self . get_object ( ) <EOL> return Response ( { identity : IdentitySerializer ( subscriber . identity ) . data [ identity ] } ) <EOL> def set_identity ( self , request , pk = None , identity = None ) : <EOL> subscriber = self . get_object ( ) <EOL> if [ identity ] != request . data . keys ( ) : <EOL> raise serializers . ValidationError ( "<STR_LIT>" % ( identity , "<STR_LIT:U+002C>" . join ( request . data . keys ( ) ) ) ) <EOL> ser = IdentitySerializer ( subscriber . identity , data = request . data ) <EOL> ser . is_valid ( raise_exception = True ) <EOL> subscriber . update_identity ( ser . validated_data ) <EOL> subscriber . save ( ) <EOL> return Response ( { identity : IdentitySerializer ( subscriber . identity ) . data [ identity ] } ) <EOL> def account_num_detail ( self , pk = None , account_num = None ) : <EOL> object_list = CordSubscriberNew . get_tenant_objects ( ) . all ( ) <EOL> object_list = [ x for x in object_list if x . service_specific_id == account_num ] <EOL> if not object_list : <EOL> return Response ( "<STR_LIT>" % account_num , status = status . HTTP_404_NOT_FOUND ) <EOL> return Response ( object_list [ <NUM_LIT:0> ] . id ) <EOL> def ssidlist ( self , request ) : <EOL> object_list = CordSubscriberNew . get_tenant_objects ( ) . all ( ) <EOL> ssidmap = [ { "<STR_LIT>" : x . service_specific_id , "<STR_LIT>" : x . id } for x in object_list ] <EOL> return Response ( { "<STR_LIT>" : ssidmap } ) <EOL> def ssiddetail ( self , pk = None , ssid = None ) : <EOL> object_list = CordSubscriberNew . get_tenant_objects ( ) . all ( ) <EOL> ssidmap = [ { "<STR_LIT>" : x . service_specific_id , "<STR_LIT>" : x . id } for x in object_list if str ( x . service_specific_id ) == str ( ssid ) ] <EOL> if len ( ssidmap ) == <NUM_LIT:0> : <EOL> raise XOSNotFound ( "<STR_LIT>" % str ( ssid ) ) <EOL> return Response ( ssidmap [ <NUM_LIT:0> ] ) </s>
