<s> from __future__ import unicode_literals <EOL> from datetime import datetime , date <EOL> from dateutil . relativedelta import relativedelta <EOL> from django . db . models import Q <EOL> from django . http import ( <EOL> Http404 , <EOL> HttpResponseRedirect , <EOL> HttpResponsePermanentRedirect , <EOL> ) <EOL> from django . shortcuts import get_object_or_404 <EOL> from django . utils import translation <EOL> from django . views . generic import ListView <EOL> from django . views . generic . detail import DetailView <EOL> from menus . utils import set_language_changer <EOL> from parler . views import TranslatableSlugMixin , ViewUrlMixin <EOL> from taggit . models import Tag <EOL> from aldryn_apphooks_config . mixins import AppConfigMixin <EOL> from aldryn_categories . models import Category <EOL> from aldryn_people . models import Person <EOL> from aldryn_newsblog . utils . utilities import get_valid_languages_from_request <EOL> from . models import Article <EOL> from . utils import add_prefix_to_path <EOL> class TemplatePrefixMixin ( object ) : <EOL> def prefix_template_names ( self , template_names ) : <EOL> if ( hasattr ( self . config , '<STR_LIT>' ) and <EOL> self . config . template_prefix ) : <EOL> prefix = self . config . template_prefix <EOL> return [ <EOL> add_prefix_to_path ( template , prefix ) <EOL> for template in template_names ] <EOL> return template_names <EOL> def get_template_names ( self ) : <EOL> template_names = super ( TemplatePrefixMixin , self ) . get_template_names ( ) <EOL> return self . prefix_template_names ( template_names ) <EOL> class EditModeMixin ( object ) : <EOL> """<STR_LIT>""" <EOL> edit_mode = False <EOL> def dispatch ( self , request , * args , ** kwargs ) : <EOL> self . edit_mode = ( <EOL> self . request . toolbar and self . request . toolbar . edit_mode ) <EOL> return super ( EditModeMixin , self ) . dispatch ( request , * args , ** kwargs ) <EOL> class PreviewModeMixin ( EditModeMixin ) : <EOL> """<STR_LIT>""" <EOL> def get_queryset ( self ) : <EOL> qs = super ( PreviewModeMixin , self ) . get_queryset ( ) <EOL> user = self . request . user <EOL> user_can_edit = user . is_staff or user . is_superuser <EOL> if not ( self . edit_mode or user_can_edit ) : <EOL> qs = qs . published ( ) <EOL> language = translation . get_language ( ) <EOL> qs = qs . active_translations ( language ) . namespace ( self . namespace ) <EOL> return qs <EOL> class AppHookCheckMixin ( object ) : <EOL> def dispatch ( self , request , * args , ** kwargs ) : <EOL> self . valid_languages = get_valid_languages_from_request ( <EOL> self . namespace , request ) <EOL> return super ( AppHookCheckMixin , self ) . dispatch ( <EOL> request , * args , ** kwargs ) <EOL> def get_queryset ( self ) : <EOL> qs = super ( AppHookCheckMixin , self ) . get_queryset ( ) <EOL> return qs . translated ( * self . valid_languages ) <EOL> class ArticleDetail ( AppConfigMixin , AppHookCheckMixin , PreviewModeMixin , <EOL> TranslatableSlugMixin , TemplatePrefixMixin , DetailView ) : <EOL> model = Article <EOL> slug_field = '<STR_LIT>' <EOL> year_url_kwarg = '<STR_LIT>' <EOL> month_url_kwarg = '<STR_LIT>' <EOL> day_url_kwarg = '<STR_LIT>' <EOL> slug_url_kwarg = '<STR_LIT>' <EOL> pk_url_kwarg = '<STR_LIT>' <EOL> def get ( self , request , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if not hasattr ( self , '<STR_LIT:object>' ) : <EOL> self . object = self . get_object ( ) <EOL> set_language_changer ( request , self . object . get_absolute_url ) <EOL> url = self . object . get_absolute_url ( ) <EOL> if ( self . config . non_permalink_handling == <NUM_LIT:200> or request . path == url ) : <EOL> return super ( ArticleDetail , self ) . get ( request , * args , ** kwargs ) <EOL> if self . config . non_permalink_handling == <NUM_LIT> : <EOL> return HttpResponseRedirect ( url ) <EOL> elif self . config . non_permalink_handling == <NUM_LIT> : <EOL> return HttpResponsePermanentRedirect ( url ) <EOL> else : <EOL> raise Http404 ( '<STR_LIT>' ) <EOL> def get_object ( self , queryset = None ) : <EOL> """<STR_LIT>""" <EOL> if queryset is None : <EOL> queryset = self . get_queryset ( ) <EOL> slug = self . kwargs . get ( self . slug_url_kwarg , None ) <EOL> pk = self . kwargs . get ( self . pk_url_kwarg , None ) <EOL> if pk is not None : <EOL> return DetailView . get_object ( self , queryset = queryset ) <EOL> elif slug is not None : <EOL> return super ( ArticleDetail , self ) . get_object ( queryset = queryset ) <EOL> raise AttributeError ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> def get_context_data ( self , ** kwargs ) : <EOL> context = super ( ArticleDetail , self ) . get_context_data ( ** kwargs ) <EOL> context [ '<STR_LIT>' ] = self . get_prev_object ( <EOL> self . queryset , self . object ) <EOL> context [ '<STR_LIT>' ] = self . get_next_object ( <EOL> self . queryset , self . object ) <EOL> return context <EOL> def get_prev_object ( self , queryset = None , object = None ) : <EOL> if queryset is None : <EOL> queryset = self . get_queryset ( ) <EOL> if object is None : <EOL> object = self . get_object ( self ) <EOL> prev_objs = queryset . filter ( <EOL> publishing_date__lt = object . publishing_date <EOL> ) . order_by ( <EOL> '<STR_LIT>' <EOL> ) [ : <NUM_LIT:1> ] <EOL> if prev_objs : <EOL> return prev_objs [ <NUM_LIT:0> ] <EOL> else : <EOL> return None <EOL> def get_next_object ( self , queryset = None , object = None ) : <EOL> if queryset is None : <EOL> queryset = self . get_queryset ( ) <EOL> if object is None : <EOL> object = self . get_object ( self ) <EOL> next_objs = queryset . filter ( <EOL> publishing_date__gt = object . publishing_date <EOL> ) . order_by ( <EOL> '<STR_LIT>' <EOL> ) [ : <NUM_LIT:1> ] <EOL> if next_objs : <EOL> return next_objs [ <NUM_LIT:0> ] <EOL> else : <EOL> return None <EOL> class ArticleListBase ( AppConfigMixin , AppHookCheckMixin , TemplatePrefixMixin , <EOL> PreviewModeMixin , ViewUrlMixin , ListView ) : <EOL> model = Article <EOL> show_header = False <EOL> def get_paginate_by ( self , queryset ) : <EOL> if self . paginate_by is not None : <EOL> return self . paginate_by <EOL> else : <EOL> try : <EOL> return self . config . paginate_by <EOL> except AttributeError : <EOL> return <NUM_LIT:10> <EOL> def get_pagination_options ( self ) : <EOL> if self . config : <EOL> options = { <EOL> '<STR_LIT>' : self . config . pagination_pages_start , <EOL> '<STR_LIT>' : self . config . pagination_pages_visible , <EOL> } <EOL> else : <EOL> options = { <EOL> '<STR_LIT>' : <NUM_LIT:10> , <EOL> '<STR_LIT>' : <NUM_LIT:4> , <EOL> } <EOL> pages_visible_negative = - options [ '<STR_LIT>' ] <EOL> options [ '<STR_LIT>' ] = pages_visible_negative <EOL> options [ '<STR_LIT>' ] = options [ '<STR_LIT>' ] + <NUM_LIT:1> <EOL> options [ '<STR_LIT>' ] = pages_visible_negative - <NUM_LIT:1> <EOL> return options <EOL> def get_context_data ( self , ** kwargs ) : <EOL> context = super ( ArticleListBase , self ) . get_context_data ( ** kwargs ) <EOL> context [ '<STR_LIT>' ] = self . get_pagination_options ( ) <EOL> return context <EOL> class ArticleList ( ArticleListBase ) : <EOL> """<STR_LIT>""" <EOL> show_header = True <EOL> def get_queryset ( self ) : <EOL> qs = super ( ArticleList , self ) . get_queryset ( ) <EOL> exclude_count = self . config . exclude_featured <EOL> if exclude_count : <EOL> featured_qs = Article . objects . all ( ) . filter ( is_featured = True ) <EOL> if not self . edit_mode : <EOL> featured_qs = featured_qs . published ( ) <EOL> exclude_featured = featured_qs [ : exclude_count ] . values_list ( '<STR_LIT>' ) <EOL> qs = qs . exclude ( pk__in = exclude_featured ) <EOL> return qs <EOL> class ArticleSearchResultsList ( ArticleListBase ) : <EOL> model = Article <EOL> http_method_names = [ '<STR_LIT>' , '<STR_LIT>' , ] <EOL> partial_name = '<STR_LIT>' <EOL> template_name = '<STR_LIT>' <EOL> def get ( self , request , * args , ** kwargs ) : <EOL> self . query = request . GET . get ( '<STR_LIT:q>' ) <EOL> self . max_articles = request . GET . get ( '<STR_LIT>' , <NUM_LIT:0> ) <EOL> self . edit_mode = ( request . toolbar and request . toolbar . edit_mode ) <EOL> return super ( ArticleSearchResultsList , self ) . get ( request ) <EOL> def get_paginate_by ( self , queryset ) : <EOL> """<STR_LIT>""" <EOL> return self . max_articles or super ( <EOL> ArticleSearchResultsList , self ) . get_paginate_by ( self . get_queryset ( ) ) <EOL> def get_queryset ( self ) : <EOL> qs = super ( ArticleSearchResultsList , self ) . get_queryset ( ) <EOL> if not self . edit_mode : <EOL> qs = qs . published ( ) <EOL> if self . query : <EOL> return qs . filter ( <EOL> Q ( translations__title__icontains = self . query ) | <EOL> Q ( translations__lead_in__icontains = self . query ) | <EOL> Q ( translations__search_data__icontains = self . query ) <EOL> ) . distinct ( ) <EOL> else : <EOL> return qs . none ( ) <EOL> def get_context_data ( self , ** kwargs ) : <EOL> cxt = super ( ArticleSearchResultsList , self ) . get_context_data ( ** kwargs ) <EOL> cxt [ '<STR_LIT>' ] = self . query <EOL> return cxt <EOL> def get_template_names ( self ) : <EOL> if self . request . is_ajax : <EOL> template_names = [ self . partial_name ] <EOL> else : <EOL> template_names = [ self . template_name ] <EOL> return self . prefix_template_names ( template_names ) <EOL> class AuthorArticleList ( ArticleListBase ) : <EOL> """<STR_LIT>""" <EOL> def get_queryset ( self ) : <EOL> return super ( AuthorArticleList , self ) . get_queryset ( ) . filter ( <EOL> author = self . author <EOL> ) <EOL> def get ( self , request , author ) : <EOL> language = translation . get_language_from_request ( <EOL> request , check_path = True ) <EOL> self . author = Person . objects . language ( language ) . active_translations ( <EOL> language , slug = author ) . first ( ) <EOL> if not self . author : <EOL> raise Http404 ( '<STR_LIT>' ) <EOL> return super ( AuthorArticleList , self ) . get ( request ) <EOL> def get_context_data ( self , ** kwargs ) : <EOL> kwargs [ '<STR_LIT>' ] = self . author <EOL> return super ( AuthorArticleList , self ) . get_context_data ( ** kwargs ) <EOL> class CategoryArticleList ( ArticleListBase ) : <EOL> """<STR_LIT>""" <EOL> def get_queryset ( self ) : <EOL> return super ( CategoryArticleList , self ) . get_queryset ( ) . filter ( <EOL> categories = self . category <EOL> ) <EOL> def get ( self , request , category ) : <EOL> self . category = get_object_or_404 ( <EOL> Category , translations__slug = category ) <EOL> return super ( CategoryArticleList , self ) . get ( request ) <EOL> def get_context_data ( self , ** kwargs ) : <EOL> kwargs [ '<STR_LIT>' ] = self . category <EOL> ctx = super ( CategoryArticleList , self ) . get_context_data ( ** kwargs ) <EOL> ctx [ '<STR_LIT>' ] = self . category <EOL> return ctx <EOL> class TagArticleList ( ArticleListBase ) : <EOL> """<STR_LIT>""" <EOL> def get_queryset ( self ) : <EOL> return super ( TagArticleList , self ) . get_queryset ( ) . filter ( <EOL> tags = self . tag <EOL> ) <EOL> def get ( self , request , tag ) : <EOL> self . tag = get_object_or_404 ( Tag , slug = tag ) <EOL> return super ( TagArticleList , self ) . get ( request ) <EOL> def get_context_data ( self , ** kwargs ) : <EOL> kwargs [ '<STR_LIT>' ] = self . tag <EOL> return super ( TagArticleList , self ) . get_context_data ( ** kwargs ) <EOL> class DateRangeArticleList ( ArticleListBase ) : <EOL> """<STR_LIT>""" <EOL> def get_queryset ( self ) : <EOL> return super ( DateRangeArticleList , self ) . get_queryset ( ) . filter ( <EOL> publishing_date__gte = self . date_from , <EOL> publishing_date__lt = self . date_to <EOL> ) <EOL> def _daterange_from_kwargs ( self , kwargs ) : <EOL> raise NotImplemented ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> def get ( self , request , ** kwargs ) : <EOL> self . date_from , self . date_to = self . _daterange_from_kwargs ( kwargs ) <EOL> return super ( DateRangeArticleList , self ) . get ( request ) <EOL> def get_context_data ( self , ** kwargs ) : <EOL> kwargs [ '<STR_LIT>' ] = ( <EOL> int ( self . kwargs . get ( '<STR_LIT>' ) ) if '<STR_LIT>' in self . kwargs else None ) <EOL> kwargs [ '<STR_LIT>' ] = ( <EOL> int ( self . kwargs . get ( '<STR_LIT>' ) ) if '<STR_LIT>' in self . kwargs else None ) <EOL> kwargs [ '<STR_LIT>' ] = ( <EOL> int ( self . kwargs . get ( '<STR_LIT>' ) ) if '<STR_LIT>' in self . kwargs else None ) <EOL> if kwargs [ '<STR_LIT>' ] : <EOL> kwargs [ '<STR_LIT>' ] = date ( <EOL> kwargs [ '<STR_LIT>' ] , <EOL> kwargs [ '<STR_LIT>' ] or <NUM_LIT:1> , <EOL> kwargs [ '<STR_LIT>' ] or <NUM_LIT:1> ) <EOL> return super ( DateRangeArticleList , self ) . get_context_data ( ** kwargs ) <EOL> class YearArticleList ( DateRangeArticleList ) : <EOL> def _daterange_from_kwargs ( self , kwargs ) : <EOL> date_from = datetime ( int ( kwargs [ '<STR_LIT>' ] ) , <NUM_LIT:1> , <NUM_LIT:1> ) <EOL> date_to = date_from + relativedelta ( years = <NUM_LIT:1> ) <EOL> return date_from , date_to <EOL> class MonthArticleList ( DateRangeArticleList ) : <EOL> def _daterange_from_kwargs ( self , kwargs ) : <EOL> date_from = datetime ( int ( kwargs [ '<STR_LIT>' ] ) , int ( kwargs [ '<STR_LIT>' ] ) , <NUM_LIT:1> ) <EOL> date_to = date_from + relativedelta ( months = <NUM_LIT:1> ) <EOL> return date_from , date_to <EOL> class DayArticleList ( DateRangeArticleList ) : <EOL> def _daterange_from_kwargs ( self , kwargs ) : <EOL> date_from = datetime ( <EOL> int ( kwargs [ '<STR_LIT>' ] ) , int ( kwargs [ '<STR_LIT>' ] ) , int ( kwargs [ '<STR_LIT>' ] ) ) <EOL> date_to = date_from + relativedelta ( days = <NUM_LIT:1> ) <EOL> return date_from , date_to </s>
