Ticket #4161: 4161.diff
File 4161.diff, 15.5 KB (added by , 18 years ago) |
---|
-
django/db/models/manipulators.py
7 7 from django.utils.functional import curry 8 8 from django.utils.datastructures import DotExpandedDict 9 9 from django.utils.text import capfirst 10 from django.utils.encoding import smart_str 10 11 import types 11 12 12 13 def add_manipulators(sender): … … 111 112 if self.change: 112 113 self.fields_added, self.fields_changed, self.fields_deleted = [], [], [] 113 114 for f in self.opts.fields: 114 if not f.primary_key and s tr(getattr(self.original_object, f.attname)) !=str(getattr(new_object, f.attname)):115 if not f.primary_key and smart_str(getattr(self.original_object, f.attname)) != smart_str(getattr(new_object, f.attname)): 115 116 self.fields_changed.append(f.verbose_name) 116 117 117 118 # Save many-to-many objects. Example: Set sites for a poll. … … 211 212 self.fields_added.append('%s "%s"' % (related.opts.verbose_name, new_rel_obj)) 212 213 else: 213 214 for f in related.opts.fields: 214 if not f.primary_key and f != related.field and s tr(getattr(old_rel_obj, f.attname)) !=str(getattr(new_rel_obj, f.attname)):215 if not f.primary_key and f != related.field and smart_str(getattr(old_rel_obj, f.attname)) != smart_str(getattr(new_rel_obj, f.attname)): 215 216 self.fields_changed.append('%s for %s "%s"' % (f.verbose_name, related.opts.verbose_name, new_rel_obj)) 216 217 217 218 # Save many-to-many objects. -
django/oldforms/__init__.py
3 3 from django.utils.html import escape 4 4 from django.conf import settings 5 5 from django.utils.translation import ugettext, ungettext 6 from django.utils.encoding import smart_unicode, smart_str 6 7 7 8 FORM_FIELD_ID_PREFIX = 'id_' 8 9 … … 166 167 167 168 def __str__(self): 168 169 "Renders the field" 169 return str(self.formfield.render(self.data))170 return unicode(self).encode('utf-8') 170 171 172 def __unicode__(self): 173 "Renders the field" 174 return self.formfield.render(self.data) 175 171 176 def __repr__(self): 172 177 return '<FormFieldWrapper for "%s">' % self.formfield.field_name 173 178 … … 196 201 self.formfield_dict = formfield_dict 197 202 198 203 def __str__(self): 199 return str(self.formfield_dict)204 return unicode(self).encode('utf-8') 200 205 206 def __str__(self): 207 return unicode(self.formfield_dict) 208 201 209 def __getitem__(self, template_key): 202 210 "Look up field by template key; raise KeyError on failure" 203 211 return self.formfield_dict[template_key] … … 294 302 Subclasses should also implement a render(data) method, which is responsible 295 303 for rending the form field in XHTML. 296 304 """ 305 297 306 def __str__(self): 298 return self.render('')307 return unicode(self).encode('utf-8') 299 308 309 def __unicode__(self): 310 return self.render(u'') 311 300 312 def __repr__(self): 301 313 return 'FormField "%s"' % self.field_name 302 314 … … 398 410 399 411 def render(self, data): 400 412 if data is None: 401 data = ''402 maxlength = ''413 data = u'' 414 maxlength = u'' 403 415 if self.maxlength: 404 maxlength = 'maxlength="%s" ' % self.maxlength 405 if isinstance(data, unicode): 406 data = data.encode(settings.DEFAULT_CHARSET) 407 return '<input type="%s" id="%s" class="v%s%s" name="%s" size="%s" value="%s" %s/>' % \ 408 (self.input_type, self.get_id(), self.__class__.__name__, self.is_required and ' required' or '', 416 maxlength = u'maxlength="%s" ' % self.maxlength 417 return u'<input type="%s" id="%s" class="v%s%s" name="%s" size="%s" value="%s" %s/>' % \ 418 (self.input_type, self.get_id(), self.__class__.__name__, self.is_required and u' required' or '', 409 419 self.field_name, self.length, escape(data), maxlength) 410 420 411 421 def html2python(data): … … 428 438 def render(self, data): 429 439 if data is None: 430 440 data = '' 431 if isinstance(data, unicode): 432 data = data.encode(settings.DEFAULT_CHARSET) 433 return '<textarea id="%s" class="v%s%s" name="%s" rows="%s" cols="%s">%s</textarea>' % \ 434 (self.get_id(), self.__class__.__name__, self.is_required and ' required' or '', 441 return u'<textarea id="%s" class="v%s%s" name="%s" rows="%s" cols="%s">%s</textarea>' % \ 442 (self.get_id(), self.__class__.__name__, self.is_required and u' required' or u'', 435 443 self.field_name, self.rows, self.cols, escape(data)) 436 444 437 445 class HiddenField(FormField): … … 441 449 self.validator_list = validator_list[:] 442 450 443 451 def render(self, data): 444 return '<input type="hidden" id="%s" name="%s" value="%s" />' % \452 return u'<input type="hidden" id="%s" name="%s" value="%s" />' % \ 445 453 (self.get_id(), self.field_name, escape(data)) 446 454 447 455 class CheckboxField(FormField): … … 456 464 checked_html = '' 457 465 if data or (data is '' and self.checked_by_default): 458 466 checked_html = ' checked="checked"' 459 return '<input type="checkbox" id="%s" class="v%s" name="%s"%s />' % \467 return u'<input type="checkbox" id="%s" class="v%s" name="%s"%s />' % \ 460 468 (self.get_id(), self.__class__.__name__, 461 469 self.field_name, checked_html) 462 470 … … 471 479 def __init__(self, field_name, choices=None, size=1, is_required=False, validator_list=None, member_name=None): 472 480 if validator_list is None: validator_list = [] 473 481 if choices is None: choices = [] 482 choices = [(k, smart_unicode(v)) for k, v in choices] 474 483 self.field_name = field_name 475 484 # choices is a list of (value, human-readable key) tuples because order matters 476 485 self.choices, self.size, self.is_required = choices, size, is_required … … 479 488 self.member_name = member_name 480 489 481 490 def render(self, data): 482 output = [ '<select id="%s" class="v%s%s" name="%s" size="%s">' % \491 output = [u'<select id="%s" class="v%s%s" name="%s" size="%s">' % \ 483 492 (self.get_id(), self.__class__.__name__, 484 self.is_required and ' required' or'', self.field_name, self.size)]485 str_data = s tr(data) # normalize to string493 self.is_required and u' required' or u'', self.field_name, self.size)] 494 str_data = smart_unicode(data) # normalize to string 486 495 for value, display_name in self.choices: 487 selected_html = ''488 if s tr(value) == str_data:489 selected_html = ' selected="selected"'490 output.append( ' <option value="%s"%s>%s</option>' % (escape(value), selected_html, escape(display_name)))491 output.append( ' </select>')492 return '\n'.join(output)496 selected_html = u'' 497 if smart_unicode(value) == str_data: 498 selected_html = u' selected="selected"' 499 output.append(u' <option value="%s"%s>%s</option>' % (escape(value), selected_html, escape(display_name))) 500 output.append(u' </select>') 501 return u'\n'.join(output) 493 502 494 503 def isValidChoice(self, data, form): 495 str_data = s tr(data)496 str_choices = [s tr(item[0]) for item in self.choices]504 str_data = smart_unicode(data) 505 str_choices = [smart_str(item[0]) for item in self.choices] 497 506 if str_data not in str_choices: 498 507 raise validators.ValidationError, ugettext("Select a valid choice; '%(data)s' is not in %(choices)s.") % {'data': str_data, 'choices': str_choices} 499 508 … … 509 518 def __init__(self, field_name, choices=None, ul_class='', is_required=False, validator_list=None, member_name=None): 510 519 if validator_list is None: validator_list = [] 511 520 if choices is None: choices = [] 521 choices = [(k, smart_unicode(v)) for k, v in choices] 512 522 self.field_name = field_name 513 523 # choices is a list of (value, human-readable key) tuples because order matters 514 524 self.choices, self.is_required = choices, is_required … … 520 530 def render(self, data): 521 531 """ 522 532 Returns a special object, RadioFieldRenderer, that is iterable *and* 523 has a default str() rendered output.533 has a default unicode() rendered output. 524 534 525 535 This allows for flexible use in templates. You can just use the default 526 536 rendering: … … 537 547 class RadioFieldRenderer: 538 548 def __init__(self, datalist, ul_class): 539 549 self.datalist, self.ul_class = datalist, ul_class 540 def __ str__(self):541 "Default str() output for this radio field -- a <ul>"542 output = [ '<ul%s>' % (self.ul_class and ' class="%s"' % self.ul_class or'')]543 output.extend([ '<li>%s %s</li>' % (d['field'], d['label']) for d in self.datalist])544 output.append( '</ul>')545 return ''.join(output)550 def __unicode__(self): 551 "Default unicode() output for this radio field -- a <ul>" 552 output = [u'<ul%s>' % (self.ul_class and u' class="%s"' % self.ul_class or u'')] 553 output.extend([u'<li>%s %s</li>' % (d['field'], d['label']) for d in self.datalist]) 554 output.append(u'</ul>') 555 return u''.join(output) 546 556 def __iter__(self): 547 557 for d in self.datalist: 548 558 yield d 549 559 def __len__(self): 550 560 return len(self.datalist) 551 561 datalist = [] 552 str_data = s tr(data) # normalize to string562 str_data = smart_unicode(data) # normalize to string 553 563 for i, (value, display_name) in enumerate(self.choices): 554 564 selected_html = '' 555 if s tr(value) == str_data:556 selected_html = ' checked="checked"'565 if smart_unicode(value) == str_data: 566 selected_html = u' checked="checked"' 557 567 datalist.append({ 558 568 'value': value, 559 569 'name': display_name, 560 'field': '<input type="radio" id="%s" name="%s" value="%s"%s/>' % \561 (self.get_id() + '_' + str(i), self.field_name, value, selected_html),562 'label': '<label for="%s">%s</label>' % \563 (self.get_id() + '_' + str(i), display_name),570 'field': u'<input type="radio" id="%s" name="%s" value="%s"%s/>' % \ 571 (self.get_id() + u'_' + unicode(i), self.field_name, value, selected_html), 572 'label': u'<label for="%s">%s</label>' % \ 573 (self.get_id() + u'_' + unicode(i), display_name), 564 574 }) 565 575 return RadioFieldRenderer(datalist, self.ul_class) 566 576 567 577 def isValidChoice(self, data, form): 568 str_data = s tr(data)569 str_choices = [s tr(item[0]) for item in self.choices]578 str_data = smart_unicode(data) 579 str_choices = [smart_unicode(item[0]) for item in self.choices] 570 580 if str_data not in str_choices: 571 581 raise validators.ValidationError, ugettext("Select a valid choice; '%(data)s' is not in %(choices)s.") % {'data':str_data, 'choices':str_choices} 572 582 … … 590 600 class SelectMultipleField(SelectField): 591 601 requires_data_list = True 592 602 def render(self, data): 593 output = [ '<select id="%s" class="v%s%s" name="%s" size="%s" multiple="multiple">' % \594 (self.get_id(), self.__class__.__name__, self.is_required and ' required' or'',603 output = [u'<select id="%s" class="v%s%s" name="%s" size="%s" multiple="multiple">' % \ 604 (self.get_id(), self.__class__.__name__, self.is_required and u' required' or u'', 595 605 self.field_name, self.size)] 596 str_data_list = map(s tr, data) # normalize to strings606 str_data_list = map(smart_unicode, data) # normalize to strings 597 607 for value, choice in self.choices: 598 selected_html = ''599 if s tr(value) in str_data_list:600 selected_html = ' selected="selected"'601 output.append( ' <option value="%s"%s>%s</option>' % (escape(value), selected_html, escape(choice)))602 output.append( ' </select>')603 return '\n'.join(output)608 selected_html = u'' 609 if smart_unicode(value) in str_data_list: 610 selected_html = u' selected="selected"' 611 output.append(u' <option value="%s"%s>%s</option>' % (escape(value), selected_html, escape(choice))) 612 output.append(u' </select>') 613 return u'\n'.join(output) 604 614 605 615 def isValidChoice(self, field_data, all_data): 606 616 # data is something like ['1', '2', '3'] 607 str_choices = [s tr(item[0]) for item in self.choices]608 for val in map(s tr, field_data):617 str_choices = [smart_unicode(item[0]) for item in self.choices] 618 for val in map(smart_unicode, field_data): 609 619 if val not in str_choices: 610 620 raise validators.ValidationError, ugettext("Select a valid choice; '%(data)s' is not in %(choices)s.") % {'data':val, 'choices':str_choices} 611 621 … … 642 652 new_data.setlist(self.field_name, data_list) 643 653 644 654 def render(self, data): 645 output = [ '<ul%s>' % (self.ul_class and ' class="%s"' % self.ul_class or'')]646 str_data_list = map(s tr, data) # normalize to strings655 output = [u'<ul%s>' % (self.ul_class and u' class="%s"' % self.ul_class or u'')] 656 str_data_list = map(smart_unicode, data) # normalize to strings 647 657 for value, choice in self.choices: 648 checked_html = ''649 if s tr(value) in str_data_list:650 checked_html = ' checked="checked"'651 field_name = '%s%s' % (self.field_name, value)652 output.append( '<li><input type="checkbox" id="%s" class="v%s" name="%s"%s value="on" /> <label for="%s">%s</label></li>' % \658 checked_html = u'' 659 if smart_unicode(value) in str_data_list: 660 checked_html = u' checked="checked"' 661 field_name = u'%s%s' % (self.field_name, value) 662 output.append(u'<li><input type="checkbox" id="%s" class="v%s" name="%s"%s value="on" /> <label for="%s">%s</label></li>' % \ 653 663 (self.get_id() + escape(value), self.__class__.__name__, field_name, checked_html, 654 664 self.get_id() + escape(value), choice)) 655 output.append( '</ul>')656 return '\n'.join(output)665 output.append(u'</ul>') 666 return u'\n'.join(output) 657 667 658 668 #################### 659 669 # FILE UPLOADS # … … 674 684 raise validators.CriticalValidationError, ugettext("The submitted file is empty.") 675 685 676 686 def render(self, data): 677 return '<input type="file" id="%s" class="v%s" name="%s" />' % \687 return u'<input type="file" id="%s" class="v%s" name="%s" />' % \ 678 688 (self.get_id(), self.__class__.__name__, self.field_name) 679 689 680 690 def html2python(data): … … 985 995 986 996 def render(self, data): 987 997 if data is None: 988 data = ''998 data = u'' 989 999 elif isinstance(data, (list, tuple)): 990 data = ','.join(data)1000 data = u','.join(data) 991 1001 return super(CommaSeparatedIntegerField, self).render(data) 992 1002 993 1003 class RawIdAdminField(CommaSeparatedIntegerField):