Ticket #12489: django12489.diff

File django12489.diff, 7.2 KB (added by v1v3kn, 14 years ago)

Implemented day_of_year & week_of_year with tests

  • tests/modeltests/basic/tests.py

     
    5050        self.assertEqual(Article.objects.get(pub_date__year=2005, pub_date__month=7), a)
    5151        self.assertEqual(Article.objects.get(pub_date__year=2005, pub_date__month=7, pub_date__day=28), a)
    5252        self.assertEqual(Article.objects.get(pub_date__week_day=5), a)
     53        self.assertEqual(Article.objects.get(pub_date__week_of_year=30), a)
     54        self.assertEqual(Article.objects.get(pub_date__day_of_year=209), a)
    5355
    5456        # The "__exact" lookup type can be omitted, as a shortcut.
    5557        self.assertEqual(Article.objects.get(id=a.id), a)
  • django/db/models/sql/where.py

     
     
    199199                        params)
    200200        elif lookup_type in ('range', 'year'):
    201201            return ('%s BETWEEN %%s and %%s' % field_sql, params)
    202         elif lookup_type in ('month', 'day', 'week_day'):
     202        elif lookup_type in ('month', 'day', 'week_day', 'week_of_year', 'day_of_year'):
    203203            return ('%s = %%s' % connection.ops.date_extract_sql(lookup_type, field_sql),
    204204                    params)
    205205        elif lookup_type == 'isnull':
  • django/db/models/sql/constants.py

     
     
    44QUERY_TERMS = dict([(x, None) for x in (
    55    'exact', 'iexact', 'contains', 'icontains', 'gt', 'gte', 'lt', 'lte', 'in',
    66    'startswith', 'istartswith', 'endswith', 'iendswith', 'range', 'year',
    7     'month', 'day', 'week_day', 'isnull', 'search', 'regex', 'iregex',
     7    'month', 'day', 'week_day', 'week_of_year', 'day_of_year', 'isnull', 'search', 'regex', 'iregex',
    88    )])
    99
    1010# Size of each "chunk" for get_iterator calls.
  • django/db/models/fields/__init__.py

     
     
    283283            return value._prepare()
    284284
    285285        if lookup_type in (
    286                 'regex', 'iregex', 'month', 'day', 'week_day', 'search',
    287                 'contains', 'icontains', 'iexact', 'startswith', 'istartswith',
     286                'regex', 'iregex', 'month', 'day', 'week_day', 'week_of_year', 'day_of_year',
     287                'search','contains', 'icontains', 'iexact', 'startswith', 'istartswith',
    288288                'endswith', 'iendswith', 'isnull'
    289289            ):
    290290            return value
     
    317317                sql, params = value._as_sql(connection=connection)
    318318            return QueryWrapper(('(%s)' % sql), params)
    319319
    320         if lookup_type in ('regex', 'iregex', 'month', 'day', 'week_day', 'search'):
     320        if lookup_type in ('regex', 'iregex', 'month', 'day', 'week_day', 'week_of_year', 'day_of_year', 'search'):
    321321            return [value]
    322322        elif lookup_type in ('exact', 'gt', 'gte', 'lt', 'lte'):
    323323            return [self.get_db_prep_value(value, connection=connection, prepared=prepared)]
     
    639639    def get_prep_lookup(self, lookup_type, value):
    640640        # For "__month", "__day", and "__week_day" lookups, convert the value
    641641        # to an int so the database backend always sees a consistent type.
    642         if lookup_type in ('month', 'day', 'week_day'):
     642        if lookup_type in ('month', 'day', 'week_day', 'week_of_year', 'day_of_year'):
    643643            return int(value)
    644644        return super(DateField, self).get_prep_lookup(lookup_type, value)
    645645
  • django/db/backends/postgresql/operations.py

     
    2424        if lookup_type == 'week_day':
    2525            # For consistency across backends, we return Sunday=1, Saturday=7.
    2626            return "EXTRACT('dow' FROM %s) + 1" % field_name
     27        elif lookup_type == 'day_of_year':
     28            # http://www.postgresql.org/docs/8.3/static/functions-formatting.html
     29            return "TO_CHAR(%s, 'DDD')" % field_name
     30        elif lookup_type == 'week_of_year':
     31            # http://www.postgresql.org/docs/8.3/static/functions-formatting.html
     32            # returns ISO week of the year.
     33            return "TO_CHAR(%s, 'IW')" % field_name
    2734        else:
    2835            return "EXTRACT('%s' FROM %s)" % (lookup_type, field_name)
    2936
  • django/db/backends/sqlite3/base.py

     
    256256        dt = util.typecast_timestamp(dt)
    257257    except (ValueError, TypeError):
    258258        return None
     259
     260    _, iso_week, iso_week_day = dt.isocalendar()
     261   
    259262    if lookup_type == 'week_day':
    260         return (dt.isoweekday() % 7) + 1
     263        return (iso_week_day % 7) + 1
     264    elif lookup_type == 'week_of_year':
     265        return iso_week
     266    elif lookup_type == 'day_of_year':
     267        return int(dt.strftime('%j'))
    261268    else:
    262269        return getattr(dt, lookup_type)
    263270
  • django/db/backends/mysql/base.py

     
    155155            # DAYOFWEEK() returns an integer, 1-7, Sunday=1.
    156156            # Note: WEEKDAY() returns 0-6, Monday=0.
    157157            return "DAYOFWEEK(%s)" % field_name
     158        elif lookup_type == 'day_of_year':
     159            # DAYOFYEAR() returns the day of the year (1-366)
     160            return "DAYOFYEAR(%s)" % field_name
     161        elif lookup_type == 'week_of_year':
     162            # WEEKOFYEAR() returns the ISO week of the year (1-53).
     163            # The first week having 4 or more days in the year is considered week 1.
     164            return "WEEKOFYEAR(%s)" % field_name
     165       
    158166        else:
    159167            return "EXTRACT(%s FROM %s)" % (lookup_type.upper(), field_name)
    160168
  • django/db/backends/oracle/base.py

     
    115115        if lookup_type == 'week_day':
    116116            # TO_CHAR(field, 'D') returns an integer from 1-7, where 1=Sunday.
    117117            return "TO_CHAR(%s, 'D')" % field_name
     118        elif lookup_type == 'day_of_year':
     119            # TO_CHAR(field, 'DDD') returns an integer from 1-366, representing the day of the year.
     120            return "TO_CHAR(%s, 'DDD')" % field_name
     121        elif lookup_type == 'week_of_year':
     122            # TO_CHAR(field, 'IW') returns an integer from 1-53, representing the ISO week of the year.
     123            return "TO_CHAR(%s, 'IW')" % field_name
    118124        else:
    119125            return "EXTRACT(%s FROM %s)" % (lookup_type, field_name)
    120126
Back to Top