本文整理汇总了Python中ckan.model.meta.Session.execute方法的典型用法代码示例。如果您正苦于以下问题:Python Session.execute方法的具体用法?Python Session.execute怎么用?Python Session.execute使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ckan.model.meta.Session
的用法示例。
在下文中一共展示了Session.execute方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: GetRecords
# 需要导入模块: from ckan.model.meta import Session [as 别名]
# 或者: from ckan.model.meta.Session import execute [as 别名]
def GetRecords(self, req):
resp = etree.Element(ntag("csw:GetRecordsResponse"), nsmap=namespaces)
etree.SubElement(resp, ntag("csw:SearchStatus"), timestamp=datetime.utcnow().isoformat())
cursor = Session.connection()
q = Session.query(distinct(HarvestObject.guid)) \
.join(Package) \
.join(HarvestSource) \
.filter(HarvestObject.current==True) \
.filter(Package.state==u'active') \
.filter(or_(HarvestSource.type=='gemini-single', \
HarvestSource.type=='gemini-waf', \
HarvestSource.type=='csw'))
### TODO Parse query instead of stupidly just returning whatever we like
startPosition = req["startPosition"] if req["startPosition"] > 0 else 1
maxRecords = req["maxRecords"] if req["maxRecords"] > 0 else 10
rset = q.offset(startPosition-1).limit(maxRecords)
total = q.count()
attrs = {
"numberOfRecordsMatched": total,
"elementSet": req["elementSetName"], # we lie here. it's always really "full"
}
if req["resultType"] == "results":
returned = rset.count()
attrs["numberOfRecordsReturned"] = returned
if (total-startPosition-1) > returned:
attrs["nextRecord"] = startPosition + returned
else:
attrs["nextRecord"] = 0
else:
attrs["numberOfRecordsReturned"] = 0
attrs = dict((k, unicode(v)) for k,v in attrs.items())
results = etree.SubElement(resp, ntag("csw:SearchResults"), **attrs)
if req["resultType"] == "results":
for guid, in Session.execute(rset):
doc = Session.query(HarvestObject) \
.join(Package) \
.filter(HarvestObject.guid==guid) \
.filter(HarvestObject.current==True) \
.filter(Package.state==u'active') \
.first()
try:
record = etree.parse(StringIO(doc.content.encode("utf-8")))
results.append(record.getroot())
except:
log.error("exception parsing document %s:\n%s", doc.id, traceback.format_exc())
raise
data = self._render_xml(resp)
log.info('GetRecords response: %r', truncate(data, LOG_XML_LENGTH))
return data