本文整理汇总了Python中database.DBSession.query方法的典型用法代码示例。如果您正苦于以下问题:Python DBSession.query方法的具体用法?Python DBSession.query怎么用?Python DBSession.query使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类database.DBSession
的用法示例。
在下文中一共展示了DBSession.query方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: api_get_distinct_host_regions
# 需要导入模块: from database import DBSession [as 别名]
# 或者: from database.DBSession import query [as 别名]
def api_get_distinct_host_regions(platform, software_versions):
"""
software_versions may equal to 'ALL' or multiple software versions
"""
clauses = []
db_session = DBSession()
clauses.append(Host.software_platform == platform)
if 'ALL' not in software_versions:
clauses.append(Host.software_version.in_(software_versions.split(',')))
region_ids = db_session.query(Host.region_id).filter(and_(*clauses)).distinct()
# Change a list of tuples to a list
region_ids_list = [region_id[0] for region_id in region_ids]
rows = []
if not is_empty(region_ids):
regions = db_session.query(Region).filter(Region.id.in_(region_ids_list)). \
order_by(Region.name.asc()).all()
for region in regions:
rows.append({'region_id': region.id, 'region_name': region.name})
return jsonify(**{'data': rows})
示例2: api_get_session_logs
# 需要导入模块: from database import DBSession [as 别名]
# 或者: from database.DBSession import query [as 别名]
def api_get_session_logs(table):
id = request.args.get("record_id")
db_session = DBSession()
if table == 'install_job':
install_job = db_session.query(InstallJob).filter(InstallJob.id == id).first()
elif table == 'install_job_history':
install_job = db_session.query(InstallJobHistory).filter(InstallJobHistory.id == id).first()
elif table == 'inventory_job_history':
install_job = db_session.query(InventoryJobHistory).filter(InventoryJobHistory.id == id).first()
if install_job is None:
abort(404)
log_folder = install_job.session_log
file_path = os.path.join(get_log_directory(), log_folder)
if not os.path.isdir(file_path):
abort(404)
rows = []
log_file_list = get_file_list(file_path)
for file in log_file_list:
row = dict()
row['filepath'] = os.path.join(file_path, file)
row['filename'] = file
rows.append(row)
return jsonify(**{'data': rows})
示例3: dispatch
# 需要导入模块: from database import DBSession [as 别名]
# 或者: from database.DBSession import query [as 别名]
def dispatch(self):
db_session = DBSession()
try:
# Check if Scheduled Installs are allowed to run.
if not db_session.query(SystemOption).first().can_install:
return
install_jobs = db_session.query(InstallJob).filter(
InstallJob.scheduled_time <= datetime.datetime.utcnow()).order_by(InstallJob.scheduled_time.asc()).all()
download_job_key_dict = get_download_job_key_dict()
if len(install_jobs) > 0:
for install_job in install_jobs:
if install_job.status != JobStatus.FAILED:
# If there is pending download, don't submit the install job
if self.is_pending_on_download(download_job_key_dict, install_job):
continue
# This install job has a dependency, check if the expected criteria is met
if install_job.dependency is not None:
dependency_completed = self.get_install_job_dependency_completed(db_session, install_job)
# If the dependency has not been completed, don't proceed
if len(dependency_completed) == 0:
continue
self.submit_job(InstallWorkUnit(install_job.host_id, install_job.id))
except Exception:
# print(traceback.format_exc())
# Purpose ignore. Otherwise, it may generate continue exception
pass
finally:
db_session.close()
示例4: refresh_all
# 需要导入模块: from database import DBSession [as 别名]
# 或者: from database.DBSession import query [as 别名]
def refresh_all(cls):
"""
Retrieves all the catalog data and SMU XML file data and updates the database.
"""
db_session = DBSession()
catalog = SMUInfoLoader.get_catalog_from_cco()
if len(catalog) > 0:
system_option = SystemOption.get(db_session)
try:
# Remove all rows first
db_session.query(CCOCatalog).delete()
for platform in catalog:
releases = catalog[platform]
for release in releases:
cco_catalog = CCOCatalog(platform=platform,release=release)
db_session.add(cco_catalog)
SMUInfoLoader(platform, release)
system_option.cco_lookup_time = datetime.datetime.utcnow()
db_session.commit()
return True
except Exception:
logger.exception('refresh_all() hit exception')
db_session.rollback()
return False
示例5: init_sys_time
# 需要导入模块: from database import DBSession [as 别名]
# 或者: from database.DBSession import query [as 别名]
def init_sys_time():
db_session = DBSession()
if db_session.query(System).count() == 0:
db_session.add(System())
db_session.commit()
else:
system = db_session.query(System).first()
system.start_time = datetime.datetime.utcnow()
db_session.commit()
示例6: process
# 需要导入模块: from database import DBSession [as 别名]
# 或者: from database.DBSession import query [as 别名]
def process(self):
db_session = DBSession()
host_id = None
inventory_job = None
ctx = None
try:
inventory_job = db_session.query(InventoryJob).filter(InventoryJob.id == self.job_id).first()
if inventory_job is None:
logger.error('Unable to retrieve inventory job: %s' % self.job_id)
return
host_id = inventory_job.host_id
host = db_session.query(Host).filter(Host.id == host_id).first()
if host is None:
logger.error('Unable to retrieve host: %s' % host_id)
handler_class = get_inventory_handler_class(host.platform)
if handler_class is None:
logger.error('Unable to get handler for %s, inventory job %s', host.platform, self.job_id)
inventory_job.set_status(JobStatus.PROCESSING)
inventory_job.session_log = create_log_directory(host.connection_param[0].host_or_ip, inventory_job.id)
db_session.commit()
# Delegates the get_software logic to the handler
ctx = InventoryContext(host, db_session, inventory_job)
handler = handler_class()
handler.execute(ctx)
if ctx.success:
archive_inventory_job(db_session, inventory_job, JobStatus.COMPLETED)
else:
# removes the host object as host.packages may have been modified.
db_session.expunge(host)
archive_inventory_job(db_session, inventory_job, JobStatus.FAILED)
# Reset the pending retrieval flag
inventory_job.pending_submit = False
db_session.commit()
except:
try:
logger.exception('InventoryManager hit exception - inventory job = %s', self.job_id)
archive_inventory_job(db_session, inventory_job, JobStatus.FAILED, trace=sys.exc_info)
# Reset the pending retrieval flag
inventory_job.pending_submit = False
db_session.commit()
except:
logger.exception('InventoryManager hit exception - inventory job = %s', self.job_id)
finally:
with lock:
if self.job_id is not None and self.job_id in in_progress_jobs: del in_progress_jobs[self.job_id]
db_session.close()
示例7: process
# 需要导入模块: from database import DBSession [as 别名]
# 或者: from database.DBSession import query [as 别名]
def process(self):
db_session = DBSession()
ctx = None
try:
install_job = db_session.query(InstallJob).filter(InstallJob.id == self.job_id).first()
if install_job is None:
# This is normal because of race condition. It means the job is already deleted (completed).
return
if not can_install(db_session):
# This will halt this host that has already been queued
return
host = db_session.query(Host).filter(Host.id == self.host_id).first()
if host is None:
logger.error('Unable to retrieve host %s', self.host_id)
return
handler_class = get_install_handler_class(host.platform)
if handler_class is None:
logger.error('Unable to get handler for %s, install job %s', host.platform, self.job_id)
install_job.start_time = datetime.datetime.utcnow()
install_job.set_status(JobStatus.PROCESSING)
install_job.session_log = create_log_directory(host.connection_param[0].host_or_ip, install_job.id)
db_session.commit()
ctx = InstallContext(host, db_session, install_job)
ctx.operation_id = get_last_operation_id(db_session, install_job)
handler = handler_class()
handler.execute(ctx)
if ctx.success:
# Update the software
self.get_software(db_session, ctx)
archive_install_job(db_session, ctx, install_job, JobStatus.COMPLETED)
else:
archive_install_job(db_session, ctx, install_job, JobStatus.FAILED)
db_session.commit()
except:
try:
logger.exception('InstallManager hit exception - install job = %s', self.job_id)
archive_install_job(db_session, ctx, install_job, JobStatus.FAILED, trace=traceback.format_exc())
db_session.commit()
except:
logger.exception('InstallManager hit exception - install job = %s', self.job_id)
finally:
# Must remove the host from the in progress list
remove_host_from_in_progress(self.host_id)
db_session.close()
示例8: host_session_log
# 需要导入模块: from database import DBSession [as 别名]
# 或者: from database.DBSession import query [as 别名]
def host_session_log(hostname, table, id):
"""
This route is also used by mailer.py for email notification.
"""
db_session = DBSession()
record = None
doc_central_log_file_path = ''
if table == 'install_job':
record = db_session.query(InstallJob).filter(InstallJob.id == id).first()
elif table == 'install_job_history':
record = db_session.query(InstallJobHistory).filter(InstallJobHistory.id == id).first()
doc_central_log_file_path = get_doc_central_log_path(record)
elif table == 'inventory_job_history':
record = db_session.query(InventoryJobHistory).filter(InventoryJobHistory.id == id).first()
if record is None:
abort(404)
file_path = request.args.get('file_path')
log_file_path = get_log_directory() + file_path
if not(os.path.isdir(log_file_path) or os.path.isfile(log_file_path)):
abort(404)
file_pairs = {}
log_file_contents = ''
file_suffix = '.diff.html'
if os.path.isdir(log_file_path):
# Returns all files under the requested directory
log_file_list = get_file_list(log_file_path)
diff_file_list = [filename for filename in log_file_list if file_suffix in filename]
for filename in log_file_list:
diff_file_path = ''
if file_suffix not in filename:
if filename + file_suffix in diff_file_list:
diff_file_path = os.path.join(file_path, filename + file_suffix)
file_pairs[os.path.join(file_path, filename)] = diff_file_path
file_pairs = collections.OrderedDict(sorted(file_pairs.items()))
else:
with io.open(log_file_path, "rt", encoding='latin-1') as fo:
log_file_contents = fo.read()
return render_template('host/session_log.html', hostname=hostname, table=table,
record_id=id, file_pairs=file_pairs, log_file_contents=log_file_contents,
is_file=os.path.isfile(log_file_path),
doc_central_log_file_path=doc_central_log_file_path)
示例9: download_system_logs
# 需要导入模块: from database import DBSession [as 别名]
# 或者: from database.DBSession import query [as 别名]
def download_system_logs():
db_session = DBSession()
logs = db_session.query(Log) \
.order_by(Log.created_time.desc())
contents = ''
for log in logs:
contents += get_datetime_string(log.created_time) + ' UTC\n'
contents += log.level + ':' + log.msg + '\n'
if log.trace is not None:
contents += log.trace + '\n'
contents += '-' * 70 + '\n'
# Create a file which contains the size of the image file.
temp_user_dir = create_temp_user_directory(current_user.username)
log_file_path = os.path.normpath(os.path.join(temp_user_dir, "system_logs"))
create_directory(log_file_path)
make_file_writable(log_file_path)
log_file = open(os.path.join(log_file_path, 'system_logs'), 'w')
log_file.write(contents)
log_file.close()
return send_file(os.path.join(log_file_path, 'system_logs'), as_attachment=True)
示例10: delete_all_installations_for_host
# 需要导入模块: from database import DBSession [as 别名]
# 或者: from database.DBSession import query [as 别名]
def delete_all_installations_for_host(hostname, status=None):
if not can_delete_install(current_user):
abort(401)
db_session = DBSession()
host = get_host(db_session, hostname)
if host is None:
abort(404)
try:
install_jobs = db_session.query(InstallJob).filter(
InstallJob.host_id == host.id, InstallJob.status == status).all()
if not install_jobs:
return jsonify(status="No record fits the delete criteria.")
for install_job in install_jobs:
db_session.delete(install_job)
if status == JobStatus.FAILED:
delete_install_job_dependencies(db_session, install_job.id)
db_session.commit()
return jsonify({'status': 'OK'})
except:
logger.exception('delete_install_job() hit exception')
return jsonify({'status': 'Failed: check system logs for details'})
示例11: api_get_scheduled_download_jobs
# 需要导入模块: from database import DBSession [as 别名]
# 或者: from database.DBSession import query [as 别名]
def api_get_scheduled_download_jobs():
dt_params = DataTableParams(request)
db_session = DBSession()
clauses = []
if len(dt_params.search_value):
criteria = '%' + dt_params.search_value + '%'
clauses.append(DownloadJob.cco_filename.like(criteria))
clauses.append(DownloadJob.scheduled_time.like(criteria))
clauses.append(DownloadJob.created_by.like(criteria))
query = db_session.query(DownloadJob)
total_count = query.filter(DownloadJob.status == None).count()
filtered_count = query.filter(and_(DownloadJob.status == None), or_(*clauses)).count()
columns = [getattr(DownloadJob.cco_filename, dt_params.sort_order)(),
getattr(DownloadJob.scheduled_time, dt_params.sort_order)(),
'',
getattr(DownloadJob.created_by, dt_params.sort_order)()]
download_jobs = query.order_by(columns[dt_params.column_order])\
.filter(and_(DownloadJob.status == None), or_(*clauses))\
.slice(dt_params.start_length, dt_params.start_length + dt_params.display_length).all()
response = dict()
response['draw'] = dt_params.draw
response['recordsTotal'] = total_count
response['recordsFiltered'] = filtered_count
response.update(get_download_job_json_dict(db_session, download_jobs))
return jsonify(**response)
示例12: api_get_hosts_by_region
# 需要导入模块: from database import DBSession [as 别名]
# 或者: from database.DBSession import query [as 别名]
def api_get_hosts_by_region(region_id, role, software):
selected_roles = []
selected_software = []
if 'ALL' not in role:
selected_roles = role.split(',')
if 'ALL' not in software:
selected_software = software.split(',')
rows = []
db_session = DBSession()
hosts = db_session.query(Host).filter(Host.region_id == region_id). \
order_by(Host.hostname.asc())
for host in hosts:
host_roles = [] if host.roles is None else host.roles.split(',')
if not selected_roles or any(role in host_roles for role in selected_roles):
if host.software_platform is not None and host.software_version is not None:
host_platform_software = host.software_platform + ' (' + host.software_version + ')'
else:
host_platform_software = UNKNOWN
if not selected_software or host_platform_software in selected_software:
row = {'hostname': host.hostname,
'roles': host.roles,
'platform_software': host_platform_software}
rows.append(row)
return jsonify(**{'data': rows})
示例13: api_get_distinct_host_roles
# 需要导入模块: from database import DBSession [as 别名]
# 或者: from database.DBSession import query [as 别名]
def api_get_distinct_host_roles(platform, software_versions, region_ids):
"""
software_versions may equal to 'ALL' or multiple software versions
region_ids may equal to 'ALL' or multiple region ids
"""
clauses = []
db_session = DBSession()
clauses.append(Host.software_platform == platform)
if 'ALL' not in software_versions:
clauses.append(Host.software_version.in_(software_versions.split(',')))
if 'ALL' not in region_ids:
clauses.append(Host.region_id.in_(region_ids.split(',')))
host_roles = db_session.query(Host.roles).filter(and_(*clauses)).distinct()
# Change a list of tuples to a list
# Example of roles_list = [u'PE Router', u'PE1,R0', u'PE1,PE4', u'PE2,R1', u'Core']
roles_list = [roles[0] for roles in host_roles if not is_empty(roles[0])]
# Collapses the comma delimited strings to list
roles_list = [] if is_empty(roles_list) else ",".join(roles_list).split(',')
# Make the list unique, then sort it
roles_list = sorted(list(set(roles_list)))
rows = []
for role in roles_list:
rows.append({'role': role})
return jsonify(**{'data': rows})
示例14: api_get_smu_details
# 需要导入模块: from database import DBSession [as 别名]
# 或者: from database.DBSession import query [as 别名]
def api_get_smu_details(smu_id):
rows = []
db_session = DBSession()
smu_info = db_session.query(SMUInfo).filter(SMUInfo.id == smu_id).first()
if smu_info is not None:
row = dict()
row['id'] = smu_info.id
row['name'] = smu_info.name
row['status'] = smu_info.status
row['type'] = smu_info.type
row['posted_date'] = smu_info.posted_date
row['ddts'] = smu_info.ddts
row['description'] = smu_info.description
row['functional_areas'] = smu_info.functional_areas
row['impact'] = smu_info.impact
row['package_bundles'] = smu_info.package_bundles
row['compressed_image_size'] = str(smu_info.compressed_image_size)
row['uncompressed_image_size'] = str(smu_info.uncompressed_image_size)
row['prerequisites'] = smu_info.prerequisites
row['supersedes'] = smu_info.supersedes
row['superseded_by'] = smu_info.superseded_by
row['composite_DDTS'] = smu_info.composite_DDTS
row['prerequisites_smu_ids'] = get_smu_ids(db_session, smu_info.prerequisites)
row['supersedes_smu_ids'] = get_smu_ids(db_session, smu_info.supersedes)
row['superseded_by_smu_ids'] = get_smu_ids(db_session, smu_info.superseded_by)
rows.append(row)
return jsonify(**{'data': rows})
示例15: scheduling
# 需要导入模块: from database import DBSession [as 别名]
# 或者: from database.DBSession import query [as 别名]
def scheduling(self, scheduler, daily_time):
# First, re-set up the scheduler for the next day the same time. It is important to have
# this logic on the top so that if any error encountered below, the scheduling still works.
t = datetime.datetime.combine(datetime.datetime.now() + datetime.timedelta(days=1), daily_time)
scheduler.enterabs(time.mktime(t.timetuple()), 1, self.scheduling, (scheduler, daily_time,))
db_session = DBSession()
try:
system_option = SystemOption.get(db_session)
# If software inventory is enabled, submit the inventory jobs
if system_option.enable_inventory:
inventory_jobs = db_session.query(InventoryJob).all()
if len(inventory_jobs)> 0:
for inventory_job in inventory_jobs:
inventory_job.pending_submit = True
db_session.commit()
#Check if there is any housekeeping work to do
self.perform_housekeeping_tasks(db_session, system_option)
except:
logger.exception('InventoryManagerScheduler hit exception')
finally:
db_session.close()