Commits

Matthew Schinckel committed 9ed168d Merge

Finalize py3k support.

Comments (0)

Files changed (6)

File contents unchanged.

timedelta/fields.py

 from django.db import models
 from django.core.exceptions import ValidationError
+from django.utils import six
 
 from collections import defaultdict
 import datetime
     def get_prep_value(self, value):
         if self.null and value == "":
             return None
-        if (value is None) or isinstance(value, (str, unicode)):
+        if (value is None) or isinstance(value, six.string_types):
             return value
         return str(value).replace(',', '')
         

timedelta/forms.py

     def clean(self, value):
         super(TimedeltaFormField, self).clean(value)
         if value == '' and not self.required:
-            return u''
+            return ''
         try:
             return parse(value)
         except TypeError:

timedelta/helpers.py

+from __future__ import division
+
 import re
 import datetime
 from decimal import Decimal
 
+from django.utils import six
+
 def nice_repr(timedelta, display="long", sep=", "):
     """
     Turns a datetime.timedelta object into a nice string repr.
     
     result = []
     
-    weeks = timedelta.days / 7
+    weeks = int(timedelta.days / 7)
     days = timedelta.days % 7
-    hours = timedelta.seconds / 3600
-    minutes = (timedelta.seconds % 3600) / 60
+    hours = int(timedelta.seconds / 3600)
+    minutes = int((timedelta.seconds % 3600) / 60)
     seconds = timedelta.seconds % 60
     
     if display == "sql":
     >>> iso8601_repr(td(days=1, hours=2, minutes=3, seconds=4))
     'P1DT2H3M4S'
     """
-    years = timedelta.days / 365
-    weeks = (timedelta.days % 365) / 7
+    years = int(timedelta.days / 365)
+    weeks = int((timedelta.days % 365) / 7)
     days = timedelta.days % 7
 
-    hours = timedelta.seconds / 3600
-    minutes = (timedelta.seconds % 3600) / 60
+    hours = int(timedelta.seconds / 3600)
+    minutes = int((timedelta.seconds % 3600) / 60)
     seconds = timedelta.seconds % 60
 
     formatting = (
     # and from serialization
     d = re.match(r'^((?P<days>\d+) days?,? )?(?P<hours>\d+):'
                  r'(?P<minutes>\d+)(:(?P<seconds>\d+(\.\d+)?))?$',
-                 unicode(string))
+                 six.text_type(string))
     if d: 
         d = d.groupdict(0)
     else:
                      r'((?P<hours>((\d*\.\d+)|\d+))\W*h(ou)?(r(s)?)?(,)?\W*)?'
                      r'((?P<minutes>((\d*\.\d+)|\d+))\W*m(in(ute)?(s)?)?(,)?\W*)?'
                      r'((?P<seconds>((\d*\.\d+)|\d+))\W*s(ec(ond)?(s)?)?)?\W*$',
-                     unicode(string))
+                     six.text_type(string))
         if not d:
             raise TypeError("'%s' is not a valid time interval" % string)
         d = d.groupdict(0)
     sec1 = obj1.days * 86400 + obj1.seconds
     if isinstance(obj2, datetime.timedelta):
         sec2 = obj2.days * 86400 + obj2.seconds
+        value = sec1 / sec2
         if as_float:
-            sec1 *= 1.0
-        return sec1 / sec2
+            return value
+        return int(value)
     else:
         if as_float:
             assert None, "as_float=True is inappropriate when dividing timedelta by a number."

timedelta/tests.py

 from django.db import models
 from django.core.exceptions import ValidationError
+from django.utils import six
+
 from unittest import TestCase
 
 import datetime
 from .widgets import TimedeltaWidget
 from .helpers import *
 
+UNICODE = {
+    'u': '' if six.PY3 else 'u'
+}
+
 class TimedeltaWidgetTest(TestCase):
     def test_render(self):
         """
         >>> t = TimedeltaWidget()
         >>> t.render('', datetime.timedelta(days=1), {})
-        u'<input type="text" name="" value="1 day" />'
+        %(u)s'<input type="text" name="" value="1 day" />'
         >>> t.render('', datetime.timedelta(days=0), {})
-        u'<input type="text" name="" />'
+        %(u)s'<input type="text" name="" />'
         >>> t.render('', datetime.timedelta(seconds=1), {})
-        u'<input type="text" name="" value="1 second" />'
+        %(u)s'<input type="text" name="" value="1 second" />'
         >>> t.render('', datetime.timedelta(seconds=10), {})
-        u'<input type="text" name="" value="10 seconds" />'
+        %(u)s'<input type="text" name="" value="10 seconds" />'
         >>> t.render('', datetime.timedelta(seconds=30), {})
-        u'<input type="text" name="" value="30 seconds" />'
+        %(u)s'<input type="text" name="" value="30 seconds" />'
         >>> t.render('', datetime.timedelta(seconds=60), {})
-        u'<input type="text" name="" value="1 minute" />'
+        %(u)s'<input type="text" name="" value="1 minute" />'
         >>> t.render('', datetime.timedelta(seconds=150), {})
-        u'<input type="text" name="" value="2 minutes, 30 seconds" />'
+        %(u)s'<input type="text" name="" value="2 minutes, 30 seconds" />'
         >>> t.render('', datetime.timedelta(seconds=1800), {})
-        u'<input type="text" name="" value="30 minutes" />'
+        %(u)s'<input type="text" name="" value="30 minutes" />'
         >>> t.render('', datetime.timedelta(seconds=3600), {})
-        u'<input type="text" name="" value="1 hour" />'
+        %(u)s'<input type="text" name="" value="1 hour" />'
         >>> t.render('', datetime.timedelta(seconds=3601), {})
-        u'<input type="text" name="" value="1 hour, 1 second" />'
+        %(u)s'<input type="text" name="" value="1 hour, 1 second" />'
         >>> t.render('', datetime.timedelta(seconds=19800), {})
-        u'<input type="text" name="" value="5 hours, 30 minutes" />'
+        %(u)s'<input type="text" name="" value="5 hours, 30 minutes" />'
         >>> t.render('', datetime.timedelta(seconds=91800), {})
-        u'<input type="text" name="" value="1 day, 1 hour, 30 minutes" />'
+        %(u)s'<input type="text" name="" value="1 day, 1 hour, 30 minutes" />'
         >>> t.render('', datetime.timedelta(seconds=302400), {})
-        u'<input type="text" name="" value="3 days, 12 hours" />'
-        """
+        %(u)s'<input type="text" name="" value="3 days, 12 hours" />'
+        """ % UNICODE
 
 class MinMaxTestModel(models.Model):
     min = TimedeltaField(min_value=datetime.timedelta(1))
         datetime.timedelta(3, 31362, 342161)
         >>> t.clean('3 days, 8:42:42.3.42161')
         Traceback (most recent call last):
-        ValidationError: [u'Enter a valid time span: e.g. "3 days, 4 hours, 2 minutes"']
+        ValidationError: ['Enter a valid time span: e.g. "3 days, 4 hours, 2 minutes"']
         >>> t.clean('5 day, 8:42:42')
         datetime.timedelta(5, 31362)
         >>> t.clean('1 days')
         datetime.timedelta(7)
         >>> t.clean('2 weeks, 2 days')
         datetime.timedelta(16)
-        >>> t.clean(u'2 we\xe8k, 2 days')
+        >>> t.clean(six.u('2 we\xe8k, 2 days'))
         Traceback (most recent call last):
-        ValidationError: [u'Enter a valid time span: e.g. "3 days, 4 hours, 2 minutes"']
-        """
+            ...
+        ValidationError: [%(u)s'Enter a valid time span: e.g. "3 days, 4 hours, 2 minutes"']
+        """ % UNICODE
     
 
 class TimedeltaHelpersTest(TestCase):

timedelta/widgets.py

+import datetime
+
 from django import forms
-import datetime
+from django.utils import six
 
 from .helpers import nice_repr, parse
 
     def render(self, name, value, attrs=None):
         if value is None:
             value = ""
-        elif isinstance(value, (str, unicode)):
+        elif isinstance(value, six.string_types):
             pass
         else:
             if isinstance(value, int):
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.