本文整理汇总了Python中eulexistdb.query.QuerySet.filter方法的典型用法代码示例。如果您正苦于以下问题:Python QuerySet.filter方法的具体用法?Python QuerySet.filter怎么用?Python QuerySet.filter使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类eulexistdb.query.QuerySet
的用法示例。
在下文中一共展示了QuerySet.filter方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: index_author
# 需要导入模块: from eulexistdb.query import QuerySet [as 别名]
# 或者: from eulexistdb.query.QuerySet import filter [as 别名]
def index_author(request, author, startswith):
qs = QuerySet(using=ExistDB(), xpath='/tei:TEI', collection='docker/texts/', model=Tei)
if startswith:
# filter by authors starting with letter
qs = qs.filter(author__startswith=author)
else:
qs = qs.filter(author=author)
return render_to_response('browser/index.html', {'tei_documents': qs}, context_instance=RequestContext(request))
示例2: test_filter_gtelte
# 需要导入模块: from eulexistdb.query import QuerySet [as 别名]
# 或者: from eulexistdb.query.QuerySet import filter [as 别名]
def test_filter_gtelte(self):
# < <= > >=
# subclass to add a numeric field to test with
class CountQueryTestModel(QueryTestModel):
name_count = xmlmap.IntegerField('count(name)')
qs = QuerySet(using=self.db, xpath='/root', collection=COLLECTION,
model=CountQueryTestModel)
# each fixture has one and only one name
self.assertEqual(0, qs.filter(name_count__gt=1).count())
self.assertEqual(4, qs.filter(name_count__gte=1).count())
self.assertEqual(4, qs.filter(name_count__lte=1).count())
self.assertEqual(0, qs.filter(name_count__lt=1).count())
示例3: test_only_raw
# 需要导入模块: from eulexistdb.query import QuerySet [as 别名]
# 或者: from eulexistdb.query.QuerySet import filter [as 别名]
def test_only_raw(self):
qs = self.qs.only_raw(id='xs:string(%(xq_var)s//name/ancestor::root/@id)').filter(name='two')
self.assertEqual('abc', qs[0].id)
# filtered version
obj = qs.get()
self.assertEqual('abc', obj.id)
# when combined with regular only, other fields come back correctly
qs = self.qs.only('name', 'description', 'substring')
obj = qs.only_raw(id='xs:string(%(xq_var)s//name/ancestor::root/@id)').get(id='abc')
self.assertEqual('two', obj.name)
self.assertEqual('t', obj.substring)
self.assertEqual('this one only has two', obj.description)
self.assertEqual('abc', obj.id)
# subfield
obj = qs.only_raw(sub__subname='normalize-space(%(xq_var)s//subname)').get(id='one')
self.assertEqual('la', obj.sub.subname)
# multiple parameters
obj = self.qs.filter(id='abc').only_raw(id='string(%(xq_var)s/@id)',
name='normalize-space(%(xq_var)s//name)').get(id='abc')
self.assertEqual('abc', obj.id)
self.assertEqual('two', obj.name)
# list field - multiple return values
class MyQueryTest(QueryTestModel):
name = xmlmap.StringListField('name')
qs = QuerySet(using=self.db, xpath='/root', collection=COLLECTION, model=MyQueryTest)
# return one object but find all the names in the test collection
obj = qs.filter(id='abc').only_raw(name='collection("/db%s")//name' % COLLECTION).get(id='abc')
# 4 names in test fixtures - should come back as a list of those 4 names
self.assertEqual(4, len(obj.name))
示例4: test_also_raw
# 需要导入模块: from eulexistdb.query import QuerySet [as 别名]
# 或者: from eulexistdb.query.QuerySet import filter [as 别名]
def test_also_raw(self):
class SubqueryTestModel(QueryTestModel):
myid = xmlmap.StringField('@id')
qs = QuerySet(using=self.db, collection=COLLECTION, model=SubqueryTestModel, xpath='/root')
qs = qs.filter(id='abc').also_raw(myid='string(%(xq_var)s//name/ancestor::root/@id)')
self.assertEqual('abc', qs[0].myid)
# filtered version of the queryset with raw
obj = qs.filter(name='two').get()
self.assertEqual('abc', obj.myid)
# multiple parameters
obj = qs.filter(id='abc').also_raw(id='string(%(xq_var)s/@id)',
name='normalize-space(%(xq_var)s//name)').get(id='abc')
self.assertEqual('abc', obj.id)
self.assertEqual('two', obj.name)
示例5: test_filter_fulltext_options
# 需要导入模块: from eulexistdb.query import QuerySet [as 别名]
# 或者: from eulexistdb.query.QuerySet import filter [as 别名]
def test_filter_fulltext_options(self):
qs = QuerySet(using=self.db, xpath='/root',
collection=COLLECTION, model=QueryTestModel,
fulltext_options={'default-operator': 'and'})
# search for terms present in fixtures - but not both present in one doc
fqs = qs.filter(description__fulltext_terms='only third')
# for now, just confirm that the option is passed through to query
self.assert_('<default-operator>and</default-operator>' in fqs.query.getQuery())
示例6: index_title
# 需要导入模块: from eulexistdb.query import QuerySet [as 别名]
# 或者: from eulexistdb.query.QuerySet import filter [as 别名]
def index_title(request, letter):
qs = QuerySet(using=ExistDB(), xpath='/tei:TEI', collection='docker/texts/', model=Tei)
# filter by titles starting with letter
qs = qs.filter(title__startswith=letter)
return render_to_response('browser/index.html', {'tei_documents': qs},
context_instance=RequestContext(request))
示例7: index
# 需要导入模块: from eulexistdb.query import QuerySet [as 别名]
# 或者: from eulexistdb.query.QuerySet import filter [as 别名]
def index(request):
qs = QuerySet(using=ExistDB(), xpath='/tei:TEI', collection='docker/texts/', model=RocheTEI)
# Make titles unique (maybe there is a better method?)
qs = qs.filter(chapter='1')
qs = qs.only('title', 'title_en', 'author')
return render_to_response('browser/index.html', {'tei_documents': qs}, context_instance=RequestContext(request))
示例8: text_view_juan
# 需要导入模块: from eulexistdb.query import QuerySet [as 别名]
# 或者: from eulexistdb.query.QuerySet import filter [as 别名]
def text_view_juan(request, title, juan):
"""
Return a single chapter from a title.
"""
qs = QuerySet(using=ExistDB(), xpath='/tei:TEI', collection='docker/texts/', model=RocheTEI)
# filter by title
qs = qs.filter(title=title)
max_juan = qs.count()
qs = qs.filter(chapter=juan)
result = qs[0].body.xsl_transform(xsl=XSL_TRANSFORM_1).serialize()
text_title = qs[0].title
data = {'tei_documents': qs, 'tei_transform': result,
'text_title': text_title, 'max_juan': max_juan, }
return render_to_response('browser/text_view.html', data,
context_instance=RequestContext(request))
示例9: text_download
# 需要导入模块: from eulexistdb.query import QuerySet [as 别名]
# 或者: from eulexistdb.query.QuerySet import filter [as 别名]
def text_download(request, title, file_format, juan=0):
"""
Download a text or a single chapter as plain text file
or as a (colored) pdf.
"""
import pinyin
pinyin_title = pinyin.get(title)
qs = QuerySet(using=ExistDB(), xpath='/tei:TEI',
collection='docker/texts/', model=RocheTEI)
qs = qs.filter(title=title)
if juan:
qs = qs.filter(chapter=juan)
result = ""
for q in qs:
for d in q.body.div:
result += d.text.replace(" ", "").replace("\n", "").replace("\t", "").replace(u"。", u"。\n\n")
if file_format == 'txt':
response = HttpResponse(content_type='text/plain')
response['Content-Disposition'] = 'attachment; filename="{}.txt"'.format(pinyin_title)
response.write(result)
else:
from fpdf import FPDF
pdf = FPDF(unit='mm', format='A4')
pdf.add_page()
pdf.add_font('Droid', '', 'DroidSansFallbackFull.ttf', uni=True)
pdf.set_font('Droid', '', 12)
pdf.write(5, unicode(result))
response = HttpResponse(pdf.output(dest='S'), content_type='application/pdf')
response['Content-Disposition'] = 'attachment; filename="{}.pdf"'.format(pinyin_title)
return response
示例10: text_info
# 需要导入模块: from eulexistdb.query import QuerySet [as 别名]
# 或者: from eulexistdb.query.QuerySet import filter [as 别名]
def text_info(request, title):
qs = QuerySet(using=ExistDB(), xpath='/tei:TEI', collection='docker/texts/', model=RocheTEI)
qs = qs.filter(title=title)
result = ""
place_names = []
persons = []
terms = []
chapter_titles = []
for q in qs:
number_characters = 0
for d in q.body.div:
text = re.sub(RE_INTERPUCTION, '', d.text)
text = text.replace("\n", "")
#text = text.replace("", "")
number_characters += len(text)
if q.chapter_title:
content = q.chapter_title.replace(" ", "").replace("\n", "")[:70]
else:
content = 'XXX'
if q.chapter:
chapter = q.chapter
else:
chapter = 1
chapter_titles.append([chapter,
content,
number_characters])
#place_names.extend(q.place_names)
#persons.extend(q.persons)
#terms.extend(q.terms)
place_names = list(set(place_names))
persons = list(set(persons))
terms = list(set(terms))
# Place names for leaflet
# place_names
js_data = json.dumps([[[50.5, 30.5], "test"]])
return render_to_response('browser/text_view_info.html', {'tei_documents': qs,
'tei_transform': result, 'place_names': place_names,
'persons': persons, 'terms': terms, 'js_data': js_data,
'chapter_titles': sorted(chapter_titles)}, context_instance=RequestContext(request))
示例11: visual_places
# 需要导入模块: from eulexistdb.query import QuerySet [as 别名]
# 或者: from eulexistdb.query.QuerySet import filter [as 别名]
def visual_places(request, title, juan):
qs = QuerySet(using=ExistDB(), xpath='/tei:TEI', collection='docker/texts/', model=RocheTEI)
qs = qs.filter(title=title, chapter=juan)
places = []
for q in qs:
places.extend(q.place_names)
sparql = SPARQLWrapper2(FUSEKI_QUERY_URL)
sparql.setQuery(SPARQL_TIMELINE_QUERY)
try:
sparql_result = sparql.query()
except:
sparql_result = {}
sparql_places = {}
return render_to_response('r/visual_places.html',
{'tei_documents': qs, 'places': places, 'juan': juan, },
context_instance=RequestContext(request))
示例12: render
# 需要导入模块: from eulexistdb.query import QuerySet [as 别名]
# 或者: from eulexistdb.query.QuerySet import filter [as 别名]
def render(self, context):
from browser.models import DDBCPlaceName
try:
self.place_name = self.place_name.resolve(context)
except template.VariableDoesNotExist:
return ''
qs = QuerySet(using=ExistDB(), xpath='/tei:TEI//tei:place', collection='docker/resources/', model=DDBCPlaceName)
qs = qs.filter(place_names=self.place_name)
ddbc_output = u''
for q in qs:
ddbc_output += '<p>'
ddbc_output += 'Other names: ' + u', '.join(q.place_names) + '<br>'
ddbc_output += 'District: ' + q.district + '<br>'
ddbc_output += 'Notes: ' + u' '.join(q.notes) + '<br>'
ddbc_output += 'Location: ' + q.geo + '<br>'
ddbc_output += '</p>'
return ddbc_output
示例13: visual_timeline
# 需要导入模块: from eulexistdb.query import QuerySet [as 别名]
# 或者: from eulexistdb.query.QuerySet import filter [as 别名]
def visual_timeline(request, title, juan):
qs = QuerySet(using=ExistDB(), xpath='/tei:TEI', collection='docker/texts/', model=RocheTEI)
qs = qs.filter(title=title, chapter=juan)
persons = []
for q in qs:
persons.extend(q.persons)
sparql = SPARQLWrapper2(FUSEKI_QUERY_URL)
sparql.setQuery(SPARQL_TIMELINE_QUERY)
try:
sparql_result = sparql.query()
except:
sparql_result = {}
sparql_persons = {}
if sparql_result:
for binding in sparql_result.bindings:
sparql_persons[binding[u"person"].value] = [binding[u"birthYear"].value, binding[u"deathYear"].value]
#persons = [u"范仲淹", u"蘇舜欽", u"韓愈"]
timeline_persons = []
for p in set(persons):
if sparql_persons.get(p, None):
row = [p, ]
row.append(int(sparql_persons[p][0]))
row.append(int(sparql_persons[p][1]))
timeline_persons.append(row)
from operator import itemgetter
timeline_persons = sorted(timeline_persons, key=itemgetter(1))
timeline_persons = json.dumps(timeline_persons)
return render_to_response('r/visual_timeline.html',
{'tei_documents': qs, 'timeline_persons': timeline_persons,
'juan': juan}, context_instance=RequestContext(request))
示例14: ExistQueryTest
# 需要导入模块: from eulexistdb.query import QuerySet [as 别名]
# 或者: from eulexistdb.query.QuerySet import filter [as 别名]
class ExistQueryTest(unittest.TestCase):
def setUp(self):
self.db = ExistDB(server_url=EXISTDB_SERVER_URL)
load_fixtures(self.db)
self.qs = QuerySet(using=self.db, xpath='/root', collection=COLLECTION, model=QueryTestModel)
def tearDown(self):
self.db.removeCollection(COLLECTION)
def test_count(self):
load_fixtures(self.db)
self.assertEqual(NUM_FIXTURES, self.qs.count(), "queryset count returns number of fixtures")
def test_getitem(self):
qs = self.qs.order_by('id') # adding sort order to test reliably
self.assertEqual("abc", qs[0].id)
self.assertEqual("def", qs[1].id)
self.assertEqual("one", qs[2].id)
self.assertEqual("xyz", qs[3].id)
def test_getitem_typeerror(self):
self.assertRaises(TypeError, self.qs.__getitem__, "foo")
def test_getitem_indexerror(self):
self.assertRaises(IndexError, self.qs.__getitem__, -1)
self.assertRaises(IndexError, self.qs.__getitem__, 23)
def test_getslice(self):
slice = self.qs.order_by('id')[0:2]
self.assert_(isinstance(slice, QuerySet))
self.assert_(isinstance(slice[0], QueryTestModel))
self.assertEqual(2, slice.count())
self.assertEqual(2, len(slice))
self.assertEqual('abc', slice[0].id)
self.assertEqual('def', slice[1].id)
self.assertRaises(IndexError, slice.__getitem__, 2)
slice = self.qs.order_by('id')[1:3]
self.assertEqual('def', slice[0].id)
self.assertEqual('one', slice[1].id)
slice = self.qs.order_by('id')[3:5]
self.assertEqual(1, slice.count())
self.assertEqual('xyz', slice[0].id)
self.assertRaises(IndexError, slice.__getitem__, 1)
# test slicing with unspecified bounds
slice = self.qs.order_by('id')[:2]
self.assertEqual(2, slice.count())
self.assertEqual('def', slice[1].id)
slice = self.qs.order_by('id')[1:]
self.assertEqual(3, slice.count())
self.assertEqual('one', slice[1].id)
self.assertEqual('xyz', slice[2].id)
def test_filter(self):
fqs = self.qs.filter(contains="two")
self.assertEqual(1, fqs.count(), "count returns 1 when filtered - contains 'two'")
self.assertEqual("two", fqs[0].name, "name matches filter")
self.assertEqual(NUM_FIXTURES, self.qs.count(), "main queryset remains unchanged by filter")
def test_filter_field(self):
fqs = self.qs.filter(name="one")
self.assertEqual(1, fqs.count(), "count returns 1 when filtered on name = 'one' (got %s)"
% self.qs.count())
self.assertEqual("one", fqs[0].name, "name matches filter")
self.assertEqual(NUM_FIXTURES, self.qs.count(), "main queryset remains unchanged by filter")
def test_filter_field_xpath(self):
fqs = self.qs.filter(id="abc")
self.assertEqual(1, fqs.count(), "count returns 1 when filtered on @id = 'abc' (got %s)"
% self.qs.count())
self.assertEqual("two", fqs[0].name, "name returned is correct for id filter")
self.assertEqual(NUM_FIXTURES, self.qs.count(), "main queryset remains unchanged by filter")
def test_filter_field_contains(self):
fqs = self.qs.filter(name__contains="o")
self.assertEqual(3, fqs.count(),
"should get 3 matches for filter on name contains 'o' (got %s)" % fqs.count())
self.assertEqual(NUM_FIXTURES, self.qs.count(), "main queryset remains unchanged by filter")
def test_filter_field_contains_special(self):
fqs = self.qs.filter(description__contains=' "quote" ')
self.assertEqual(1, fqs.count(),
"should get 1 match for filter on desc contains ' \"quote\" ' (got %s)" % fqs.count())
self.assertEqual(NUM_FIXTURES, self.qs.count(), "main queryset remains unchanged by filter")
fqs = self.qs.filter(description__contains=' &!')
self.assertEqual(1, fqs.count(),
"should get 1 match for filter on desc contains ' &!' (got %s)" % fqs.count())
self.assertEqual(NUM_FIXTURES, self.qs.count(), "main queryset remains unchanged by filter")
def test_filter_field_startswith(self):
fqs = self.qs.filter(name__startswith="o")
self.assertEqual(1, fqs.count(),
"should get 1 match for filter on name starts with 'o' (got %s)" % fqs.count())
self.assertEqual(NUM_FIXTURES, self.qs.count(), "main queryset remains unchanged by filter")
#.........这里部分代码省略.........
示例15: ExistQueryTest__FullText
# 需要导入模块: from eulexistdb.query import QuerySet [as 别名]
# 或者: from eulexistdb.query.QuerySet import filter [as 别名]
class ExistQueryTest__FullText(unittest.TestCase):
# when full-text indexing is enabled, eXist must index files when they are loaded to the db
# this makes tests *significantly* slower
# any tests that require full-text queries should be here
# sample lucene configuration for testing full-text queries
FIXTURE_INDEX = '''
<collection xmlns="http://exist-db.org/collection-config/1.0">
<index>
<lucene>
<analyzer class="org.apache.lucene.analysis.standard.StandardAnalyzer"/>
<text qname="description"/>
<text qname="root"/>
</lucene>
</index>
</collection>
'''
def setUp(self):
self.db = ExistDB(server_url=EXISTDB_SERVER_URL)
# create index for collection - should be applied to newly loaded files
self.db.loadCollectionIndex(COLLECTION, self.FIXTURE_INDEX)
load_fixtures(self.db)
self.qs = QuerySet(using=self.db, xpath='/root',
collection=COLLECTION, model=QueryTestModel)
def tearDown(self):
self.db.removeCollection(COLLECTION)
self.db.removeCollectionIndex(COLLECTION)
def test_filter_fulltext_terms(self):
fqs = self.qs.filter(description__fulltext_terms='only two')
self.assertEqual(1, fqs.count(),
"should get 1 match for fulltext_terms search on = 'only two' (got %s)" % fqs.count())
def test_filter_fulltext_options(self):
qs = QuerySet(using=self.db, xpath='/root',
collection=COLLECTION, model=QueryTestModel,
fulltext_options={'default-operator': 'and'})
# search for terms present in fixtures - but not both present in one doc
fqs = qs.filter(description__fulltext_terms='only third')
# for now, just confirm that the option is passed through to query
self.assert_('<default-operator>and</default-operator>' in fqs.query.getQuery())
# TODO: test this properly!
# query options not supported in current version of eXist
# self.assertEqual(0, fqs.count())
def test_order_by__fulltext_score(self):
fqs = self.qs.filter(description__fulltext_terms='one').order_by('-fulltext_score')
self.assertEqual('one', fqs[0].name) # one appears 3 times, should be first
def test_only__fulltext_score(self):
fqs = self.qs.filter(description__fulltext_terms='one').only('fulltext_score', 'name')
self.assert_(isinstance(fqs[0], QueryTestModel)) # actually a Partial type derived from this
# fulltext score attribute should be present
self.assertNotEqual(fqs[0].fulltext_score, None)
self.assert_(float(fqs[0].fulltext_score) > 0.5) # full-text score should be a float
def test_fulltext_highlight(self):
fqs = self.qs.filter(description__fulltext_terms='only two')
# result from fulltext search - by default, xml should have exist:match tags
self.assert_('<exist:match' in fqs[0].serialize())
fqs = self.qs.filter(description__fulltext_terms='only two', highlight=False)
# with highlighting disabled, should not have exist:match tags
self.assert_('<exist:match' not in fqs[0].serialize())
# order of args in the same filter should not matter
fqs = self.qs.filter(highlight=False, description__fulltext_terms='only two')
# with highlighting disabled, should not have exist:match tags
self.assert_('<exist:match' not in fqs[0].serialize())
# separate filters should also work
fqs = self.qs.filter(description__fulltext_terms='only two').filter(highlight=False)
# with highlighting disabled, should not have exist:match tags
self.assert_('<exist:match' not in fqs[0].serialize())
def test_highlight(self):
fqs = self.qs.filter(highlight='supercalifragilistic')
self.assertEqual(4, fqs.count(),
"highlight filter returns all documents even though search term is not present")
fqs = self.qs.filter(highlight='one').order_by('id')
self.assert_('<exist:match' in fqs[0].serialize())
def test_match_count(self):
fqs = self.qs.filter(id='one', highlight='one').only('match_count')
self.assertEqual(fqs[0].match_count, 4, "4 matched words should be found")
def test_using(self):
fqs = self.qs.using('new-collection')
# using should update the collection on the xquery object
self.assertEqual('new-collection', fqs.query.collection)