class SyncApi(): def __init__(self): pass def on_request(self, ch, method, props, body): pkt = NetworkPacket.fromJson(body) uuid = pkt.data['uuid'] cli_utc = pkt.data['message']['timestamp'] try: user = User.select().where(User.uuid == uuid).get() except DoesNotExist, e: ApiWorker.send_error(ch, method, props, "User does not Exist") return if user.db != None: srv_utc = user.timestamp else: srv_utc = 0 # print srv_utc, cli_utc if cli_utc > srv_utc: # print byteify(json.dumps(pkt.data['message']['db'])) user.db = byteify(json.dumps(pkt.data['message']['db'])) user.timestamp = cli_utc user.save() n = NetworkPacket() n.data['status'] = "OK" n.data['message'] = "Thank you" ApiWorker.send_reply(ch, method, props, n.toJson()) else: # print byteify(json.loads(user.db)) n = NetworkPacket() n.data['status'] = "UPDATE" n.data['message'] = byteify(json.loads(user.db)) # print n.data['message'] ApiWorker.send_reply(ch, method, props, n.toJson())
def create_new_user(self, userSocialData, uuid, pkt): user = User(db=None, uuid=uuid) user.save() 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.user = user userDevice.save() userSocialData.user = user userSocialData.data = byteify(json.dumps(pkt.data['message']['medium_data'])) userSocialData.save() user.save() print " -- new device: ", createdDevice print " -- User has been created" return user
def create_new_user(self, userSocialData, uuid, pkt): user = User(db=None, uuid=uuid) user.save() 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.user = user userDevice.save() userSocialData.user = user userSocialData.data = byteify( json.dumps(pkt.data['message']['medium_data'])) userSocialData.save() user.save() print " -- new device: ", createdDevice print " -- User has been created" return user
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