Django

Code

Ticket #4430: rss-unicode.patch

File rss-unicode.patch, 5.9 kB (added by Almad, 2 years ago)

patch fixing unicode description issues.

  • django/utils/feedgenerator.py

    old new  
    1919""" 
    2020 
    2121from django.utils.xmlutils import SimplerXMLGenerator 
     22from django.utils.encoding import smart_unicode 
    2223import datetime, re, time 
    2324import email.Utils 
    2425 
     
    157158    def write_items(self, handler): 
    158159        for item in self.items: 
    159160            handler.startElement(u"item", {}) 
    160             handler.addQuickElement(u"title", item['title']
    161             handler.addQuickElement(u"link", item['link']
     161            handler.addQuickElement(u"title", smart_unicode(item['title'])
     162            handler.addQuickElement(u"link", smart_unicode(item['link'])
    162163            if item['description'] is not None: 
    163                 handler.addQuickElement(u"description", item['description']
    164  
     164                handler.addQuickElement(u"description", smart_unicode(item['description'])
     165             
    165166            # Author information. 
    166167            if item["author_name"] and item["author_email"]: 
    167                 handler.addQuickElement(u"author", "%s (%s)" % \ 
    168                     (item['author_email'], item['author_name'])) 
     168                handler.addQuickElement(u"author", u"%s (%s)" % \ 
     169                    (smart_unicode(item['author_email']), smart_unicode(item['author_name']))) 
    169170            elif item["author_email"]: 
    170                 handler.addQuickElement(u"author", item["author_email"]
     171                handler.addQuickElement(u"author", smart_unicode(item["author_email"])
    171172            elif item["author_name"]: 
    172                 handler.addQuickElement(u"dc:creator", item["author_name"], {"xmlns:dc": u"http://purl.org/dc/elements/1.1/"}) 
    173  
     173                handler.addQuickElement(u"dc:creator", smart_unicode(item["author_name"]), {"xmlns:dc": u"http://purl.org/dc/elements/1.1/"}) 
     174             
    174175            if item['pubdate'] is not None: 
    175176                handler.addQuickElement(u"pubDate", rfc2822_date(item['pubdate']).decode('ascii')) 
    176177            if item['comments'] is not None: 
    177                 handler.addQuickElement(u"comments", item['comments']
     178                handler.addQuickElement(u"comments", smart_unicode(item['comments'])
    178179            if item['unique_id'] is not None: 
    179                 handler.addQuickElement(u"guid", item['unique_id']
    180  
     180                handler.addQuickElement(u"guid", smart_unicode(item['unique_id'])
     181             
    181182            # Enclosure. 
    182183            if item['enclosure'] is not None: 
    183184                handler.addQuickElement(u"enclosure", '', 
    184185                    {u"url": item['enclosure'].url, u"length": item['enclosure'].length, 
    185186                        u"type": item['enclosure'].mime_type}) 
    186  
     187             
    187188            # Categories. 
    188189            for cat in item['categories']: 
    189                 handler.addQuickElement(u"category", cat
     190                handler.addQuickElement(u"category", smart_unicode(cat)
    190191 
    191192            handler.endElement(u"item") 
    192193 
     
    227228    def write_items(self, handler): 
    228229        for item in self.items: 
    229230            handler.startElement(u"entry", {}) 
    230             handler.addQuickElement(u"title", item['title']
    231             handler.addQuickElement(u"link", u"", {u"href": item['link'], u"rel": u"alternate"}) 
     231            handler.addQuickElement(u"title", smart_unicode(item['title'])
     232            handler.addQuickElement(u"link", u"", {u"href": smart_unicode(item['link']), u"rel": u"alternate"}) 
    232233            if item['pubdate'] is not None: 
    233234                handler.addQuickElement(u"updated", rfc3339_date(item['pubdate']).decode('ascii')) 
    234235 
    235236            # Author information. 
    236237            if item['author_name'] is not None: 
    237238                handler.startElement(u"author", {}) 
    238                 handler.addQuickElement(u"name", item['author_name']
     239                handler.addQuickElement(u"name", smart_unicode(item['author_name'])
    239240                if item['author_email'] is not None: 
    240                     handler.addQuickElement(u"email", item['author_email']
     241                    handler.addQuickElement(u"email", smart_unicode(item['author_email'])
    241242                if item['author_link'] is not None: 
    242                     handler.addQuickElement(u"uri", item['author_link']
     243                    handler.addQuickElement(u"uri", smart_unicode(item['author_link'])
    243244                handler.endElement(u"author") 
    244245 
    245246            # Unique ID. 
    246247            if item['unique_id'] is not None: 
    247248                unique_id = item['unique_id'] 
    248249            else: 
    249                 unique_id = get_tag_uri(item['link'], item['pubdate']
     250                unique_id = get_tag_uri(smart_unicode(item['link']), smart_unicode(item['pubdate'])
    250251            handler.addQuickElement(u"id", unique_id) 
    251252 
    252253            # Summary. 
    253254            if item['description'] is not None: 
    254                 handler.addQuickElement(u"summary", item['description'], {u"type": u"html"}) 
     255                handler.addQuickElement(u"summary", smart_unicode(item['description']), {u"type": u"html"}) 
    255256 
    256257            # Enclosure. 
    257258            if item['enclosure'] is not None: 
    258259                handler.addQuickElement(u"link", '', 
    259260                    {u"rel": u"enclosure", 
    260                      u"href": item['enclosure'].url
     261                     u"href": smart_unicode(item['enclosure'].url)
    261262                     u"length": item['enclosure'].length, 
    262263                     u"type": item['enclosure'].mime_type}) 
    263264 
    264265            # Categories. 
    265266            for cat in item['categories']: 
    266                 handler.addQuickElement(u"category", u"", {u"term": cat}) 
     267                handler.addQuickElement(u"category", u"", {u"term": smart_unicode(cat)}) 
    267268 
    268269            # Rights. 
    269270            if item['item_copyright'] is not None: 
    270                 handler.addQuickElement(u"rights", item['item_copyright']
     271                handler.addQuickElement(u"rights", smart_unicode(item['item_copyright'])
    271272 
    272273            handler.endElement(u"entry") 
    273274