def main(): # import pprint # pp = pprint.PrettyPrinter(indent=4) # pp.pprint(n.data) n = NetworkPacket() n.data['api'] = 'auth' n.data['func'] = 'login' n.data['message'] = {} n.data['message']['medium_type'] = 'email' n.data['message']['medium_data'] = {} n.data['message']['medium_data']['login'] = '******' n.data['message']['medium_data']['password'] = '******' n.data['message']['medium_data']['action'] = 'register' n.data['message']['device_data'] = {} n.data['message']['device_data']['device_id'] = 'test_id' n.data['message']['device_data']['device_name'] = 'test_name' pp.pprint(n.data) print "\n", n.toJson(),"\n" # { 'accounts': [{ 'data': u'korshakv', 'id': 1, 'medium': u'vk'}], # 'db': None, # 'devices': [ { 'device_id': u'test_id', # 'device_name': u'test_name', # 'id': 1}], # 'fb': None, # 'gp': None, # 'id': 1, # 'phone': None, # 'uuid': '13ab9ad4-4904-5fa7-91d6-b5808245b46b', # 'vk': u'korshakv'} # {"message": {"medium_type": "email", "device_data": {"device_name": "test_name", "device_id": "test_id"}, "medium_data": "*****@*****.**"}, "api": "auth", "func": "login"} # {"message": {"medium_type": "fb", "device_data": {"timestamp": 1439448064478, "device_name": "LGE Nexus 4", "device_id": "9a9622ef5c84229"}, "medium_data": {"name": "Alex Yermolenko", "DOB": "", "gender": "male", "url": "https://www.facebook.com/app_scoped_user_id/10202972046261630/", "email": "*****@*****.**", "fotourl": "https://graph.facebook.com/10202972046261630/picture?type=large"}}, "api": "auth", "func": "login"} n.data['api'] = 'auth' n.data['func'] = 'add_account' n.data['uuid'] = '488416f4-fcaf-5027-8c63-0105cfa213ea' n.data['message'] = {} n.data['message']['medium_type'] = 'fb' n.data['message']['medium_data'] = {'url':'www.google.com'} n.data['message']['device_data'] = {} n.data['message']['device_data']['device_id'] = 'test_id' n.data['message']['device_data']['device_name'] = 'test_name' pp.pprint(n.data) print "\n", n.toJson(),"\n"
def main(): # import pprint # pp = pprint.PrettyPrinter(indent=4) # pp.pprint(n.data) n = NetworkPacket() n.data['api'] = 'auth' n.data['func'] = 'login' n.data['message'] = {} n.data['message']['medium_type'] = 'email' n.data['message']['medium_data'] = {} n.data['message']['medium_data']['login'] = '******' n.data['message']['medium_data']['password'] = '******' n.data['message']['medium_data']['action'] = 'register' n.data['message']['device_data'] = {} n.data['message']['device_data']['device_id'] = 'test_id' n.data['message']['device_data']['device_name'] = 'test_name' pp.pprint(n.data) print "\n", n.toJson(), "\n" # { 'accounts': [{ 'data': u'korshakv', 'id': 1, 'medium': u'vk'}], # 'db': None, # 'devices': [ { 'device_id': u'test_id', # 'device_name': u'test_name', # 'id': 1}], # 'fb': None, # 'gp': None, # 'id': 1, # 'phone': None, # 'uuid': '13ab9ad4-4904-5fa7-91d6-b5808245b46b', # 'vk': u'korshakv'} # {"message": {"medium_type": "email", "device_data": {"device_name": "test_name", "device_id": "test_id"}, "medium_data": "*****@*****.**"}, "api": "auth", "func": "login"} # {"message": {"medium_type": "fb", "device_data": {"timestamp": 1439448064478, "device_name": "LGE Nexus 4", "device_id": "9a9622ef5c84229"}, "medium_data": {"name": "Alex Yermolenko", "DOB": "", "gender": "male", "url": "https://www.facebook.com/app_scoped_user_id/10202972046261630/", "email": "*****@*****.**", "fotourl": "https://graph.facebook.com/10202972046261630/picture?type=large"}}, "api": "auth", "func": "login"} n.data['api'] = 'auth' n.data['func'] = 'add_account' n.data['uuid'] = '488416f4-fcaf-5027-8c63-0105cfa213ea' n.data['message'] = {} n.data['message']['medium_type'] = 'fb' n.data['message']['medium_data'] = {'url': 'www.google.com'} n.data['message']['device_data'] = {} n.data['message']['device_data']['device_id'] = 'test_id' n.data['message']['device_data']['device_name'] = 'test_name' pp.pprint(n.data) print "\n", n.toJson(), "\n"
def on_request(self, ch, method, props, body): pkt = NetworkPacket.fromJson(body) type = pkt.data['catalog_name'] n = NetworkPacket() if type in self.CATALOGS.keys(): func = pkt.data['func'] message = pkt.data['message'] if func in self.FUNCS: if func == 'get_catalog': offset = message['offset'] limit = message['limit'] n.data['status'] = 'OK' n.data['message'] = toJson(self.CATALOGS[type], offset, limit) else: ApiWorker.send_error(ch, method, props, body, 'Invalid func field') self.db.close() else: ApiWorker.send_error(ch, method, props, body, 'Invalid catalog name') self.db.close() return ApiWorker.send_reply(ch, method, props, n.toJson()) self.db.close() return
def handle_addaccount(self, ch, method, props, pkt): try: uuid = pkt.data['uuid'] medium = pkt.data['message']['medium_type'] value = pkt.data['message']['medium_data'] # find user user = User.get(User.uuid == uuid) if medium in VALID_MEDIUM_TYPES: createdSocial = None userSocialData = None if medium in ['vk', 'fb', 'gp']: url = value['url'] userSocialData, createdSocial = SocialData.get_or_create( medium=medium, value=url) uuid = self.generate_uuid(url) if medium in ['phone']: userSocialData, createdSocial = SocialData.get_or_create( medium=medium, value=value) uuid = self.generate_uuid(value) if createdSocial == True: userSocialData.user = user userSocialData.data = pkt.data['message']['medium_data'] userSocialData.save() user.save() n = NetworkPacket() n.data['status'] = "OK" n.data['message'] = {} n.data['message']['uuid'] = uuid ApiWorker.send_reply(ch, method, props, n.toJson()) else: ApiWorker.send_error( ch, method, props, 'Social account is linked to another User') else: ApiWorker.send_error(ch, method, props, 'Invalid login medium') except KeyError as e: ApiWorker.send_error( ch, method, props, 'Have you forgotten some field? ' + str(e.message)) except DoesNotExist as e: ApiWorker.send_error( ch, method, props, 'User with this UUID does not exist.' + str(e.message))
def send_error(ch, method, props, msg): n = NetworkPacket() n.data['status'] = 'ERROR' n.data['message'] = msg payload = n.toJson() if props.reply_to != None: ch.basic_publish(exchange='', routing_key=props.reply_to, properties=pika.BasicProperties(correlation_id=props.correlation_id), body=payload) ch.basic_nack(delivery_tag=method.delivery_tag, multiple=False, requeue=False) # print " ~~ Error msg sent to " + str(props.reply_to) + ": " + payload Log().send(type = "info", msg = " ~~ Error msg sent to " + str(props.reply_to) + ": " + payload)
def handle_addaccount(self, ch, method, props, pkt): try: uuid = pkt.data['uuid'] medium = pkt.data['message']['medium_type'] value = pkt.data['message']['medium_data'] # find user user = User.get(User.uuid == uuid) if medium in VALID_MEDIUM_TYPES: createdSocial = None userSocialData = None if medium in ['vk','fb','gp']: url = value['url'] userSocialData, createdSocial = SocialData.get_or_create(medium=medium, value=url) uuid = self.generate_uuid(url) if medium in ['phone']: userSocialData, createdSocial = SocialData.get_or_create(medium=medium, value=value) uuid = self.generate_uuid(value) if createdSocial == True: userSocialData.user = user userSocialData.data = pkt.data['message']['medium_data'] userSocialData.save() user.save() n = NetworkPacket() n.data['status'] = "OK" n.data['message'] = {} n.data['message']['uuid'] = uuid ApiWorker.send_reply(ch, method, props, n.toJson()) else: ApiWorker.send_error(ch, method, props, 'Social account is linked to another User') else: ApiWorker.send_error(ch, method, props, 'Invalid login medium') except KeyError as e: ApiWorker.send_error(ch, method, props, 'Have you forgotten some field? ' + str(e.message)) except DoesNotExist as e: ApiWorker.send_error(ch, method, props, 'User with this UUID does not exist.' + str(e.message))
def send_error(ch, method, props, msg): n = NetworkPacket() n.data['status'] = 'ERROR' n.data['message'] = msg payload = n.toJson() if props.reply_to != None: ch.basic_publish(exchange='', routing_key=props.reply_to, properties=pika.BasicProperties( correlation_id=props.correlation_id), body=payload) ch.basic_nack(delivery_tag=method.delivery_tag, multiple=False, requeue=False) # print " ~~ Error msg sent to " + str(props.reply_to) + ": " + payload Log().send(type="info", msg=" ~~ Error msg sent to " + str(props.reply_to) + ": " + payload)
def handle_login(self, ch, method, props, pkt): # lets check his account information first # medium: phone, vk, fb, gp, loginpass medium = pkt.data['message']['medium_type'] if medium == 'guest': userDevice, createdDevice = Device.get_or_create( device_id=pkt.data['message']['device_data']['device_id'], device_name=pkt.data['message']['device_data']['device_name']) userDevice.save() n = NetworkPacket() n.data['status'] = "OK" n.data['message'] = None ApiWorker.send_reply(ch, method, props, n.toJson()) return value = pkt.data['message']['medium_data'] if medium not in VALID_MEDIUM_TYPES: ApiWorker.send_error(ch, method, props, 'Invalid login medium') return else: userSocialData = None createdSocial = None uuid = None if medium in ['vk', 'fb', 'gp']: url = value['url'] userSocialData, createdSocial = SocialData.get_or_create(medium=medium, value=url) uuid = self.generate_uuid(url) if medium in ['phone']: userSocialData, createdSocial = SocialData.get_or_create(medium=medium, value=value) uuid = self.generate_uuid(value) if medium in ['email']: userSocialData, createdSocial = self.handle_email(ch, method, props, pkt) uuid = uuid = self.generate_uuid(value['login']) if userSocialData is not None: if createdSocial is False: # user exists if have not created a social account for him user = userSocialData.user print " -- user account exists " else: print " -- new account is going to be created" # we have a new incoming user trying to login user = self.create_new_user(userSocialData, uuid, pkt) # prepare the reply packet n = NetworkPacket() n.data['status'] = "OK" n.data['message'] = {} if user.db is not None: n.data['message']['db'] = byteify(json.loads(user.db)) else: n.data['message']['db'] = None n.data['message']['uuid'] = user.uuid ApiWorker.send_reply(ch, method, props, n.toJson()) return
def handle_login(self, ch, method, props, pkt): # lets check his account information first # medium: phone, vk, fb, gp, loginpass medium = pkt.data['message']['medium_type'] if medium == 'guest': userDevice, createdDevice = Device.get_or_create( device_id=pkt.data['message']['device_data']['device_id'], device_name=pkt.data['message']['device_data']['device_name']) userDevice.save() n = NetworkPacket() n.data['status'] = "OK" n.data['message'] = None ApiWorker.send_reply(ch, method, props, n.toJson()) return value = pkt.data['message']['medium_data'] if medium not in VALID_MEDIUM_TYPES: ApiWorker.send_error(ch, method, props, 'Invalid login medium') return else: userSocialData = None createdSocial = None uuid = None if medium in ['vk', 'fb', 'gp']: url = value['url'] userSocialData, createdSocial = SocialData.get_or_create( medium=medium, value=url) uuid = self.generate_uuid(url) if medium in ['phone']: userSocialData, createdSocial = SocialData.get_or_create( medium=medium, value=value) uuid = self.generate_uuid(value) if medium in ['email']: userSocialData, createdSocial = self.handle_email( ch, method, props, pkt) uuid = uuid = self.generate_uuid(value['login']) if userSocialData is not None: if createdSocial is False: # user exists if have not created a social account for him user = userSocialData.user print " -- user account exists " else: print " -- new account is going to be created" # we have a new incoming user trying to login user = self.create_new_user(userSocialData, uuid, pkt) # prepare the reply packet n = NetworkPacket() n.data['status'] = "OK" n.data['message'] = {} if user.db is not None: n.data['message']['db'] = byteify(json.loads(user.db)) else: n.data['message']['db'] = None n.data['message']['uuid'] = user.uuid ApiWorker.send_reply(ch, method, props, n.toJson()) return