~ubuntu-branches/ubuntu/quantal/python-django/quantal-security

« back to all changes in this revision

Viewing changes to django/forms/widgets.py

  • Committer: Bazaar Package Importer
  • Author(s): Jamie Strandboge
  • Date: 2010-10-12 11:34:35 UTC
  • mfrom: (1.2.7 upstream)
  • mto: This revision was merged to the branch mainline in revision 30.
  • Revision ID: james.westby@ubuntu.com-20101012113435-9lnsrh0i3mxozbt0
Tags: upstream-1.2.3
ImportĀ upstreamĀ versionĀ 1.2.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
308
308
        super(DateInput, self).__init__(attrs)
309
309
        if format:
310
310
            self.format = format
 
311
            self.manual_format = True
 
312
        else:
 
313
            self.format = formats.get_format('DATE_INPUT_FORMATS')[0]
 
314
            self.manual_format = False
311
315
 
312
316
    def _format_value(self, value):
313
 
        if self.is_localized:
 
317
        if self.is_localized and not self.manual_format:
314
318
            return formats.localize_input(value)
315
319
        elif hasattr(value, 'strftime'):
316
320
            value = datetime_safe.new_date(value)
336
340
        super(DateTimeInput, self).__init__(attrs)
337
341
        if format:
338
342
            self.format = format
 
343
            self.manual_format = True
 
344
        else:
 
345
            self.format = formats.get_format('DATETIME_INPUT_FORMATS')[0]
 
346
            self.manual_format = False
339
347
 
340
348
    def _format_value(self, value):
341
 
        if self.is_localized:
 
349
        if self.is_localized and not self.manual_format:
342
350
            return formats.localize_input(value)
343
351
        elif hasattr(value, 'strftime'):
344
352
            value = datetime_safe.new_datetime(value)
364
372
        super(TimeInput, self).__init__(attrs)
365
373
        if format:
366
374
            self.format = format
 
375
            self.manual_format = True
 
376
        else:
 
377
            self.format = formats.get_format('TIME_INPUT_FORMATS')[0]
 
378
            self.manual_format = False
367
379
 
368
380
    def _format_value(self, value):
369
 
        if self.is_localized:
 
381
        if self.is_localized and not self.manual_format:
370
382
            return formats.localize_input(value)
371
383
        elif hasattr(value, 'strftime'):
372
384
            return value.strftime(self.format)
438
450
        output.append(u'</select>')
439
451
        return mark_safe(u'\n'.join(output))
440
452
 
 
453
    def render_option(self, selected_choices, option_value, option_label):
 
454
        option_value = force_unicode(option_value)
 
455
        selected_html = (option_value in selected_choices) and u' selected="selected"' or ''
 
456
        return u'<option value="%s"%s>%s</option>' % (
 
457
            escape(option_value), selected_html,
 
458
            conditional_escape(force_unicode(option_label)))
 
459
 
441
460
    def render_options(self, choices, selected_choices):
442
 
        def render_option(option_value, option_label):
443
 
            option_value = force_unicode(option_value)
444
 
            selected_html = (option_value in selected_choices) and u' selected="selected"' or ''
445
 
            return u'<option value="%s"%s>%s</option>' % (
446
 
                escape(option_value), selected_html,
447
 
                conditional_escape(force_unicode(option_label)))
448
461
        # Normalize to strings.
449
462
        selected_choices = set([force_unicode(v) for v in selected_choices])
450
463
        output = []
452
465
            if isinstance(option_label, (list, tuple)):
453
466
                output.append(u'<optgroup label="%s">' % escape(force_unicode(option_value)))
454
467
                for option in option_label:
455
 
                    output.append(render_option(*option))
 
468
                    output.append(self.render_option(selected_choices, *option))
456
469
                output.append(u'</optgroup>')
457
470
            else:
458
 
                output.append(render_option(option_value, option_label))
 
471
                output.append(self.render_option(selected_choices, option_value, option_label))
459
472
        return u'\n'.join(output)
460
473
 
461
474
class NullBooleanSelect(Select):
751
764
    time_format = TimeInput.format
752
765
 
753
766
    def __init__(self, attrs=None, date_format=None, time_format=None):
754
 
        if date_format:
755
 
            self.date_format = date_format
756
 
        if time_format:
757
 
            self.time_format = time_format
758
 
        widgets = (DateInput(attrs=attrs, format=self.date_format),
759
 
                   TimeInput(attrs=attrs, format=self.time_format))
 
767
        widgets = (DateInput(attrs=attrs, format=date_format),
 
768
                   TimeInput(attrs=attrs, format=time_format))
760
769
        super(SplitDateTimeWidget, self).__init__(widgets, attrs)
761
770
 
762
771
    def decompress(self, value):