1. Matthew Schinckel
  2. django-weekday-field

Commits

Matthew Schinckel  committed 371a38d

Make widget display correctly in admin.
Allow python object to see it as a list of ints.

  • Participants
  • Parent commits c6c7a72
  • Branches default

Comments (0)

Files changed (3)

File weekday_field/fields.py

View file
 
 from forms import WeekdayFormField
 
+def validate_csv(data):
+    return all(map(lambda x:isinstance(x, int), data))
+    
 class WeekdayField(models.CommaSeparatedIntegerField):
-
+    """
+    Field to simplify the handling of a multiple choice of None->all days.
+    
+    Stores as CSInt.
+    """
     __metaclass__ = models.SubfieldBase
     
     description = "CSV Weekday Field"
+    default_validators = [validate_csv]
     
     def __init__(self, *args, **kwargs):
         kwargs['max_length'] = 14
     def formfield(self, **kwargs):
         return super(WeekdayField, self).formfield(form_class=WeekdayFormField, **kwargs)
     
+    def to_python(self, value):
+        if isinstance(value, basestring):
+            if value:
+                value = [int(x) for x in value.split(',') if x]
+            else:
+                value = []
+        return value
+
+    def get_db_prep_value(self, value, connection=None, prepared=False):
+        return ",".join([str(x) for x in value])
+    
 try:
     from south.modelsinspector import add_introspection_rules
     add_introspection_rules([], ['^weekday_field\.fields\.WeekdayField'])

File weekday_field/forms.py

View file
     def __init__(self, *args, **kwargs):
         kwargs['choices'] = utils.DAY_CHOICES
         kwargs.pop('max_length', None)
-        super(WeekdayFormField, self).__init__(*args, **kwargs)
+        kwargs['widget'] = forms.widgets.SelectMultiple
+        super(WeekdayFormField, self).__init__(*args, **kwargs)
+        
+    def clean(self, value):
+        value = super(WeekdayFormField, self).clean(value)
+        return ",".join([str(x) for x in value])

File weekday_field/widgets.py

View file
+from django import forms
+
+class WeekdayWidget(forms.widgets.SelectMultiple):
+    def render(self, *args, **kwargs):
+        return super(WeekdayWidget, self).render(*args, **kwargs)