Skip to content

Commit

Permalink
Merge branch 'feature/genericipaddressfield'
Browse files Browse the repository at this point in the history
  • Loading branch information
tiliv committed Jul 23, 2014
2 parents a9f8d41 + e73a8e1 commit 31b916b
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 14 deletions.
4 changes: 2 additions & 2 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
include README.md
include requirements.txt
recursive-include datatableview static/*
recursive-include datatableview templates/*
recursive-include datatableview/static *
recursive-include datatableview/templates *
4 changes: 2 additions & 2 deletions datatableview/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

import six

from .utils import resolve_orm_path, FIELD_TYPES
from .utils import resolve_orm_path, XEDITABLE_FIELD_TYPES

if get_version().split('.') >= ['1', '5']:
from django.utils.timezone import localtime
Expand Down Expand Up @@ -231,7 +231,7 @@ def make_xeditable(instance=None, extra_attrs=[], *args, **kwargs):
if field.choices:
field_type = 'select'
else:
field_type = FIELD_TYPES.get(field.get_internal_type(), 'text')
field_type = XEDITABLE_FIELD_TYPES.get(field.get_internal_type(), 'text')
else:
field_type = 'text'
attrs['data-type'] = field_type
Expand Down
23 changes: 21 additions & 2 deletions datatableview/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
except ImportError:
from UserDict import UserDict

from django.db import models
from django.db.models.fields import FieldDoesNotExist
from django.template.loader import render_to_string
from django.forms.util import flatatt
Expand Down Expand Up @@ -46,9 +47,27 @@
'sort_column_direction': 'sSortDir_%d',
}

# Mapping of Django's supported field types to their more generic type names.
# These values are primarily used for the xeditable field type lookups
# Mapping of Django field categories to the set of field classes falling into that category.
# This is used during field searches to know which ORM language queries can be applied to a field,
# such as "__icontains" or "__year".
FIELD_TYPES = {
'text': [models.CharField, models.TextField, models.FileField],
'date': [models.DateField],
'boolean': [models.BooleanField],
'integer': [models.IntegerField, models.AutoField],
'float': [models.FloatField, models.DecimalField],

# This is a special type for fields that should be passed up, since there is no intuitive
# meaning for searches done agains the FK field directly.
'ignored': [models.ForeignKey],
}
if hasattr(models, 'GenericIPAddressField'):
FIELD_TYPES['text'].append(models.GenericIPAddressField)

# Mapping of Django's supported field types to their more generic type names.
# These values are primarily used for the xeditable field type lookups.
# TODO: Would be nice if we can derive these from FIELD_TYPES so there's less repetition.
XEDITABLE_FIELD_TYPES = {
'AutoField': 'number',
'BooleanField': 'text',
'CharField': 'text',
Expand Down
15 changes: 7 additions & 8 deletions datatableview/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import dateutil.parser

from .forms import XEditableUpdateForm
from .utils import (ObjectListResult, DatatableOptions, split_real_fields,
from .utils import (FIELD_TYPES, ObjectListResult, DatatableOptions, split_real_fields,
filter_real_fields, get_datatable_structure, resolve_orm_path, get_first_orm_bit,
get_field_definition)

Expand Down Expand Up @@ -136,10 +136,9 @@ def apply_queryset_options(self, queryset):
field_queries = [] # Queries generated to search this database field for the search term

field = resolve_orm_path(self.model, component_name)

if isinstance(field, (models.CharField, models.TextField, models.FileField)):
if isinstance(field, tuple(FIELD_TYPES['text'])):
field_queries = [{component_name + '__icontains': term}]
elif isinstance(field, models.DateField):
elif isinstance(field, tuple(FIELD_TYPES['date'])):
try:
date_obj = dateutil.parser.parse(term)
except ValueError:
Expand All @@ -163,7 +162,7 @@ def apply_queryset_options(self, queryset):
field_queries.append({component_name + '__month': numerical_value})
if 0 < numerical_value <= 31:
field_queries.append({component_name + '__day': numerical_value})
elif isinstance(field, models.BooleanField):
elif isinstance(field, tuple(FIELD_TYPES['boolean'])):
if term.lower() in ('true', 'yes'):
term = True
elif term.lower() in ('false', 'no'):
Expand All @@ -172,17 +171,17 @@ def apply_queryset_options(self, queryset):
continue

field_queries = [{component_name: term}]
elif isinstance(field, (models.IntegerField, models.AutoField)):
elif isinstance(field, tuple(FIELD_TYPES['integer'])):
try:
field_queries = [{component_name: int(term)}]
except ValueError:
pass
elif isinstance(field, (models.FloatField, models.DecimalField)):
elif isinstance(field, tuple(FIELD_TYPES['float'])):
try:
field_queries = [{component_name: float(term)}]
except ValueError:
pass
elif isinstance(field, models.ForeignKey):
elif isinstance(field, tuple(FIELD_TYPES['ignored'])):
pass
else:
raise ValueError("Unhandled field type for %s (%r) in search." % (component_name, type(field)))
Expand Down

0 comments on commit 31b916b

Please sign in to comment.