Skip to content

HAN-39 fixes #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -158,3 +158,4 @@ cython_debug/
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/
.DS_Store
7 changes: 7 additions & 0 deletions app/exceptions/UserException.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,10 @@ def __init__(self, id: int):
status_code = status.HTTP_404_NOT_FOUND
detail = f"User with id {id} not found"
super().__init__(status_code=status_code, detail=detail)


class InvalidURL(HTTPException):
def __init__(self, id: int):
status_code = status.HTTP_400_BAD_REQUEST
detail = "Invalid URL"
super().__init__(status_code=status_code, detail=detail)
4 changes: 2 additions & 2 deletions app/schemas/Schemas.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from pydantic import BaseModel, HttpUrl
from pydantic import BaseModel
from typing import Optional


Expand All @@ -21,4 +21,4 @@ class LoginRequest(BaseModel):
class UpdateUserSchema(BaseModel):
name: Optional[str] = None
gender: Optional[str] = None
photo: Optional[HttpUrl] = None
photo: str
13 changes: 11 additions & 2 deletions app/service/Users.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
from exceptions.UserException import UserNotFound
from exceptions.UserException import UserNotFound, InvalidURL
from exceptions.LoginException import AuthenticationError
from models.users import User
from repository.Users import UsersRepository
import requests
import os
import re


class UsersService:
Expand Down Expand Up @@ -76,4 +77,12 @@ def update_user(self, user_id: int, update_data: dict):
# es el propio usuario editando sus datos y no permitir
# que un usuario edite los de un tercero
self.get_user(user_id)
self.user_repository.edit_user(user_id, update_data)
filtered_update_data = {k: v for k, v in update_data.items()
if v is not None}
if 'photo' in filtered_update_data:
photo_url = filtered_update_data['photo']
if not re.match(r'^https?://(?:[a-zA-Z0-9-]+\.)+[a-zA-Z]{2,6}'
r'(?:/[^/#?]+)+\.(?:jpg|jpeg|png|gif)$',
photo_url):
raise InvalidURL("Invalid photo URL")
self.user_repository.edit_user(user_id, filtered_update_data)