<s> from django . shortcuts import get_object_or_404 <EOL> from django . core . exceptions import ObjectDoesNotExist <EOL> from django . template import RequestContext <EOL> from django . http import HttpResponse , Http404 <EOL> from django . template . loader import select_template <EOL> from django . utils . translation import ugettext_lazy as _ <EOL> try : <EOL> from django . views . generic import DetailView , ListView <EOL> except ImportError : <EOL> try : <EOL> from cbv import DetailView , ListView <EOL> except ImportError : <EOL> from django . core . exceptions import ImproperlyConfigured <EOL> raise ImproperlyConfigured ( "<STR_LIT>" ) <EOL> from . models import Category <EOL> def category_detail ( request , path , template_name = '<STR_LIT>' , extra_context = { } ) : <EOL> path_items = path . strip ( '<STR_LIT:/>' ) . split ( '<STR_LIT:/>' ) <EOL> if len ( path_items ) >= <NUM_LIT:2> : <EOL> category = get_object_or_404 ( <EOL> Category , <EOL> slug__iexact = path_items [ - <NUM_LIT:1> ] , <EOL> level = len ( path_items ) - <NUM_LIT:1> , <EOL> parent__slug__iexact = path_items [ - <NUM_LIT:2> ] ) <EOL> else : <EOL> category = get_object_or_404 ( <EOL> Category , <EOL> slug__iexact = path_items [ - <NUM_LIT:1> ] , <EOL> level = len ( path_items ) - <NUM_LIT:1> ) <EOL> templates = [ ] <EOL> while path_items : <EOL> templates . append ( '<STR_LIT>' % '<STR_LIT:_>' . join ( path_items ) ) <EOL> path_items . pop ( ) <EOL> templates . append ( template_name ) <EOL> context = RequestContext ( request ) <EOL> context . update ( { '<STR_LIT>' : category } ) <EOL> if extra_context : <EOL> context . update ( extra_context ) <EOL> return HttpResponse ( select_template ( templates ) . render ( context ) ) <EOL> def get_category_for_path ( path , queryset = Category . objects . all ( ) ) : <EOL> path_items = path . strip ( '<STR_LIT:/>' ) . split ( '<STR_LIT:/>' ) <EOL> if len ( path_items ) >= <NUM_LIT:2> : <EOL> queryset = queryset . filter ( <EOL> slug__iexact = path_items [ - <NUM_LIT:1> ] , <EOL> level = len ( path_items ) - <NUM_LIT:1> , <EOL> parent__slug__iexact = path_items [ - <NUM_LIT:2> ] ) <EOL> else : <EOL> queryset = queryset . filter ( <EOL> slug__iexact = path_items [ - <NUM_LIT:1> ] , <EOL> level = len ( path_items ) - <NUM_LIT:1> ) <EOL> return queryset . get ( ) <EOL> class CategoryDetailView ( DetailView ) : <EOL> model = Category <EOL> path_field = '<STR_LIT:path>' <EOL> def get_object ( self , ** kwargs ) : <EOL> if self . path_field not in self . kwargs : <EOL> raise AttributeError ( "<STR_LIT>" <EOL> "<STR_LIT>" % self . __class__ . __name__ , self . path_field ) <EOL> if self . queryset is None : <EOL> queryset = self . get_queryset ( ) <EOL> try : <EOL> return get_category_for_path ( self . kwargs [ self . path_field ] , self . model . objects . all ( ) ) <EOL> except ObjectDoesNotExist : <EOL> raise Http404 ( _ ( "<STR_LIT>" ) % <EOL> { '<STR_LIT>' : queryset . model . _meta . verbose_name } ) <EOL> def get_template_names ( self ) : <EOL> names = [ ] <EOL> path_items = self . kwargs [ self . path_field ] . strip ( '<STR_LIT:/>' ) . split ( '<STR_LIT:/>' ) <EOL> while path_items : <EOL> names . append ( '<STR_LIT>' % '<STR_LIT:_>' . join ( path_items ) ) <EOL> path_items . pop ( ) <EOL> names . extend ( super ( CategoryDetailView , self ) . get_template_names ( ) ) <EOL> return names <EOL> class CategoryRelatedDetail ( DetailView ) : <EOL> path_field = '<STR_LIT>' <EOL> object_name_field = None <EOL> def get_object ( self , ** kwargs ) : <EOL> queryset = super ( CategoryRelatedDetail , self ) . get_queryset ( ) <EOL> category = get_category_for_path ( self . kwargs [ self . path_field ] ) <EOL> return queryset . get ( category = category , slug = self . kwargs [ self . slug_field ] ) <EOL> def get_template_names ( self ) : <EOL> names = [ ] <EOL> opts = self . object . _meta <EOL> path_items = self . kwargs [ self . path_field ] . strip ( '<STR_LIT:/>' ) . split ( '<STR_LIT:/>' ) <EOL> if self . object_name_field : <EOL> path_items . append ( getattr ( self . object , self . object_name_field ) ) <EOL> while path_items : <EOL> names . append ( '<STR_LIT>' % ( <EOL> opts . app_label , <EOL> '<STR_LIT:_>' . join ( path_items ) , <EOL> opts . object_name . lower ( ) , <EOL> self . template_name_suffix ) <EOL> ) <EOL> path_items . pop ( ) <EOL> names . append ( '<STR_LIT>' % ( <EOL> opts . app_label , <EOL> opts . object_name . lower ( ) , <EOL> self . template_name_suffix ) <EOL> ) <EOL> names . extend ( super ( CategoryRelatedDetail , self ) . get_template_names ( ) ) <EOL> return names <EOL> class CategoryRelatedList ( ListView ) : <EOL> path_field = '<STR_LIT>' <EOL> def get_queryset ( self ) : <EOL> queryset = super ( CategoryRelatedList , self ) . get_queryset ( ) <EOL> category = get_category_for_path ( self . kwargs [ '<STR_LIT>' ] ) <EOL> return queryset . filter ( category = category ) <EOL> def get_template_names ( self ) : <EOL> names = [ ] <EOL> if hasattr ( self . object_list , '<STR_LIT>' ) : <EOL> opts = self . object_list . model . _meta <EOL> path_items = self . kwargs [ self . path_field ] . strip ( '<STR_LIT:/>' ) . split ( '<STR_LIT:/>' ) <EOL> while path_items : <EOL> names . append ( '<STR_LIT>' % ( <EOL> opts . app_label , <EOL> '<STR_LIT:_>' . join ( path_items ) , <EOL> opts . object_name . lower ( ) , <EOL> self . template_name_suffix ) <EOL> ) <EOL> path_items . pop ( ) <EOL> names . append ( '<STR_LIT>' % ( <EOL> opts . app_label , <EOL> opts . object_name . lower ( ) , <EOL> self . template_name_suffix ) <EOL> ) <EOL> names . extend ( super ( CategoryRelatedList , self ) . get_template_names ( ) ) <EOL> return names </s>
