<s> from __future__ import unicode_literals <EOL> from calendar import timegm <EOL> from django . conf import settings <EOL> from django . contrib . sites . models import get_current_site <EOL> from django . core . exceptions import ImproperlyConfigured , ObjectDoesNotExist <EOL> from django . http import HttpResponse , Http404 <EOL> from django . template import loader , TemplateDoesNotExist , RequestContext <EOL> from django . utils import feedgenerator , tzinfo <EOL> from django . utils . encoding import force_text , iri_to_uri , smart_text <EOL> from django . utils . html import escape <EOL> from django . utils . http import http_date <EOL> from django . utils . timezone import is_naive <EOL> def add_domain ( domain , url , secure = False ) : <EOL> protocol = '<STR_LIT>' if secure else '<STR_LIT:http>' <EOL> if url . startswith ( '<STR_LIT>' ) : <EOL> url = '<STR_LIT>' % ( protocol , url ) <EOL> elif not ( url . startswith ( '<STR_LIT>' ) <EOL> or url . startswith ( '<STR_LIT>' ) <EOL> or url . startswith ( '<STR_LIT>' ) ) : <EOL> url = iri_to_uri ( '<STR_LIT>' % ( protocol , domain , url ) ) <EOL> return url <EOL> class FeedDoesNotExist ( ObjectDoesNotExist ) : <EOL> pass <EOL> class Feed ( object ) : <EOL> feed_type = feedgenerator . DefaultFeed <EOL> title_template = None <EOL> description_template = None <EOL> def __call__ ( self , request , * args , ** kwargs ) : <EOL> try : <EOL> obj = self . get_object ( request , * args , ** kwargs ) <EOL> except ObjectDoesNotExist : <EOL> raise Http404 ( '<STR_LIT>' ) <EOL> feedgen = self . get_feed ( obj , request ) <EOL> response = HttpResponse ( content_type = feedgen . mime_type ) <EOL> if hasattr ( self , '<STR_LIT>' ) : <EOL> response [ '<STR_LIT>' ] = http_date ( <EOL> timegm ( feedgen . latest_post_date ( ) . utctimetuple ( ) ) ) <EOL> feedgen . write ( response , '<STR_LIT:utf-8>' ) <EOL> return response <EOL> def item_title ( self , item ) : <EOL> return escape ( force_text ( item ) ) <EOL> def item_description ( self , item ) : <EOL> return force_text ( item ) <EOL> def item_link ( self , item ) : <EOL> try : <EOL> return item . get_absolute_url ( ) <EOL> except AttributeError : <EOL> raise ImproperlyConfigured ( '<STR_LIT>' % item . __class__ . __name__ ) <EOL> def __get_dynamic_attr ( self , attname , obj , default = None ) : <EOL> try : <EOL> attr = getattr ( self , attname ) <EOL> except AttributeError : <EOL> return default <EOL> if callable ( attr ) : <EOL> if hasattr ( attr , '<STR_LIT>' ) : <EOL> argcount = attr . __code__ . co_argcount <EOL> else : <EOL> argcount = attr . __call__ . __code__ . co_argcount <EOL> if argcount == <NUM_LIT:2> : <EOL> return attr ( obj ) <EOL> else : <EOL> return attr ( ) <EOL> return attr <EOL> def feed_extra_kwargs ( self , obj ) : <EOL> """<STR_LIT>""" <EOL> return { } <EOL> def item_extra_kwargs ( self , item ) : <EOL> """<STR_LIT>""" <EOL> return { } <EOL> def get_object ( self , request , * args , ** kwargs ) : <EOL> return None <EOL> def get_feed ( self , obj , request ) : <EOL> """<STR_LIT>""" <EOL> current_site = get_current_site ( request ) <EOL> link = self . __get_dynamic_attr ( '<STR_LIT>' , obj ) <EOL> link = add_domain ( current_site . domain , link , request . is_secure ( ) ) <EOL> feed = self . feed_type ( <EOL> title = self . __get_dynamic_attr ( '<STR_LIT:title>' , obj ) , <EOL> subtitle = self . __get_dynamic_attr ( '<STR_LIT>' , obj ) , <EOL> link = link , <EOL> description = self . __get_dynamic_attr ( '<STR_LIT:description>' , obj ) , <EOL> language = settings . LANGUAGE_CODE , <EOL> feed_url = add_domain ( <EOL> current_site . domain , <EOL> self . __get_dynamic_attr ( '<STR_LIT>' , obj ) or request . path , <EOL> request . is_secure ( ) , <EOL> ) , <EOL> author_name = self . __get_dynamic_attr ( '<STR_LIT>' , obj ) , <EOL> author_link = self . __get_dynamic_attr ( '<STR_LIT>' , obj ) , <EOL> author_email = self . __get_dynamic_attr ( '<STR_LIT>' , obj ) , <EOL> categories = self . __get_dynamic_attr ( '<STR_LIT>' , obj ) , <EOL> feed_copyright = self . __get_dynamic_attr ( '<STR_LIT>' , obj ) , <EOL> feed_guid = self . __get_dynamic_attr ( '<STR_LIT>' , obj ) , <EOL> ttl = self . __get_dynamic_attr ( '<STR_LIT>' , obj ) , <EOL> ** self . feed_extra_kwargs ( obj ) <EOL> ) <EOL> title_tmp = None <EOL> if self . title_template is not None : <EOL> try : <EOL> title_tmp = loader . get_template ( self . title_template ) <EOL> except TemplateDoesNotExist : <EOL> pass <EOL> description_tmp = None <EOL> if self . description_template is not None : <EOL> try : <EOL> description_tmp = loader . get_template ( self . description_template ) <EOL> except TemplateDoesNotExist : <EOL> pass <EOL> for item in self . __get_dynamic_attr ( '<STR_LIT>' , obj ) : <EOL> if title_tmp is not None : <EOL> title = title_tmp . render ( RequestContext ( request , { '<STR_LIT>' : item , '<STR_LIT>' : current_site } ) ) <EOL> else : <EOL> title = self . __get_dynamic_attr ( '<STR_LIT>' , item ) <EOL> if description_tmp is not None : <EOL> description = description_tmp . render ( RequestContext ( request , { '<STR_LIT>' : item , '<STR_LIT>' : current_site } ) ) <EOL> else : <EOL> description = self . __get_dynamic_attr ( '<STR_LIT>' , item ) <EOL> link = add_domain ( <EOL> current_site . domain , <EOL> self . __get_dynamic_attr ( '<STR_LIT>' , item ) , <EOL> request . is_secure ( ) , <EOL> ) <EOL> enc = None <EOL> enc_url = self . __get_dynamic_attr ( '<STR_LIT>' , item ) <EOL> if enc_url : <EOL> enc = feedgenerator . Enclosure ( <EOL> url = smart_text ( enc_url ) , <EOL> length = smart_text ( self . __get_dynamic_attr ( '<STR_LIT>' , item ) ) , <EOL> mime_type = smart_text ( self . __get_dynamic_attr ( '<STR_LIT>' , item ) ) <EOL> ) <EOL> author_name = self . __get_dynamic_attr ( '<STR_LIT>' , item ) <EOL> if author_name is not None : <EOL> author_email = self . __get_dynamic_attr ( '<STR_LIT>' , item ) <EOL> author_link = self . __get_dynamic_attr ( '<STR_LIT>' , item ) <EOL> else : <EOL> author_email = author_link = None <EOL> pubdate = self . __get_dynamic_attr ( '<STR_LIT>' , item ) <EOL> if pubdate and is_naive ( pubdate ) : <EOL> ltz = tzinfo . LocalTimezone ( pubdate ) <EOL> pubdate = pubdate . replace ( tzinfo = ltz ) <EOL> feed . add_item ( <EOL> title = title , <EOL> link = link , <EOL> description = description , <EOL> unique_id = self . __get_dynamic_attr ( '<STR_LIT>' , item , link ) , <EOL> enclosure = enc , <EOL> pubdate = pubdate , <EOL> author_name = author_name , <EOL> author_email = author_email , <EOL> author_link = author_link , <EOL> categories = self . __get_dynamic_attr ( '<STR_LIT>' , item ) , <EOL> item_copyright = self . __get_dynamic_attr ( '<STR_LIT>' , item ) , <EOL> ** self . item_extra_kwargs ( item ) <EOL> ) <EOL> return feed </s>
