Django

Code

Ticket #1086: feeds.py.2.diff

File feeds.py.2.diff, 3.7 kB (added by Esaj, 4 years ago)

Oops, forgot to import the exception...

  • django/contrib/syndication/feeds.py

    old new  
    33from django.models.core import sites 
    44from django.utils import feedgenerator 
    55from django.conf.settings import LANGUAGE_CODE, SETTINGS_MODULE 
     6from exceptions import UnicodeDecodeError 
    67 
    78def add_domain(domain, url): 
    89    if not url.startswith('http://'): 
     
    7778            description_template = Template('{{ obj }}') 
    7879 
    7980        for item in self.__get_dynamic_attr('items', obj): 
    80             link = add_domain(current_site.domain, self.__get_dynamic_attr('item_link', item)) 
    81             enc = None 
    82             enc_url = self.__get_dynamic_attr('item_enclosure_url', item) 
    83             if enc_url: 
    84                 enc = feedgenerator.Enclosure( 
    85                     url = enc_url.decode('utf-8'), 
    86                     length = str(self.__get_dynamic_attr('item_enclosure_length', item)).decode('utf-8'), 
    87                     mime_type = self.__get_dynamic_attr('item_enclosure_mime_type', item).decode('utf-8'), 
     81            try: 
     82                link = add_domain(current_site.domain, self.__get_dynamic_attr('item_link', item)) 
     83                enc = None 
     84                enc_url = self.__get_dynamic_attr('item_enclosure_url', item) 
     85                if enc_url: 
     86                    enc = feedgenerator.Enclosure( 
     87                        url = enc_url.decode('utf-8'), 
     88                        length = str(self.__get_dynamic_attr('item_enclosure_length', item)).decode('utf-8'), 
     89                        mime_type = self.__get_dynamic_attr('item_enclosure_mime_type', item).decode('utf-8'), 
     90                    ) 
     91                author_name = self.__get_dynamic_attr('item_author_name', item) 
     92                if author_name is not None: 
     93                    author_email = self.__get_dynamic_attr('item_author_email', item) 
     94                    author_link = self.__get_dynamic_attr('item_author_link', item) 
     95                else: 
     96                    author_email = author_link = None 
     97                feed.add_item( 
     98                    title = title_template.render(Context({'obj': item, 'site': current_site})).decode('utf-8'), 
     99                    link = link, 
     100                    description = description_template.render(Context({'obj': item, 'site': current_site})).decode('utf-8'), 
     101                    unique_id = link, 
     102                    enclosure = enc, 
     103                    pubdate = self.__get_dynamic_attr('item_pubdate', item), 
     104                    author_name = author_name, 
     105                    author_email = author_email, 
     106                    author_link = author_link, 
    88107                ) 
    89             author_name = self.__get_dynamic_attr('item_author_name', item) 
    90             if author_name is not None: 
    91                 author_email = self.__get_dynamic_attr('item_author_email', item) 
    92                 author_link = self.__get_dynamic_attr('item_author_link', item) 
    93             else: 
    94                 author_email = author_link = None 
    95             feed.add_item( 
    96                 title = title_template.render(Context({'obj': item, 'site': current_site})).decode('utf-8'), 
    97                 link = link, 
    98                 description = description_template.render(Context({'obj': item, 'site': current_site})).decode('utf-8'), 
    99                 unique_id = link, 
    100                 enclosure = enc, 
    101                 pubdate = self.__get_dynamic_attr('item_pubdate', item), 
    102                 author_name = author_name, 
    103                 author_email = author_email, 
    104                 author_link = author_link, 
    105             ) 
     108            except UnicodeDecodeError: 
     109                pass 
    106110        return feed