async def parse_data(request): if request.method == 'POST': if request.content_type.startswith('application/json'): request.__data__ = await request.json() logger.info('request json: %s' % str(request.__data__)) elif request.content_type.startswith( 'application/x-www-form-urlencoded'): request.__data__ = await request.post() logger.info('request form: %s' % str(request.__data__)) return (await handler(request))
async def init(loop): await orm.create_pool(loop=loop, **configs.db) app = web.Application( loop=loop, middlewares=[logger_factory, auth_factory, response_factory]) init_jinja2(app, filters=dict(datetime=datetime_filter)) add_routes(app, 'handlers') add_static(app) srv = await loop.create_server(app.make_handler(), '127.0.0.1', 9000) logger.info('server started at http://127.0.0.1:9000...') return srv
def input_param(**kw): ''' :param kw: :return: ''' copy = utils.Dict(**web.input(_unicode=False)) if kw: for k, v in kw.iteritems(): if not k in copy: copy[k] = v logger.info("recv url: %s data: %s" % (web.ctx.path, copy)) return copy
def input_param(**kw): ''' :param kw: :return: ''' copy = utils.Dict(**web.input(_unicode=False)) if kw: for k, v in kw.iteritems(): if not k in copy: copy[k] = v logger.info("recv url: %s data: %s" % (web.ctx.path, copy)) return copy
def input_data(**kw): ''' request data :return: ''' copy = loads(web.data()) if kw: for k, v in kw.iteritems(): if not k in copy: copy[k] = v logger.info("recv url: %s data: %s" % (web.ctx.path, copy)) return copy
def input_data(**kw): ''' request data :return: ''' copy = loads(web.data()) if kw: for k, v in kw.iteritems(): if not k in copy: copy[k] = v logger.info("recv url: %s data: %s" % (web.ctx.path, copy)) return copy
async def select(sql, args, size=None): log(sql, args) global __pool async with __pool.get() as conn: async with conn.cursor(aiomysql.DictCursor) as cur: await cur.execute(sql.replace('?', '%s'), args or ()) if size: rs = await cur.fetchmany(size) else: rs = await cur.fetchall() await cur.close() logger.info('rows returned: %s' % len(rs)) return rs
async def auth(request): logger.info('check user: %s %s' % (request.method, request.path)) request.__user__ = None cookie_str = request.cookies.get(COOKIE_NAME) if cookie_str: user = await cookie2user(cookie_str) if user: logger.info('set current user: %s' % user.email) request.__user__ = user if request.path.startswith('/manage/') and ( request.__user__ is None or not request.__user__.admin): return web.HTTPFound('/signin') return (await handler(request))
def run(self): logger.info('Start SaltMan.') lc = util.LoopChecker('Main loop') while True: try: targets = self.collect_delegate.collect(self.config) self.compress_delegate.compress(self.config, targets) time.sleep(1) lc.check() if len(targets) > 0: lc.reset() except: e = sys.exc_info()[0] logger.error(e) time.sleep(5)
async def create_pool(loop, **kwargs): logger.info('start create database connection pool...') global __pool __pool = await aiomysql.create_pool( host=kwargs.get('host', 'localhost'), port=kwargs.get('port', 3306), user=kwargs['user'], password=kwargs['password'], db=kwargs['database'], charset=kwargs.get('charset', 'utf8'), autocommit=kwargs.get('autocommit', True), maxsize=kwargs.get('maxsize', 10), minsize=kwargs.get('minsize', 1), loop=loop, )
def __new__(cls, name, bases, attrs): # 排除Model类本身 if name == 'Model': return type.__new__(cls, name, bases, attrs) tableName = attrs.get('__table__', None) or name logger.info('found model: %s (table: %s)' % (name, tableName)) # 获取所有的Field和主键名: mappings = dict() fields = [] primaryKey = None for k, v in attrs.items(): if isinstance(v, Field): logger.info('Found mapping: %s ==> %s' % (k, v)) mappings[k] = v if v.primary_key: # 找到主键 if primaryKey: raise RuntimeError( 'Duplicate primary key for field: %s' % k) primaryKey = k else: fields.append(k) if not primaryKey: raise RuntimeError('Primary key not found!') for k in mappings.keys(): attrs.pop(k) escaped_fields = list(map(lambda f: '`%s`' % f, fields)) attrs['__mappings__'] = mappings # 保存属性和列的映射关系 attrs['__table__'] = tableName attrs['__primary_key__'] = primaryKey # 主键属性名 attrs['__fields__'] = fields # 除主键外的属性名 # 构造默认的SELECT,INSERT,UPDATE和DELETE语句 attrs['__select__'] = 'select `%s`,%s from `%s`' % ( primaryKey, ','.join(fields), tableName) attrs['__insert__'] = 'insert into `%s` (%s, `%s`) VALUES (%s)' % ( tableName, ','.join(escaped_fields), primaryKey, create_args_string(len(escaped_fields) + 1)) attrs['__update__'] = 'update `%s` set %s where `%s`=?' % ( tableName, ', '.join( map(lambda f: '`%s`=?' % (mappings.get(f).name or f), fields)), primaryKey) attrs['__delete__'] = 'delete from `%s` where `%s`=?' % (tableName, primaryKey) return type.__new__(cls, name, bases, attrs)
def init_jinja2(app, **kw): logger.info('init jinja2...') options = dict(autoescape=kw.get('autoescape', True), block_start_string=kw.get('block_start_string', '{%'), block_end_string=kw.get('block_end_string', '%}'), variable_start_string=kw.get('variable_start_string', '{{'), variable_end_string=kw.get('variable_end_string', '}}'), auto_reload=kw.get('auto_reload', True)) path = kw.get('path', None) if path is None: path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'templates') logger.info('set jinja2 template path: %s' % path) env = Environment(loader=FileSystemLoader(path), **options) filters = kw.get('filters', None) if filters is not None: for name, f in filters.items(): env.filters[name] = f app['__templating__'] = env
async def response(request): logger.info('Response handler...') r = await handler(request) if isinstance(r, web.StreamResponse): return r if isinstance(r, bytes): resp = web.Response(body=r) resp.content_type = 'application/octet-stream' return resp if isinstance(r, str): if r.startswith('redirect:'): return web.HTTPFound(r[9:]) resp = web.Response(body=r.encode('utf-8')) resp.content_type = 'text/html;charset=utf-8' return resp if isinstance(r, dict): template = r.get('__template__') if template is None: resp = web.Response(body=json.dumps( r, ensure_ascii=False, default=lambda o: o.__dict__).encode('utf-8')) resp.content_type = 'application/json;charset=utf-8' return resp else: r['__user__'] = request.__user__ resp = web.Response( body=app['__templating__'].get_template(template).render( **r).encode('utf-8')) resp.content_type = 'text/html;charset=utf-8' return resp if isinstance(r, int) and r >= 100 and r < 600: return web.Response(r) if isinstance(r, tuple) and len(r) == 2: t, m = r if isinstance(t, int) and t >= 100 and t < 600: return web.Response(t, str(m)) # default: resp = web.Response(body=str(r).encode('utf-8')) resp.content_type = 'text/plain;charset=utf-8' return resp
def __init__(self): super(Config, self).__init__() self.config = None cur = os.path.dirname(os.path.realpath(__file__)) self.filename = cur + '/../config/config.base.json' self.local_filename = cur + '/../config/config.local.json' with open(self.filename, 'r') as fd: self.base = fd.read() if os.path.isfile(self.local_filename): with open(self.local_filename, 'r') as fd: self.local = fd.read() else: self.local = '{}' b = json.loads(self.base) l = json.loads(self.local) self.config = { key: value for (key, value) in (b.items() + l.items()) } logger.info(self.config)
msg=STATUS_CODE[CODE_SUCCESS]) resp = func(*args, **kw) if resp: resp_obj.result = resp r = json.dumps(_encode_utf8(**resp_obj), ensure_ascii=False) except APIError, e: error_obj = _encode_utf8(rtn=e.error_code, msg=e.message) r = json.dumps(error_obj, ensure_ascii=False) except Exception, e: # raise error_obj = _encode_utf8(rtn=CODE_INTERNAL_ERROR, msg='[%s]%s' % (e.__class__.__name__, e.message)) r = json.dumps(error_obj, ensure_ascii=False) web.header('Content-Type', 'application/json;charset=utf-8') logger.info("resp data: %s" % r) return r return _wrapper def assert_not_empty(text, name): if not text: raise APIValueError("%s is empty" % name) return text def assert_int(s, name): try: return int(s) except ValueError:
async def close_pool(): logger.info('close database connection pool...') global __pool if __pool is not None: __pool.close() await __pool.wait_closed()
def log(sql, args=()): logger.info('SQL: %s, args: %s' % (sql, args))
def check(self): self.cur_count += 1 if self.cur_count >= self.next_count: self.next_count *= 2 logger.info('%s (%d)' % (self.msg, self.cur_count))
def _wrapper(*args, **kw): try: resp_obj = utils.Dict(rtn=CODE_SUCCESS, msg=STATUS_CODE[CODE_SUCCESS]) resp = func(*args, **kw) if resp: resp_obj.result = resp r = json.dumps(_encode_utf8(**resp_obj), ensure_ascii=False) except APIError, e: error_obj = _encode_utf8(rtn=e.error_code, msg=e.message) r = json.dumps(error_obj, ensure_ascii=False) except Exception, e: # raise error_obj = _encode_utf8(rtn=CODE_INTERNAL_ERROR, msg='[%s]%s' % (e.__class__.__name__, e.message)) r = json.dumps(error_obj, ensure_ascii=False) web.header('Content-Type', 'application/json;charset=utf-8') logger.info("resp data: %s" % r) return r return _wrapper def assert_not_empty(text, name): if not text: raise APIValueError("%s is empty" % name) return text def assert_int(s, name): try: return int(s) except ValueError:
async def logger1(request): logger.info('Request: %s %s' % (request.method, request.path)) return (await handler(request))
def logon(request): ''' 登录界面视图 登录处理视图 ''' logger.info("logon: %s" % request) logout(request) request.csrf_processing_done = True ret = login(request) # 登录页面视图接口 if not request.POST: return ret #软件狗验证, #国外逻辑定义:客户端操作系统的语言非中文(IE),浏览器语言非中文(其他浏览器ff,so) # client_language=request.POST.get("client_language") # check_ret=authorize_fun.login_check(request,client_language) # print settings.ZKECO_DEVICE_LIMIT,settings.ATT_DEVICE_LIMIT,settings.MAX_ACPANEL_COUNT,'\n' # if check_ret!=True: # return HttpResponse(check_ret) #指纹登入,修改request.user # register_finger = request.REQUEST.get("fpflag", "") # if register_finger == 'fplogin': # template = identify_operatorfinger(request) #用户比对指纹进入系统 # if template == "USER_ERROR": # return HttpResponse("user_error") # elif template == FP_NO_LICENCES: # return HttpResponse('1') # elif template == FP_IDENTIFY_FAILED: # return HttpResponse("error") # else: # user = template.user # template_logon(request,user) user = request.user logger.info('user: %s' % user) # 登录日志记录 if user.is_anonymous(): return HttpResponse( u"%(msg)s" % {"msg": _(u"用户名或密码错误,原因可能是:忘记密码;未区分字母大小写;未开启小键盘!")}) eb = -1 try: eb = user.backend.find('EmployeeBackend') # 重载实现员工登录验证 except: pass if eb == -1: # 用户登录 user.employee = None if 'employee' in request.session: del request.session["employee"] else: # 员工登录 request.session["employee"] = user.employee if user.is_staff: from base.options import options from django.utils.translation import check_for_language from base.middleware.threadlocals import _thread_locals #删除菜单缓存 cache_key = "%s_%s" % (user.username, "menu_list") cache.delete(cache_key) _thread_locals.user = user lang_code = options['base.language'] if not lang_code: #使用默认安装时选择的语言 lang_code = settings.LANGUAGE_CODE if lang_code and check_for_language(lang_code): if hasattr(request, 'session'): request.session['django_language'] = lang_code else: ret.set_cookie(settings.LANGUAGE_COOKIE_NAME, lang_code) try: from base.models_logentry import LogEntry, LOGIN from django.contrib.contenttypes.models import ContentType LogEntry.objects.log_action(user_id=user and user.pk or None, content_type_id=None, object_id=user.pk, object_repr="", action_flag=LOGIN, change_message="") except: import traceback traceback.print_exc() return HttpResponse('ok') # 登录处理结束 将结果返回到前端 else: logout(request) return HttpResponse(u"%s" % _(u"您还没有分配权限!"))
def logon(request): """ 登录界面视图 登录处理视图 """ logger.info("logon: %s" % request) logout(request) request.csrf_processing_done = True ret = login(request) # 登录页面视图接口 if not request.POST: return ret # 软件狗验证, # 国外逻辑定义:客户端操作系统的语言非中文(IE),浏览器语言非中文(其他浏览器ff,so) # client_language=request.POST.get("client_language") # check_ret=authorize_fun.login_check(request,client_language) # print settings.ZKECO_DEVICE_LIMIT,settings.ATT_DEVICE_LIMIT,settings.MAX_ACPANEL_COUNT,'\n' # if check_ret!=True: # return HttpResponse(check_ret) # 指纹登入,修改request.user # register_finger = request.REQUEST.get("fpflag", "") # if register_finger == 'fplogin': # template = identify_operatorfinger(request) #用户比对指纹进入系统 # if template == "USER_ERROR": # return HttpResponse("user_error") # elif template == FP_NO_LICENCES: # return HttpResponse('1') # elif template == FP_IDENTIFY_FAILED: # return HttpResponse("error") # else: # user = template.user # template_logon(request,user) user = request.user logger.info("user: %s" % user) # 登录日志记录 if user.is_anonymous(): return HttpResponse(u"%(msg)s" % {"msg": _(u"用户名或密码错误,原因可能是:忘记密码;未区分字母大小写;未开启小键盘!")}) eb = -1 try: eb = user.backend.find("EmployeeBackend") # 重载实现员工登录验证 except: pass if eb == -1: # 用户登录 user.employee = None if "employee" in request.session: del request.session["employee"] else: # 员工登录 request.session["employee"] = user.employee if user.is_staff: from base.options import options from django.utils.translation import check_for_language from base.middleware.threadlocals import _thread_locals # 删除菜单缓存 cache_key = "%s_%s" % (user.username, "menu_list") cache.delete(cache_key) _thread_locals.user = user lang_code = options["base.language"] if not lang_code: # 使用默认安装时选择的语言 lang_code = settings.LANGUAGE_CODE if lang_code and check_for_language(lang_code): if hasattr(request, "session"): request.session["django_language"] = lang_code else: ret.set_cookie(settings.LANGUAGE_COOKIE_NAME, lang_code) try: from base.models_logentry import LogEntry, LOGIN from django.contrib.contenttypes.models import ContentType LogEntry.objects.log_action( user_id=user and user.pk or None, content_type_id=None, object_id=user.pk, object_repr="", action_flag=LOGIN, change_message="", ) except: import traceback traceback.print_exc() return HttpResponse("ok") # 登录处理结束 将结果返回到前端 else: logout(request) return HttpResponse(u"%s" % _(u"您还没有分配权限!"))