<s> import functools <EOL> from django . contrib . contenttypes . models import ContentType <EOL> from preserialize . serialize import serialize <EOL> from restlib2 . params import Parametizer , BoolParam <EOL> from avocado . history . models import Revision <EOL> from . base import ThrottledResource <EOL> from . import templates <EOL> from . . links import reverse_tmpl <EOL> __all__ = ( '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' ) <EOL> def revision_posthook ( instance , data , request , object_template , embed = False ) : <EOL> if embed : <EOL> data [ '<STR_LIT:object>' ] = serialize ( instance . content_object , ** object_template ) <EOL> return data <EOL> class RevisionParametizer ( Parametizer ) : <EOL> """<STR_LIT>""" <EOL> embed = BoolParam ( False ) <EOL> class RevisionsResource ( ThrottledResource ) : <EOL> cache_max_age = <NUM_LIT:0> <EOL> private_cache = True <EOL> object_model = None <EOL> object_model_template = None <EOL> object_model_base_uri = None <EOL> model = Revision <EOL> template = templates . Revision <EOL> parametizer = RevisionParametizer <EOL> def get_link_templates ( self , request ) : <EOL> if self . object_model is None : <EOL> return { } <EOL> uri = request . build_absolute_uri <EOL> object_uri = self . object_model_base_uri <EOL> return { <EOL> '<STR_LIT>' : reverse_tmpl ( <EOL> uri , <EOL> '<STR_LIT>' . format ( object_uri ) , <EOL> { <EOL> '<STR_LIT>' : ( int , '<STR_LIT>' ) , <EOL> '<STR_LIT>' : ( int , '<STR_LIT:id>' ) <EOL> } <EOL> ) , <EOL> '<STR_LIT:object>' : reverse_tmpl ( <EOL> uri , <EOL> '<STR_LIT>' . format ( object_uri ) , <EOL> { '<STR_LIT>' : ( int , '<STR_LIT:id>' ) } <EOL> ) <EOL> } <EOL> def prepare ( self , request , instance , template = None , embed = False ) : <EOL> if template is None : <EOL> template = self . template <EOL> posthook = functools . partial ( <EOL> revision_posthook , request = request , <EOL> object_template = self . object_model_template , embed = embed ) <EOL> return serialize ( instance , posthook = posthook , ** template ) <EOL> def get_queryset ( self , request , ** kwargs ) : <EOL> "<STR_LIT>" <EOL> if not self . object_model : <EOL> return self . model . objects . none ( ) <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> kwargs [ '<STR_LIT>' ] = ContentType . objects . get_for_model ( <EOL> self . object_model ) <EOL> return self . model . objects . filter ( ** kwargs ) <EOL> def get ( self , request ) : <EOL> params = self . get_params ( request ) <EOL> queryset = self . get_queryset ( request ) <EOL> return self . prepare ( request , queryset , embed = params [ '<STR_LIT>' ] ) <EOL> class ObjectRevisionsResource ( RevisionsResource ) : <EOL> """<STR_LIT>""" <EOL> def get ( self , request , ** kwargs ) : <EOL> query_kwargs = { '<STR_LIT>' : int ( kwargs [ '<STR_LIT>' ] ) } <EOL> params = self . get_params ( request ) <EOL> queryset = self . get_queryset ( request , ** query_kwargs ) <EOL> return self . prepare ( request , queryset , embed = params [ '<STR_LIT>' ] ) <EOL> class ObjectRevisionResource ( RevisionsResource ) : <EOL> """<STR_LIT>""" <EOL> def get_object ( self , request , object_pk = None , revision_pk = None , ** kwargs ) : <EOL> if not object_pk : <EOL> raise ValueError ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> if not revision_pk : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> if not hasattr ( request , '<STR_LIT>' ) : <EOL> queryset = self . get_queryset ( request , ** kwargs ) <EOL> try : <EOL> instance = queryset . get ( pk = revision_pk , object_id = object_pk ) <EOL> except self . model . DoesNotExist : <EOL> instance = None <EOL> request . instance = instance <EOL> return request . instance <EOL> def is_not_found ( self , request , response , ** kwargs ) : <EOL> try : <EOL> return self . get_object ( request , ** kwargs ) is None <EOL> except ValueError : <EOL> return True <EOL> def get ( self , request , ** kwargs ) : <EOL> instance = self . get_object ( request , ** kwargs ) <EOL> return self . prepare ( request , instance ) </s>
