async def update_angel(angel_id: UUID, angel_info: AngelUpdateDto) -> AngelDto: """Update angel info. Params: ------- - angel_id: UUID - The angel ID. - angel_data: AngelUpdateDto - The info to update. Return: ------- - angel: AngelDto - The updated angel. """ angel_data: dict = angel_info.dict() address_data = angel_data.get("address") angel_data.pop("address") if angel_data.get("photo"): angel_data["photo"] = get_or_update_image(angel_data["photo"]) try: angel = await Angel.get(id=angel_id) angel.update_from_dict(angel_data) await angel.save() if address_data: address = await Address.get(id=angel.address_id) address.update_from_dict(address_data) await address.save() except DoesNotExist: return exceptions.not_found_404("Angel not found") except IntegrityError as e: return exceptions.conflict_409(e.args[0]) angel_updated = await Angel.get(id=angel_id).prefetch_related("address") return await AngelDto.from_tortoise_orm(angel_updated)
async def delete_angel(angel_id: UUID) -> None: """Delete an angel by ID. Params: ------- - angel_id: UUID - The angel ID. """ try: angel = await Angel.get(id=angel_id) await angel.delete() except DoesNotExist: return exceptions.not_found_404("Angel does not exist")
async def confirm_email(token: str) -> None: """Change the status of "is_verified" to True. Params: ------- - token: str - Verification token. """ email = get_from_token(token) user = await User.filter(email=email).first() if not user: return exceptions.not_found_404("User not found.") user.is_verified = True await user.save()
async def get_angel(angel_id: UUID) -> AngelDto: """Retrieve and angel by Id. Params: ------- - angel_id: UUID - The ID of the target angel. Return: ------- - angel: AngelDto - The angel info. """ try: angel = await Angel.get(id=angel_id) except DoesNotExist: return exceptions.not_found_404("The angel does not exist") return await AngelDto.from_tortoise_orm(angel)
async def delete_user(user_id: UUID) -> None: """Update a user. Params: ------- - user_data: UserUpdateDto - The new data to update. - user_id: UUID - The user pk. Return: ------- - user: UserDto - The user updated. """ try: user = await User.get(id=user_id) await user.delete() except DoesNotExist: return exceptions.not_found_404("User not found")
async def reset_password(token: str, password: str) -> None: """Reset password and return the user. Params: ------- - token: str - Email token. - password: str - User new raw password. Return: ------- - user: UserDto - User info. """ email = get_from_token(token) user = await User.filter(email=email).first() if not user: return exceptions.not_found_404("User not found.") user.password = hash_password(password) await user.save()
async def get_qr_code(angel_id: UUID) -> str: """Generate a QR code image to the angel profile. Params: ------- - angel_id: UUID - The angel id. Return: ------- - pdf: str - The path to pdf gafet file. """ try: angel = await Angel.get(id=angel_id) except DoesNotExist: return exceptions.not_found_404("Angel not found") angel_url = f"{settings.WEB_HOST}/angels/{angel.id}" qr_image = generate_qr_base64_web(url=angel_url) date = str(datetime.now()) date = date.split(" ")[0] pdf = get_mosine_gafete(qr_code=qr_image, date=date) return pdf
async def update_angel_contact(contact_id: UUID, contact_info: ContactDto) -> AngelDto: """Update an angel contact data and return the angel updated. Params: ------- - contact_id: UUID - The contact ID. - contact_info: ContactDto - The contact data to update. Return: ------- - angel: AngelDto - The angel updated. """ try: contact = await Contact.get(id=contact_id) contact.update_from_dict(contact_info.dict()) await contact.save() except DoesNotExist: return exceptions.not_found_404("Contact not found") except IntegrityError as e: return exceptions.conflict_409(e.args[0]) angel = await Angel.get(contacts=contact.id).prefetch_related("address") return await AngelDto.from_tortoise_orm(angel)