<s> from __future__ import unicode_literals <EOL> from django . core . exceptions import ImproperlyConfigured , ObjectDoesNotExist <EOL> from django . db import models <EOL> from django . http import Http404 <EOL> from django . utils . translation import ugettext as _ <EOL> from base import TemplateResponseMixin , ContextMixin , View <EOL> class SingleObjectMixin ( ContextMixin ) : <EOL> """<STR_LIT>""" <EOL> detail_model = None <EOL> detail_context_object_name = None <EOL> detail_queryset = None <EOL> detail_pk_url_kwarg = '<STR_LIT>' <EOL> slug_field = '<STR_LIT>' <EOL> slug_url_kwarg = '<STR_LIT>' <EOL> def get_object ( self , queryset = None , pk_url_kwarg = None ) : <EOL> """<STR_LIT>""" <EOL> if queryset is None : <EOL> queryset = self . get_queryset ( ) <EOL> pk = self . kwargs . get ( pk_url_kwarg , None ) <EOL> slug = self . kwargs . get ( self . slug_url_kwarg , None ) <EOL> if pk is not None : <EOL> queryset = queryset . filter ( pk = pk ) <EOL> elif slug is not None : <EOL> slug_field = self . get_slug_field ( ) <EOL> queryset = queryset . filter ( ** { slug_field : slug } ) <EOL> else : <EOL> raise AttributeError ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> % self . __class__ . __name__ ) <EOL> try : <EOL> obj = queryset . get ( ) <EOL> except ObjectDoesNotExist : <EOL> raise Http404 ( _ ( "<STR_LIT>" ) % <EOL> { '<STR_LIT>' : queryset . model . _meta . verbose_name } ) <EOL> return obj <EOL> def get_detail_object ( self , queryset = None ) : <EOL> return self . get_object ( queryset or self . get_detail_queryset ( ) , self . detail_pk_url_kwarg ) <EOL> def get_queryset ( self , model ) : <EOL> """<STR_LIT>""" <EOL> if model : <EOL> return model . _default_manager . all ( ) <EOL> else : <EOL> raise ImproperlyConfigured ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % { <EOL> '<STR_LIT>' : self . __class__ . __name__ <EOL> } ) <EOL> def get_detail_queryset ( self ) : <EOL> if self . detail_queryset : <EOL> return self . detail_queryset . _clone ( ) <EOL> else : <EOL> return self . get_queryset ( self . detail_model ) <EOL> def get_slug_field ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . slug_field <EOL> def get_detail_context_object_name ( self , obj ) : <EOL> """<STR_LIT>""" <EOL> if self . detail_context_object_name : <EOL> return self . detail_context_object_name <EOL> elif isinstance ( obj , models . Model ) : <EOL> return obj . _meta . object_name . lower ( ) <EOL> else : <EOL> return None <EOL> def get_detail_context_data ( self , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> context = { } <EOL> context_object_name = self . get_detail_context_object_name ( self . detail_object ) <EOL> if context_object_name : <EOL> context [ context_object_name ] = self . detail_object <EOL> context . update ( kwargs ) <EOL> return context <EOL> def detail_absolute_url ( self ) : <EOL> return self . get_detail_object ( ) . get_absolute_url ( ) <EOL> class BaseDetailView ( SingleObjectMixin , View ) : <EOL> """<STR_LIT>""" <EOL> def detail_get ( self , request , * args , ** kwargs ) : <EOL> self . detail_object = self . get_detail_object ( ) <EOL> return self . get_detail_context_data ( detail_object = self . detail_object ) <EOL> class SingleObjectTemplateResponseMixin ( TemplateResponseMixin ) : <EOL> template_name_field = None <EOL> template_name_suffix = '<STR_LIT>' <EOL> def get_template_names ( self ) : <EOL> return self . _get_template_names ( self . detail_object , self . detail_model ) <EOL> def _get_template_names ( self , object = None , model = None ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> names = super ( SingleObjectTemplateResponseMixin , self ) . get_template_names ( ) <EOL> except ImproperlyConfigured : <EOL> names = [ ] <EOL> if object and self . template_name_field : <EOL> name = getattr ( self . detail_object , self . template_name_field , None ) <EOL> if name : <EOL> names . insert ( <NUM_LIT:0> , name ) <EOL> if isinstance ( object , models . Model ) : <EOL> names . append ( "<STR_LIT>" % ( <EOL> object . _meta . app_label , <EOL> object . _meta . object_name . lower ( ) , <EOL> self . template_name_suffix <EOL> ) ) <EOL> elif model is not None and issubclass ( model , models . Model ) : <EOL> names . append ( "<STR_LIT>" % ( <EOL> model . _meta . app_label , <EOL> model . _meta . object_name . lower ( ) , <EOL> self . template_name_suffix <EOL> ) ) <EOL> return names <EOL> class DetailView ( SingleObjectTemplateResponseMixin , BaseDetailView ) : <EOL> """<STR_LIT>""" </s>
