<s> import logging <EOL> from corehq . apps . api . serializers import XFormInstanceSerializer <EOL> from corehq . apps . domain . models import Domain <EOL> from corehq . apps . accounting . models import Subscription <EOL> from corehq . apps . api . resources import HqBaseResource , CouchResourceMixin <EOL> from corehq . apps . api . resources . v0_1 import ( <EOL> CustomResourceMeta , <EOL> AdminAuthentication , <EOL> ) <EOL> from corehq . apps . es . domains import DomainES <EOL> from tastypie import fields <EOL> from tastypie . exceptions import NotFound <EOL> from tastypie . resources import Resource <EOL> from dimagi . utils . dates import force_to_datetime <EOL> def _get_domain ( bundle ) : <EOL> return bundle . obj <EOL> class DomainQuerySetAdapter ( object ) : <EOL> def __init__ ( self , es_query ) : <EOL> self . es_query = es_query <EOL> def count ( self ) : <EOL> return self . es_query . size ( <NUM_LIT:0> ) . run ( ) . total <EOL> def __getitem__ ( self , item ) : <EOL> if isinstance ( item , slice ) : <EOL> return map ( Domain . wrap , self . es_query . start ( item . start ) . size ( item . stop - item . start ) . run ( ) . hits ) <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> class DomainMetadataResource ( CouchResourceMixin , HqBaseResource ) : <EOL> billing_properties = fields . DictField ( ) <EOL> calculated_properties = fields . DictField ( ) <EOL> domain_properties = fields . DictField ( ) <EOL> def dispatch ( self , request_type , request , ** kwargs ) : <EOL> return Resource . dispatch ( self , request_type , request , ** kwargs ) <EOL> def dehydrate_billing_properties ( self , bundle ) : <EOL> domain = _get_domain ( bundle ) <EOL> plan_version , subscription = ( <EOL> Subscription . get_subscribed_plan_by_domain ( domain ) <EOL> ) <EOL> return { <EOL> "<STR_LIT>" : ( subscription . date_start <EOL> if subscription is not None else None ) , <EOL> "<STR_LIT>" : ( subscription . date_end <EOL> if subscription is not None else None ) , <EOL> "<STR_LIT>" : plan_version , <EOL> } <EOL> def dehydrate_calculated_properties ( self , bundle ) : <EOL> domain = _get_domain ( bundle ) <EOL> try : <EOL> es_data = ( DomainES ( ) <EOL> . in_domains ( [ domain . name ] ) <EOL> . size ( <NUM_LIT:1> ) <EOL> . run ( ) <EOL> . hits [ <NUM_LIT:0> ] ) <EOL> return { <EOL> prop_name : es_data [ prop_name ] <EOL> for prop_name in es_data if prop_name [ : <NUM_LIT:3> ] == '<STR_LIT>' <EOL> } <EOL> except IndexError : <EOL> logging . exception ( '<STR_LIT>' . format ( domain . name ) ) <EOL> return { } <EOL> def dehydrate_domain_properties ( self , bundle ) : <EOL> return _get_domain ( bundle ) . _doc <EOL> def obj_get ( self , bundle , ** kwargs ) : <EOL> domain = Domain . get_by_name ( kwargs . get ( '<STR_LIT>' ) ) <EOL> if domain is None : <EOL> raise NotFound <EOL> return domain <EOL> def obj_get_list ( self , bundle , ** kwargs ) : <EOL> if kwargs . get ( '<STR_LIT>' ) : <EOL> return [ self . obj_get ( bundle , ** kwargs ) ] <EOL> else : <EOL> filters = { } <EOL> if hasattr ( bundle . request , '<STR_LIT:GET>' ) : <EOL> filters = bundle . request . GET <EOL> params = { } <EOL> if '<STR_LIT>' in filters : <EOL> params [ '<STR_LIT>' ] = force_to_datetime ( filters [ '<STR_LIT>' ] ) <EOL> if '<STR_LIT>' in filters : <EOL> params [ '<STR_LIT>' ] = force_to_datetime ( filters [ '<STR_LIT>' ] ) <EOL> return DomainQuerySetAdapter ( DomainES ( ) . last_modified ( ** params ) . sort ( '<STR_LIT>' ) ) <EOL> class Meta ( CustomResourceMeta ) : <EOL> authentication = AdminAuthentication ( ) <EOL> list_allowed_methods = [ '<STR_LIT>' ] <EOL> detail_allowed_methods = [ '<STR_LIT>' ] <EOL> object_class = Domain <EOL> resource_name = '<STR_LIT>' <EOL> serializer = XFormInstanceSerializer ( formats = [ '<STR_LIT>' ] ) </s>
