Ticket #3268: __init__.diff

File __init__.diff, 6.4 KB (added by ryan.moe@…, 8 years ago)
  • __init__.py

     
    337337    def formfield(self, initial=None):
    338338        "Returns a django.newforms.Field instance for this database Field."
    339339        # TODO: This is just a temporary default during development.
    340         return forms.CharField(required=not self.blank, label=capfirst(self.verbose_name), initial=initial)
     340        if self.choices:
     341            return forms.ChoiceField(required=not self.blank, label=capfirst(self.verbose_name), initial=initial, choices=self.choices)
     342        else:
     343            return forms.CharField(required=not self.blank, label=capfirst(self.verbose_name), initial=initial)
    341344
    342345    def value_from_object(self, obj):
    343346        "Returns the value of this field in the given model instance."
     
    397400        return [oldforms.CheckboxField]
    398401
    399402    def formfield(self, initial=None):
    400         return forms.BooleanField(required=not self.blank, label=capfirst(self.verbose_name), initial=initial)
     403        if self.choices:
     404            return forms.ChoiceField(required=not self.blank, label=capfirst(self.verbose_name), initial=initial, choices=self.choices)
     405        else:       
     406            return forms.BooleanField(required=not self.blank, label=capfirst(self.verbose_name), initial=initial)
    401407
    402408class CharField(Field):
    403409    def get_manipulator_field_objs(self):
     
    414420        return str(value)
    415421
    416422    def formfield(self, initial=None):
    417         return forms.CharField(max_length=self.maxlength, required=not self.blank, label=capfirst(self.verbose_name), initial=initial)
     423        if self.choices:
     424            return forms.ChoiceField(required=not self.blank, label=capfirst(self.verbose_name), initial=initial, choices=self.choices)
     425        else:       
     426            return forms.CharField(max_length=self.maxlength, required=not self.blank, label=capfirst(self.verbose_name), initial=initial)
    418427
    419428# TODO: Maybe move this into contrib, because it's specialized.
    420429class CommaSeparatedIntegerField(CharField):
     
    488497        return {self.attname: (val is not None and val.strftime("%Y-%m-%d") or '')}
    489498
    490499    def formfield(self, initial=None):
    491         return forms.DateField(required=not self.blank, label=capfirst(self.verbose_name), initial=initial)
     500        if self.choices:
     501            return forms.ChoiceField(required=not self.blank, label=capfirst(self.verbose_name), initial=initial, choices=self.choices)
     502        else:       
     503            return forms.DateField(required=not self.blank, label=capfirst(self.verbose_name), initial=initial)
    492504
    493505class DateTimeField(DateField):
    494506    def to_python(self, value):
     
    549561                time_field: (val is not None and val.strftime("%H:%M:%S") or '')}
    550562
    551563    def formfield(self, initial=None):
    552         return forms.DateTimeField(required=not self.blank, label=capfirst(self.verbose_name), initial=initial)
     564        if self.choices:
     565            return forms.ChoiceField(required=not self.blank, label=capfirst(self.verbose_name), initial=initial, choices=self.choices)
     566        else:       
     567            return forms.DateTimeField(required=not self.blank, label=capfirst(self.verbose_name), initial=initial)
    553568
    554569class EmailField(CharField):
    555570    def __init__(self, *args, **kwargs):
     
    566581        validators.isValidEmail(field_data, all_data)
    567582
    568583    def formfield(self, initial=None):
    569         return forms.EmailField(required=not self.blank, label=capfirst(self.verbose_name), initial=initial)
     584        if self.choices:
     585            return forms.ChoiceField(required=not self.blank, label=capfirst(self.verbose_name), initial=initial, choices=self.choices)
     586        else:       
     587            return forms.EmailField(required=not self.blank, label=capfirst(self.verbose_name), initial=initial)
    570588
    571589class FileField(Field):
    572590    def __init__(self, verbose_name=None, name=None, upload_to='', **kwargs):
     
    701719        return [oldforms.IntegerField]
    702720
    703721    def formfield(self, initial=None):
    704         return forms.IntegerField(required=not self.blank, label=capfirst(self.verbose_name), initial=initial)
     722        if self.choices:
     723            return forms.ChoiceField(required=not self.blank, label=capfirst(self.verbose_name), initial=initial, choices=self.choices)
     724        else:       
     725            return forms.IntegerField(required=not self.blank, label=capfirst(self.verbose_name), initial=initial)
    705726
    706727class IPAddressField(Field):
    707728    def __init__(self, *args, **kwargs):
     
    758779        return [oldforms.LargeTextField]
    759780
    760781    def formfield(self, initial=None):
    761         return forms.CharField(required=not self.blank, widget=forms.Textarea, label=capfirst(self.verbose_name), initial=initial)
     782        if self.choices:
     783            return forms.ChoiceField(required=not self.blank, label=capfirst(self.verbose_name), initial=initial, choices=self.choices)
     784        else:       
     785            return forms.CharField(required=not self.blank, widget=forms.Textarea, label=capfirst(self.verbose_name), initial=initial)
    762786
    763787class TimeField(Field):
    764788    empty_strings_allowed = False
     
    801825        return {self.attname: (val is not None and val.strftime("%H:%M:%S") or '')}
    802826
    803827    def formfield(self, initial=None):
    804         return forms.TimeField(required=not self.blank, label=capfirst(self.verbose_name), initial=initial)
     828        if self.choices:
     829            return forms.ChoiceField(required=not self.blank, label=capfirst(self.verbose_name), initial=initial, choices=self.choices)
     830        else:       
     831            return forms.TimeField(required=not self.blank, label=capfirst(self.verbose_name), initial=initial)
    805832
    806833class URLField(CharField):
    807834    def __init__(self, verbose_name=None, name=None, verify_exists=True, **kwargs):
     
    818845        return "CharField"
    819846
    820847    def formfield(self, initial=None):
    821         return forms.URLField(required=not self.blank, verify_exists=self.verify_exists, label=capfirst(self.verbose_name), initial=initial)
     848        if self.choices:
     849            return forms.ChoiceField(required=not self.blank, label=capfirst(self.verbose_name), initial=initial, choices=self.choices)
     850        else:       
     851            return forms.URLField(required=not self.blank, verify_exists=self.verify_exists, label=capfirst(self.verbose_name), initial=initial)
    822852
    823853class USStateField(Field):
    824854    def get_manipulator_field_objs(self):
Back to Top