Commits

Kevin Wetzels  committed 7d68dc3

Initial version

  • Participants

Comments (0)

Files changed (9)

+syntax: glob
+
+**/.DS_Store
+.DS_Store
+**/*.pyc
+*.pyc

File __init__.py

Empty file added.
+# -*- coding: utf-8 -*-
+from __future__ import division
+import re
+
+from django.forms import IntegerField
+from django.utils.encoding import smart_str
+
+from widgets import MinutesWidget
+
+class MinutesField(IntegerField):
+    
+    widget = MinutesWidget
+    
+    def clean(self, value):
+        value = smart_str(value).strip()
+        match = re.search(r'^(\d+):(\d{1,2})$', value)
+        if match:
+            groups = match.groups()
+            hours = int(groups[0])
+            minutes = int(groups[1])
+            if minutes > 59:
+                raise forms.ValidationError(self.error_messages['invalid'])
+            value = (hours * 60) + minutes
+        else:
+            value = value.replace(',', '.')
+            if '.' in value:
+                parts = value.split('.')
+                try:
+                    hours = int(parts[0])
+                    fraction = int(parts[1])
+                    value = int((hours * 60) + (60 / 100 * fraction))
+                except (ValueError, TypeError):
+                    raise forms.ValidationError(self.error_messages['invalid'])
+        return super(MinutesField, self).clean(value)
+from django.db import models
+
+# Create your models here.

File templatetags/__init__.py

Empty file added.

File templatetags/stdfieldstags.py

+# -*- coding: utf-8 -*-
+from __future__ import absolute_import
+from django import template
+
+from stdfields.widgets import format_minutes
+
+register = template.Library()
+
+@register.filter 
+def minutes(value):
+    return format_minutes(value)
+"""
+This file demonstrates writing tests using the unittest module. These will pass
+when you run "manage.py test".
+
+Replace this with more appropriate tests for your application.
+"""
+
+from django.test import TestCase
+
+
+class SimpleTest(TestCase):
+    def test_basic_addition(self):
+        """
+        Tests that 1 + 1 always equals 2.
+        """
+        self.assertEqual(1 + 1, 2)
+# Create your views here.
+# -*- coding: utf-8 -*-
+from django.forms.widgets import TextInput
+
+def format_minutes(value):
+    if value is None or value == '':
+        return u''
+    divided = MinutesWidget.divide(value)
+    return u'%d:%02d' % divided
+
+class MinutesWidget(TextInput):
+    
+    def _format_value(self, value):
+        return format_minutes(value)
+        
+    @classmethod
+    def divide(cls, total):
+        if total <= 0:
+            return (0, 0)
+        hours = 0 if total <= 60 else total / 60
+        return (hours, total - (hours * 60))