本文整理匯總了Python中ebpub.db.schemafilters.FilterChain類的典型用法代碼示例。如果您正苦於以下問題:Python FilterChain類的具體用法?Python FilterChain怎麽用?Python FilterChain使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
在下文中一共展示了FilterChain類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: get_featured_lookups_by_schema
def get_featured_lookups_by_schema(context):
"""
Get all :ref:`featured_lookups` names and URLs for them;
puts in the context as
'featured_lookups', a mapping grouped by schema.
Example:
.. code-block:: html+django
{% get_featured_lookups_by_schema %}
{% for schema, lookups in featured_lookups.items %}
<ul>{{ schema }}
{% for info in lookups %}
<a href="{{ info.url }}">{{ info.lookup }}</a>
...
{% endfor %}
{% endfor %}
"""
lookups = {}
for lookup in Lookup.objects.filter(featured=True).select_related():
sf = lookup.schema_field
schema = sf.schema
filters = FilterChain(schema=schema)
filters.add(sf, lookup)
info = {"lookup": lookup.name, "url": filters.make_url()}
lookups.setdefault(schema.slug, []).append(info)
context["featured_lookups"] = lookups
return u""
示例2: _make_chain
def _make_chain(self, url):
request = RequestFactory().get(url)
crime = models.Schema.objects.get(slug='crime')
context = {'schema': crime}
chain = FilterChain(request=request, context=context, schema=crime)
chain.update_from_request(filter_sf_dict={})
return chain
示例3: test_copy_and_mutate
def test_copy_and_mutate(self):
schema = mock.Mock()
chain = FilterChain(schema=schema)
chain.lookup_descriptions.append(1)
chain.base_url = 'http://xyz'
chain['foo'] = 'bar'
chain['qux'] = 'whee'
clone = chain.copy()
# Attributes are copied...
self.assertEqual(clone.lookup_descriptions, [1])
self.assertEqual(clone.base_url, chain.base_url)
self.assertEqual(clone.schema, chain.schema, schema)
# ... and mutating them doesn't affect the original.
clone.lookup_descriptions.pop()
self.assertEqual(chain.lookup_descriptions, [1])
# Likewise, items are copied, and mutating doesn't affect the copy.
self.assertEqual(clone['foo'], 'bar')
del chain['foo']
self.assertEqual(clone['foo'], 'bar')
del clone['qux']
self.assertEqual(chain['qux'], 'whee')
# Likewise, clearing.
clone.clear()
self.assertEqual(clone.items(), [])
self.assertEqual(chain['qux'], 'whee')
示例4: test_sort__real_filters
def test_sort__real_filters(self):
req = mock.Mock()
qs = mock.Mock()
schema = mock.Mock()
context = {"newsitem_qs": qs, "schema": schema}
from ebpub.db.schemafilters import TextSearchFilter, BoolFilter
from ebpub.db.schemafilters import LookupFilter, LocationFilter
from ebpub.db.schemafilters import DateFilter
def mock_schemafield(name):
# mock.Mock(name='foo') does something magic, but I just
# want to set the name attribute.
sf = mock.Mock()
sf.name = name
return sf
all_filters = [
TextSearchFilter(req, context, qs, "hi", schemafield=mock_schemafield(name="mock text sf")),
BoolFilter(req, context, qs, "yes", schemafield=mock_schemafield(name="mock bool sf")),
LookupFilter(req, context, qs, schemafield=mock_schemafield(name="mock lookup sf")),
LocationFilter(req, context, qs, "neighborhoods"),
DateFilter(req, context, qs, "2011-04-11", "2011-04-12"),
]
chain = FilterChain([(item.slug, item) for item in all_filters])
ordered_chain = chain.copy()
ordered_chain.sort()
self.assertEqual(ordered_chain.keys(), ["date", "mock bool sf", "location", "mock lookup sf", "mock text sf"])
示例5: test_sort__real_filters
def test_sort__real_filters(self):
req = mock.Mock()
qs = mock.Mock()
schema = mock.Mock()
context = {'newsitem_qs': qs, 'schema': schema}
from ebpub.db.schemafilters import TextSearchFilter, BoolFilter
from ebpub.db.schemafilters import LookupFilter, LocationFilter
from ebpub.db.schemafilters import DateFilter
def mock_schemafield(name):
# mock.Mock(name='foo') does something magic, but I just
# want to set the name attribute.
sf = mock.Mock()
sf.name = name
return sf
all_filters = [
TextSearchFilter(req, context, qs, 'hi',
schemafield=mock_schemafield(name='mock text sf')),
BoolFilter(req, context, qs, 'yes',
schemafield=mock_schemafield(name='mock bool sf')),
LookupFilter(req, context, qs,
schemafield=mock_schemafield(name='mock lookup sf')),
LocationFilter(req, context, qs, 'neighborhoods'),
DateFilter(req, context, qs, '2011-04-11', '2011-04-12'),
]
chain = FilterChain([(item.slug, item) for item in all_filters])
ordered_chain = chain.copy()
ordered_chain.sort()
self.assertEqual(ordered_chain.keys(),
['date', 'mock bool sf', 'location', 'mock lookup sf', 'mock text sf'])
示例6: schema_filter
def schema_filter(request, slug, args_from_url):
"""
List NewsItems for one schema, filtered by various criteria in the
URL (date, location, or values of SchemaFields).
"""
s = get_object_or_404(get_schema_manager(request), slug=slug, is_special_report=False)
if not s.allow_charting:
return HttpResponsePermanentRedirect(s.url())
context = {
'bodyclass': 'schema-filter',
'bodyid': s.slug,
'schema': s,
}
# Breadcrumbs. We can assign this early because it's a generator,
# so it'll get the full context no matter what.
context['breadcrumbs'] = breadcrumbs.schema_filter(context)
filter_sf_list = list(SchemaField.objects.filter(schema__id=s.id, is_filter=True).order_by('display_order'))
textsearch_sf_list = list(SchemaField.objects.filter(schema__id=s.id, is_searchable=True).order_by('display_order'))
# Use SortedDict to preserve the display_order.
filter_sf_dict = SortedDict([(sf.name, sf) for sf in filter_sf_list] + [(sf.name, sf) for sf in textsearch_sf_list])
# Determine what filters to apply, based on path and/or query string.
filterchain = FilterChain(request=request, context=context, schema=s)
context['filters'] = filterchain
try:
filterchain.update_from_request(args_from_url, filter_sf_dict)
filters_need_more = filterchain.validate()
except FilterError, e:
if getattr(e, 'url', None) is not None:
return HttpResponseRedirect(e.url)
raise Http404(str(e))
示例7: _make_chain
def _make_chain(self, url):
request = RequestFactory().get(url)
argstring = request.path.split("filter/", 1)[-1]
crime = models.Schema.objects.get(slug="crime")
context = {"schema": crime}
chain = FilterChain(request=request, context=context, schema=crime)
chain.update_from_request(argstring=argstring, filter_sf_dict={})
return chain
示例8: test_ordering
def test_ordering(self):
chain = FilterChain()
args = range(10)
random.shuffle(args)
for i in args:
chain[i] = i
self.assertEqual(chain.items(), [(i, i) for i in args])
self.assertEqual(chain.keys(), args)
示例9: test_add_date__whole_month
def test_add_date__whole_month(self):
# Special syntax for adding a whole month, convenient for templates
# where you don't want to have to calculate the end date.
chain = FilterChain()
import datetime
chain.add('date', datetime.date(2011, 8, 13), 'month')
self.assertEqual(chain['date'].start_date, datetime.date(2011, 8, 1))
self.assertEqual(chain['date'].end_date, datetime.date(2011, 8, 31))
示例10: test_add__pubdate
def test_add__pubdate(self):
# Key gets overridden.
chain = FilterChain()
import datetime
chain.add('pubdate', datetime.date(2011, 8, 13))
self.assert_(chain.has_key('date'))
self.failIf(chain.has_key('pubdate'))
self.assertEqual(chain['date'].start_date, datetime.date(2011, 8, 13))
示例11: test_filters_for_display
def test_filters_for_display(self):
class Dummy(object):
def __init__(self, label):
self.label = label
chain = FilterChain(
[("foo", Dummy("yes")), ("bar", Dummy(None)), ("bat", Dummy("yes also")), ("baz", Dummy(None))]
)
self.assertEqual(len(chain.values()), 4)
self.assertEqual(len(chain.filters_for_display()), 2)
self.assert_(all([f.label for f in chain.filters_for_display()]))
示例12: test_update_from_request__empty
def test_update_from_request__empty(self):
request = mock.Mock()
class StubQueryDict(dict):
def getlist(self, key):
return []
def copy(self):
return StubQueryDict(self.items())
request.GET = StubQueryDict()
chain = FilterChain(request=request)
chain.update_from_request({})
self.assertEqual(len(chain), 0)
示例13: test_filters_for_display
def test_filters_for_display(self):
class Dummy(object):
def __init__(self, label):
self.label = label
chain = FilterChain([('foo', Dummy('yes')),
('bar', Dummy(None)),
('bat', Dummy('yes also')),
('baz', Dummy(None)),
])
self.assertEqual(len(chain.values()), 4)
self.assertEqual(len(chain.filters_for_display()), 2)
self.assert_(all([f.label for f in chain.filters_for_display()]))
示例14: test_add_by_place_id
def test_add_by_place_id(self, mock_get_object_or_404):
chain = FilterChain()
from ebpub.streets.models import Block
from ebpub.db.schemafilters import BlockFilter
block = Block(city='city', street_slug='street_slug',
pretty_name='pretty_name',
street_pretty_name='street_pretty_name',
street='street',
from_num='123', to_num='456',
)
mock_get_object_or_404.return_value = block
chain.add_by_place_id('b:123.1')
self.assert_(isinstance(chain['location'], BlockFilter))
示例15: test_sort
def test_sort(self):
class Dummy(object):
def __init__(self, sort_value):
self._sort_value = sort_value
dummies = [Dummy(i) for i in range(10)]
random.shuffle(dummies)
chain = FilterChain()
for i in range(10):
chain[i] = dummies[i]
self.assertNotEqual(range(10), [v._sort_value for v in chain.values()])
normalized = chain.copy()
normalized.sort()
self.assertEqual(range(10), [v._sort_value for v in normalized.values()])