<s> from tastypie import fields <EOL> from tastypie import http <EOL> from tastypie . resources import ModelResource , ALL , ALL_WITH_RELATIONS <EOL> from tastypie . exceptions import ImmediateHttpResponse <EOL> from . . mtapi import MTResource , MTAuthorization <EOL> from . models import Profile , Environment , Element , Category <EOL> import logging <EOL> logger = logging . getLogger ( __name__ ) <EOL> class EnvironmentAuthorization ( MTAuthorization ) : <EOL> """<STR_LIT>""" <EOL> @ property <EOL> def permission ( self ) : <EOL> """<STR_LIT>""" <EOL> return "<STR_LIT>" <EOL> class ProfileResource ( MTResource ) : <EOL> """<STR_LIT>""" <EOL> class Meta ( MTResource . Meta ) : <EOL> queryset = Profile . objects . all ( ) <EOL> fields = [ "<STR_LIT:id>" , "<STR_LIT:name>" ] <EOL> authorization = EnvironmentAuthorization ( ) <EOL> ordering = [ "<STR_LIT:id>" , "<STR_LIT:name>" ] <EOL> filtering = { <EOL> "<STR_LIT:name>" : ALL , <EOL> } <EOL> @ property <EOL> def model ( self ) : <EOL> """<STR_LIT>""" <EOL> return Profile <EOL> class CategoryResource ( MTResource ) : <EOL> """<STR_LIT>""" <EOL> elements = fields . ToManyField ( <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> full = True , <EOL> readonly = True <EOL> ) <EOL> class Meta ( MTResource . Meta ) : <EOL> queryset = Category . objects . all ( ) <EOL> fields = [ "<STR_LIT:id>" , "<STR_LIT:name>" ] <EOL> authorization = EnvironmentAuthorization ( ) <EOL> ordering = [ "<STR_LIT:id>" , "<STR_LIT:name>" ] <EOL> filtering = { <EOL> "<STR_LIT:name>" : ALL , <EOL> } <EOL> @ property <EOL> def model ( self ) : <EOL> """<STR_LIT>""" <EOL> return Category <EOL> class ElementResource ( MTResource ) : <EOL> """<STR_LIT>""" <EOL> category = fields . ForeignKey ( CategoryResource , "<STR_LIT>" ) <EOL> class Meta ( MTResource . Meta ) : <EOL> queryset = Element . objects . all ( ) <EOL> fields = [ "<STR_LIT:id>" , "<STR_LIT:name>" , "<STR_LIT>" ] <EOL> authorization = EnvironmentAuthorization ( ) <EOL> filtering = { <EOL> "<STR_LIT>" : ALL_WITH_RELATIONS , <EOL> "<STR_LIT:name>" : ALL , <EOL> } <EOL> ordering = [ "<STR_LIT:id>" , "<STR_LIT:name>" ] <EOL> @ property <EOL> def model ( self ) : <EOL> """<STR_LIT>""" <EOL> return Element <EOL> @ property <EOL> def read_create_fields ( self ) : <EOL> """<STR_LIT>""" <EOL> return [ "<STR_LIT>" ] <EOL> class EnvironmentResource ( MTResource ) : <EOL> """<STR_LIT>""" <EOL> elements = fields . ToManyField ( ElementResource , "<STR_LIT>" ) <EOL> profile = fields . ForeignKey ( ProfileResource , "<STR_LIT>" , null = True ) <EOL> class Meta ( MTResource . Meta ) : <EOL> queryset = Environment . objects . all ( ) <EOL> list_allowed_methods = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> detail_allowed_methods = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> fields = [ "<STR_LIT:id>" , "<STR_LIT>" , "<STR_LIT>" ] <EOL> filtering = { <EOL> "<STR_LIT>" : ALL , <EOL> "<STR_LIT>" : ALL_WITH_RELATIONS , <EOL> } <EOL> ordering = [ "<STR_LIT:id>" , "<STR_LIT>" ] <EOL> @ property <EOL> def model ( self ) : <EOL> """<STR_LIT>""" <EOL> return Environment <EOL> def hydrate_m2m ( self , bundle ) : <EOL> """<STR_LIT>""" <EOL> bundle = super ( EnvironmentResource , self ) . hydrate_m2m ( bundle ) <EOL> elem_categories = [ elem . data [ '<STR_LIT>' ] for elem in <EOL> bundle . data [ '<STR_LIT>' ] ] <EOL> if len ( set ( elem_categories ) ) != len ( bundle . data [ '<STR_LIT>' ] ) : <EOL> error_msg = "<STR_LIT>" <EOL> logger . error ( error_msg ) <EOL> raise ImmediateHttpResponse ( <EOL> response = http . HttpBadRequest ( error_msg ) ) <EOL> return bundle <EOL> def patch_list ( self , request , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> import itertools <EOL> from django . db import transaction <EOL> from tastypie . utils import dict_strip_unicode_keys <EOL> deserialized = self . deserialize ( <EOL> request , <EOL> request . body , <EOL> format = request . META . get ( '<STR_LIT>' , '<STR_LIT:application/json>' ) ) <EOL> categories = deserialized . pop ( '<STR_LIT>' , [ ] ) <EOL> if not categories or not isinstance ( categories , list ) : <EOL> error_msg = "<STR_LIT>" <EOL> logger . error ( error_msg ) <EOL> raise ImmediateHttpResponse ( <EOL> response = http . HttpBadRequest ( error_msg ) ) <EOL> elem_lists = [ ] <EOL> for cat in categories : <EOL> if isinstance ( cat , basestring ) : <EOL> cat = Category . objects . filter ( id = self . _id_from_uri ( cat ) ) <EOL> elem_list = Element . objects . filter ( category = cat ) <EOL> elif isinstance ( cat , dict ) : <EOL> category = Category . objects . filter ( <EOL> id = self . _id_from_uri ( cat [ '<STR_LIT>' ] ) ) <EOL> elem_list = Element . objects . filter ( category = category ) <EOL> if '<STR_LIT>' in cat : <EOL> exclude_uris = cat [ '<STR_LIT>' ] <EOL> exclude_ids = [ int ( <EOL> self . _id_from_uri ( x ) ) for x in exclude_uris ] <EOL> elem_list = [ elem for elem in elem_list <EOL> if elem . id not in exclude_ids ] <EOL> elif '<STR_LIT>' in cat : <EOL> include_uris = cat [ '<STR_LIT>' ] <EOL> include_ids = [ int ( <EOL> self . _id_from_uri ( x ) ) for x in include_uris ] <EOL> elem_list = [ elem for elem in elem_list <EOL> if elem . id in include_ids ] <EOL> else : <EOL> pass <EOL> else : <EOL> error_msg = "<STR_LIT>" <EOL> logger . error ( error_msg ) <EOL> raise ImmediateHttpResponse ( <EOL> response = http . HttpBadRequest ( error_msg ) ) <EOL> elem_lists . append ( elem_list ) <EOL> combinatorics = itertools . product ( * elem_lists ) <EOL> with transaction . commit_on_success ( ) : <EOL> for combo in combinatorics : <EOL> deserialized [ '<STR_LIT>' ] = combo <EOL> bundle = self . build_bundle ( <EOL> data = dict_strip_unicode_keys ( deserialized ) ) <EOL> bundle . request . META [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> if getattr ( request , '<STR_LIT:user>' , None ) : <EOL> bundle . request . user = request . user <EOL> self . is_valid ( bundle ) <EOL> self . obj_create ( bundle , request = request ) <EOL> return http . HttpAccepted ( ) </s>
