Skip to content

Commit

Permalink
fix url regex
Browse files Browse the repository at this point in the history
  • Loading branch information
feijooso committed Apr 4, 2024
1 parent 43620a6 commit b37b6ee
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 16 deletions.
2 changes: 1 addition & 1 deletion app/schemas/Schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class LoginRequest(BaseModel):
class UpdateUserSchema(BaseModel):
name: Optional[str] = None
gender: Optional[str] = None
photo: Optional[HttpUrl] = None
photo: Optional[str] = None
birthdate: Optional[date] = None
location: Optional[Dict] = None
nickname: Optional[str] = None
Expand Down
29 changes: 14 additions & 15 deletions app/service/Users.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,20 @@ def create_user(self, user_data: dict):
raise InvalidData()
return self.user_repository.create_user(**user_data)

def update_user(self, user_id: int, update_data: dict):
# TODO: aca habria que chequear a partir del token, session o algo que
# es el propio usuario editando sus datos y no permitir
# que un usuario edite los de un tercero
self.get_user(user_id)
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'(?:/[^/#?]+)+(?:\?.*)?$', photo_url):
raise InvalidURL("Invalid photo URL")
self.user_repository.edit_user(user_id, filtered_update_data)

def login(self, auth_code: str):
access_token = self._get_access_token(auth_code)
if access_token is None:
Expand Down Expand Up @@ -72,21 +86,6 @@ def _get_user_info(self, access_token):
user_data['photo'] = response.json().get("picture")
return user_data

def update_user(self, user_id: int, update_data: dict):
# TODO: aca habria que chequear a partir del token, session o algo que
# es el propio usuario editando sus datos y no permitir
# que un usuario edite los de un tercero
self.get_user(user_id)
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)

def _validate_location(self, location):
if "lat" in location and "long" in location:
if -90 <= location["lat"] <= 90 and \
Expand Down

0 comments on commit b37b6ee

Please sign in to comment.