def delegate(Webapp2Instance, DictionaryName, MethodName, API_HANDLERS_MAP, additionalPayload={}): ### Load the dictionary and the method in that dictionary from the Permissions Map ### Webapp2Instance.response.headers['Content-Type'] = "application/javascript" # if either does not exist throw an error dictionary = getattr(API_HANDLERS_MAP, DictionaryName, None) if dictionary != None: method = getattr(dictionary, MethodName, None) if callable(method): import types func = types.MethodType( method, dictionary(), dictionary ) # bind 'method' to it's parent class, 'dictionary'. This is so it isn't an unbound function: this would throw errors # used so that if the request isn't a post, then the 'get' dictionary is required and so that the request body isn't parsed if Webapp2Instance.request.method == 'POST': if DictionaryName == 'get': # 'get' dictionary is exclusively used for GET requests result = response.throw(103) else: from json import loads as ParseJSON payload = ParseJSON(Webapp2Instance.request.body) payload['__Webapp2Instance__'] = Webapp2Instance result = func(payload) if result == False: return result = response.reply() if result == None else result else: if DictionaryName == 'get': # if there is a callback function specified, use it result = func(Webapp2Instance) if result == False: return result = response.reply() if result == None else result result = dict(result.items() + additionalPayload.items()) if Webapp2Instance.request.get( 'callback' ) != None and Webapp2Instance.request.get( 'callback') != '': data = '%s(%s);' % (Webapp2Instance.request.get( 'callback'), response.compile(result)) else: data = response.compile(result) Webapp2Instance.response.out.write(data) return result = response.throw(102) else: result = response.throw(100, (DictionaryName, MethodName)) else: result = response.throw(101, DictionaryName) result = dict(additionalPayload.items() + result.items()) Webapp2Instance.response.out.write(response.compile(result))
def delegate(request): # the json body params = json.loads(request.body) # get api method method = params['method'] del params['method'] # regex pattern for the method formatting = r'^(?:([a-z][a-z0-9]*)\.)*([a-z][a-z0-9]*)$' # the method applied to the regex pattern match = re.search(formatting, method) # if the method is poorly formatted throw an error if not match: return response.throw(0) # parse the different parts of the method (separated by a '.') groups = [group for group in match.groups() if group] # get for the api method api = MethodsClass for part in groups: if not hasattr(api, part): # the api method does not exist return response.throw(1) api = getattr(api, part) # run the method output = api(params) # check for a good response if 'stat' in output and output['stat'] == 'fail': # return failure return output # return a success return response.reply(output)
def changepassword(self, payload): user = payload['__Webapp2Instance__'].user worked = user.changePassword(payload['old_password'], payload['password']) if not worked: return response.throw(201) user.unlock() return response.reply({'passlocked':False})
def signup(self, payload): from ..sealeduser.model import SealedUser user = SealedUser.create(payload['email'], payload['password']); if user == SealedUser.EMAIL_IS_USED: return response.throw(200) return response.reply({ 'setsession': True, 'session': user.session.toDict(), 'user': user.toDict() })
def delegate(Webapp2Instance, DictionaryName, MethodName, API_HANDLERS_MAP, additionalPayload={}): ### Load the dictionary and the method in that dictionary from the Permissions Map ### Webapp2Instance.response.headers['Content-Type'] = "application/javascript" # if either does not exist throw an error dictionary = getattr(API_HANDLERS_MAP, DictionaryName, None) if dictionary != None: method = getattr(dictionary, MethodName, None) if callable(method): import types func = types.MethodType(method, dictionary(), dictionary)# bind 'method' to it's parent class, 'dictionary'. This is so it isn't an unbound function: this would throw errors # used so that if the request isn't a post, then the 'get' dictionary is required and so that the request body isn't parsed if Webapp2Instance.request.method == 'POST': if DictionaryName == 'get':# 'get' dictionary is exclusively used for GET requests result = response.throw(103) else: from json import loads as ParseJSON payload = ParseJSON(Webapp2Instance.request.body) payload['__Webapp2Instance__'] = Webapp2Instance result = func(payload) if result == False: return result = response.reply() if result == None else result else: if DictionaryName == 'get': # if there is a callback function specified, use it result = func(Webapp2Instance) if result == False: return result = response.reply() if result == None else result result = dict(result.items() + additionalPayload.items()) if Webapp2Instance.request.get('callback') != None and Webapp2Instance.request.get('callback') != '': data = '%s(%s);' % (Webapp2Instance.request.get('callback'), response.compile(result)) else: data = response.compile(result) Webapp2Instance.response.out.write(data) return result = response.throw(102) else: result = response.throw(100, (DictionaryName, MethodName)) else: result = response.throw(101, DictionaryName) result = dict(additionalPayload.items() + result.items()) Webapp2Instance.response.out.write(response.compile(result))
def send(self, payload): disappearing = payload['disappearing'] if 'disappearing' in payload else False user = payload['__Webapp2Instance__'].user contacts = user.sendMessage( payload['uri'], payload['recipients'], payload['date'], disappearing ) return response.reply({ 'contacts': contacts })
def unlock(self, payload): user = payload['__Webapp2Instance__'].user status = user.unlockSession( payload['email'], payload['password'] ) if status == user.INCORRECT_LOGIN: return response.throw(201) else: return response.reply({ 'setsession': True, 'session': user.session.toDict() })
def process(message_data): user_db = database.connect_db() type = message_data.get('post_type') message_type = message_data.get('message_type') if type == 'message' and message_type == 'private': uid = message_data.get('user_id') message = message_data.get('message') exist = database.check_user(uid, user_db) matched = re.search(pattern=r"[(.*?)]", string=message, flags=re.M | re.I) if exist == 0: if matched: database.user_register(uid, matched.group(1), user_db) return 0 else: send._pri_m('数据库中并没有查询到您的数据呢QAQ\n来注册一个吧', uid) send._pri_m('注册格式为: [您喜欢的名称] (保留方括号噢@A@)', uid) send._pri_m('请尽量使用英文字母噢(*^_^*)', uid) return 0 else: reply = response.reply(uid, type, message) data = reply.get_response() if data != 'ERROR': send._pri_m(data, uid) return 0 elif type == 'message' and message_type == 'group': uid = message_data.get('user_id') gid = message_data.get('group_id') message = message_data.get('message') self_id = message_data.get('self_id') if not database.check_user(uid, user_db): stri = '数据库中并没有查询到您的数据呢QAQ\n来注册一个吧\n向机器人私聊发送消息完成注册' send._group_m(stri, gid, uid) return 0 reply = response.group_reply(str(self_id), uid, type, message) data = reply.get_response() print data if data != 'ERROR': send._group_m(data, gid, uid) return 0 elif type == 'requests': uid = message_data.get('user_id') flag = message_data.get('flag') send._acc_request(flag) return 0
def login(self, payload): from ..sealeduser.model import SealedUser user = SealedUser.login( payload['email'], payload['password'], timezone = None if not 'timezone' in payload else payload['timezone'] ) if user == SealedUser.USER_DOESNT_EXIST: return response.throw(203) elif user == SealedUser.INCORRECT_LOGIN: return response.throw(201) elif user == SealedUser.BRUTE_SUSPECTED: return response.throw(202) else: return response.reply({ 'setsession': True, 'session': user.session.toDict(), 'user': user.toDict() })
def get(self, payload): from ..shards import Integer return response.reply({ 'value': Integer.getOrCreate(payload['name']).getValue() })
def profile(self, payload): from ..shards import Integer return response.reply(Integer.getOrCreate(payload['name']).profile());
def remove(self, payload): from ..testing.alphas import emails emails.remove(payload['email']) return response.reply()
def list(self, payload): from ..testing.alphas import emails return response.reply({ 'list': emails.all() })
def unlock(self, payload): user = payload['__Webapp2Instance__'].user user.unlock() return response.reply({'passlocked':False})
def get(self, payload): user = payload['__Webapp2Instance__'].user return response.reply({ 'messages': user.getMessages(json=True) })
def reply(self, data={}): self.response.headers['Content-Type'] = 'application/json' self.response.out.write(response.reply(data, compiled=True))