def on_post(self, req, res): req = to_snake_case(req.context['json']) req['permissions'] = [] if (req['password'] != req['password_confirmation']): raise PasswordNotMatch( description='Password and passwordConfirmation don\'t match!') del req['password_confirmation'] username_length = 0 try: user_exists = findOne(SCHEMA, username=req['username']) username_length = len(user_exists['username']) if (username_length > 0): raise UsernameNotAvailable( description='Username is not available') except Exception as e: if (username_length > 0): raise UsernameNotAvailable( description='Username is not available') try: req['password'] = b_encode(req['password']) user = create(SCHEMA, req) del user['password'] token = jwt_encode(user) data = {'apikey': token, 'user': user} self.on_success(res, to_camel_case(data)) except Exception as e: self.on_error( res, error={ 'code': 500, 'message': 'El servidor encontró una condición inesperada que le impidió cumplir con la solicitud.', 'status': falcon.HTTP_500 })
def on_get(self, req, res, id): try: data = findOne(SCHEMA, id=id) self.on_success(res, to_camel_case(data)) except Exception as e: self.on_error(res, error={ 'code': 404, 'message': 'No se encontro ningun registro!', 'status': falcon.HTTP_400 })
def on_post(self, req, res): try: req = to_snake_case(req.context['json']) try: one = findOne(SCHEMA, **req) if (one): self.on_error(res, error={ 'code': 404, 'message': 'Ya existe el registro!', 'status': falcon.HTTP_400 }) except Exception as e: try: one_one = findOne(SCHEMA_ACCOUNT, id=req['starting_account_id']) one_two = findOne(SCHEMA_ACCOUNT, id=req['starting_account_id']) req['id'] = str(uuid.uuid4()) data = create(SCHEMA, req) self.on_success(res, to_camel_case(data)) except Exception as e: self.on_error( res, error={ 'code': 404, 'message': 'No existe alguno de los registros proveidos!', 'status': falcon.HTTP_400 }) except Exception as e: self.on_error( res, error={ 'code': 500, 'message': 'El servidor encontró una condición inesperada que le impidió cumplir con la solicitud.', 'status': falcon.HTTP_500 })
def on_get(self, req, res, id): try: data = findOne(SCHEMA, id=id) data['friend_collection_url'] = os.getenv( 'URL_WEB') + data['friend_collection_url'] self.on_success(res, to_camel_case(data)) except Exception as e: self.on_error(res, error={ 'code': 404, 'message': 'No se encontro ningun registro!', 'status': falcon.HTTP_400 })
def funcion_HTTP(self, req, res, **kwargs): token = req.headers.get('AUTHORIZATION', None) try: userpayload = jwt_decode(token) user = findOne(SCHEMA, username=userpayload['username']) if(len(user.keys()) == 0): raise UnauthorizedError(description='Acceso no autorizado!') except Exception as e: raise UnauthorizedError(description='Invalid token') result = funcion_a(self, req, res) return result
def on_post(self, req, res): req = to_snake_case(req.context['json']) try: username = req['user'] try: user = findOne(SCHEMA_USERS, username=username) try: req['id'] = str(uuid.uuid4()) req['friend_collection_url'] = '/account/' + \ req['id'] + '/friends' data = create(SCHEMA, req) data['friend_collection_url'] = os.getenv( 'URL_WEB') + data['friend_collection_url'] self.on_success(res, to_camel_case(data)) except Exception as e: self.on_error(res, error={ 'code': 404, 'message': 'No se encontro ningun registro!', 'status': falcon.HTTP_400 }) except Exception as e: self.on_error(res, error={ 'code': 404, 'message': 'No se encontro ningun registro!', 'status': falcon.HTTP_400 }) except Exception as e: try: req['id'] = str(uuid.uuid4()) req['user'] = '' index = req['id'] req['friend_collection_url'] = '/account/' + index + '/friends' data = create(SCHEMA, req) data['friend_collection_url'] = os.getenv( 'URL_WEB') + data['friend_collection_url'] self.on_success(res, to_camel_case(data)) except Exception as e: self.on_error(res, error={ 'code': 404, 'message': 'No se encontro ningun registro!', 'status': falcon.HTTP_400 })
def on_put(self, req, res, id): req = to_snake_case(req.context['json']) try: username = req['user'] try: user = findOne(SCHEMA_USERS, username=username) try: data = update(SCHEMA, req, id=id) data['friend_collection_url'] = os.getenv( 'URL_WEB') + data['friend_collection_url'] self.on_success(res, to_camel_case(data)) except Exception as e: self.on_error(res, error={ 'code': 404, 'message': 'No se encontro ningun registro!', 'status': falcon.HTTP_400 }) except Exception as e: self.on_error(res, error={ 'code': 404, 'message': 'No se encontro ningun registro!', 'status': falcon.HTTP_400 }) except Exception as e: try: req['user'] = '' data = update(SCHEMA, req, id=id) data['friend_collection_url'] = os.getenv( 'URL_WEB') + data['friend_collection_url'] self.on_success(res, to_camel_case(data)) except Exception as e: self.on_error(res, error={ 'code': 404, 'message': 'No se encontro ningun registro!', 'status': falcon.HTTP_400 })
def on_post(self, req, res): req = to_snake_case(req.context['json']) try: user = findOne(SCHEMA, username=req['username']) except Exception as e: raise UserNotExistsError( description='User or password is not available') if (b_compare(req['password'], user['password']) is False): raise UserNotExistsError( description='User or password is not available') try: del user['password'] token = jwt_encode(user) data = {'apikey': token, 'user': user} self.on_success(res, to_camel_case(data)) except Exception as e: self.on_error( res, error={ 'code': 500, 'message': 'El servidor encontró una condición inesperada que le impidió cumplir con la solicitud.', 'status': falcon.HTTP_500 })