def alter_password(self, user_id, password, new_password): user = User.get(id=user_id) passwd_encode = self.encode_password(password) if user.password == passwd_encode: new_password_encode = self.encode_password(new_password) user.password = new_password_encode commit()
def create_donation(self, user_id ,**kwargs): unit = Unit.get(unit_id=kwargs["unit_id"]) user = User.get(user_id=user_id) donation_detaiils = DonationTypeDetails.get(donation_type_details_id=kwargs["donation_type_details_id"]) donation_type = DonationType.get(donation_type_id=kwargs["donation_type_id"]) date_entry = datetime.strptime(kwargs["date_entry"], "%d/%m/%Y %H:%M") date_out = datetime.strptime("01/01/9999", "%d/%m/%Y") donation = DonationUser(donation_type_details_id=donation_detaiils, donation_type_id=donation_type, unit_id=unit, date_entry=date_entry, date_out=date_out, amount_entry=kwargs["amount_entry"], user_id=user, status="PENDING" ) donation.flush() commit() response = {"user_id": donation.user_id.user_id, "unit_id": unit.unit_id, "unit_id_description": unit.description, "donation_user_id": donation.donation_user_id, "donation_type_id": donation_type.donation_type_id, "donation_type_id_description": donation_type.description, "donation_type_details_id": donation.donation_type_details_id.donation_type_details_id, "donation_type_details_id_description": donation.donation_type_details_id.description, "amount_entry": donation.amount_entry, "date_entry": kwargs["date_entry"], "date_out": "01/01/9999", "status": donation.status } return response
def get_user(self, id, export_json=True): user = User.get(user_id=id) phones_user = select(user_phone for user_phone in UserPhone if user_phone.user_id == user) phones = [] for phone_user in phones_user: phone = Phone.get(phone_id=phone_user.phone_id.phone_id) phones.append(phone.number) response = { "user_id": id, "nickname": user.nickname, "name": user.name, "email": user.email, "gender": user.gender, "role": user.role_id.description, "role_id": user.role_id.role_id, "bith_date": user.birth_date.strftime("%d/%m/%Y"), "city": user.city_id.city, "city_id": user.city_id.city_id, "state": user.city_id.state_id.state, "state_id": user.city_id.state_id.state_id, "phones": phones, "street": user.street, "number": user.number, "complement": user.complement, "district": user.district, "postal_code": user.postal_code, } if export_json: response = json.dumps(response) return response
def link_institution(self, user_id, institution_id): user = User.get(user_id=user_id) if self.limit_institution_number(user_id=user_id): crud_inst = CRUDInstitution() inst = crud_inst.get_institutions(institution_id=institution_id) user_inst = UserInstitution.get(user_id=user_id, institution_id=institution_id) if not user_inst is None: user_inst.status = "PENDING" else: user_inst = UserInstitution(user_id=user_id, institution_id=institution_id, status="PENDING", event_date=datetime.today()) commit() inst_data = crud_inst.get_institution( institution_id=institution_id, export_json=False) inst_data["status"] = "PENDING" result = inst_data else: result = { "message": "USUARIO NAO PODE SE VINCULAR EM MAIS DE 1 INSTITUICAO" } return result
def remove_phone(self, user_id, phone_id): user = User.get(user_id=user_id) phone = Phone.get(phone_id=phone_id) phone_user = UserPhone.get(user_id=user, phone_id=phone) phone.delete() phone_user.delete() commit()
def delete_user(self, user_id): try: user = User.get(user_id=user_id) user.active = False commit() return {"message": "user deleted"} except Exception as ex: print(ex) return {"message": "erro"}
def create_user(self, **kwargs): if self.validate_email(kwargs["email"]): user = User.get(email=kwargs["email"]) if user is None: if kwargs.get("role_id") is None: role = Role.get(description="user") else: role = Role.get(role_id=kwargs["role_id"]) passwd_encode = generate_password_hash(kwargs["password"]) user = User(city_id=kwargs["city_id"], email=kwargs["email"], password=passwd_encode, name=kwargs["name"], nickname=kwargs["nickname"], gender=kwargs["gender"], active=True, birth_date=datetime.strptime( kwargs["birth_date"], "%d/%m/%Y"), role_id=role, street=kwargs["street"], number=kwargs["number"], complement=kwargs["complement"], district=kwargs["district"], postal_code=kwargs["postal_code"], create_date=datetime.now()) user.flush() phones = kwargs["phone"] self.add_phones_user(phones, user) commit() response = { "user_id": user.user_id, "nickname": user.nickname, "name": user.name, "email": user.email, "gender": user.gender, "role": role.description, "role_id": role.role_id, "street": user.street, "city": user.city_id.city, "city_id": user.city_id.city_id, "state": user.city_id.state_id.state, "state_id": user.city_id.state_id.state_id, "number": user.number, "complement": user.complement, "district": user.district, "postal_code": user.postal_code, "phones": kwargs["phone"] } else: response = {"message": "Email exist", "type": "ERROR"} else: response = {"message": "Email not valide", "type": "ERROR"} return json.dumps(response)
def autenticate(self, email, password): user = User.get(email=email) user_controller = UserController() if user and check_password_hash(user.password, password): user_data = user_controller.get_user(id=user.user_id, export_json=False) token = self.encode_token(data={ "email": email, "role": user_data["role"] }) user_data["token"] = token return json.dumps(user_data) return None
def update_phone(self, user_id, phone_id, phone_number): if self.is_phone_valid(phone_str=phone_number): user = User.get(user_id=user_id) phone = Phone.get(phone_id=phone_id) phone_user = UserPhone.get(user_id=user, phone_id=phone) phone.number = phone_number commit() return json.dumps({ "id": user_id, "phone_id": phone_id, "phone_number": phone_number }) return {"message": "phone not valide", "type": "ERROR"}
def list_user_phones(self, user_id): user = User.get(user_id=user_id) phones_users = select(user_phone for user_phone in UserPhone if user_phone.user_id == user) phones = [] for phone_user in phones_users: phone = Phone.get(phone_id=phone_user.phone_id.phone_id) phone_json = { "id": user_id, "phone_id": phone_user.phone_id.phone_id, "number": phone.number } phones.append(phone_json) return json.dumps(phones)
def update_user(self, user_id, **kwargs): erro = "OK" try: user = User.get(user_id=user_id) if user is None: result = {"message": "user not found"} erro = "NOFOUND" else: user.set(**kwargs) commit() result = self.get_user(user_id, export_json=False) except Exception as ex: erro = "ERRO" result = {"message": "database erro"} return {"erro": erro, "result": result}