Skip to content

Commit

Permalink
added utility function to limit filtering abilities on model resource…
Browse files Browse the repository at this point in the history
…s and applied it to the sentences model
  • Loading branch information
loolmeh committed Jul 15, 2015
1 parent 544d2c0 commit 8d4f521
Showing 1 changed file with 24 additions and 6 deletions.
30 changes: 24 additions & 6 deletions tatoeba2-django/tatoeba2/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,28 @@
'gt', 'gte', 'lt', 'lte'
]

FILTERS = ['exact', 'in']
FILTERS_NUM = FILTERS + ['lt', 'lte', 'gt', 'gte', 'range']
FILTERS_DATE = FILTERS_NUM + ['year', 'month', 'day', 'hour', 'minute']

def set_filters(resource, exclude=[]):
filtering = {}

for f in resource._meta.object_class._meta.fields:
if f.name in exclude:
continue

ftype = type(f).__name__

if ftype == 'CharField':
filtering.update({f.name: FILTERS})
elif ftype in ('AutoField', 'IntegerField'):
filtering.update({f.name: FILTERS_NUM})
elif ftype in ('DateTimeField', 'DateField'):
filtering.update({f.name: FILTERS_DATE})

resource._meta.filtering = filtering


class SentencesResource(ModelResource):
created = fields.DateTimeField(attribute='created', default=datetime(1, 1, 1))
Expand All @@ -24,16 +46,12 @@ class SentencesResource(ModelResource):
class Meta:
queryset = Sentences.objects.all()
allowed_methods = ['get']
excludes = ['dico_id']
authorization = Authorization()
paginator_class = IDPaginator
max_limit = 100

filtering = {}

for f in SentencesResource._meta.object_class._meta.fields:
filtering.update({f.name: ALL})

SentencesResource._meta.filtering = filtering
set_filters(SentencesResource, exclude=['text'])


class SentencesSearchResource(BaseSearchResource):
Expand Down

0 comments on commit 8d4f521

Please sign in to comment.