Anonymous avatar Anonymous committed abd357f

[soc2009/i18n] implemented more efficient way to get formats

Comments (0)

Files changed (4)

django/contrib/admin/templatetags/admin_list.py

 from django.contrib.admin.views.main import ORDER_VAR, ORDER_TYPE_VAR, PAGE_VAR, SEARCH_VAR
 from django.core.exceptions import ObjectDoesNotExist
 from django.db import models
-from django.utils import dateformat
+from django.utils import formats
 from django.utils.html import escape, conditional_escape
 from django.utils.text import capfirst
 from django.utils.safestring import mark_safe
             # Dates and times are special: They're formatted in a certain way.
             elif isinstance(f, models.DateField) or isinstance(f, models.TimeField):
                 if field_val:
-                    date_format = getformat('DATE_FORMAT')
-                    datetime_format = getformat('DATETIME_FORMAT')
-                    time_format = getformat('TIME_FORMAT')
                     if isinstance(f, models.DateTimeField):
-                        result_repr = capfirst(dateformat.format(field_val, datetime_format))
+                        result_repr = capfirst(formats.date_and_time_format(field_val, 'DATETIME_FORMAT'))
                     elif isinstance(f, models.TimeField):
-                        result_repr = capfirst(dateformat.time_format(field_val, time_format))
+                        result_repr = capfirst(formats.date_and_time_format(field_val, 'TIME_FORMAT'))
                     else:
-                        result_repr = capfirst(dateformat.format(field_val, date_format))
+                        result_repr = capfirst(formats.format(field_val, 'DATE_FORMAT'))
                 else:
                     result_repr = EMPTY_CHANGELIST_VALUE
                 row_class = ' class="nowrap"'
         year_lookup = cl.params.get(year_field)
         month_lookup = cl.params.get(month_field)
         day_lookup = cl.params.get(day_field)
-        year_month_format = getformat('YEAR_MONTH_FORMAT')
-        month_day_format = getformat('MONTH_DAY_FORMAT')
 
         link = lambda d: mark_safe(cl.get_query_string(d, [field_generic]))
 
                 'show': True,
                 'back': {
                     'link': link({year_field: year_lookup, month_field: month_lookup}),
-                    'title': dateformat.format(day, year_month_format)
+                    'title': formats.date_and_time_format(day, 'YEAR_MONTH_FORMAT')
                 },
-                'choices': [{'title': dateformat.format(day, month_day_format)}]
+                'choices': [{'title': formats.date_and_time_format(day, 'MONTH_DAY_FORMAT')}]
             }
         elif year_lookup and month_lookup:
             days = cl.query_set.filter(**{year_field: year_lookup, month_field: month_lookup}).dates(field_name, 'day')
                 },
                 'choices': [{
                     'link': link({year_field: year_lookup, month_field: month_lookup, day_field: day.day}),
-                    'title': dateformat.format(day, month_day_format)
+                    'title': formats.date_and_time_format(day, 'MONTH_DAY_FORMAT')
                 } for day in days]
             }
         elif year_lookup:
                 },
                 'choices': [{
                     'link': link({year_field: year_lookup, month_field: month.month}),
-                    'title': dateformat.format(month, year_month_format)
+                    'title': formats.date_and_time_format(month, 'YEAR_MONTH_FORMAT')
                 } for month in months]
             }
         else:

django/contrib/databrowse/datastructures.py

 """
 
 from django.db import models
-from django.utils import dateformat
+from django.utils import formats
 from django.utils.text import capfirst
 from django.utils.encoding import smart_unicode, smart_str, iri_to_uri
 from django.utils.safestring import mark_safe
             objs = dict(self.field.choices).get(self.raw_value, EMPTY_VALUE)
         elif isinstance(self.field, models.DateField) or isinstance(self.field, models.TimeField):
             if self.raw_value:
-                date_format = getformat('DATE_FORMAT')
-                datetime_format = getformat('DATETIME_FORMAT')
-                time_format = getformat('TIME_FORMAT')
                 if isinstance(self.field, models.DateTimeField):
-                    objs = capfirst(dateformat.format(self.raw_value, datetime_format))
+                    objs = capfirst(formats.date_and_time_format(self.raw_value, 'DATETIME_FORMAT'))
                 elif isinstance(self.field, models.TimeField):
-                    objs = capfirst(dateformat.time_format(self.raw_value, time_format))
+                    objs = capfirst(formats.date_and_time_format(self.raw_value, 'TIME_FORMAT'))
                 else:
-                    objs = capfirst(dateformat.format(self.raw_value, date_format))
+                    objs = capfirst(formats.date_and_time_format(self.raw_value, 'DATE_FORMAT'))
             else:
                 objs = EMPTY_VALUE
         elif isinstance(self.field, models.BooleanField) or isinstance(self.field, models.NullBooleanField):

django/utils/formats.py

 else:
     getformat = getformat_null
 
+def date_and_time_format(value, format):
+    import dateformat
+    return dateformat.format(value, getformat('DATE_FORMAT'))
+
+def date_format(value):
+    import dateformat
+    return dateformat.format(value, getformat('DATE_FORMAT'))
+
+def datetime_format(value):
+    import dateformat
+    return dateformat.format(value, getformat('DATE_FORMAT'))
+
+def time_format(value):
+    import dateformat
+    return dateformat.time_format(value, getformat('TIME_FORMAT'))
+
+def number_format(value):
+    import numberformat
+    return numberformat.format(
+        value,
+        getformat('DECIMAL_SEPARATOR'),
+        2, # TODO: decide how to set decimal positions later
+        3, # TODO: get it from formats
+        getformat('THOUSAND_SEPARATOR'),
+    )
+

django/utils/numberformat.py

+def format(number, decimal_sep, decimal_pos, groupping=0, thousand_sep=''):
+    """
+    """
+    # sign
+    if number < 0:
+        sign = '-'
+    else:
+        sign = ''
+    # decimal part
+    str_number = str(abs(number))
+    if '.' in str_number:
+        int_part, dec_part = str_number.split('.')
+        dec_part = dec_part[:decimal_pos]
+    else:
+        int_part, dec_part = str_number, ''
+    dec_part = dec_part + ('0' * (decimal_pos - len(dec_part)))
+    if dec_part: dec_part = decimal_sep + dec_part
+    # groupping
+    if groupping:
+        int_part_gd = ''
+        for cnt, digit in enumerate(int_part[::-1]):
+            if cnt and not cnt % groupping:
+                int_part_gd += thousand_sep
+            int_part_gd += digit
+        int_part = int_part_gd[::-1]
+
+
+    return sign + int_part + dec_part
+
+if __name__ == '__main__':
+    import decimal
+    print format_number(-1000000, ',', 4, 3, '.')
+    print format_number(-100.100, ',', 4, 3, '.')
+    print format_number(decimal.Decimal('1000.100'), ',', 4)
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.