<s> import functools <EOL> from preserialize . serialize import serialize <EOL> from restlib2 . http import codes <EOL> from avocado . models import DataQuery <EOL> from serrano . links import reverse_tmpl <EOL> from serrano . resources import templates <EOL> from serrano . resources . query . base import query_posthook , QueryBase <EOL> class QueryForksResource ( QueryBase ) : <EOL> "<STR_LIT>" <EOL> template = templates . ForkedQuery <EOL> def is_not_found ( self , request , response , ** kwargs ) : <EOL> return self . get_object ( request , ** kwargs ) is None <EOL> def get_link_templates ( self , request ) : <EOL> uri = request . build_absolute_uri <EOL> return { <EOL> '<STR_LIT>' : reverse_tmpl ( <EOL> uri , '<STR_LIT>' , { '<STR_LIT>' : ( int , '<STR_LIT:id>' ) } ) , <EOL> '<STR_LIT>' : reverse_tmpl ( <EOL> uri , '<STR_LIT>' , { '<STR_LIT>' : ( int , '<STR_LIT>' ) } ) , <EOL> } <EOL> def get_queryset ( self , request , ** kwargs ) : <EOL> instance = self . get_object ( request , ** kwargs ) <EOL> return self . model . objects . filter ( parent = instance . pk ) <EOL> def get_object ( self , request , pk = None , ** kwargs ) : <EOL> if not pk : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> if not hasattr ( request , '<STR_LIT>' ) : <EOL> try : <EOL> instance = self . model . objects . get ( pk = pk ) <EOL> except self . model . DoesNotExist : <EOL> instance = None <EOL> request . instance = instance <EOL> return request . instance <EOL> def prepare ( self , request , instance , template = None ) : <EOL> if template is None : <EOL> template = self . template <EOL> return serialize ( instance , ** template ) <EOL> def _requestor_can_get_forks ( self , request , instance ) : <EOL> """<STR_LIT>""" <EOL> if instance . public : <EOL> return True <EOL> if not getattr ( request , '<STR_LIT:user>' , None ) : <EOL> return False <EOL> return ( request . user . is_authenticated ( ) and <EOL> request . user == instance . user ) <EOL> def _requestor_can_fork ( self , request , instance ) : <EOL> """<STR_LIT>""" <EOL> if instance . public : <EOL> return True <EOL> if getattr ( request , '<STR_LIT:user>' , None ) and request . user . is_authenticated ( ) : <EOL> return ( request . user == instance . user or <EOL> instance . shared_users . filter ( pk = request . user . pk ) . exists ( ) ) <EOL> return False <EOL> def get ( self , request , ** kwargs ) : <EOL> instance = self . get_object ( request , ** kwargs ) <EOL> if self . _requestor_can_get_forks ( request , instance ) : <EOL> return self . prepare ( request , self . get_queryset ( request , ** kwargs ) ) <EOL> data = { <EOL> '<STR_LIT:message>' : '<STR_LIT>' , <EOL> } <EOL> return self . render ( request , data , status = codes . unauthorized ) <EOL> def post ( self , request , ** kwargs ) : <EOL> instance = self . get_object ( request , ** kwargs ) <EOL> if self . _requestor_can_fork ( request , instance ) : <EOL> fork = DataQuery ( name = instance . name , <EOL> description = instance . description , <EOL> view_json = instance . view_json , <EOL> context_json = instance . context_json , <EOL> parent = instance ) <EOL> if getattr ( request , '<STR_LIT:user>' , None ) : <EOL> fork . user = request . user <EOL> elif request . session . session_key : <EOL> fork . session_key = request . session . session_key <EOL> fork . save ( ) <EOL> request . session . modified = True <EOL> posthook = functools . partial ( query_posthook , request = request ) <EOL> data = serialize ( fork , posthook = posthook , ** templates . Query ) <EOL> return self . render ( request , data , status = codes . created ) <EOL> data = { <EOL> '<STR_LIT:message>' : '<STR_LIT>' , <EOL> } <EOL> return self . render ( request , data , status = codes . unauthorized ) </s>
