本文整理汇总了Python中ckan.model.meta.Session.query方法的典型用法代码示例。如果您正苦于以下问题:Python Session.query方法的具体用法?Python Session.query怎么用?Python Session.query使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ckan.model.meta.Session
的用法示例。
在下文中一共展示了Session.query方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: clean_harvest_log
# 需要导入模块: from ckan.model.meta import Session [as 别名]
# 或者: from ckan.model.meta.Session import query [as 别名]
def clean_harvest_log(condition):
Session.query(HarvestLog).filter(HarvestLog.created <= condition).delete(synchronize_session=False)
try:
Session.commit()
except InvalidRequestError:
Session.rollback()
log.error("An error occurred while trying to clean-up the harvest log table")
log.info("Harvest log table clean-up finished successfully")
示例2: GetRecords
# 需要导入模块: from ckan.model.meta import Session [as 别名]
# 或者: from ckan.model.meta.Session import query [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
示例3: setup
# 需要导入模块: from ckan.model.meta import Session [as 别名]
# 或者: from ckan.model.meta.Session import query [as 别名]
def setup():
if harvest_source_table is None:
define_harvester_tables()
log.debug("Harvest tables defined in memory")
if not model.package_table.exists():
log.debug("Harvest table creation deferred")
return
if not harvest_source_table.exists():
# Create each table individually rather than
# using metadata.create_all()
harvest_source_table.create()
harvest_job_table.create()
harvest_object_table.create()
harvest_gather_error_table.create()
harvest_object_error_table.create()
harvest_object_extra_table.create()
harvest_log_table.create()
log.debug("Harvest tables created")
else:
from ckan.model.meta import engine
log.debug("Harvest tables already exist")
# Check if existing tables need to be updated
inspector = Inspector.from_engine(engine)
columns = inspector.get_columns("harvest_source")
column_names = [column["name"] for column in columns]
if not "title" in column_names:
log.debug("Harvest tables need to be updated")
migrate_v2()
if not "frequency" in column_names:
log.debug("Harvest tables need to be updated")
migrate_v3()
# Check if this instance has harvest source datasets
source_ids = Session.query(HarvestSource.id).filter_by(active=True).all()
source_package_ids = Session.query(model.Package.id).filter_by(type=u"harvest", state="active").all()
sources_to_migrate = set(source_ids) - set(source_package_ids)
if sources_to_migrate:
log.debug("Creating harvest source datasets for %i existing sources", len(sources_to_migrate))
sources_to_migrate = [s[0] for s in sources_to_migrate]
migrate_v3_create_datasets(sources_to_migrate)
# Check if harvest_log table exist - needed for existing users
if not "harvest_log" in inspector.get_table_names():
harvest_log_table.create()
# Check if harvest_object has a index
index_names = [index["name"] for index in inspector.get_indexes("harvest_object")]
if not "harvest_job_id_idx" in index_names:
log.debug("Creating index for harvest_object")
Index("harvest_job_id_idx", harvest_object_table.c.harvest_job_id).create()
示例4: GetRecordById
# 需要导入模块: from ckan.model.meta import Session [as 别名]
# 或者: from ckan.model.meta.Session import query [as 别名]
def GetRecordById(self, req):
resp = etree.Element(ntag("csw:GetRecordByIdResponse"), nsmap=namespaces)
seen = set()
for ident in req["id"]:
doc = Session.query(HarvestObject) \
.join(Package) \
.join(HarvestJob).join(HarvestSource) \
.filter(HarvestSource.active==True) \
.filter(HarvestObject.guid==ident) \
.filter(HarvestObject.package!=None) \
.filter(Package.state==u'active') \
.order_by(HarvestObject.gathered.desc()) \
.limit(1).first()
if doc is None:
continue
if 'MD_Metadata' in doc.content:
try:
record = etree.parse(StringIO(doc.content.encode("utf-8")))
resp.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('GetRecordById response: %r', truncate(data, LOG_XML_LENGTH))
return data
示例5: _do_drupal_login_by_key
# 需要导入模块: from ckan.model.meta import Session [as 别名]
# 或者: from ckan.model.meta.Session import query [as 别名]
def _do_drupal_login_by_key(self, environ, drupal_apikey, new_headers):
from ckan import model
from ckan.model.meta import Session
try:
user = Session.query(model.User).filter_by(apikey=drupal_apikey).one()
except Exception, e:
log.debug('Drupal user not found in CKAN: %s', e)
return
示例6: get_showcase_ids_for_package
# 需要导入模块: from ckan.model.meta import Session [as 别名]
# 或者: from ckan.model.meta.Session import query [as 别名]
def get_showcase_ids_for_package(cls, package_id):
'''
Return a list of showcase ids associated with the passed package_id.
'''
showcase_package_association_list = \
Session.query(cls.showcase_id).filter_by(
package_id=package_id).all()
return showcase_package_association_list
示例7: get_package_ids_for_showcase
# 需要导入模块: from ckan.model.meta import Session [as 别名]
# 或者: from ckan.model.meta.Session import query [as 别名]
def get_package_ids_for_showcase(cls, showcase_id):
'''
Return a list of package ids associated with the passed showcase_id.
'''
showcase_package_association_list = \
Session.query(cls.package_id).filter_by(
showcase_id=showcase_id).all()
return showcase_package_association_list
示例8: gen_new_name
# 需要导入模块: from ckan.model.meta import Session [as 别名]
# 或者: from ckan.model.meta.Session import query [as 别名]
def gen_new_name(title):
name = munge_title_to_name(title).replace("_", "-")
while "--" in name:
name = name.replace("--", "-")
pkg_obj = Session.query(Package).filter(Package.name == name).first()
if pkg_obj:
return name + str(uuid.uuid4())[:5]
else:
return name
示例9: setup
# 需要导入模块: from ckan.model.meta import Session [as 别名]
# 或者: from ckan.model.meta.Session import query [as 别名]
def setup():
if harvest_source_table is None:
define_harvester_tables()
log.debug('Harvest tables defined in memory')
if not model.package_table.exists():
log.debug('Harvest table creation deferred')
return
if not harvest_source_table.exists():
# Create each table individually rather than
# using metadata.create_all()
harvest_source_table.create()
harvest_job_table.create()
harvest_object_table.create()
harvest_gather_error_table.create()
harvest_object_error_table.create()
harvest_object_extra_table.create()
log.debug('Harvest tables created')
else:
from ckan.model.meta import engine
log.debug('Harvest tables already exist')
# Check if existing tables need to be updated
inspector = Inspector.from_engine(engine)
columns = inspector.get_columns('harvest_source')
column_names = [column['name'] for column in columns]
if not 'title' in column_names:
log.debug('Harvest tables need to be updated')
migrate_v2()
if not 'frequency' in column_names:
log.debug('Harvest tables need to be updated')
migrate_v3()
# Check if this instance has harvest source datasets
source_ids = Session.query(HarvestSource.id).filter_by(active=True).all()
source_package_ids = Session.query(model.Package.id).filter_by(type=u'harvest', state='active').all()
sources_to_migrate = set(source_ids) - set(source_package_ids)
if sources_to_migrate:
log.debug('Creating harvest source datasets for %i existing sources', len(sources_to_migrate))
sources_to_migrate = [s[0] for s in sources_to_migrate]
migrate_v3_create_datasets(sources_to_migrate)
示例10: migrate_v2
# 需要导入模块: from ckan.model.meta import Session [as 别名]
# 或者: from ckan.model.meta.Session import query [as 别名]
def migrate_v2():
log.debug("Migrating harvest tables to v2. This may take a while...")
conn = Session.connection()
statements = """
ALTER TABLE harvest_source ADD COLUMN title text;
ALTER TABLE harvest_object ADD COLUMN current boolean;
ALTER TABLE harvest_object ADD COLUMN harvest_source_id text;
ALTER TABLE harvest_object ADD CONSTRAINT harvest_object_harvest_source_id_fkey FOREIGN KEY (harvest_source_id) REFERENCES harvest_source(id);
UPDATE harvest_object o SET harvest_source_id = j.source_id FROM harvest_job j WHERE o.harvest_job_id = j.id;
"""
conn.execute(statements)
# Flag current harvest_objects
guids = (
Session.query(distinct(HarvestObject.guid))
.join(Package)
.filter(HarvestObject.package != None)
.filter(Package.state == u"active")
)
update_statement = """
UPDATE harvest_object
SET current = TRUE
WHERE id = (
SELECT o.id
FROM harvest_object o JOIN package p ON p.id = o.package_id
WHERE o.package_id IS NOT null AND p.state = 'active'
AND o.guid = '%s'
ORDER BY metadata_modified_date DESC, fetch_finished DESC, gathered DESC
LIMIT 1)
"""
for guid in guids:
conn.execute(update_statement % guid)
conn.execute("UPDATE harvest_object SET current = FALSE WHERE current IS NOT TRUE")
Session.commit()
log.info("Harvest tables migrated to v2")
示例11: by_pipeline_id
# 需要导入模块: from ckan.model.meta import Session [as 别名]
# 或者: from ckan.model.meta.Session import query [as 别名]
def by_pipeline_id(cls, pipeline_id):
assert pipeline_id
return Session.query(cls)\
.filter_by(pipeline_id = pipeline_id).first()
示例12: get
# 需要导入模块: from ckan.model.meta import Session [as 别名]
# 或者: from ckan.model.meta.Session import query [as 别名]
def get(user_id):
q = Session.query(UserGithubInfo).\
filter(UserGithubInfo.user_id == user_id)
return q.first()
示例13: get_showcase_admin_ids
# 需要导入模块: from ckan.model.meta import Session [as 别名]
# 或者: from ckan.model.meta.Session import query [as 别名]
def get_showcase_admin_ids(cls):
'''
Return a list of showcase admin user ids.
'''
id_list = [i for (i, ) in Session.query(cls.user_id).all()]
return id_list
示例14: by_id
# 需要导入模块: from ckan.model.meta import Session [as 别名]
# 或者: from ckan.model.meta.Session import query [as 别名]
def by_id(cls, id):
assert id
return Session.query(cls).filter_by(id=id).first()
示例15: has_catalog
# 需要导入模块: from ckan.model.meta import Session [as 别名]
# 或者: from ckan.model.meta.Session import query [as 别名]
def has_catalog(cls, package_id, url):
return Session.query(cls).filter_by(package_id=package_id, url=url).count() > 0;