<s> from datetime import datetime , timedelta <EOL> from django . conf import settings <EOL> from django . contrib . contenttypes . models import ContentType <EOL> from django . db import models <EOL> from django . http import Http404 <EOL> from django . shortcuts import redirect , render <EOL> from django . template . defaultfilters import slugify <EOL> from django . template . response import TemplateResponse <EOL> from django . utils . translation import ugettext_lazy as _ <EOL> from django . views . generic . list import ListView <EOL> from ella . core . models import Listing , Category , Publishable , Author <EOL> from ella . core . cache import get_cached_object_or_404 , cache_this , get_cached_object <EOL> from ella . core import custom_urls <EOL> from ella . core . conf import core_settings <EOL> from ella . core . signals import object_rendering , object_rendered <EOL> from ella . api import render_as_api <EOL> from ella . utils . timezone import now , localize <EOL> __docformat__ = "<STR_LIT>" <EOL> CONTENT_TYPE_MAPPING = { } <EOL> class AuthorView ( ListView ) : <EOL> model = Publishable <EOL> context_object_name = '<STR_LIT>' <EOL> allow_empty = True <EOL> paginate_by = core_settings . CATEGORY_LISTINGS_PAGINATE_BY <EOL> template_name = '<STR_LIT>' <EOL> def get ( self , request , * args , ** kwargs ) : <EOL> if '<STR_LIT>' not in kwargs : <EOL> raise Http404 <EOL> if '<STR_LIT:p>' in request . GET : <EOL> self . kwargs . update ( { '<STR_LIT>' : request . GET [ '<STR_LIT:p>' ] } ) <EOL> self . author = get_cached_object_or_404 ( Author , slug = kwargs [ '<STR_LIT>' ] ) <EOL> response = render_as_api ( request , self . author ) <EOL> if response : <EOL> return response <EOL> return super ( AuthorView , self ) . get ( request , * args , ** kwargs ) <EOL> def get_queryset ( self ) : <EOL> return self . author . recently_published ( ) <EOL> def get_context_data ( self , ** kwargs ) : <EOL> base = super ( AuthorView , self ) . get_context_data ( ** kwargs ) <EOL> base . update ( { '<STR_LIT:object>' : self . author } ) <EOL> if base [ '<STR_LIT>' ] : <EOL> base . update ( { '<STR_LIT>' : base [ '<STR_LIT>' ] } ) <EOL> return base <EOL> class EllaCoreView ( object ) : <EOL> '<STR_LIT>' <EOL> template_name = '<STR_LIT>' <EOL> def get_context ( self , request , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( ) <EOL> def get_templates ( self , context , template_name = None ) : <EOL> "<STR_LIT>" <EOL> if not template_name : <EOL> template_name = self . template_name <EOL> kw = { } <EOL> if '<STR_LIT:object>' in context : <EOL> o = context [ '<STR_LIT:object>' ] <EOL> kw [ '<STR_LIT>' ] = o . slug <EOL> if context . get ( '<STR_LIT>' , False ) : <EOL> ct = context [ '<STR_LIT>' ] <EOL> kw [ '<STR_LIT>' ] = ct . app_label <EOL> kw [ '<STR_LIT>' ] = ct . model <EOL> return get_templates ( template_name , category = context [ '<STR_LIT>' ] , ** kw ) <EOL> def render ( self , request , context , template ) : <EOL> return TemplateResponse ( request , template , context ) <EOL> def __call__ ( self , request , ** kwargs ) : <EOL> context = self . get_context ( request , ** kwargs ) <EOL> return self . render ( request , context , self . get_templates ( context ) ) <EOL> class ObjectDetail ( EllaCoreView ) : <EOL> """<STR_LIT>""" <EOL> template_name = '<STR_LIT>' <EOL> class WrongUrl ( Http404 ) : pass <EOL> def __call__ ( self , request , category , slug , year = None , month = None , day = None , id = None , url_remainder = None ) : <EOL> try : <EOL> context = self . get_context ( request , category , slug , year , month , day , id ) <EOL> except self . WrongUrl , e : <EOL> message , obj = e . args <EOL> url = obj . get_absolute_url ( ) <EOL> if url_remainder : <EOL> url += url_remainder <EOL> return redirect ( url , permanent = True ) <EOL> obj = context [ '<STR_LIT:object>' ] <EOL> object_rendering . send ( sender = context [ '<STR_LIT:object>' ] . __class__ , request = request , category = context [ '<STR_LIT>' ] , publishable = context [ '<STR_LIT:object>' ] ) <EOL> if url_remainder : <EOL> return custom_urls . resolver . call_custom_view ( request , obj , url_remainder , context ) <EOL> response = render_as_api ( request , obj ) <EOL> if response : <EOL> return response <EOL> if custom_urls . resolver . has_custom_detail ( obj ) : <EOL> return custom_urls . resolver . call_custom_detail ( request , context ) <EOL> object_rendered . send ( sender = context [ '<STR_LIT:object>' ] . __class__ , request = request , category = context [ '<STR_LIT>' ] , publishable = context [ '<STR_LIT:object>' ] ) <EOL> return self . render ( request , context , self . get_templates ( context ) ) <EOL> def get_context ( self , request , category , slug , year , month , day , id ) : <EOL> try : <EOL> cat = Category . objects . get_by_tree_path ( category ) <EOL> except Category . DoesNotExist : <EOL> if year : <EOL> raise Http404 ( "<STR_LIT>" % category ) <EOL> else : <EOL> cat = None <EOL> if year : <EOL> start_date = localize ( datetime ( int ( year ) , int ( month ) , int ( day ) ) ) <EOL> end_date = start_date + timedelta ( days = <NUM_LIT:1> ) <EOL> lookup = { <EOL> '<STR_LIT>' : start_date , <EOL> '<STR_LIT>' : end_date , <EOL> '<STR_LIT>' : cat , <EOL> '<STR_LIT>' : slug , <EOL> '<STR_LIT>' : False <EOL> } <EOL> try : <EOL> publishable = get_cached_object ( Publishable , published = True , ** lookup ) <EOL> except Publishable . DoesNotExist : <EOL> if request . user . is_staff : <EOL> try : <EOL> p = Publishable . objects . filter ( published = False , ** lookup ) [ <NUM_LIT:0> ] <EOL> publishable = p . content_type . model_class ( ) . _default_manager . get ( pk = p . pk ) <EOL> except IndexError : <EOL> raise Http404 <EOL> else : <EOL> raise Http404 <EOL> else : <EOL> publishable = get_cached_object_or_404 ( Publishable , pk = id ) <EOL> if not ( publishable . is_published ( ) or request . user . is_staff ) : <EOL> raise Http404 <EOL> if not year : <EOL> if cat is None : <EOL> raise self . WrongUrl ( '<STR_LIT>' % category , publishable ) <EOL> elif not publishable . static : <EOL> raise self . WrongUrl ( '<STR_LIT>' % publishable , publishable ) <EOL> elif slug != publishable . slug : <EOL> raise self . WrongUrl ( '<STR_LIT>' % slug , publishable ) <EOL> elif publishable . category_id != cat . pk : <EOL> raise self . WrongUrl ( '<STR_LIT>' % publishable , publishable ) <EOL> publishable . category = cat <EOL> context = { <EOL> '<STR_LIT:object>' : publishable , <EOL> '<STR_LIT>' : cat , <EOL> '<STR_LIT>' : slugify ( publishable . content_type . model_class ( ) . _meta . verbose_name_plural ) , <EOL> '<STR_LIT>' : publishable . content_type <EOL> } <EOL> return context <EOL> def archive_year_cache_key ( self , category ) : <EOL> return '<STR_LIT>' % category . pk <EOL> class ListContentType ( EllaCoreView ) : <EOL> """<STR_LIT>""" <EOL> empty_homepage_template_name = '<STR_LIT>' <EOL> class EmptyHomepageException ( Exception ) : pass <EOL> def __call__ ( self , request , ** kwargs ) : <EOL> try : <EOL> cat = self . get_category ( request , kwargs . pop ( '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> except self . EmptyHomepageException : <EOL> return self . render ( request , { } , self . empty_homepage_template_name ) <EOL> object_rendering . send ( sender = Category , request = request , category = cat , publishable = None ) <EOL> object_rendered . send ( sender = Category , request = request , category = cat , publishable = None ) <EOL> response = render_as_api ( request , cat ) <EOL> if response : <EOL> return response <EOL> context = self . get_context ( request , cat , ** kwargs ) <EOL> if custom_urls . resolver . has_custom_detail ( cat ) : <EOL> context [ '<STR_LIT:object>' ] = cat <EOL> return custom_urls . resolver . call_custom_detail ( request , context ) <EOL> template_name = cat . template <EOL> archive_template = cat . app_data . ella . archive_template <EOL> if archive_template and not context . get ( '<STR_LIT>' ) : <EOL> template_name = archive_template <EOL> return self . render ( request , context , self . get_templates ( context , template_name ) ) <EOL> @ cache_this ( archive_year_cache_key , timeout = <NUM_LIT> * <NUM_LIT> * <NUM_LIT> ) <EOL> def _archive_entry_year ( self , category ) : <EOL> "<STR_LIT>" <EOL> year = getattr ( settings , '<STR_LIT>' , None ) <EOL> if not year : <EOL> n = now ( ) <EOL> try : <EOL> year = Listing . objects . filter ( <EOL> category__site__id = settings . SITE_ID , <EOL> category__tree_path__startswith = category . tree_path , <EOL> publish_from__lte = n <EOL> ) . values ( '<STR_LIT>' ) [ <NUM_LIT:0> ] [ '<STR_LIT>' ] . year <EOL> except : <EOL> year = n . year <EOL> return year <EOL> def get_category ( self , request , category_path ) : <EOL> try : <EOL> cat = Category . objects . get_by_tree_path ( category_path ) <EOL> except Category . DoesNotExist : <EOL> if settings . DEBUG is True and not category_path : <EOL> raise self . EmptyHomepageException ( ) <EOL> raise Http404 ( _ ( '<STR_LIT>' ) % <EOL> { '<STR_LIT:path>' : category_path , '<STR_LIT>' : settings . SITE_ID } ) <EOL> return cat <EOL> def get_context ( self , request , category , year = None , month = None , day = None ) : <EOL> ella_data = category . app_data . ella <EOL> no_home_listings = ella_data . no_home_listings <EOL> page_no = None <EOL> if '<STR_LIT:p>' in request . GET and request . GET [ '<STR_LIT:p>' ] . isdigit ( ) : <EOL> page_no = int ( request . GET [ '<STR_LIT:p>' ] ) <EOL> category_title_page = ( page_no is None or ( not no_home_listings and page_no == <NUM_LIT:1> ) ) and not year <EOL> if page_no is None : <EOL> page_no = <NUM_LIT:1> <EOL> kwa = { '<STR_LIT>' : ella_data . child_behavior } <EOL> if '<STR_LIT>' in request . GET : <EOL> kwa [ '<STR_LIT:source>' ] = request . GET [ '<STR_LIT>' ] <EOL> else : <EOL> kwa [ '<STR_LIT:source>' ] = ella_data . listing_handler <EOL> if day : <EOL> try : <EOL> start_day = datetime ( int ( year ) , int ( month ) , int ( day ) ) <EOL> kwa [ '<STR_LIT>' ] = ( start_day , start_day + timedelta ( seconds = <NUM_LIT> * <NUM_LIT> - <NUM_LIT:1> ) ) <EOL> except ( ValueError , OverflowError ) : <EOL> raise Http404 ( _ ( '<STR_LIT>' ) % day ) <EOL> elif month : <EOL> try : <EOL> start_day = datetime ( int ( year ) , int ( month ) , <NUM_LIT:1> ) <EOL> kwa [ '<STR_LIT>' ] = ( start_day , ( start_day + timedelta ( days = <NUM_LIT:32> ) ) . replace ( day = <NUM_LIT:1> ) - timedelta ( seconds = <NUM_LIT:1> ) ) <EOL> except ( ValueError , OverflowError ) : <EOL> raise Http404 ( _ ( '<STR_LIT>' ) % month ) <EOL> elif year : <EOL> try : <EOL> start_day = datetime ( int ( year ) , <NUM_LIT:1> , <NUM_LIT:1> ) <EOL> kwa [ '<STR_LIT>' ] = ( start_day , ( start_day + timedelta ( days = <NUM_LIT> ) ) . replace ( day = <NUM_LIT:1> ) - timedelta ( seconds = <NUM_LIT:1> ) ) <EOL> except ( ValueError , OverflowError ) : <EOL> raise Http404 ( _ ( '<STR_LIT>' ) % year ) <EOL> if '<STR_LIT>' in kwa : <EOL> kwa [ '<STR_LIT>' ] = tuple ( map ( localize , kwa [ '<STR_LIT>' ] ) ) <EOL> context = { <EOL> '<STR_LIT>' : category , <EOL> '<STR_LIT>' : category_title_page and not category . tree_parent_id , <EOL> '<STR_LIT>' : category_title_page , <EOL> '<STR_LIT>' : lambda : self . _archive_entry_year ( category ) , <EOL> } <EOL> if category_title_page and no_home_listings : <EOL> return context <EOL> page = ella_data . get_listings_page ( page_no , ** kwa ) <EOL> context . update ( { <EOL> '<STR_LIT>' : page . has_other_pages ( ) , <EOL> '<STR_LIT>' : page . paginator . per_page , <EOL> '<STR_LIT>' : page , <EOL> '<STR_LIT>' : page . object_list , <EOL> } ) <EOL> return context <EOL> object_detail = ObjectDetail ( ) <EOL> home = category_detail = list_content_type = ListContentType ( ) <EOL> def get_content_type ( ct_name ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> ct = CONTENT_TYPE_MAPPING [ ct_name ] <EOL> except KeyError : <EOL> for model in models . get_models ( ) : <EOL> if ct_name == slugify ( model . _meta . verbose_name_plural ) : <EOL> ct = ContentType . objects . get_for_model ( model ) <EOL> CONTENT_TYPE_MAPPING [ ct_name ] = ct <EOL> break <EOL> else : <EOL> raise Http404 <EOL> return ct <EOL> def get_templates ( name , slug = None , category = None , app_label = None , model_label = None ) : <EOL> """<STR_LIT>""" <EOL> def category_templates ( category , incomplete_template , params ) : <EOL> paths = [ ] <EOL> parts = category . path . split ( '<STR_LIT:/>' ) <EOL> for i in reversed ( range ( <NUM_LIT:1> , len ( parts ) + <NUM_LIT:1> ) ) : <EOL> params . update ( { '<STR_LIT>' : '<STR_LIT:/>' . join ( parts [ : i ] ) } ) <EOL> paths . append ( incomplete_template % params ) <EOL> return paths <EOL> FULL = '<STR_LIT>' <EOL> FULL_NO_SLUG = '<STR_LIT>' <EOL> BY_CATEGORY = '<STR_LIT>' <EOL> BY_CONTENT_TYPE = '<STR_LIT>' <EOL> templates = [ ] <EOL> params = { '<STR_LIT:name>' : name } <EOL> if app_label and model_label : <EOL> params . update ( { '<STR_LIT>' : app_label , '<STR_LIT>' : model_label } ) <EOL> if slug : <EOL> params . update ( { '<STR_LIT>' : slug } ) <EOL> if category : <EOL> if app_label and model_label : <EOL> if slug : <EOL> templates += category_templates ( category , FULL , params ) <EOL> templates += category_templates ( category , FULL_NO_SLUG , params ) <EOL> templates += category_templates ( category , BY_CATEGORY , params ) <EOL> if app_label and model_label : <EOL> templates . append ( BY_CONTENT_TYPE % params ) <EOL> templates . append ( '<STR_LIT>' % params ) <EOL> return templates <EOL> def get_templates_from_publishable ( name , publishable ) : <EOL> """<STR_LIT>""" <EOL> slug = publishable . slug <EOL> category = publishable . category <EOL> app_label = publishable . content_type . app_label <EOL> model_label = publishable . content_type . model <EOL> return get_templates ( name , slug , category , app_label , model_label ) <EOL> def get_export_key ( request , count , name = '<STR_LIT>' , content_type = None ) : <EOL> return '<STR_LIT>' % ( <EOL> settings . SITE_ID , count , name , content_type <EOL> ) <EOL> @ cache_this ( get_export_key , timeout = core_settings . CACHE_TIMEOUT_LONG ) <EOL> def export ( request , count , name = '<STR_LIT>' , content_type = None ) : <EOL> """<STR_LIT>""" <EOL> t_list = [ ] <EOL> if name : <EOL> t_list . append ( '<STR_LIT>' % name ) <EOL> t_list . append ( '<STR_LIT>' ) <EOL> try : <EOL> cat = Category . objects . get_by_tree_path ( '<STR_LIT>' ) <EOL> except Category . DoesNotExist : <EOL> raise Http404 ( ) <EOL> listing = Listing . objects . get_listing ( count = count , category = cat ) <EOL> return render ( <EOL> request , <EOL> t_list , <EOL> { '<STR_LIT>' : cat , '<STR_LIT>' : listing } , <EOL> content_type = content_type <EOL> ) <EOL> def page_not_found ( request ) : <EOL> response = TemplateResponse ( request , '<STR_LIT>' , { } ) <EOL> response . status_code = <NUM_LIT> <EOL> return response . render ( ) <EOL> def handle_error ( request ) : <EOL> response = TemplateResponse ( request , '<STR_LIT>' , { } ) <EOL> response . status_code = <NUM_LIT> <EOL> return response . render ( ) </s>
