def delete_function(oid): if oid is not None: UserFunctionCache.flush_all() context = DBContext() context.delete_byid("um_function", oid) # delete related auth delete_auth_byidentity(oid, "UmFunction")
def delete_auth_byidentity(oid, entity): context = DBContext() context.execute_delete("um_auth", "(sourceid = :oid and sourceentity = :entity) or " "(grantid = :oid and grantentity = :entity)", oid=oid, entity=entity)
def get_auth_bydetail(source_id, source_entity, grant_id, grant_entity): context = DBContext() return context.create_query( "um_auth", "sourceid=:sourceid and sourceentity=:sourceentity " "and grantid=:grantid and grantentity=:grantentity", sourceid=source_id, sourceentity=source_entity, grantid=grant_id, grantentity=grant_entity).first()
def find_schedule_logs(**params): sql = ['1=1'] cond = {} ctx = DBContext() if 'scheduleid' in params: sql.append('and scheduleid = :scheduleid') cond['scheduleid'] = params['scheduleid'] if 'status' in params: sql.append('and status = :status') cond['status'] = num.safe_int(params['status']) return ctx.create_query('cm_schedule_log', ' '.join(sql), **cond)
def find_function_user_auth(function_id): context = DBContext() sql_arr = [ "select u.id, u.loginid, u.name, a.id authid from um_user u", "left join (", " select id, grantid from um_auth", " where sourceentity = :functionentity and sourceid = :functionid and grantentity = :userentity", ") a on u.id = a.grantid", "where u.loginid <> 'admin'" ] return context.create_sql_query("\n".join(sql_arr), functionentity="UmFunction", functionid=function_id, userentity="UmUser")
def find_my_menu_db(user_id): context = DBContext() ret = [] user = get_user_byid(user_id) if user is None: return ret if user["loginid"] == "admin": return find_menu() else: sql = [ "select m.* from um_menu m", "right join (", " select * from um_auth where sourceentity = 'UmMenu' and grantentity = 'UmUser' and grantid = :userid", ") a on m.id = a.sourceid" ] return context.create_sql_query("\n".join(sql), userid=user_id)
def task_execution_listener(e): from apscheduler.events import JobExecutionEvent from raphael.app.modules.schedule import models as schedule from raphael.utils.dao.context import DBContext assert isinstance(e, JobExecutionEvent) # add to task schedule history database if e.jobstore == schedule.TASK_DATABASE: status_dict = { events.EVENT_JOB_EXECUTED: 1, events.EVENT_JOB_ERROR: 2, events.EVENT_JOB_MISSED: 3, events.EVENT_JOB_MAX_INSTANCES: 4 } _schedule = schedule.get_schedule(e.job_id) if _schedule is not None: with DBContext(): if _schedule.get('type') == 1: _schedule['enabled'] = 0 schedule.save_schedule(_schedule) schedule_log = { 'scheduleid': e.job_id, 'executiontime': e.scheduled_run_time, 'retval': None if e.retval is None else str(e.retval), 'status': status_dict[e.code], 'exception': str(e.exception) } schedule.save_schedule_log(schedule_log)
def delete_schedule(oid): with DBContext() as ctx: ctx.delete_byid('cm_schedule', oid) ctx.execute_delete('cm_schedule_log', 'scheduleid = :sid', sid=oid) try: task.remove_job(oid, TASK_DATABASE) except JobLookupError: logger.error_traceback()
def actual_get(cls, key): sql = [ "select f.name from um_function f", "right join (", " select * from um_auth where sourceentity = 'UmFunction' and grantentity = 'UmUser' and grantid = :userid", ") a on f.id = a.sourceid" ] funcs = DBContext().create_sql_query('\n'.join(sql), userid=key).fetch() return [f.get('name') for f in funcs]
def find_menu(**params): sql = ['1=1'] cond = {} if 'parentid' in params: sql.append('parentid = :parentid') cond['parentid'] = params['parentid'] if 'sort' in params: sql.append('sort = :sort') cond['sort'] = num.safe_int(params['sort']) return DBContext().create_query("um_menu", ' and '.join(sql), **cond)
def find_schedules(**params): ctx = DBContext() sql = ['1=1'] cond = {} if 'enabled' in params: sql.append('and enabled = :enabled') cond['enabled'] = params['enabled'] if 'type' in params: sql.append('and type = :type') cond['type'] = params['type'] if 'module' in params: sql.append('and module = :module') cond['module'] = params['module'] if 'modulelike' in params: sql.append('and module like :modulelike') cond['modulelike'] = '%' + params['modulelike'] + '%' if 'sourceid' in params: sql.append('and sourceid = :sourceid') cond['sourceid'] = params['sourceid'] return ctx.create_query('cm_schedule', ' '.join(sql), **cond)
def add_umsession(user_id, duration): import datetime token = strings.uuid() DBContext().save( 'um_session', { 'token': token, 'user_id': user_id, 'expire_at': time.utcnow() + datetime.timedelta(seconds=duration), }) return token
def find_auth(**params): context = DBContext() where = ['1=1'] argdict = {} for attribute in 'sourceentity', 'sourceid', 'grantentity', 'grantid': try: value = params[attribute] where.append('%s=:%s' % (attribute, attribute)) argdict[attribute] = value except KeyError: pass for a in 'sourceentityin', 'sourceidin', 'grantentityin', 'grantidin': try: attribute = a[:-2] values = params[a] if not isinstance(values, collections.Iterable): raise Exception('%s should be iterable', a) where.append('%s in %s' % (attribute, query.escape_sequence(values))) except KeyError: pass return context.create_query('um_auth', ' and '.join(where), **argdict)
def delete_menu(oid): menu = get_menu(oid) if menu is not None: with DBContext() as ctx: ctx.delete_byid("um_menu", oid) # revise sort ctx.execute( 'update um_menu set sort = sort - 1 where parentid = :parentid and sort > :sort', parentid=menu['parentid'], sort=menu['sort']) # remove children ctx.execute_delete('um_menu', 'parentid = :parentid', parentid=menu['id']) # delete related auth delete_auth_byidentity(oid, "UmMenu") # clear my menu cache UserMenuCache.flush_all()
def find_settings(**params): where = ['1=1'] cond = {} if 'name' in params: where.append('name = :name') cond['name'] = params['name'] if 'namelike' in params: where.append('name like :namelike') cond['namelike'] = '%' + params['namelike'] + '%' if 'namelikeleft' in params: where.append('name like :namelikeleft') cond['namelikeleft'] = params['namelikeleft'] + '%' if 'value' in params: where.append('value = :value') cond['value'] = params['value'] if 'notid' in params: where.append('id != :notid') cond['notid'] = params['notid'] return DBContext().create_query(CM_SETTING, ' and '.join(where), **cond)
def move_menu(menu, operator): with DBContext() as ctx: changed = False if operator == 'top': if menu['sort'] != 1: ctx.execute( 'update um_menu set sort = sort + 1 where parentid = :parentid and sort < :sort', parentid=menu['parentid'], sort=menu['sort']) menu['sort'] = 1 changed = True elif operator == 'up': if menu['sort'] != 1: ctx.execute( 'update um_menu set sort = sort + 1 where parentid = :parentid and sort = :sort', parentid=menu['parentid'], sort=menu['sort'] - 1) menu['sort'] = menu['sort'] - 1 changed = True elif operator == 'down': if menu['sort'] != find_menu(parentid=menu['parentid']).count(): ctx.execute( 'update um_menu set sort = sort - 1 where parentid = :parentid and sort = :sort', parentid=menu['parentid'], sort=menu['sort'] + 1) menu['sort'] = menu['sort'] + 1 changed = True elif operator == 'bottom': if menu['sort'] != find_menu(parentid=menu['parentid']).count(): ctx.execute( 'update um_menu set sort = sort - 1 where parentid = :parentid and sort > :sort', parentid=menu['parentid'], sort=menu['sort']) menu['sort'] = find_menu(parentid=menu['parentid']).count() changed = True if changed: ctx.save('um_menu', menu) UserMenuCache.flush_all()
def find_umsessions(**params): sql_arr = ['1=1'] cond = {} # token try: v = params['token'] sql_arr.append('token = :token') cond['token'] = v except KeyError: pass # user_id try: v = params['user_id'] sql_arr.append('user_id = :user_id') cond['user_id'] = v except KeyError: pass # expire_at try: v = params['gt_expire_at'] sql_arr.append('expire_at > :gt_expire_at') cond['gt_expire_at'] = v except KeyError: pass try: v = params['lt_expire_at'] sql_arr.append('expire_at > :lt_expire_at') cond['lt_expire_at'] = v except KeyError: pass return DBContext().create_query('um_session', ' and '.join(sql_arr), **cond)
def save_schedule_manually(o): with DBContext(): save_schedule(o) save_task_schedule(o)
def save_schedule(o): with DBContext() as ctx: ctx.save('cm_schedule', o)
def save_schedule_log(o): ctx = DBContext() ctx.save('cm_schedule_log', o)
def get_schedule(oid): ctx = DBContext() return ctx.get('cm_schedule', oid)
def actual_save(cls, key, obj): DBContext().save(cls.prefix, obj)
def actual_remove(cls, key): DBContext().execute_delete(cls.prefix, cls.index_key + '=:indexkey', indexkey=key)
def save_auth(auth): assert isinstance(auth, dict) context = DBContext() context.save("um_auth", auth)
def get_setting(oid): return DBContext().get(CM_SETTING, oid)
def find_users(): return DBContext().create_query("um_user", "1=1")
def scrub_expired_umsessions(): DBContext().execute_delete('um_session', 'expire_at < :now', now=time.utcnow())
def get_umsession(oid): return DBContext().get('um_session', oid)
def actual_get(cls, key): if cls.index_key == PRIMARY_IDENTIFIER: return DBContext().get(cls.prefix, key) else: return DBContext().create_query(cls.prefix, cls.index_key + '=:' + cls.index_key, **{cls.index_key: key}).first()
def delete_auth(oid): if oid is not None: context = DBContext() context.delete_byid("um_auth", oid)