Permalink
Browse files

filters_as_Q public method.

  • Loading branch information...
l0kix2 committed Nov 25, 2012
1 parent 89efb3b commit 1f98516cec6daedd7701e4313b666892f387e9d7
View
@@ -8,7 +8,7 @@
setup(
name='django-stored-filters',
- version='0.1',
+ version='0.1.1',
packages=[
'stored_filters',
'stored_filters.tests',
View
@@ -1,2 +1,23 @@
# coding: utf-8
from __future__ import unicode_literals
+
+from django.contrib.contenttypes.models import ContentType
+
+from .models import Filter
+from .query import filters_as_Q
+
+def create_filter(conditions=None, exclusions=None,
+ model_class=None, app_label=None, model_name=None):
+ if model_class:
+ content_type = ContentType.objects.get_for_model(model_class)
+ elif app_label and model_name:
+ content_type = ContentType.objects.get_by_natural_key(
+ app_label=app_label, model=model_name)
+ else:
+ raise Exception(
+ 'You should specify model_class or app_label and model_name')
+
+ return Filter.objects.create(
+ conditions=conditions, exclusions=exclusions, content_type=content_type
+ )
+
@@ -0,0 +1,7 @@
+# coding: utf-8
+from __future__ import unicode_literals
+
+def fetch_lookups(lookups):
+ for lookup in lookups:
+ yield {'conditions': lookup.conditions,
+ 'exclusions': lookup.exclusions}
@@ -0,0 +1,45 @@
+# -*- coding: utf-8 -*-
+import datetime
+from south.db import db
+from south.v2 import SchemaMigration
+from django.db import models
+
+
+class Migration(SchemaMigration):
+
+ def forwards(self, orm):
+ # Adding model 'Filter'
+ db.create_table('stored_filters_filter', (
+ ('id', self.gf('django.db.models.fields.AutoField')(primary_key=True)),
+ ('description', self.gf('django.db.models.fields.CharField')(max_length=255, blank=True)),
+ ('conditions', self.gf('jsonfield.fields.JSONField')(null=True, blank=True)),
+ ('exclusions', self.gf('jsonfield.fields.JSONField')(null=True, blank=True)),
+ ('content_type', self.gf('django.db.models.fields.related.ForeignKey')(to=orm['contenttypes.ContentType'])),
+ ))
+ db.send_create_signal('stored_filters', ['Filter'])
+
+
+ def backwards(self, orm):
+ # Deleting model 'Filter'
+ db.delete_table('stored_filters_filter')
+
+
+ models = {
+ 'contenttypes.contenttype': {
+ 'Meta': {'ordering': "('name',)", 'unique_together': "(('app_label', 'model'),)", 'object_name': 'ContentType', 'db_table': "'django_content_type'"},
+ 'app_label': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
+ 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+ 'model': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
+ 'name': ('django.db.models.fields.CharField', [], {'max_length': '100'})
+ },
+ 'stored_filters.filter': {
+ 'Meta': {'object_name': 'Filter'},
+ 'conditions': ('jsonfield.fields.JSONField', [], {'null': 'True', 'blank': 'True'}),
+ 'content_type': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['contenttypes.ContentType']"}),
+ 'description': ('django.db.models.fields.CharField', [], {'max_length': '255', 'blank': 'True'}),
+ 'exclusions': ('jsonfield.fields.JSONField', [], {'null': 'True', 'blank': 'True'}),
+ 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'})
+ }
+ }
+
+ complete_apps = ['stored_filters']
View
@@ -4,11 +4,18 @@
import operator
from django.db.models import Q
+from stored_filters.helpers import fetch_lookups
+
+
+def filters_as_Q(filters, context=None):
+ filters = list(fetch_lookups(filters))
+ return QueryBuilder(context=context).convert_lookups_to_Q(filters)
+
class QueryBuilder(object):
- def __init__(self, user):
- self.user = user
+ def __init__(self, context=None):
+ self.context = context or {}
def convert_lookups_to_Q(self, lookups):
lookups = self.exclude_blank(lookups)
@@ -50,11 +57,12 @@ def convert_lookup_dict_to_Q(self, lookup_dict):
return Q(**lookup_dict)
def process_macros(self, lookup_dict):
- macro_regex = re.compile(r'^%(?P<attribute>\w+)%$')
+ macro_regex = re.compile(r'^%(?P<macros_key>\w+)%$')
for key, value in lookup_dict.iteritems():
match = macro_regex.match(value)
if match:
- attribute = match.group('attribute')
- user_attribute_value = getattr(self.user, attribute)
- lookup_dict[key] = user_attribute_value
+ macros_key = match.group('macros_key')
+ if macros_key in self.context:
+ user_attribute_value = self.context[macros_key]
+ lookup_dict[key] = user_attribute_value
@@ -13,7 +13,10 @@ def setUp(self):
email='batman@gotham.us')
self.joker = User.objects.create_user(username='joker',
email='joker@gotham.us')
- self.builder = QueryBuilder(user=self.batman)
+ self.builder = QueryBuilder(context={
+ 'username': self.batman.username,
+ 'email': self.batman.email,
+ })
def test_process_macros(self):
lookup_dict = {'username': '%username%',
@@ -98,16 +101,6 @@ def test_merge_conditions_and_exclusions_to_Q_exclusions_only(self):
[self.joker]
)
-# def test_merge_conditions_and_exclusions_to_Q_blank_filters(self):
-# lookup_pair = {
-# 'conditions': None,
-# 'exclusions': None,
-# }
-#
-# query = self.builder.merge_conditions_and_exclusions_to_Q(lookup_pair)
-#
-# self.assertIsNone(query)
-
def test_convert_lookups_to_Q(self):
lookups = [{
'conditions': [{'username__startswith': 'batman'}],

0 comments on commit 1f98516

Please sign in to comment.