<s> import json <EOL> from django import http <EOL> from django . conf import settings <EOL> from django . utils . encoding import force_unicode <EOL> from django . core . exceptions import ImproperlyConfigured <EOL> from django . utils . translation import ugettext_lazy as _ <EOL> from . utils import get_page_model <EOL> from . defaults import FP_HOMEPAGE_NAME <EOL> from . utils import unicode_slugify as slugify <EOL> class FancyPageMixin ( object ) : <EOL> object_attr_name = '<STR_LIT:object>' <EOL> pk_url_kwarg = '<STR_LIT>' <EOL> slug_url_kwarg = '<STR_LIT>' <EOL> page_attr_name = '<STR_LIT>' <EOL> node_attr_name = '<STR_LIT>' <EOL> def get_object ( self ) : <EOL> FancyPage = get_page_model ( ) <EOL> try : <EOL> page = FancyPage . objects . select_related ( '<STR_LIT>' ) . get ( <EOL> node__slug = self . kwargs . get ( self . slug_url_kwarg ) ) <EOL> except FancyPage . DoesNotExist : <EOL> raise http . Http404 ( ) <EOL> return page <EOL> def get_template_names ( self ) : <EOL> page = getattr ( self , self . page_attr_name ) <EOL> if not page . page_type : <EOL> return [ settings . FP_DEFAULT_TEMPLATE ] <EOL> return [ page . page_type . template_name ] <EOL> def get ( self , request , * args , ** kwargs ) : <EOL> page = self . get_object ( ) <EOL> if not request . user . is_staff : <EOL> if not page . is_visible : <EOL> raise http . Http404 ( ) <EOL> setattr ( self , self . page_attr_name , page ) <EOL> setattr ( self , self . node_attr_name , page . node ) <EOL> return super ( FancyPageMixin , self ) . get ( request , * args , ** kwargs ) <EOL> class FancyHomeMixin ( FancyPageMixin ) : <EOL> HOMEPAGE_NAME = getattr ( settings , '<STR_LIT>' , FP_HOMEPAGE_NAME ) <EOL> def get_object ( self ) : <EOL> FancyPage = get_page_model ( ) <EOL> slug = slugify ( self . HOMEPAGE_NAME ) <EOL> try : <EOL> page = FancyPage . objects . get ( node__slug = slug ) <EOL> except FancyPage . DoesNotExist : <EOL> page = FancyPage . add_root ( <EOL> node__name = self . HOMEPAGE_NAME , node__slug = slug , <EOL> status = FancyPage . PUBLISHED ) <EOL> return page <EOL> class TemplateNamesModelMixin ( object ) : <EOL> """<STR_LIT>""" <EOL> template_name = None <EOL> default_template_names = [ ] <EOL> def get_template_names ( self , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if not self . template_name and not self . default_template_names : <EOL> raise ImproperlyConfigured ( <EOL> _ ( "<STR_LIT>" ) ) <EOL> default_names = list ( self . default_template_names ) <EOL> template_kwargs = { '<STR_LIT>' : self . _meta . module_name } <EOL> template_kwargs . update ( kwargs ) <EOL> if self . template_name : <EOL> default_names = [ self . template_name ] + default_names <EOL> language_code = getattr ( self , '<STR_LIT>' , None ) <EOL> try : <EOL> parent_code , __ = language_code . split ( '<STR_LIT:->' ) <EOL> except ( AttributeError , ValueError ) : <EOL> parent_code = None <EOL> template_names = [ ] <EOL> for tmpl_name in default_names : <EOL> try : <EOL> tmpl_name = tmpl_name . format ( ** template_kwargs ) <EOL> except KeyError : <EOL> pass <EOL> basename , ext = tmpl_name . rsplit ( '<STR_LIT:.>' , <NUM_LIT:1> ) <EOL> if language_code : <EOL> template_names . append ( <EOL> "<STR_LIT>" . format ( basename , language_code , ext ) ) <EOL> if parent_code : <EOL> template_names . append ( <EOL> "<STR_LIT>" . format ( basename , parent_code , ext ) ) <EOL> template_names . append ( tmpl_name ) <EOL> return template_names <EOL> class JSONResponseMixin ( object ) : <EOL> json_filename = '<STR_LIT>' <EOL> def get_content_type ( self ) : <EOL> http_accept = self . request . META . get ( '<STR_LIT>' , [ ] ) <EOL> if "<STR_LIT:application/json>" in http_accept : <EOL> return "<STR_LIT:application/json>" <EOL> else : <EOL> return "<STR_LIT>" <EOL> def render_to_response ( self , context ) : <EOL> "<STR_LIT>" <EOL> if '<STR_LIT:success>' not in context : <EOL> context [ '<STR_LIT:success>' ] = True <EOL> return self . get_json_response ( self . convert_context_to_json ( context ) ) <EOL> def render_to_error_response ( self , reason , context = None ) : <EOL> if context is None : <EOL> context = { } <EOL> context [ '<STR_LIT:success>' ] = False <EOL> context [ '<STR_LIT>' ] = force_unicode ( reason ) <EOL> return self . render_to_response ( context ) <EOL> def get_json_response ( self , content , ** httpresponse_kwargs ) : <EOL> "<STR_LIT>" <EOL> response = http . HttpResponse ( <EOL> content , <EOL> content_type = self . get_content_type ( ) , <EOL> ** httpresponse_kwargs <EOL> ) <EOL> response [ '<STR_LIT>' ] = '<STR_LIT>' . format ( <EOL> self . json_filename <EOL> ) <EOL> return response <EOL> def convert_context_to_json ( self , context ) : <EOL> "<STR_LIT>" <EOL> return json . dumps ( context ) </s>
