本文整理汇总了Python中ckan.model.meta.Session类的典型用法代码示例。如果您正苦于以下问题:Python Session类的具体用法?Python Session怎么用?Python Session使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Session类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: migrate_extras
def migrate_extras():
conn = Session.connection()
statements = '''
ALTER TABLE datarequests ADD COLUMN extras text;
'''
conn.execute(statements)
Session.commit()
示例2: migrate_to_v0_6
def migrate_to_v0_6():
conn = Session.connection()
statement = """
ALTER TABLE external_catalog
ADD COLUMN create_as_private BOOLEAN NOT NULL DEFAULT FALSE;
"""
conn.execute(statement)
Session.commit()
示例3: GetRecords
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
示例4: migrate_to_v0_4
def migrate_to_v0_4():
conn = Session.connection()
statement = """
ALTER TABLE external_catalog
ADD COLUMN ext_org_id text;
"""
conn.execute(statement)
Session.commit()
示例5: migrate_visibility
def migrate_visibility():
conn = Session.connection()
statements = '''
ALTER TABLE datarequests ADD COLUMN visibility integer;
'''
conn.execute(statements)
Session.commit()
示例6: setup
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()
示例7: migrate_to_v0_3
def migrate_to_v0_3():
conn = Session.connection()
statement = """
ALTER TABLE external_catalog
ADD COLUMN last_updated timestamp,
ADD COLUMN status smallint not null default 0;
"""
conn.execute(statement)
Session.commit()
示例8: create
def create(cls, message, job):
"""
Helper function to create an error object and save it.
"""
err = cls(message=message, job=job)
try:
err.save()
except InvalidRequestError:
Session.rollback()
err.save()
finally:
# No need to alert administrator so don't log as an error
log.info(message)
示例9: create
def create(cls, message, object, stage=u'Fetch', line=None):
'''
Helper function to create an error object and save it.
'''
err = cls(message=message, object=object,
stage=stage, line=line)
try:
err.save()
except InvalidRequestError:
Session.rollback()
err.save()
finally:
log_message = '{0}, line {1}'.format(message, line) \
if line else message
log.debug(log_message)
示例10: migrate_v3
def migrate_v3():
log.debug("Migrating harvest tables to v3. This may take a while...")
conn = Session.connection()
statement = """CREATE TABLE harvest_object_extra (
id text NOT NULL,
harvest_object_id text,
"key" text,
"value" text
);
ALTER TABLE harvest_object
ADD COLUMN import_started timestamp without time zone,
ADD COLUMN import_finished timestamp without time zone,
ADD COLUMN "state" text,
ADD COLUMN "report_status" text;
ALTER TABLE harvest_source
ADD COLUMN frequency text,
ADD COLUMN next_run timestamp without time zone;
ALTER TABLE harvest_job
ADD COLUMN finished timestamp without time zone;
ALTER TABLE harvest_object_extra
ADD CONSTRAINT harvest_object_extra_pkey PRIMARY KEY (id);
ALTER TABLE harvest_object_extra
ADD CONSTRAINT harvest_object_extra_harvest_object_id_fkey FOREIGN KEY (harvest_object_id) REFERENCES harvest_object(id);
UPDATE harvest_object set state = 'COMPLETE' where package_id is not null;
UPDATE harvest_object set state = 'ERROR' where package_id is null;
UPDATE harvest_object set retry_times = 0;
UPDATE harvest_object set report_status = 'updated' where package_id is not null;
UPDATE harvest_object set report_status = 'errored' where package_id is null;
UPDATE harvest_source set frequency = 'MANUAL';
ALTER TABLE harvest_object DROP CONSTRAINT harvest_object_package_id_fkey;
ALTER TABLE harvest_object
ADD CONSTRAINT harvest_object_package_id_fkey FOREIGN KEY (package_id) REFERENCES package(id) DEFERRABLE;
ALTER TABLE harvest_object_error
ADD COLUMN line integer;
"""
conn.execute(statement)
Session.commit()
log.info("Harvest tables migrated to v3")
示例11: GetRecordById
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
示例12: get_package_ids_for_showcase
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
示例13: get_showcase_ids_for_package
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
示例14: _do_drupal_login_by_key
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
示例15: gen_new_name
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