Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

unicode: Audited most of the remaining contrib/ modules for unicode c…

…ompliance

(only sitemaps remaining). Fixes here for databrowse, markup and webdesign.


git-svn-id: http://code.djangoproject.com/svn/django/branches/unicode@5274 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit a8404e66fd1f04334a3a4488c63976762974bd4c 1 parent 5b4e263
Malcolm Tredinnick authored May 17, 2007
33  django/contrib/databrowse/datastructures.py
@@ -3,10 +3,12 @@
3 3
 convenience functionality and permalink functions for the databrowse app.
4 4
 """
5 5
 
  6
+import urllib
6 7
 from django.db import models
7 8
 from django.utils import dateformat
8 9
 from django.utils.text import capfirst
9 10
 from django.utils.translation import get_date_formats
  11
+from django.utils.encoding import smart_unicode, smart_str
10 12
 
11 13
 EMPTY_VALUE = '(None)'
12 14
 
@@ -19,7 +21,7 @@ def __init__(self, site, model):
19 21
         self.verbose_name_plural = model._meta.verbose_name_plural
20 22
 
21 23
     def __repr__(self):
22  
-        return '<EasyModel for %s>' % self.model._meta.object_name
  24
+        return '<EasyModel for %s>' % smart_str(self.model._meta.object_name)
23 25
 
24 26
     def model_databrowse(self):
25 27
         "Returns the ModelDatabrowse class for this model."
@@ -54,7 +56,7 @@ def __init__(self, easy_model, field):
54 56
         self.model, self.field = easy_model, field
55 57
 
56 58
     def __repr__(self):
57  
-        return '<EasyField for %s.%s>' % (self.model.model._meta.object_name, self.field.name)
  59
+        return smart_str(u'<EasyField for %s.%s>' % (self.model.model._meta.object_name, self.field.name))
58 60
 
59 61
     def choices(self):
60 62
         for value, label in self.field.choices:
@@ -72,29 +74,32 @@ def __init__(self, easy_model, field, value, label):
72 74
         self.value, self.label = value, label
73 75
 
74 76
     def __repr__(self):
75  
-        return '<EasyChoice for %s.%s>' % (self.model.model._meta.object_name, self.field.name)
  77
+        return smart_str(u'<EasyChoice for %s.%s>' % (self.model.model._meta.object_name, self.field.name))
76 78
 
77 79
     def url(self):
78  
-        return '%s%s/%s/%s/%s/' % (self.model.site.root_url, self.model.model._meta.app_label, self.model.model._meta.module_name, self.field.field.name, self.value)
  80
+        return '%s%s/%s/%s/%s/' % (self.model.site.root_url, self.model.model._meta.app_label, self.model.model._meta.module_name, self.field.field.name, urllib.quote(smart_str(self.value)))
79 81
 
80 82
 class EasyInstance(object):
81 83
     def __init__(self, easy_model, instance):
82 84
         self.model, self.instance = easy_model, instance
83 85
 
84 86
     def __repr__(self):
85  
-        return '<EasyInstance for %s (%s)>' % (self.model.model._meta.object_name, self.instance._get_pk_val())
  87
+        return smart_str(u'<EasyInstance for %s (%s)>' % (self.model.model._meta.object_name, self.instance._get_pk_val()))
86 88
 
87  
-    def __str__(self):
88  
-        val = str(self.instance)
  89
+    def __unicode__(self):
  90
+        val = smart_unicode(self.instance)
89 91
         if len(val) > 30:
90  
-            return val[:30] + '...'
  92
+            return val[:30] + u'...'
91 93
         return val
92 94
 
  95
+    def __str__(self):
  96
+        return self.__unicode__().encode('utf-8')
  97
+
93 98
     def pk(self):
94 99
         return self.instance._get_pk_val()
95 100
 
96 101
     def url(self):
97  
-        return '%s%s/%s/objects/%s/' % (self.model.site.root_url, self.model.model._meta.app_label, self.model.model._meta.module_name, self.pk())
  102
+        return '%s%s/%s/objects/%s/' % (self.model.site.root_url, self.model.model._meta.app_label, self.model.model._meta.module_name, urllib.quote(smart_str(self.pk())))
98 103
 
99 104
     def fields(self):
100 105
         """
@@ -126,7 +131,7 @@ def __init__(self, easy_model, instance, field):
126 131
         self.raw_value = getattr(instance.instance, field.name)
127 132
 
128 133
     def __repr__(self):
129  
-        return '<EasyInstanceField for %s.%s>' % (self.model.model._meta.object_name, self.field.name)
  134
+        return smart_str(u'<EasyInstanceField for %s.%s>' % (self.model.model._meta.object_name, self.field.name))
130 135
 
131 136
     def values(self):
132 137
         """
@@ -175,18 +180,18 @@ def urls(self):
175 180
             if self.field.rel.to in self.model.model_list:
176 181
                 lst = []
177 182
                 for value in self.values():
178  
-                    url = '%s%s/%s/objects/%s/' % (self.model.site.root_url, m.model._meta.app_label, m.model._meta.module_name, value._get_pk_val())
179  
-                    lst.append((str(value), url))
  183
+                    url = '%s%s/%s/objects/%s/' % (self.model.site.root_url, m.model._meta.app_label, m.model._meta.module_name, urllib.quote(smart_str(value._get_pk_val())))
  184
+                    lst.append((smart_unicode(value), url))
180 185
             else:
181 186
                 lst = [(value, None) for value in self.values()]
182 187
         elif self.field.choices:
183 188
             lst = []
184 189
             for value in self.values():
185  
-                url = '%s%s/%s/fields/%s/%s/' % (self.model.site.root_url, self.model.model._meta.app_label, self.model.model._meta.module_name, self.field.name, self.raw_value)
  190
+                url = '%s%s/%s/fields/%s/%s/' % (self.model.site.root_url, self.model.model._meta.app_label, self.model.model._meta.module_name, self.field.name, urllib.quote(smart_str(self.raw_value)))
186 191
                 lst.append((value, url))
187 192
         elif isinstance(self.field, models.URLField):
188 193
             val = self.values()[0]
189  
-            lst = [(val, val)]
  194
+            lst = [(val, urllib.quote(smart_str(val)))]
190 195
         else:
191 196
             lst = [(self.values()[0], None)]
192 197
         return lst
8  django/contrib/databrowse/plugins/calendars.py
@@ -27,13 +27,13 @@ def field_dict(self, model):
27 27
     def model_index_html(self, request, model, site):
28 28
         fields = self.field_dict(model)
29 29
         if not fields:
30  
-            return ''
31  
-        return '<p class="filter"><strong>View calendar by:</strong> %s</p>' % \
32  
-            ', '.join(['<a href="calendars/%s/">%s</a>' % (f.name, capfirst(f.verbose_name)) for f in fields.values()])
  30
+            return u''
  31
+        return u'<p class="filter"><strong>View calendar by:</strong> %s</p>' % \
  32
+            u', '.join(['<a href="calendars/%s/">%s</a>' % (f.name, capfirst(f.verbose_name)) for f in fields.values()])
33 33
 
34 34
     def urls(self, plugin_name, easy_instance_field):
35 35
         if isinstance(easy_instance_field.field, models.DateField):
36  
-            return ['%s%s/%s/%s/%s/%s/' % (easy_instance_field.model.url(),
  36
+            return [u'%s%s/%s/%s/%s/%s/' % (easy_instance_field.model.url(),
37 37
                 plugin_name, easy_instance_field.field.name,
38 38
                 easy_instance_field.raw_value.year,
39 39
                 easy_instance_field.raw_value.strftime('%b').lower(),
12  django/contrib/databrowse/plugins/fieldchoices.py
@@ -4,9 +4,11 @@
4 4
 from django.contrib.databrowse.sites import DatabrowsePlugin
5 5
 from django.shortcuts import render_to_response
6 6
 from django.utils.text import capfirst
  7
+from django.utils.encoding import smart_str
7 8
 from django.views.generic import date_based
8 9
 import datetime
9 10
 import time
  11
+import urllib
10 12
 
11 13
 class FieldChoicePlugin(DatabrowsePlugin):
12 14
     def __init__(self, field_filter=None):
@@ -29,15 +31,15 @@ def field_dict(self, model):
29 31
     def model_index_html(self, request, model, site):
30 32
         fields = self.field_dict(model)
31 33
         if not fields:
32  
-            return ''
33  
-        return '<p class="filter"><strong>View by:</strong> %s</p>' % \
34  
-            ', '.join(['<a href="fields/%s/">%s</a>' % (f.name, capfirst(f.verbose_name)) for f in fields.values()])
  34
+            return u''
  35
+        return u'<p class="filter"><strong>View by:</strong> %s</p>' % \
  36
+            u', '.join(['<a href="fields/%s/">%s</a>' % (f.name, capfirst(f.verbose_name)) for f in fields.values()])
35 37
 
36 38
     def urls(self, plugin_name, easy_instance_field):
37 39
         if easy_instance_field.field in self.field_dict(easy_instance_field.model.model).values():
38  
-            return ['%s%s/%s/%s/' % (easy_instance_field.model.url(),
  40
+            return [u'%s%s/%s/%s/' % (easy_instance_field.model.url(),
39 41
                 plugin_name, easy_instance_field.field.name,
40  
-                easy_instance_field.raw_value)]
  42
+                urllib.quote(smart_str(easy_instance_field.raw_value)))]
41 43
 
42 44
     def model_view(self, request, model_databrowse, url):
43 45
         self.model, self.site = model_databrowse.model, model_databrowse.site
2  django/contrib/databrowse/sites.py
@@ -60,7 +60,7 @@ def root(self, request, url):
60 60
 
61 61
     def main_view(self, request):
62 62
         easy_model = EasyModel(self.site, self.model)
63  
-        html_snippets = '\n'.join([p.model_index_html(request, self.model, self.site) for p in self.plugins.values()])
  63
+        html_snippets = u'\n'.join([p.model_index_html(request, self.model, self.site) for p in self.plugins.values()])
64 64
         return render_to_response('databrowse/model_detail.html', {
65 65
             'model': easy_model,
66 66
             'root_url': self.site.root_url,
2  django/contrib/databrowse/templates/databrowse/fieldchoice_list.html
@@ -10,7 +10,7 @@
10 10
 
11 11
 <ul class="objectlist">
12 12
 {% for object in object_list %}
13  
-<li class="{% cycle odd,even %}"><a href="{{ object }}/">{{ object|escape }}</a></li>
  13
+<li class="{% cycle odd,even %}"><a href="{{ object|urlencode }}/">{{ object|escape }}</a></li>
14 14
 {% endfor %}
15 15
 </ul>
16 16
 
15  django/contrib/markup/templatetags/markup.py
@@ -16,6 +16,7 @@
16 16
 
17 17
 from django import template
18 18
 from django.conf import settings
  19
+from django.utils.encoding import smart_str, smart_unicode
19 20
 
20 21
 register = template.Library()
21 22
 
@@ -25,9 +26,9 @@ def textile(value):
25 26
     except ImportError:
26 27
         if settings.DEBUG:
27 28
             raise template.TemplateSyntaxError, "Error in {% textile %} filter: The Python textile library isn't installed."
28  
-        return value
  29
+        return smart_unicode(value)
29 30
     else:
30  
-        return textile.textile(value, encoding=settings.DEFAULT_CHARSET, output=settings.DEFAULT_CHARSET)
  31
+        return smart_unicode(textile.textile(smart_str(value), encoding='utf-8', output='utf-8'))
31 32
 
32 33
 def markdown(value):
33 34
     try:
@@ -35,9 +36,9 @@ def markdown(value):
35 36
     except ImportError:
36 37
         if settings.DEBUG:
37 38
             raise template.TemplateSyntaxError, "Error in {% markdown %} filter: The Python markdown library isn't installed."
38  
-        return value
  39
+        return smart_unicode(value)
39 40
     else:
40  
-        return markdown.markdown(value)
  41
+        return smart_unicode(markdown.markdown(smart_str(value)))
41 42
 
42 43
 def restructuredtext(value):
43 44
     try:
@@ -45,11 +46,11 @@ def restructuredtext(value):
45 46
     except ImportError:
46 47
         if settings.DEBUG:
47 48
             raise template.TemplateSyntaxError, "Error in {% restructuredtext %} filter: The Python docutils library isn't installed."
48  
-        return value
  49
+        return smart_unicode(value)
49 50
     else:
50 51
         docutils_settings = getattr(settings, "RESTRUCTUREDTEXT_FILTER_SETTINGS", {})
51  
-        parts = publish_parts(source=value, writer_name="html4css1", settings_overrides=docutils_settings)
52  
-        return parts["fragment"]
  52
+        parts = publish_parts(source=smart_str(value), writer_name="html4css1", settings_overrides=docutils_settings)
  53
+        return smart_unicode(parts["fragment"])
53 54
 
54 55
 register.filter(textile)
55 56
 register.filter(markdown)
46  django/contrib/webdesign/lorem_ipsum.py
@@ -5,8 +5,40 @@
5 5
 import random
6 6
 
7 7
 COMMON_P = 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.'
8  
-WORDS = ('exercitationem', 'perferendis', 'perspiciatis', 'laborum', 'eveniet', 'sunt', 'iure', 'nam', 'nobis', 'eum', 'cum', 'officiis', 'excepturi', 'odio', 'consectetur', 'quasi', 'aut', 'quisquam', 'vel', 'eligendi', 'itaque', 'non', 'odit', 'tempore', 'quaerat', 'dignissimos', 'facilis', 'neque', 'nihil', 'expedita', 'vitae', 'vero', 'ipsum', 'nisi', 'animi', 'cumque', 'pariatur', 'velit', 'modi', 'natus', 'iusto', 'eaque', 'sequi', 'illo', 'sed', 'ex', 'et', 'voluptatibus', 'tempora', 'veritatis', 'ratione', 'assumenda', 'incidunt', 'nostrum', 'placeat', 'aliquid', 'fuga', 'provident', 'praesentium', 'rem', 'necessitatibus', 'suscipit', 'adipisci', 'quidem', 'possimus', 'voluptas', 'debitis', 'sint', 'accusantium', 'unde', 'sapiente', 'voluptate', 'qui', 'aspernatur', 'laudantium', 'soluta', 'amet', 'quo', 'aliquam', 'saepe', 'culpa', 'libero', 'ipsa', 'dicta', 'reiciendis', 'nesciunt', 'doloribus', 'autem', 'impedit', 'minima', 'maiores', 'repudiandae', 'ipsam', 'obcaecati', 'ullam', 'enim', 'totam', 'delectus', 'ducimus', 'quis', 'voluptates', 'dolores', 'molestiae', 'harum', 'dolorem', 'quia', 'voluptatem', 'molestias', 'magni', 'distinctio', 'omnis', 'illum', 'dolorum', 'voluptatum', 'ea', 'quas', 'quam', 'corporis', 'quae', 'blanditiis', 'atque', 'deserunt', 'laboriosam', 'earum', 'consequuntur', 'hic', 'cupiditate', 'quibusdam', 'accusamus', 'ut', 'rerum', 'error', 'minus', 'eius', 'ab', 'ad', 'nemo', 'fugit', 'officia', 'at', 'in', 'id', 'quos', 'reprehenderit', 'numquam', 'iste', 'fugiat', 'sit', 'inventore', 'beatae', 'repellendus', 'magnam', 'recusandae', 'quod', 'explicabo', 'doloremque', 'aperiam', 'consequatur', 'asperiores', 'commodi', 'optio', 'dolor', 'labore', 'temporibus', 'repellat', 'veniam', 'architecto', 'est', 'esse', 'mollitia', 'nulla', 'a', 'similique', 'eos', 'alias', 'dolore', 'tenetur', 'deleniti', 'porro', 'facere', 'maxime', 'corrupti')
9  
-COMMON_WORDS = ('lorem', 'ipsum', 'dolor', 'sit', 'amet', 'consectetur', 'adipisicing', 'elit', 'sed', 'do', 'eiusmod', 'tempor', 'incididunt', 'ut', 'labore', 'et', 'dolore', 'magna', 'aliqua')
  8
+
  9
+WORDS = ('exercitationem', 'perferendis', 'perspiciatis', 'laborum', 'eveniet',
  10
+        'sunt', 'iure', 'nam', 'nobis', 'eum', 'cum', 'officiis', 'excepturi',
  11
+        'odio', 'consectetur', 'quasi', 'aut', 'quisquam', 'vel', 'eligendi',
  12
+        'itaque', 'non', 'odit', 'tempore', 'quaerat', 'dignissimos',
  13
+        'facilis', 'neque', 'nihil', 'expedita', 'vitae', 'vero', 'ipsum',
  14
+        'nisi', 'animi', 'cumque', 'pariatur', 'velit', 'modi', 'natus',
  15
+        'iusto', 'eaque', 'sequi', 'illo', 'sed', 'ex', 'et', 'voluptatibus',
  16
+        'tempora', 'veritatis', 'ratione', 'assumenda', 'incidunt', 'nostrum',
  17
+        'placeat', 'aliquid', 'fuga', 'provident', 'praesentium', 'rem',
  18
+        'necessitatibus', 'suscipit', 'adipisci', 'quidem', 'possimus',
  19
+        'voluptas', 'debitis', 'sint', 'accusantium', 'unde', 'sapiente',
  20
+        'voluptate', 'qui', 'aspernatur', 'laudantium', 'soluta', 'amet',
  21
+        'quo', 'aliquam', 'saepe', 'culpa', 'libero', 'ipsa', 'dicta',
  22
+        'reiciendis', 'nesciunt', 'doloribus', 'autem', 'impedit', 'minima',
  23
+        'maiores', 'repudiandae', 'ipsam', 'obcaecati', 'ullam', 'enim',
  24
+        'totam', 'delectus', 'ducimus', 'quis', 'voluptates', 'dolores',
  25
+        'molestiae', 'harum', 'dolorem', 'quia', 'voluptatem', 'molestias',
  26
+        'magni', 'distinctio', 'omnis', 'illum', 'dolorum', 'voluptatum', 'ea',
  27
+        'quas', 'quam', 'corporis', 'quae', 'blanditiis', 'atque', 'deserunt',
  28
+        'laboriosam', 'earum', 'consequuntur', 'hic', 'cupiditate',
  29
+        'quibusdam', 'accusamus', 'ut', 'rerum', 'error', 'minus', 'eius',
  30
+        'ab', 'ad', 'nemo', 'fugit', 'officia', 'at', 'in', 'id', 'quos',
  31
+        'reprehenderit', 'numquam', 'iste', 'fugiat', 'sit', 'inventore',
  32
+        'beatae', 'repellendus', 'magnam', 'recusandae', 'quod', 'explicabo',
  33
+        'doloremque', 'aperiam', 'consequatur', 'asperiores', 'commodi',
  34
+        'optio', 'dolor', 'labore', 'temporibus', 'repellat', 'veniam',
  35
+        'architecto', 'est', 'esse', 'mollitia', 'nulla', 'a', 'similique',
  36
+        'eos', 'alias', 'dolore', 'tenetur', 'deleniti', 'porro', 'facere',
  37
+        'maxime', 'corrupti')
  38
+
  39
+COMMON_WORDS = ('lorem', 'ipsum', 'dolor', 'sit', 'amet', 'consectetur',
  40
+        'adipisicing', 'elit', 'sed', 'do', 'eiusmod', 'tempor', 'incididunt',
  41
+        'ut', 'labore', 'et', 'dolore', 'magna', 'aliqua')
10 42
 
11 43
 def sentence():
12 44
     """
@@ -17,10 +49,10 @@ def sentence():
17 49
     """
18 50
     # Determine the number of comma-separated sections and number of words in
19 51
     # each section for this sentence.
20  
-    sections = [' '.join(random.sample(WORDS, random.randint(3, 12))) for i in range(random.randint(1, 5))]
21  
-    s = ', '.join(sections)
  52
+    sections = [u' '.join(random.sample(WORDS, random.randint(3, 12))) for i in range(random.randint(1, 5))]
  53
+    s = u', '.join(sections)
22 54
     # Convert to sentence case and add end punctuation.
23  
-    return '%s%s%s' % (s[0].upper(), s[1:], random.choice('?.'))
  55
+    return u'%s%s%s' % (s[0].upper(), s[1:], random.choice('?.'))
24 56
 
25 57
 def paragraph():
26 58
     """
@@ -28,7 +60,7 @@ def paragraph():
28 60
 
29 61
     The paragraph consists of between 1 and 4 sentences, inclusive.
30 62
     """
31  
-    return ' '.join([sentence() for i in range(random.randint(1, 4))])
  63
+    return u' '.join([sentence() for i in range(random.randint(1, 4))])
32 64
 
33 65
 def paragraphs(count, common=True):
34 66
     """
@@ -66,4 +98,4 @@ def words(count, common=True):
66 98
             word_list += random.sample(WORDS, c)
67 99
     else:
68 100
         word_list = word_list[:count]
69  
-    return ' '.join(word_list)
  101
+    return u' '.join(word_list)
2  django/contrib/webdesign/templatetags/webdesign.py
@@ -18,7 +18,7 @@ def render(self, context):
18 18
             paras = paragraphs(count, common=self.common)
19 19
         if self.method == 'p':
20 20
             paras = ['<p>%s</p>' % p for p in paras]
21  
-        return '\n\n'.join(paras)
  21
+        return u'\n\n'.join(paras)
22 22
 
23 23
 #@register.tag
24 24
 def lorem(parser, token):

0 notes on commit a8404e6

Please sign in to comment.
Something went wrong with that request. Please try again.