Commits

Anonymous committed 6416f13

Adding bitwise weekday field and bitwise weekdays in utils with short name

Comments (0)

Files changed (4)

 
 setup(
     name = "django-weekday-field",
-    version = "0.1",
+    version = "0.1.2",
     description = "Weekday field for django models",
     url = "http://bitbucket.org/schinckel/django-weekday-field/",
-    author = "Matthew Schinckel",
-    author_email = "matt@schinckel.net",
+    author = "Matthew Schinckel and Andrew Gall",
+    author_email = "matt@schinckel.net and housepage@gmail.com",
     packages = [
         "weekday_field",
     ],

weekday_field/forms.py

 from django import forms
 
 import utils
+import operator
 
 class WeekdayFormField(forms.TypedMultipleChoiceField):
     def __init__(self, *args, **kwargs):
-        kwargs['choices'] = utils.DAY_CHOICES
+        if 'choices' not in kwargs:
+          kwargs['choices'] = utils.DAY_CHOICES
         kwargs.pop('max_length', None)
-        kwargs['widget'] = forms.widgets.SelectMultiple
+        if 'widget' not in 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])
+      value = super(WeekdayFormField, self).clean(value)
+      return value
+
+class BitwiseWeekdayFormField(WeekdayFormField):
+  def __init__(self, *args, **kwargs):
+    if 'short' in kwargs:
+      if kwargs['short']:
+        kwargs['choices'] = [(x[0],x[1]) for x in utils.BITWISE_DAY_CHOICES]
+      del kwargs['short']
+    else:
+      kwargs['choices'] = [(x[0],x[2]) for x in utils.BITWISE_DAY_CHOICES]
+    super(BitwiseWeekdayFormField, self).__init__(*args, **kwargs)
+
+  def clean(self,value):
+    value = [int(x) for x in value]
+    value = reduce(operator.or_, value)
+    return value

weekday_field/utils.py

     (6, "Sunday")
 )
 
+BITWISE_DAY_CHOICES = (
+    (1, "Sun","Sunday"),
+    (2, "Mon","Monday"),
+    (4, "Tue","Tuesday"),
+    (8, "Wed","Wednesday"),
+    (16, "Thu","Thursday"),
+    (32, "Fri","Friday"),
+    (64, "Sat","Saturday"),
+)

weekday_field/widgets.py

-from django import forms
-
-class WeekdayWidget(forms.widgets.SelectMultiple):
-    def render(self, *args, **kwargs):
-        return super(WeekdayWidget, self).render(*args, **kwargs)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.