def update(request, app): try: user = User.find(request.get('id')) user.name = request.get('name') user.username = request.get('username') user.save() return handler_response( app, 201, f'Se actualizo el usuario {user.username}') except Exception as e: return handler_response(app, 400, str(e))
def companies_create(self, app, data): try: respuesta = CompaniesModel.insert({ #'id': 2, 'name': data.get('name'), 'ruc': data.get('ruc'), 'status': data.get('status') }) return handler_response( app, 201, f'''Se creo la compañia {data.get('name')}''') except Exception as e: return handler_response(app, 501, str(e))
def create(request, app): try: password = hashpw( request.get('password').encode('utf-8'), gensalt()) user = User.create(name=request.get('name'), username=request.get('username'), password=password.decode('utf-8'), rol=request.get('rol')) return handler_response(app, 201, f'Se creo el usuario {user.username}') except Exception as e: return handler_response(app, 400, str(e))
def login(request, app): try: user = User.where('username', '=', request.get('username')).first() if user and user.password_valid(request.get('password')): token = encode(user.serialize(), jwt_secret(), algorithm='HS256') response = {'token': token, 'user': user.serialize()} return handler_response(app, 200, 'Logeado con exito', response) message = f'el usuario : {user.username} y/o la contraseña es incorrecta' return handler_response(app, 401, message) except Exception as error: return handler_response(app, 500, str(error))
def read(raw_token, app): token_info = decode_token(raw_token) if token_info[0]: current_user = User(**token_info[1]) try: if current_user.obtener_rol() == 'Administrador': user = User.all() return handler_response(app, 200, user.serialize()) else: return handler_response( app, 403, "El usuario no es un administrador") except Exception as e: return handler_response(app, 400, str(e)) return handler_response(app, 403, "Envie un token valido.")
def companies_all(self, app): try: result = CompaniesModel.get() companies_json = [] for i in result: companies = { 'id': i.id, 'name': i.name, 'ruc': i.ruc, 'status': i.status } companies_json.append(companies) return handler_response(app, 200, 'Data compañias', True, companies_json) except Exception as e: return handler_response(app, 501, str(e))
def companies_all(self, app): try: conn = Conexion() query = f'''SELECT * FROM companies;''' result = conn.execute_query(query) company = result.fetchone() companies = { 'id': company[0], 'name': company[1], 'ruc': company[2], 'status': company[3] } result_json = companies return handler_response(app, 200, 'Data companies', True, result_json) except Exception as e: return handler_response(app, 501, str(e))
def companies_get(self, app, company_id): try: result = CompaniesModel.where('id', company_id).first() if result: companies = { 'id': result.id, 'name': result.name, 'ruc': result.ruc, 'status': result.status } return handler_response(app, 200, f'Dato compañia {company_id}', True, companies) else: return handler_response(app, 404, f'La compania no fue encontrada') except Exception as e: return handler_response(app, 501, str(e))