<s> from tastypie import http , fields <EOL> from tastypie . exceptions import ImmediateHttpResponse <EOL> from tastypie . resources import ModelResource , ALL , ALL_WITH_RELATIONS <EOL> from . . core . api import ( ProductVersionResource , ProductResource , <EOL> UserResource ) <EOL> from . models import CaseVersion , Case , Suite , CaseStep , SuiteCase <EOL> from ... model . core . models import ProductVersion <EOL> from . . mtapi import MTResource , MTAuthorization <EOL> from . . environments . api import EnvironmentResource <EOL> from . . tags . api import TagResource <EOL> import logging <EOL> logger = logging . getLogger ( __name__ ) <EOL> class SuiteCaseAuthorization ( MTAuthorization ) : <EOL> """<STR_LIT>""" <EOL> @ property <EOL> def permission ( self ) : <EOL> """<STR_LIT>""" <EOL> return "<STR_LIT>" <EOL> class CaseVersionAuthorization ( MTAuthorization ) : <EOL> """<STR_LIT>""" <EOL> @ property <EOL> def permission ( self ) : <EOL> """<STR_LIT>""" <EOL> return "<STR_LIT>" <EOL> class SuiteResource ( MTResource ) : <EOL> """<STR_LIT>""" <EOL> product = fields . ToOneField ( ProductResource , "<STR_LIT>" ) <EOL> created_by = fields . ForeignKey ( <EOL> UserResource , <EOL> "<STR_LIT>" , <EOL> full = True , <EOL> null = True , <EOL> ) <EOL> modified_by = fields . ForeignKey ( <EOL> UserResource , <EOL> "<STR_LIT>" , <EOL> full = True , <EOL> null = True , <EOL> ) <EOL> class Meta ( MTResource . Meta ) : <EOL> queryset = Suite . objects . all ( ) <EOL> fields = [ "<STR_LIT:name>" , "<STR_LIT>" , "<STR_LIT:description>" , "<STR_LIT:status>" , "<STR_LIT:id>" , "<STR_LIT>" ] <EOL> filtering = { <EOL> "<STR_LIT:name>" : ALL , <EOL> "<STR_LIT>" : ALL_WITH_RELATIONS , <EOL> "<STR_LIT:status>" : ALL , <EOL> "<STR_LIT>" : ALL_WITH_RELATIONS , <EOL> "<STR_LIT>" : ALL_WITH_RELATIONS , <EOL> } <EOL> ordering = [ '<STR_LIT:name>' , '<STR_LIT>' , '<STR_LIT:id>' , '<STR_LIT>' ] <EOL> @ property <EOL> def model ( self ) : <EOL> """<STR_LIT>""" <EOL> return Suite <EOL> @ property <EOL> def read_create_fields ( self ) : <EOL> """<STR_LIT>""" <EOL> return [ "<STR_LIT>" ] <EOL> class CaseResource ( MTResource ) : <EOL> """<STR_LIT>""" <EOL> suites = fields . ToManyField ( <EOL> SuiteResource , <EOL> "<STR_LIT>" , <EOL> readonly = True , <EOL> null = True , <EOL> ) <EOL> product = fields . ForeignKey ( ProductResource , "<STR_LIT>" ) <EOL> class Meta ( MTResource . Meta ) : <EOL> queryset = Case . objects . all ( ) <EOL> fields = [ "<STR_LIT:id>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] <EOL> filtering = { <EOL> "<STR_LIT>" : ALL_WITH_RELATIONS , <EOL> "<STR_LIT>" : ALL_WITH_RELATIONS , <EOL> } <EOL> @ property <EOL> def model ( self ) : <EOL> """<STR_LIT>""" <EOL> return Case <EOL> @ property <EOL> def read_create_fields ( self ) : <EOL> """<STR_LIT>""" <EOL> return [ "<STR_LIT>" ] <EOL> class CaseStepResource ( MTResource ) : <EOL> """<STR_LIT>""" <EOL> caseversion = fields . ForeignKey ( <EOL> "<STR_LIT>" , "<STR_LIT>" ) <EOL> class Meta ( MTResource . Meta ) : <EOL> queryset = CaseStep . objects . all ( ) <EOL> fields = [ "<STR_LIT:id>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] <EOL> filtering = { <EOL> "<STR_LIT>" : ALL_WITH_RELATIONS , <EOL> "<STR_LIT>" : ALL , <EOL> "<STR_LIT>" : ALL , <EOL> } <EOL> ordering = [ "<STR_LIT>" , "<STR_LIT:id>" ] <EOL> authorization = CaseVersionAuthorization ( ) <EOL> @ property <EOL> def model ( self ) : <EOL> """<STR_LIT>""" <EOL> return CaseStep <EOL> @ property <EOL> def read_create_fields ( self ) : <EOL> """<STR_LIT>""" <EOL> return [ "<STR_LIT>" ] <EOL> class SuiteCaseResource ( MTResource ) : <EOL> """<STR_LIT>""" <EOL> case = fields . ForeignKey ( CaseResource , '<STR_LIT>' ) <EOL> suite = fields . ForeignKey ( SuiteResource , '<STR_LIT>' ) <EOL> class Meta ( MTResource . Meta ) : <EOL> queryset = SuiteCase . objects . all ( ) <EOL> fields = [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT:id>" ] <EOL> filtering = { <EOL> "<STR_LIT>" : ALL_WITH_RELATIONS , <EOL> "<STR_LIT>" : ALL_WITH_RELATIONS <EOL> } <EOL> authorization = SuiteCaseAuthorization ( ) <EOL> @ property <EOL> def model ( self ) : <EOL> return SuiteCase <EOL> @ property <EOL> def read_create_fields ( self ) : <EOL> """<STR_LIT>""" <EOL> return [ "<STR_LIT>" , "<STR_LIT>" ] <EOL> def hydrate_case ( self , bundle ) : <EOL> """<STR_LIT>""" <EOL> if bundle . request . META [ '<STR_LIT>' ] == '<STR_LIT:POST>' : <EOL> case_id = self . _id_from_uri ( bundle . data [ '<STR_LIT>' ] ) <EOL> case = Case . objects . get ( id = case_id ) <EOL> suite_id = self . _id_from_uri ( bundle . data [ '<STR_LIT>' ] ) <EOL> suite = Suite . objects . get ( id = suite_id ) <EOL> if case . product . id != suite . product . id : <EOL> error_message = str ( <EOL> "<STR_LIT>" <EOL> ) <EOL> logger . error ( <EOL> "<STR_LIT:\n>" . join ( [ error_message , "<STR_LIT>" ] ) , <EOL> case . product . id , suite . product . id ) <EOL> raise ImmediateHttpResponse ( <EOL> response = http . HttpBadRequest ( error_message ) ) <EOL> return bundle <EOL> class CaseVersionResource ( MTResource ) : <EOL> """<STR_LIT>""" <EOL> case = fields . ForeignKey ( CaseResource , "<STR_LIT>" ) <EOL> steps = fields . ToManyField ( <EOL> CaseStepResource , "<STR_LIT>" , full = True , readonly = True ) <EOL> environments = fields . ToManyField ( <EOL> EnvironmentResource , "<STR_LIT>" , full = True , readonly = True ) <EOL> productversion = fields . ForeignKey ( <EOL> ProductVersionResource , "<STR_LIT>" ) <EOL> tags = fields . ToManyField ( TagResource , "<STR_LIT>" , full = True , readonly = True ) <EOL> created_by = fields . ForeignKey ( <EOL> UserResource , <EOL> "<STR_LIT>" , <EOL> full = True , <EOL> null = True , <EOL> ) <EOL> modified_by = fields . ForeignKey ( <EOL> UserResource , <EOL> "<STR_LIT>" , <EOL> full = True , <EOL> null = True , <EOL> ) <EOL> class Meta ( MTResource . Meta ) : <EOL> queryset = CaseVersion . objects . all ( ) <EOL> fields = [ "<STR_LIT:id>" , "<STR_LIT:name>" , "<STR_LIT:description>" , "<STR_LIT>" , "<STR_LIT:status>" , "<STR_LIT>" ] <EOL> filtering = { <EOL> "<STR_LIT>" : ALL , <EOL> "<STR_LIT>" : ALL_WITH_RELATIONS , <EOL> "<STR_LIT>" : ALL_WITH_RELATIONS , <EOL> "<STR_LIT>" : ALL_WITH_RELATIONS , <EOL> "<STR_LIT>" : ALL , <EOL> "<STR_LIT:name>" : ALL , <EOL> "<STR_LIT:status>" : ALL , <EOL> "<STR_LIT>" : ALL_WITH_RELATIONS , <EOL> "<STR_LIT>" : ALL_WITH_RELATIONS , <EOL> "<STR_LIT:description>" : ALL , <EOL> "<STR_LIT>" : ALL_WITH_RELATIONS , <EOL> } <EOL> ordering = [ "<STR_LIT:id>" , "<STR_LIT:name>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] <EOL> authorization = CaseVersionAuthorization ( ) <EOL> @ property <EOL> def model ( self ) : <EOL> """<STR_LIT>""" <EOL> return CaseVersion <EOL> def dehydrate ( self , bundle ) : <EOL> """<STR_LIT>""" <EOL> case = bundle . obj . case <EOL> bundle . data [ "<STR_LIT>" ] = unicode ( case . priority ) <EOL> bundle . data [ "<STR_LIT>" ] = bundle . obj . productversion . name <EOL> return bundle <EOL> @ property <EOL> def read_create_fields ( self ) : <EOL> """<STR_LIT>""" <EOL> return [ "<STR_LIT>" , "<STR_LIT>" ] <EOL> def obj_update ( self , bundle , request = None , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> request = request or bundle . request <EOL> bundle = self . check_read_create ( bundle ) <EOL> try : <EOL> bundle = super ( MTResource , self ) . obj_update ( <EOL> bundle , ** kwargs ) <EOL> bundle . obj . cc_version = self . model . objects . get ( <EOL> id = bundle . obj . id ) . cc_version <EOL> bundle . obj . save ( user = request . user ) <EOL> return bundle <EOL> except Exception : <EOL> logger . exception ( "<STR_LIT>" , bundle ) <EOL> raise <EOL> def hydrate_productversion ( self , bundle ) : <EOL> """<STR_LIT>""" <EOL> if bundle . request . META [ '<STR_LIT>' ] == '<STR_LIT:POST>' : <EOL> pv_id = self . _id_from_uri ( bundle . data [ '<STR_LIT>' ] ) <EOL> pv = ProductVersion . objects . get ( id = pv_id ) <EOL> case_id = self . _id_from_uri ( bundle . data [ '<STR_LIT>' ] ) <EOL> case = Case . objects . get ( id = case_id ) <EOL> if not case . product . id == pv . product . id : <EOL> message = str ( "<STR_LIT>" ) <EOL> logger . error ( "<STR_LIT:\n>" . join ( [ message , <EOL> "<STR_LIT>" ] , ) , <EOL> pv . product . id , <EOL> case . product . id ) <EOL> raise ImmediateHttpResponse ( <EOL> response = http . HttpBadRequest ( message ) ) <EOL> return bundle <EOL> class BaseSelectionResource ( ModelResource ) : <EOL> """<STR_LIT>""" <EOL> def apply_filters ( self , <EOL> request , applicable_filters , applicable_excludes = { } ) : <EOL> """<STR_LIT>""" <EOL> return self . get_object_list ( request ) . filter ( <EOL> ** applicable_filters ) . exclude ( ** applicable_excludes ) <EOL> def obj_get_list ( self , bundle , request = None , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> filters = { } <EOL> request = request or bundle . request <EOL> if hasattr ( request , '<STR_LIT:GET>' ) : <EOL> filters = request . GET . copy ( ) <EOL> filters . update ( kwargs ) <EOL> new_filters = { } <EOL> excludes = { } <EOL> for key , value in filters . items ( ) : <EOL> if key . endswith ( '<STR_LIT>' ) : <EOL> key = key [ : - <NUM_LIT:4> ] <EOL> excludes [ key ] = value <EOL> else : <EOL> new_filters [ key ] = value <EOL> filters = new_filters <EOL> applicable_filters = self . build_filters ( filters = filters ) <EOL> applicable_excludes = self . build_filters ( filters = excludes ) <EOL> base_object_list = self . apply_filters ( <EOL> request , applicable_filters , applicable_excludes ) <EOL> return self . authorized_read_list ( base_object_list , bundle ) <EOL> class CaseSelectionResource ( BaseSelectionResource ) : <EOL> """<STR_LIT>""" <EOL> case = fields . ForeignKey ( CaseResource , "<STR_LIT>" ) <EOL> productversion = fields . ForeignKey ( <EOL> ProductVersionResource , "<STR_LIT>" ) <EOL> tags = fields . ToManyField ( TagResource , "<STR_LIT>" , full = True ) <EOL> created_by = fields . ForeignKey ( <EOL> UserResource , <EOL> "<STR_LIT>" , <EOL> full = True , <EOL> null = True , <EOL> ) <EOL> modified_by = fields . ForeignKey ( <EOL> UserResource , <EOL> "<STR_LIT>" , <EOL> full = True , <EOL> null = True , <EOL> ) <EOL> class Meta : <EOL> queryset = CaseVersion . objects . filter ( latest = True ) . select_related ( <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> ) . prefetch_related ( <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> ) <EOL> list_allowed_methods = [ '<STR_LIT>' ] <EOL> fields = [ "<STR_LIT:id>" , "<STR_LIT:name>" , "<STR_LIT>" , "<STR_LIT>" ] <EOL> filtering = { <EOL> "<STR_LIT>" : ALL_WITH_RELATIONS , <EOL> "<STR_LIT>" : ALL_WITH_RELATIONS , <EOL> "<STR_LIT>" : ALL_WITH_RELATIONS , <EOL> "<STR_LIT>" : ALL_WITH_RELATIONS , <EOL> "<STR_LIT>" : ALL_WITH_RELATIONS , <EOL> "<STR_LIT:name>" : ALL <EOL> } <EOL> ordering = [ "<STR_LIT:id>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT:name>" ] <EOL> def dehydrate ( self , bundle ) : <EOL> """<STR_LIT>""" <EOL> case = bundle . obj . case <EOL> bundle . data [ "<STR_LIT>" ] = case . id <EOL> bundle . data [ "<STR_LIT>" ] = case . product_id <EOL> bundle . data [ "<STR_LIT>" ] = { "<STR_LIT:id>" : case . product_id } <EOL> bundle . data [ "<STR_LIT>" ] = unicode ( case . priority ) <EOL> return bundle <EOL> class CaseVersionSelectionResource ( BaseSelectionResource ) : <EOL> """<STR_LIT>""" <EOL> case = fields . ForeignKey ( CaseResource , "<STR_LIT>" ) <EOL> productversion = fields . ForeignKey ( <EOL> ProductVersionResource , "<STR_LIT>" , full = True ) <EOL> tags = fields . ToManyField ( TagResource , "<STR_LIT>" , full = True ) <EOL> created_by = fields . ForeignKey ( <EOL> UserResource , <EOL> "<STR_LIT>" , <EOL> full = True , <EOL> null = True , <EOL> ) <EOL> class Meta : <EOL> queryset = CaseVersion . objects . all ( ) . select_related ( <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> ) . prefetch_related ( <EOL> "<STR_LIT>" , <EOL> ) <EOL> list_allowed_methods = [ '<STR_LIT>' ] <EOL> fields = [ "<STR_LIT:id>" , "<STR_LIT:name>" , "<STR_LIT>" , "<STR_LIT>" ] <EOL> filtering = { <EOL> "<STR_LIT>" : ALL_WITH_RELATIONS , <EOL> "<STR_LIT>" : ALL_WITH_RELATIONS , <EOL> "<STR_LIT>" : ALL_WITH_RELATIONS , <EOL> "<STR_LIT>" : ALL_WITH_RELATIONS <EOL> } <EOL> ordering = [ "<STR_LIT:name>" ] <EOL> def dehydrate ( self , bundle ) : <EOL> """<STR_LIT>""" <EOL> case = bundle . obj . case <EOL> bundle . data [ "<STR_LIT>" ] = case . id <EOL> bundle . data [ "<STR_LIT>" ] = case . product_id <EOL> bundle . data [ "<STR_LIT>" ] = { "<STR_LIT:id>" : case . product_id } <EOL> bundle . data [ "<STR_LIT>" ] = bundle . obj . productversion . name <EOL> bundle . data [ "<STR_LIT>" ] = unicode ( case . priority ) <EOL> return bundle <EOL> class CaseVersionSearchResource ( BaseSelectionResource ) : <EOL> """<STR_LIT>""" <EOL> case = fields . ForeignKey ( CaseResource , "<STR_LIT>" ) <EOL> productversion = fields . ForeignKey ( <EOL> ProductVersionResource , "<STR_LIT>" , full = True ) <EOL> tags = fields . ToManyField ( TagResource , "<STR_LIT>" , full = True ) <EOL> created_by = fields . ForeignKey ( <EOL> UserResource , <EOL> "<STR_LIT>" , <EOL> full = True , <EOL> null = True , <EOL> ) <EOL> modified_by = fields . ForeignKey ( <EOL> UserResource , <EOL> "<STR_LIT>" , <EOL> full = True , <EOL> null = True , <EOL> ) <EOL> class Meta : <EOL> queryset = CaseVersion . objects . all ( ) <EOL> list_allowed_methods = [ '<STR_LIT>' ] <EOL> detail_allowed_methods = [ '<STR_LIT>' ] <EOL> fields = [ "<STR_LIT:id>" , "<STR_LIT:name>" , "<STR_LIT:status>" , "<STR_LIT>" ] <EOL> filtering = { <EOL> "<STR_LIT>" : ALL , <EOL> "<STR_LIT>" : ALL_WITH_RELATIONS , <EOL> "<STR_LIT>" : ALL_WITH_RELATIONS , <EOL> "<STR_LIT>" : ALL_WITH_RELATIONS , <EOL> "<STR_LIT>" : ALL , <EOL> "<STR_LIT:name>" : ALL , <EOL> } <EOL> ordering = [ "<STR_LIT:name>" , "<STR_LIT>" ] <EOL> def dehydrate ( self , bundle ) : <EOL> """<STR_LIT>""" <EOL> case = bundle . obj . case <EOL> bundle . data [ "<STR_LIT>" ] = case . id <EOL> bundle . data [ "<STR_LIT>" ] = bundle . obj . productversion . name <EOL> bundle . data [ "<STR_LIT>" ] = unicode ( case . priority ) <EOL> return bundle </s>
