def getgame(self,**kw): json = http.request.jsonrequest session = self.check_session(json['sid']) if not session: return {'messageļ¼':'AccessDenied'} db = session.db uid = session.uid user = http.request.env['res.users'].sudo().browse(uid) model_name = json['model'] method=json['method'] args = json.get('args',() ) kw = json.get('kw',{}) ret = model.execute_kw(db,uid, model_name, method, args, kw) #obj = http.request.env['ogi.game.brg'].sudo() #ret = getattr(obj,method).() return ret
def perform_request(self, object, method=None, id=None, kwargs={}, user=None): """ Authenticate User. If valid, perform openration as per request. """ datas = {} request_code, request_data = 200, {} ids, payload = [id] if id else [], {} arguments, k_arguments = [], {} if not method: method = self._HTTP_METHODS.get(request.httprequest.method) request_code = self._HTTP_REQUEST_CODE.get(method) if method in ('create', 'write'): for f in ['vals', 'args']: payload = kwargs.get(f) and self.evaluate(kwargs[f]) if not payload: payload = request.httprequest.data and self.evaluate( request.httprequest.data).get(f) or {} if payload: break if not payload: payload = request.httprequest.data and self.evaluate( request.httprequest.data) or {} if payload: payload = type(payload) is list and payload[0] or payload if request.httprequest.data and type( self.evaluate(request.httprequest.data)) is dict: kwargs.update(self.evaluate(request.httprequest.data)) if kwargs.get('ids'): ids.extend( type(kwargs['ids']) is list and kwargs['ids'] or kwargs['ids'].split(',')) ids = map(self.evaluate, ids) if self._METHOD_WITH_ARGS_KWARGS.get(method): args = self._METHOD_WITH_ARGS_KWARGS.get(method) arguments.extend( [self.evaluate(kwargs.get(arg)) for arg in args['arg']]) k_arguments = dict([(arg, self.evaluate(kwargs[arg])) for arg in args['kwargs'] if kwargs.get(arg)]) if method in self._GET_METHODS: if type(self.evaluate(kwargs.get('domain'))) is list and ids: arguments[0].append(('id', 'in', ids)) elif ids: arguments[0] = [('id', 'in', ids)] elif not self.evaluate(kwargs.get('domain')): arguments[0] = [] elif ids: arguments.insert(0, ids) elif method in ('create', 'write'): arguments = [ids, payload] if ids else [payload] method = ids and 'write' or 'create' k_arguments = kwargs.get('context') and { 'context': kwargs['context'] } or {} else: if ids: arguments.append(ids) arguments.extend( (kwargs.get('args') and self.evaluate(kwargs['args'])) or (request.httprequest.data and self.evaluate(request.httprequest.data).get('args')) or []) k_arguments = (kwargs.get('kwargs') and self.evaluate(kwargs['kwargs']) ) or (request.httprequest.data and self.evaluate( request.httprequest.data).get('kwargs') or {}) try: data = model.execute_kw(request.cr.dbname, user.id, object, method, arguments, kw=k_arguments) request.cr.commit() records = [] context = dict(request._context) if k_arguments.get('context'): context.update(k_arguments['context']) if method in ['create', 'write']: env_obj = request.env[object].with_context(context).sudo() if id: data = env_obj.search_read([('id', '=', id)]) elif kwargs.get('ids'): data = env_obj.search_read([('id', 'in', kwargs['ids'].split(','))]) else: data = env_obj.search_read([('id', '=', data)]) data_description = object if method == 'search_count': data_description = 'count' if method == 'check_access_rights': data_description = 'return' if data and isinstance(data, list): if isinstance(data[0], int) or len(data) > 1: datas.update({data_description: data}) else: datas.update({data_description: data[0]}) elif data: if method == 'unlink': datas = '{}' else: datas.update({data_description: data}) else: if method == 'search_count': datas.update({data_description: 0}) return self.get_response(request_code, str(request_code), datas) return self.get_response(404, '404', { 'code': 404, 'message': 'Record not found' }) except Exception as e: return self.get_response( 403, '403', {'error': { 'code': 403, 'message': e.message or e.name }}) return self.get_response(request_code, str(request_code), datas) # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
def call_service(self, model, method, *args, **kwargs): db = self.env.cr.dbname uid = self.env.user.id return execute_kw(db, uid, model, method, args, kwargs)
def perform_request(self, object, method=None, id=None, kwargs={}, user=None): """ Authenticate User. If valid, perform operation as per request. """ datas = {} request_code, request_data = 200, {} ids, payload = [id] if id else [], {} arguments, k_arguments = [], {} if not method: method = self._HTTP_METHODS.get(request.httprequest.method) request_code = self._HTTP_REQUEST_CODE.get(method) if method in ('create', 'write'): for f in ['vals', 'args']: payload = kwargs.get(f) and self.evaluate(kwargs[f]) if not payload: payload = request.httprequest.data and self.evaluate( request.httprequest.data).get(f) or {} if payload: break if not payload: payload = request.httprequest.data and self.evaluate( request.httprequest.data) or {} if payload: payload = type(payload) is list and payload[0] or payload if request.httprequest.data and type( self.evaluate(request.httprequest.data)) is dict: kwargs.update(self.evaluate(request.httprequest.data)) singleRecordStatus = False if kwargs.get('ids'): ids.extend( type(kwargs['ids']) is list and kwargs['ids'] or kwargs['ids'].split(',')) ids = list(map(self.evaluate, ids)) else: singleRecordStatus = True if self._METHOD_WITH_ARGS_KWARGS.get(method): args = self._METHOD_WITH_ARGS_KWARGS.get(method) arguments.extend( [self.evaluate(kwargs.get(arg)) for arg in args['arg']]) k_arguments = dict([(arg, self.evaluate(kwargs[arg])) for arg in args['kwargs'] if kwargs.get(arg)]) if method in self._GET_METHODS: if type(self.evaluate(kwargs.get('domain'))) is list and ids: arguments[0].append(('id', 'in', ids)) elif ids: arguments[0] = [('id', 'in', ids)] elif not self.evaluate(kwargs.get('domain')): arguments[0] = [] elif ids: arguments.insert(0, ids) elif method in ('create', 'write'): arguments = [ids, payload] if ids else [payload] method = ids and 'write' or 'create' k_arguments = kwargs.get('context') and { 'context': kwargs['context'] } or {} else: if ids: arguments.append(ids) arguments.extend( (kwargs.get('args') and self.evaluate(kwargs['args'])) or (request.httprequest.data and self.evaluate(request.httprequest.data).get('args')) or []) k_arguments = (kwargs.get('kwargs') and self.evaluate(kwargs['kwargs']) ) or (request.httprequest.data and self.evaluate( request.httprequest.data).get('kwargs') or {}) try: ### getting passkey loginStatus = False login = '' passkey = '' key_temp = [] if method == 'search_read' and object in ['res.users']: for key in arguments[0]: if key[0] == 'passkey': loginStatus = True passkey = key[2] if key[0] == 'login': login = key[2] if key[0] != 'passkey': key_temp.append(key) arguments = [key_temp] ## data = model.execute_kw(globalDbName, globalUserId, object, method, arguments, kw=k_arguments) # data = model.execute_kw(globalDbName, user.id, object, method, arguments, kw=k_arguments) request.cr.commit() records = [] context = dict(request._context) if k_arguments.get('context', False): context.update(k_arguments['context']) if method in ['create', 'write']: env_obj = request.env[object].with_context(context).sudo() if id: data = env_obj.search_read([('id', '=', id)]) elif kwargs.get('ids'): data = env_obj.search_read([('id', 'in', kwargs['ids'].split(','))]) else: data = env_obj.search_read([('id', '=', data)]) data_description = object ### handle custom request ## singleRecordStatus==True if method == 'search_read' and data_description in [ 'res.users', 'res.partner' ]: i = 0 for key in data: data_temp = {} # for attribute, value in key.iteritems(): for attribute, value in key.items(): if attribute[-3:] == '_id': try: data_temp[attribute] = value[0] except Exception as e: data_temp[attribute] = value else: data_temp[attribute] = value data[i] = data_temp i += 1 ## ### check login api if loginStatus == True: #encrypt_pwd = pb.encrypt('qW1@', rounds=25000) verify = False query = "SELECT password_crypt FROM res_users WHERE login=%s" params = (login, ) request.cr.execute(query, params) pwd_data = request.cr.dictfetchall() if (len(pwd_data) > 0): verify = pb.verify(passkey, pwd_data[0]['password_crypt']) if verify == False: return self.get_response(404, '404', { 'code': 404, 'message': 'Record not found' }) ## if method == 'search_count': data_description = 'count' if method == 'check_access_rights': data_description = 'return' if data and isinstance(data, list): for val in data: if isinstance(val, dict): for v in list(val): if isinstance(val[v], bytes): val[v] = val[v].decode('utf-8') # if isinstance(data[0], int) or len(data)>1: # datas.update({data_description: data}) # else: # datas.update({data_description: data[0]}) datas.update({data_description: data}) elif data: if method == 'unlink': datas = '{}' else: datas.update({data_description: data}) else: if method == 'search_count': datas.update({data_description: 0}) return self.get_response(request_code, str(request_code), datas) return self.get_response(404, '404', { 'code': 404, 'message': 'Record not found' }) except requests.exceptions.RequestException: return self.get_response(403, '403', { 'code': 403, 'message': "Connection Error" }) except Exception as e: return self.get_response( 403, '403', { 'error': { 'code': 403, 'message': hasattr(e, 'message') and str(e.message) or hasattr(e, 'name') and e.name or e } }) return self.get_response(request_code, str(request_code), datas) # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: