<s> from __future__ import unicode_literals <EOL> from datetime import datetime <EOL> import functools <EOL> from django . db . models import Q <EOL> from modeltree . tree import trees , MODELTREE_DEFAULT_ALIAS <EOL> from preserialize . serialize import serialize <EOL> from restlib2 . http import codes <EOL> from restlib2 . params import Parametizer , StrParam <EOL> from avocado . events import usage <EOL> from avocado . models import DataQuery <EOL> from avocado . query import pipeline <EOL> from serrano . forms import QueryForm <EOL> from serrano . links import reverse_tmpl <EOL> from serrano . resources import templates <EOL> from serrano . resources . base import ThrottledResource <EOL> from serrano . utils import send_mail <EOL> DELETE_QUERY_EMAIL_TITLE = "<STR_LIT>" <EOL> DELETE_QUERY_EMAIL_BODY = """<STR_LIT>""" <EOL> def query_posthook ( instance , data , request ) : <EOL> if getattr ( instance , '<STR_LIT:user>' , None ) and instance . user . is_authenticated ( ) : <EOL> data [ '<STR_LIT>' ] = instance . user == request . user <EOL> else : <EOL> data [ '<STR_LIT>' ] = instance . session_key == request . session . session_key <EOL> if not data [ '<STR_LIT>' ] : <EOL> del data [ '<STR_LIT>' ] <EOL> return data <EOL> class QueryParametizer ( Parametizer ) : <EOL> tree = StrParam ( MODELTREE_DEFAULT_ALIAS , choices = trees ) <EOL> processor = StrParam ( '<STR_LIT:default>' , choices = pipeline . query_processors ) <EOL> class QueryBase ( ThrottledResource ) : <EOL> cache_max_age = <NUM_LIT:0> <EOL> private_cache = True <EOL> model = DataQuery <EOL> template = templates . Query <EOL> parametizer = QueryParametizer <EOL> def prepare ( self , request , instance , template = None ) : <EOL> if template is None : <EOL> template = self . template <EOL> posthook = functools . partial ( query_posthook , request = request ) <EOL> return serialize ( instance , posthook = posthook , ** template ) <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:id>' ) } ) , <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:id>' ) } ) , <EOL> } <EOL> def get_queryset ( self , request , ** kwargs ) : <EOL> "<STR_LIT>" <EOL> if getattr ( request , '<STR_LIT:user>' , None ) and request . user . is_authenticated ( ) : <EOL> kwargs [ '<STR_LIT:user>' ] = request . user <EOL> elif request . session . session_key : <EOL> kwargs [ '<STR_LIT>' ] = request . session . session_key <EOL> else : <EOL> return self . model . objects . none ( ) <EOL> return self . model . objects . filter ( ** kwargs ) <EOL> def get_object ( self , request , pk = None , session = None , ** kwargs ) : <EOL> if not pk and not session : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> if not hasattr ( request , '<STR_LIT>' ) : <EOL> queryset = self . get_queryset ( request , ** kwargs ) <EOL> try : <EOL> if pk : <EOL> instance = queryset . get ( pk = pk ) <EOL> else : <EOL> instance = queryset . get ( session = True ) <EOL> except self . model . DoesNotExist : <EOL> instance = None <EOL> request . instance = instance <EOL> return request . instance <EOL> class QueriesResource ( QueryBase ) : <EOL> "<STR_LIT>" <EOL> template = templates . Query <EOL> def prepare ( self , request , instance , template = None ) : <EOL> if template is None : <EOL> template = self . template <EOL> posthook = functools . partial ( query_posthook , request = request ) <EOL> return serialize ( instance , posthook = posthook , ** template ) <EOL> def get_queryset ( self , request , ** kwargs ) : <EOL> if getattr ( request , '<STR_LIT:user>' , None ) and request . user . is_authenticated ( ) : <EOL> f = Q ( user = request . user ) | Q ( shared_users__pk = request . user . pk ) <EOL> elif request . session . session_key : <EOL> f = Q ( session_key = request . session . session_key ) <EOL> else : <EOL> return super ( QueriesResource , self ) . get_queryset ( request , ** kwargs ) <EOL> return self . model . objects . filter ( f , ** kwargs ) . order_by ( '<STR_LIT>' ) . distinct ( ) <EOL> def get ( self , request ) : <EOL> queryset = self . get_queryset ( request ) <EOL> return self . prepare ( request , queryset ) <EOL> def post ( self , request ) : <EOL> form = QueryForm ( request , request . data ) <EOL> if form . is_valid ( ) : <EOL> instance = form . save ( ) <EOL> usage . log ( '<STR_LIT>' , instance = instance , request = request ) <EOL> request . session . modified = True <EOL> response = self . render ( request , self . prepare ( request , instance ) , <EOL> status = codes . created ) <EOL> else : <EOL> data = { <EOL> '<STR_LIT:message>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : dict ( form . errors ) , <EOL> } <EOL> response = self . render ( request , data , <EOL> status = codes . unprocessable_entity ) <EOL> return response <EOL> class PublicQueriesResource ( QueryBase ) : <EOL> "<STR_LIT>" <EOL> template = templates . BriefQuery <EOL> def prepare ( self , request , instance , template = None ) : <EOL> if template is None : <EOL> template = self . template <EOL> posthook = functools . partial ( query_posthook , request = request ) <EOL> return serialize ( instance , posthook = posthook , ** template ) <EOL> def get_queryset ( self , request , ** kwargs ) : <EOL> kwargs [ '<STR_LIT>' ] = True <EOL> return self . model . objects . filter ( ** kwargs ) . order_by ( '<STR_LIT>' ) . distinct ( ) <EOL> def get ( self , request ) : <EOL> queryset = self . get_queryset ( request ) <EOL> return self . prepare ( request , queryset ) <EOL> class QueryResource ( QueryBase ) : <EOL> "<STR_LIT>" <EOL> def is_not_found ( self , request , response , ** kwargs ) : <EOL> return self . get_object ( request , ** kwargs ) is None <EOL> def get ( self , request , ** kwargs ) : <EOL> instance = self . get_object ( request , ** kwargs ) <EOL> usage . log ( '<STR_LIT>' , instance = instance , request = request ) <EOL> self . model . objects . filter ( pk = instance . pk ) . update ( <EOL> accessed = datetime . now ( ) ) <EOL> return self . prepare ( request , instance ) <EOL> def put ( self , request , ** kwargs ) : <EOL> instance = self . get_object ( request , ** kwargs ) <EOL> form = QueryForm ( request , request . data , instance = instance ) <EOL> if form . is_valid ( ) : <EOL> instance = form . save ( ) <EOL> usage . log ( '<STR_LIT>' , instance = instance , request = request ) <EOL> request . session . modified = True <EOL> response = self . render ( request , self . prepare ( request , instance ) ) <EOL> else : <EOL> data = { <EOL> '<STR_LIT:message>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : dict ( form . errors ) , <EOL> } <EOL> response = self . render ( request , data , <EOL> status = codes . unprocessable_entity ) <EOL> return response <EOL> def delete ( self , request , ** kwargs ) : <EOL> instance = self . get_object ( request , ** kwargs ) <EOL> if instance . session : <EOL> data = { <EOL> '<STR_LIT:message>' : '<STR_LIT>' , <EOL> } <EOL> return self . render ( request , data , status = codes . bad_request ) <EOL> send_mail ( instance . shared_users . values_list ( '<STR_LIT:email>' , flat = True ) , <EOL> DELETE_QUERY_EMAIL_TITLE . format ( instance . name ) , <EOL> DELETE_QUERY_EMAIL_BODY . format ( instance . name ) ) <EOL> instance . delete ( ) <EOL> usage . log ( '<STR_LIT>' , instance = instance , request = request ) <EOL> request . session . modified = True </s>
