Code

Ticket #3268: __init__.diff

File __init__.diff, 6.4 KB (added by ryan.moe@…, 7 years ago)
Line 
1Index: __init__.py
2===================================================================
3--- __init__.py (revision 4358)
4+++ __init__.py (working copy)
5@@ -337,7 +337,10 @@
6     def formfield(self, initial=None):
7         "Returns a django.newforms.Field instance for this database Field."
8         # TODO: This is just a temporary default during development.
9-        return forms.CharField(required=not self.blank, label=capfirst(self.verbose_name), initial=initial)
10+        if self.choices:
11+            return forms.ChoiceField(required=not self.blank, label=capfirst(self.verbose_name), initial=initial, choices=self.choices)
12+        else:
13+            return forms.CharField(required=not self.blank, label=capfirst(self.verbose_name), initial=initial)
14 
15     def value_from_object(self, obj):
16         "Returns the value of this field in the given model instance."
17@@ -397,7 +400,10 @@
18         return [oldforms.CheckboxField]
19 
20     def formfield(self, initial=None):
21-        return forms.BooleanField(required=not self.blank, label=capfirst(self.verbose_name), initial=initial)
22+        if self.choices:
23+            return forms.ChoiceField(required=not self.blank, label=capfirst(self.verbose_name), initial=initial, choices=self.choices)
24+        else:       
25+            return forms.BooleanField(required=not self.blank, label=capfirst(self.verbose_name), initial=initial)
26 
27 class CharField(Field):
28     def get_manipulator_field_objs(self):
29@@ -414,7 +420,10 @@
30         return str(value)
31 
32     def formfield(self, initial=None):
33-        return forms.CharField(max_length=self.maxlength, required=not self.blank, label=capfirst(self.verbose_name), initial=initial)
34+        if self.choices:
35+            return forms.ChoiceField(required=not self.blank, label=capfirst(self.verbose_name), initial=initial, choices=self.choices)
36+        else:       
37+            return forms.CharField(max_length=self.maxlength, required=not self.blank, label=capfirst(self.verbose_name), initial=initial)
38 
39 # TODO: Maybe move this into contrib, because it's specialized.
40 class CommaSeparatedIntegerField(CharField):
41@@ -488,7 +497,10 @@
42         return {self.attname: (val is not None and val.strftime("%Y-%m-%d") or '')}
43 
44     def formfield(self, initial=None):
45-        return forms.DateField(required=not self.blank, label=capfirst(self.verbose_name), initial=initial)
46+        if self.choices:
47+            return forms.ChoiceField(required=not self.blank, label=capfirst(self.verbose_name), initial=initial, choices=self.choices)
48+        else:       
49+            return forms.DateField(required=not self.blank, label=capfirst(self.verbose_name), initial=initial)
50 
51 class DateTimeField(DateField):
52     def to_python(self, value):
53@@ -549,7 +561,10 @@
54                 time_field: (val is not None and val.strftime("%H:%M:%S") or '')}
55 
56     def formfield(self, initial=None):
57-        return forms.DateTimeField(required=not self.blank, label=capfirst(self.verbose_name), initial=initial)
58+        if self.choices:
59+            return forms.ChoiceField(required=not self.blank, label=capfirst(self.verbose_name), initial=initial, choices=self.choices)
60+        else:       
61+            return forms.DateTimeField(required=not self.blank, label=capfirst(self.verbose_name), initial=initial)
62 
63 class EmailField(CharField):
64     def __init__(self, *args, **kwargs):
65@@ -566,7 +581,10 @@
66         validators.isValidEmail(field_data, all_data)
67 
68     def formfield(self, initial=None):
69-        return forms.EmailField(required=not self.blank, label=capfirst(self.verbose_name), initial=initial)
70+        if self.choices:
71+            return forms.ChoiceField(required=not self.blank, label=capfirst(self.verbose_name), initial=initial, choices=self.choices)
72+        else:       
73+            return forms.EmailField(required=not self.blank, label=capfirst(self.verbose_name), initial=initial)
74 
75 class FileField(Field):
76     def __init__(self, verbose_name=None, name=None, upload_to='', **kwargs):
77@@ -701,7 +719,10 @@
78         return [oldforms.IntegerField]
79 
80     def formfield(self, initial=None):
81-        return forms.IntegerField(required=not self.blank, label=capfirst(self.verbose_name), initial=initial)
82+        if self.choices:
83+            return forms.ChoiceField(required=not self.blank, label=capfirst(self.verbose_name), initial=initial, choices=self.choices)
84+        else:       
85+            return forms.IntegerField(required=not self.blank, label=capfirst(self.verbose_name), initial=initial)
86 
87 class IPAddressField(Field):
88     def __init__(self, *args, **kwargs):
89@@ -758,7 +779,10 @@
90         return [oldforms.LargeTextField]
91 
92     def formfield(self, initial=None):
93-        return forms.CharField(required=not self.blank, widget=forms.Textarea, label=capfirst(self.verbose_name), initial=initial)
94+        if self.choices:
95+            return forms.ChoiceField(required=not self.blank, label=capfirst(self.verbose_name), initial=initial, choices=self.choices)
96+        else:       
97+            return forms.CharField(required=not self.blank, widget=forms.Textarea, label=capfirst(self.verbose_name), initial=initial)
98 
99 class TimeField(Field):
100     empty_strings_allowed = False
101@@ -801,7 +825,10 @@
102         return {self.attname: (val is not None and val.strftime("%H:%M:%S") or '')}
103 
104     def formfield(self, initial=None):
105-        return forms.TimeField(required=not self.blank, label=capfirst(self.verbose_name), initial=initial)
106+        if self.choices:
107+            return forms.ChoiceField(required=not self.blank, label=capfirst(self.verbose_name), initial=initial, choices=self.choices)
108+        else:       
109+            return forms.TimeField(required=not self.blank, label=capfirst(self.verbose_name), initial=initial)
110 
111 class URLField(CharField):
112     def __init__(self, verbose_name=None, name=None, verify_exists=True, **kwargs):
113@@ -818,7 +845,10 @@
114         return "CharField"
115 
116     def formfield(self, initial=None):
117-        return forms.URLField(required=not self.blank, verify_exists=self.verify_exists, label=capfirst(self.verbose_name), initial=initial)
118+        if self.choices:
119+            return forms.ChoiceField(required=not self.blank, label=capfirst(self.verbose_name), initial=initial, choices=self.choices)
120+        else:       
121+            return forms.URLField(required=not self.blank, verify_exists=self.verify_exists, label=capfirst(self.verbose_name), initial=initial)
122 
123 class USStateField(Field):
124     def get_manipulator_field_objs(self):