From dd04cedf0edbf1bbf050f86c898aa75bb259ac4f Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Sat, 4 Nov 2023 20:45:07 +0100 Subject: [PATCH 01/16] change response return --- app/models/users.py | 3 ++- app/routers/users.py | 59 ++++++++++++++++++++++++++++++++------------ 2 files changed, 45 insertions(+), 17 deletions(-) diff --git a/app/models/users.py b/app/models/users.py index 9d71f9d..9064d53 100644 --- a/app/models/users.py +++ b/app/models/users.py @@ -14,8 +14,9 @@ class User(BaseModel): birth: str | None = None created_at: datetime = datetime.today() connected_at: datetime | None = None - updated_at: datetime = datetime.today() + updated_at: datetime | None = None deleted_at: datetime | None = None + disabled_at: datetime | None = None class UserOut(BaseModel): id: ObjectIdField = None diff --git a/app/routers/users.py b/app/routers/users.py index 309ce13..4940019 100644 --- a/app/routers/users.py +++ b/app/routers/users.py @@ -94,14 +94,21 @@ async def read_users_id(item_id : str, authorize: Annotated[bool, Depends(permis return user -@router.delete("/users/me",tags=["users"], response_model=users.User, response_model_exclude=["id", "password", "roles", "status"]) +@router.delete("/users/me",tags=["users"]) async def delete_users_me(current_user: Annotated[users.User, Depends(users_token.get_current_active_user)], authorize: Annotated[bool, Depends(permissions_checker.PermissionChecker(roles=["Admin", "User"]))], remove: bool = False): user_repository = users.UserRepository(database=database.database) - current_user.status = 0 + if remove is True: + current_user.deleted_at = datetime.today() current_user.status = -1 + content = {"message": "users are deleted"} + else: + current_user.status = 0 + current_user.deleted_at = datetime.today() + content = {"message": "users are disabled"} user_repository.save(current_user) - return current_user + response = JSONResponse(content=content) + return response @router.delete("/users/groups",tags=["users"]) async def delete_users_groups(authorize: Annotated[bool, Depends(permissions_checker.PermissionChecker(roles=["Admin"]))], remove: bool = False, userids: users.UserIDS | None = None): @@ -112,30 +119,38 @@ async def delete_users_groups(authorize: Annotated[bool, Depends(permissions_che ) user_repository = users.UserRepository(database=database.database) - content = {"message": "users are disabled"} for i in userids.ids: user = user_repository.find_one_by_id(ObjectId(i)) - user.status = 0 if remove is True: user.status = -1 + user.deleted_at = datetime.today() content = {"message": "users are deleted "} + else: + user.status = 0 + user.disabled_at = datetime.today() + content = {"message": "users are disabled"} user_repository.save(user) - response = JSONResponse(content=content) return response -@router.delete("/users/{item_id}", tags=["users"], response_model=users.User) +@router.delete("/users/{item_id}", tags=["users"]) async def delete_users_id(item_id : str, authorize: Annotated[bool, Depends(permissions_checker.PermissionChecker(roles=["Admin"]))], remove : bool = False): user_repository = users.UserRepository(database=database.database) user = user_repository.find_one_by_id(ObjectId(item_id)) - user.status = 0 if remove is True: user.status = -1 + user.deleted_at = datetime.today() + content = {"message": "users are deleted"} + else: + user.status = 0 + user.disabled_at = datetime.today() + content = {"message": "users are disabled"} user_repository.save(user) - return user + response = JSONResponse(content=content) + return response -@router.put("/users/me",tags=["users"], response_model=users.User, response_model_exclude=["id", "password", "roles", "status"]) +@router.put("/users/me",tags=["users"]) async def update_users_me(current_user: Annotated[users.User, Depends(users_token.get_current_active_user)], authorize: Annotated[bool, Depends(permissions_checker.PermissionChecker(roles=["Admin", "User"]))], userSingle: users.UserIn | None = None): user_repository = users.UserRepository(database=database.database) current_user.username = userSingle.username @@ -143,9 +158,11 @@ async def update_users_me(current_user: Annotated[users.User, Depends(users_toke current_user.roles = userSingle.roles current_user.email = userSingle.email user_repository.save(current_user) + content = {"message": "user is updated"} + response = JSONResponse(content=content) return current_user -@router.put("/users", tags=["users"], response_model=users.User, status_code=status.HTTP_201_CREATED) +@router.put("/users", tags=["users"], status_code=status.HTTP_201_CREATED) async def update_users(authorize: Annotated[bool, Depends(permissions_checker.PermissionChecker(roles=["Admin"]))], userSingle: users.UserIn | None = None, response: Response = Response): if userSingle is None: raise HTTPException( @@ -174,12 +191,14 @@ async def update_users(authorize: Annotated[bool, Depends(permissions_checker.Pe user.firstName = userSingle.firstName user.name = userSingle.name user.birth = userSingle.birth - user.updated_at = datetime.today() + user.created_at = datetime.today() user_repository.save(user) - return user + content = {"message": "user is created"} + response = JSONResponse(content=content) + return response -@router.put("/users/{item_id}", tags=["users"], response_model=users.User, status_code=status.HTTP_200_OK) +@router.put("/users/{item_id}", tags=["users"], status_code=status.HTTP_200_OK) async def update_users_id(item_id: str, authorize: Annotated[bool, Depends(permissions_checker.PermissionChecker(roles=["Admin"]))], userSingle: users.UserIn | None = None, response: Response = Response): if userSingle is None: raise HTTPException( @@ -204,7 +223,9 @@ async def update_users_id(item_id: str, authorize: Annotated[bool, Depends(permi user.birth = userSingle.birth user.updated_at = datetime.today() user_repository.save(user) - return user + content = {"message": "user is updated"} + response = JSONResponse(content=content) + return response @router.patch("/users/groups",tags=["users"]) @@ -220,6 +241,8 @@ async def patch_users_groups(authorize: Annotated[bool, Depends(permissions_chec for i in userids.ids: user = user_repository.find_one_by_id(ObjectId(i)) user.status = 1 + user.disabled_at = None + user.deleted_at = None user_repository.save(user) @@ -231,5 +254,9 @@ async def patch_users_id(item_id : str, authorize: Annotated[bool, Depends(permi user_repository = users.UserRepository(database=database.database) user = user_repository.find_one_by_id(ObjectId(item_id)) user.status = 1 + user.disabled_at = None + user.deleted_at = None user_repository.save(user) - return user \ No newline at end of file + content = {"message": "user is enabled"} + response = JSONResponse(content=content) + return response \ No newline at end of file From d206a9f8a3b5e45a84c5f2ba10b5ffae3c5191d3 Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Sat, 4 Nov 2023 23:36:19 +0100 Subject: [PATCH 02/16] update put method --- app/routers/users.py | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/app/routers/users.py b/app/routers/users.py index 4940019..5a7444b 100644 --- a/app/routers/users.py +++ b/app/routers/users.py @@ -163,7 +163,7 @@ async def update_users_me(current_user: Annotated[users.User, Depends(users_toke return current_user @router.put("/users", tags=["users"], status_code=status.HTTP_201_CREATED) -async def update_users(authorize: Annotated[bool, Depends(permissions_checker.PermissionChecker(roles=["Admin"]))], userSingle: users.UserIn | None = None, response: Response = Response): +async def update_users(authorize: Annotated[bool, Depends(permissions_checker.PermissionChecker(roles=["Admin"]))], userSingle: users.UserIn | None = None): if userSingle is None: raise HTTPException( status_code=status.HTTP_400_BAD_REQUEST, @@ -173,7 +173,6 @@ async def update_users(authorize: Annotated[bool, Depends(permissions_checker.Pe user = user_repository.find_one_by({"$or":[{"username": {'$eq': userSingle.username}}, {"email": {"$eq": userSingle.email}}]}) if user is not None: - response.status_code = status.HTTP_201_CREATED if user.username == userSingle.username: raise HTTPException( status_code=status.HTTP_204_NO_CONTENT, @@ -184,17 +183,16 @@ async def update_users(authorize: Annotated[bool, Depends(permissions_checker.Pe status_code=status.HTTP_204_NO_CONTENT, detail="email" ) - user.username = userSingle.username - user.password = users_token.get_password_hash(userSingle.password) + + user = users.User(username=userSingle.username, password=users_token.get_password_hash(userSingle.password), email=userSingle.email) user.roles = userSingle.roles - user.email = userSingle.email user.firstName = userSingle.firstName user.name = userSingle.name user.birth = userSingle.birth user.created_at = datetime.today() user_repository.save(user) content = {"message": "user is created"} - response = JSONResponse(content=content) + response = JSONResponse(content=content, status_code=status.HTTP_201_CREATED) return response From 75a6802c155bf567fbd8b9fca812fae39138d383 Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Mon, 6 Nov 2023 23:15:52 +0100 Subject: [PATCH 03/16] add event --- app/models/events.py | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 app/models/events.py diff --git a/app/models/events.py b/app/models/events.py new file mode 100644 index 0000000..c6fef02 --- /dev/null +++ b/app/models/events.py @@ -0,0 +1,43 @@ +from pydantic import BaseModel, EmailStr +from pydantic_mongo import AbstractRepository, ObjectIdField +from datetime import datetime, date + +class Event(BaseModel): + id: ObjectIdField = None + name: str + place: str + status: int = 0 + participants: list[str] | None = None + organizers: list[str] | None = None + interested: list[str] | None = None + start_date: datetime | None = None + end_date: datetime | None = None + created_at: datetime = datetime.today() + updated_at: datetime | None = None + deleted_at: datetime | None = None + disabled_at: datetime | None = None + +class EventOut(BaseModel): + id: ObjectIdField = None + name: str + place: str + status: int = 0 + participants: list[str] | None = None + organizers: list[str] | None = None + interested: list[str] | None = None + start_date: datetime | None = None + +class EventIn(BaseModel): + name: str + place: str + status: int = 0 + participants: list[str] | None = None + organizers: list[str] | None = None + interested: list[str] | None = None + start_date: datetime | None = None + + + +class EventRepository(AbstractRepository[Event]): + class Meta: + collection_name = "events" \ No newline at end of file From ff72cf3c0515dbae6446f182d053ddc301ba1455 Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Mon, 6 Nov 2023 23:27:47 +0100 Subject: [PATCH 04/16] add routers --- app/routers/events.py | 241 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 241 insertions(+) diff --git a/app/routers/events.py b/app/routers/events.py index e69de29..1705703 100644 --- a/app/routers/events.py +++ b/app/routers/events.py @@ -0,0 +1,241 @@ +from fastapi import APIRouter, Depends, HTTPException, status, Response +from fastapi.responses import JSONResponse +from datetime import datetime +from ..dependencies import users_token, permissions_checker, database +from ..models import events +from pydantic import EmailStr +from typing import Annotated +from bson import ObjectId +router = APIRouter() + + + +@router.get("/events", tags=["events"], response_model=list[events.EventOut]) +async def read_users(authorize: Annotated[bool, Depends(permissions_checker.PermissionChecker(roles=["Admin"]))], skip: int = 0, limit: int = 20, id_event: str | None = None, status: int | None = None): + if limit < 1 or skip < 0 or limit < skip: + raise HTTPException( + status_code=status.HTTP_400_BAD_REQUEST, + detail="skip should be greater than 0 and limit should be greater than 1. Limit should be greater than skip" + ) + limit = limit + skip + listEvents = [] + event_repository = events.EventRepository(database=database.database) + object_search = {} + if status is not None: + object_search = {"status":{"$eq": status}} + if id_event is not None: + eventid = ObjectId(id_event) + object_search = {"id": {"$regex": userid}} + if status is not None: + object_search = {"$and":[{"id":{"$regex": eventid}}, {"status":{"$eq":status}}]} + + + for event_index in event_repository.find_by(object_search, limit=limit, skip=skip): + event = users.UserOut(id=event_index.id, name=event_index.name, place=event_index.place, status=event_index.status, start_date=event_index.start_date, end_date=event_index.end_date) + listEvents.append(event) + return listEvents + +@router.get("/users/search", tags=["users"], response_model=list[users.UserOut]) +async def read_users_search(authorize: Annotated[bool, Depends(permissions_checker.PermissionChecker(roles=["Admin", "User"]))], skip: int = 0, limit: int = 20): + if limit < 1 or skip < 0 or limit < skip: + raise HTTPException( + status_code=status.HTTP_400_BAD_REQUEST, + detail="skip should be greater than 0 and limit should be greater than 1. Limit should be greater than skip" + ) + if key is None or value is None: + raise HTTPException( + status_code=status.HTTP_400_BAD_REQUEST, + detail="Key or/and value parameter is empty" + ) + limit = limit + skip + listUsers = [] + user_repository = users.UserRepository(database=database.database) + for user_index in user_repository.find_by({key: {'$regex': value}}, limit=limit, skip=skip): + user = users.UserOut(id=user_index.id, username=user_index.username, status=user_index.status, roles=user_index.roles, email=user_index.email, firstName=user_index.firstName, name=user_index.name) + listUsers.append(user) + return listUsers + + +@router.get("/users/me",tags=["users"], response_model=users.User, response_model_exclude=["id", "password", "roles", "status"]) +async def read_users_me(current_user: Annotated[users.User, Depends(users_token.get_current_active_user)], authorize: Annotated[bool, Depends(permissions_checker.PermissionChecker(roles=["Admin", "User"]))]): + return current_user + +@router.get("/users/count", tags=["users"]) +async def read_users_count(authorize: Annotated[bool, Depends(permissions_checker.PermissionChecker(roles=["Admin"]))]): + count = database.database.get_collection("users").estimated_document_count() + content = {"count":count} + response = JSONResponse(content=content) + return response + + +@router.get("/users/{item_id}", tags=["users"], response_model=users.User) +async def read_users_id(item_id : str, authorize: Annotated[bool, Depends(permissions_checker.PermissionChecker(roles=["Admin"]))]): + user_repository = users.UserRepository(database=database.database) + user = user_repository.find_one_by_id(ObjectId(item_id)) + return user + + +@router.delete("/users/me",tags=["users"]) +async def delete_users_me(current_user: Annotated[users.User, Depends(users_token.get_current_active_user)], authorize: Annotated[bool, Depends(permissions_checker.PermissionChecker(roles=["Admin", "User"]))], remove: bool = False): + user_repository = users.UserRepository(database=database.database) + + if remove is True: + current_user.deleted_at = datetime.today() + current_user.status = -1 + content = {"message": "users are deleted"} + else: + current_user.status = 0 + current_user.deleted_at = datetime.today() + content = {"message": "users are disabled"} + user_repository.save(current_user) + response = JSONResponse(content=content) + return response + +@router.delete("/users/groups",tags=["users"]) +async def delete_users_groups(authorize: Annotated[bool, Depends(permissions_checker.PermissionChecker(roles=["Admin"]))], remove: bool = False, userids: users.UserIDS | None = None): + if len(userids.ids) == 0: + raise HTTPException( + status_code=status.HTTP_400_BAD_REQUEST, + detail="userids should be greater than 0" + ) + + user_repository = users.UserRepository(database=database.database) + for i in userids.ids: + user = user_repository.find_one_by_id(ObjectId(i)) + if remove is True: + user.status = -1 + user.deleted_at = datetime.today() + content = {"message": "users are deleted "} + else: + user.status = 0 + user.disabled_at = datetime.today() + content = {"message": "users are disabled"} + user_repository.save(user) + + response = JSONResponse(content=content) + return response + +@router.delete("/users/{item_id}", tags=["users"]) +async def delete_users_id(item_id : str, authorize: Annotated[bool, Depends(permissions_checker.PermissionChecker(roles=["Admin"]))], remove : bool = False): + user_repository = users.UserRepository(database=database.database) + user = user_repository.find_one_by_id(ObjectId(item_id)) + if remove is True: + user.status = -1 + user.deleted_at = datetime.today() + content = {"message": "users are deleted"} + else: + user.status = 0 + user.disabled_at = datetime.today() + content = {"message": "users are disabled"} + user_repository.save(user) + response = JSONResponse(content=content) + return response + +@router.put("/users/me",tags=["users"]) +async def update_users_me(current_user: Annotated[users.User, Depends(users_token.get_current_active_user)], authorize: Annotated[bool, Depends(permissions_checker.PermissionChecker(roles=["Admin", "User"]))], userSingle: users.UserIn | None = None): + user_repository = users.UserRepository(database=database.database) + current_user.username = userSingle.username + current_user.password = user_token.get_password_hash(userSingle.password) + current_user.roles = userSingle.roles + current_user.email = userSingle.email + user_repository.save(current_user) + content = {"message": "user is updated"} + response = JSONResponse(content=content) + return current_user + +@router.put("/users", tags=["users"], status_code=status.HTTP_201_CREATED) +async def update_users(authorize: Annotated[bool, Depends(permissions_checker.PermissionChecker(roles=["Admin"]))], userSingle: users.UserIn | None = None): + if userSingle is None: + raise HTTPException( + status_code=status.HTTP_400_BAD_REQUEST, + detail="Body request is empty" + ) + user_repository = users.UserRepository(database=database.database) + + user = user_repository.find_one_by({"$or":[{"username": {'$eq': userSingle.username}}, {"email": {"$eq": userSingle.email}}]}) + if user is not None: + if user.username == userSingle.username: + raise HTTPException( + status_code=status.HTTP_204_NO_CONTENT, + detail="username" + ) + if user.email == userSingle.email: + raise HTTPException( + status_code=status.HTTP_204_NO_CONTENT, + detail="email" + ) + + user = users.User(username=userSingle.username, password=users_token.get_password_hash(userSingle.password), email=userSingle.email) + user.roles = userSingle.roles + user.firstName = userSingle.firstName + user.name = userSingle.name + user.birth = userSingle.birth + user.created_at = datetime.today() + user_repository.save(user) + content = {"message": "user is created"} + response = JSONResponse(content=content, status_code=status.HTTP_201_CREATED) + return response + + +@router.put("/users/{item_id}", tags=["users"], status_code=status.HTTP_200_OK) +async def update_users_id(item_id: str, authorize: Annotated[bool, Depends(permissions_checker.PermissionChecker(roles=["Admin"]))], userSingle: users.UserIn | None = None, response: Response = Response): + if userSingle is None: + raise HTTPException( + status_code=status.HTTP_400_BAD_REQUEST, + detail="Body request is empty" + ) + user_repository = users.UserRepository(database=database.database) + + user = user_repository.find_one_by({"id": {'$eq': ObjectId(item_id)}}) + if user is None: + raise HTTPException( + status_code=status.HTTP_404_NOT_FOUND, + detail="User not found" + ) + + user.username = userSingle.username + user.password = users_token.get_password_hash(userSingle.password) + user.roles = userSingle.roles + user.email = userSingle.email + user.firstName = userSingle.firstName + user.name = userSingle.name + user.birth = userSingle.birth + user.updated_at = datetime.today() + user_repository.save(user) + content = {"message": "user is updated"} + response = JSONResponse(content=content) + return response + + +@router.patch("/users/groups",tags=["users"]) +async def patch_users_groups(authorize: Annotated[bool, Depends(permissions_checker.PermissionChecker(roles=["Admin"]))], userids: users.UserIDS | None = None): + if len(userids.ids) == 0: + raise HTTPException( + status_code=status.HTTP_400_BAD_REQUEST, + detail="userids should be greater than 0" + ) + + user_repository = users.UserRepository(database=database.database) + content = {"message": "users are enabled"} + for i in userids.ids: + user = user_repository.find_one_by_id(ObjectId(i)) + user.status = 1 + user.disabled_at = None + user.deleted_at = None + user_repository.save(user) + + + response = JSONResponse(content=content) + return response + +@router.patch("/users/{item_id}", tags=["users"], response_model=users.User) +async def patch_users_id(item_id : str, authorize: Annotated[bool, Depends(permissions_checker.PermissionChecker(roles=["Admin"]))]): + user_repository = users.UserRepository(database=database.database) + user = user_repository.find_one_by_id(ObjectId(item_id)) + user.status = 1 + user.disabled_at = None + user.deleted_at = None + user_repository.save(user) + content = {"message": "user is enabled"} + response = JSONResponse(content=content) + return response \ No newline at end of file From 51634184affc9dd3a87e8d4636806fbb2e4b3ba4 Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Mon, 6 Nov 2023 23:29:12 +0100 Subject: [PATCH 05/16] add router events --- app/main.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/app/main.py b/app/main.py index 8e8d558..276681c 100644 --- a/app/main.py +++ b/app/main.py @@ -1,7 +1,7 @@ from fastapi import FastAPI from fastapi.middleware.cors import CORSMiddleware -from .routers import users, token, mail +from .routers import users, token, mail, events from .dependencies import user_add import os @@ -24,6 +24,7 @@ app.add_middleware( app.include_router(users.router) app.include_router(token.router) app.include_router(mail.router) +app.include_router(events.router) @app.on_event("startup") From 6cc43005f0044fb16679f8454cee273769551ecb Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Tue, 7 Nov 2023 21:08:58 +0100 Subject: [PATCH 06/16] add name --- app/routers/events.py | 26 +++++--------------------- app/routers/users.py | 33 +++++++++++---------------------- 2 files changed, 16 insertions(+), 43 deletions(-) diff --git a/app/routers/events.py b/app/routers/events.py index 1705703..51fd6ab 100644 --- a/app/routers/events.py +++ b/app/routers/events.py @@ -11,7 +11,7 @@ router = APIRouter() @router.get("/events", tags=["events"], response_model=list[events.EventOut]) -async def read_users(authorize: Annotated[bool, Depends(permissions_checker.PermissionChecker(roles=["Admin"]))], skip: int = 0, limit: int = 20, id_event: str | None = None, status: int | None = None): +async def read_events(authorize: Annotated[bool, Depends(permissions_checker.PermissionChecker(roles=["Admin", "User"]))], skip: int = 0, limit: int = 20, id_event: str | None = None, name: str | None = None, status: int | None = None): if limit < 1 or skip < 0 or limit < skip: raise HTTPException( status_code=status.HTTP_400_BAD_REQUEST, @@ -28,6 +28,10 @@ async def read_users(authorize: Annotated[bool, Depends(permissions_checker.Perm object_search = {"id": {"$regex": userid}} if status is not None: object_search = {"$and":[{"id":{"$regex": eventid}}, {"status":{"$eq":status}}]} + if name is not None: + object_search = {"name": {"$regex": name}} + if status is not None: + object_search = {"$and":[{"name":{"$regex": name}}, {"status":{"$eq":status}}]} for event_index in event_repository.find_by(object_search, limit=limit, skip=skip): @@ -35,26 +39,6 @@ async def read_users(authorize: Annotated[bool, Depends(permissions_checker.Perm listEvents.append(event) return listEvents -@router.get("/users/search", tags=["users"], response_model=list[users.UserOut]) -async def read_users_search(authorize: Annotated[bool, Depends(permissions_checker.PermissionChecker(roles=["Admin", "User"]))], skip: int = 0, limit: int = 20): - if limit < 1 or skip < 0 or limit < skip: - raise HTTPException( - status_code=status.HTTP_400_BAD_REQUEST, - detail="skip should be greater than 0 and limit should be greater than 1. Limit should be greater than skip" - ) - if key is None or value is None: - raise HTTPException( - status_code=status.HTTP_400_BAD_REQUEST, - detail="Key or/and value parameter is empty" - ) - limit = limit + skip - listUsers = [] - user_repository = users.UserRepository(database=database.database) - for user_index in user_repository.find_by({key: {'$regex': value}}, limit=limit, skip=skip): - user = users.UserOut(id=user_index.id, username=user_index.username, status=user_index.status, roles=user_index.roles, email=user_index.email, firstName=user_index.firstName, name=user_index.name) - listUsers.append(user) - return listUsers - @router.get("/users/me",tags=["users"], response_model=users.User, response_model_exclude=["id", "password", "roles", "status"]) async def read_users_me(current_user: Annotated[users.User, Depends(users_token.get_current_active_user)], authorize: Annotated[bool, Depends(permissions_checker.PermissionChecker(roles=["Admin", "User"]))]): diff --git a/app/routers/users.py b/app/routers/users.py index 5a7444b..fdbda41 100644 --- a/app/routers/users.py +++ b/app/routers/users.py @@ -11,7 +11,7 @@ router = APIRouter() @router.get("/users", tags=["users"], response_model=list[users.UserOut]) -async def read_users(authorize: Annotated[bool, Depends(permissions_checker.PermissionChecker(roles=["Admin"]))], skip: int = 0, limit: int = 20, id_user: str | None = None, roles: str | None = None, status: int | None = None, email: EmailStr | None = None): +async def read_users(authorize: Annotated[bool, Depends(permissions_checker.PermissionChecker(roles=["Admin", "User"]))], skip: int = 0, limit: int = 20, id_user: str | None = None, roles: str | None = None, status: int | None = None, email: EmailStr | None = None, name: str | None = None): if limit < 1 or skip < 0 or limit < skip: raise HTTPException( status_code=status.HTTP_400_BAD_REQUEST, @@ -48,33 +48,22 @@ async def read_users(authorize: Annotated[bool, Depends(permissions_checker.Perm if roles is not None: object_search = {"$and":[{"email":{"$eq": email}}, {"roles":{"$eq":roles}}]} + if name is not None: + object_search = {"username": {"$regex": name}} + if status is not None and roles is not None: + object_search = {"$and":[{"username":{"$regex": name}}, {"roles":{"$eq": roles}}, {"status":{"$eq":status}}]} + else: + if status is not None: + object_search = {"$and":[{"username":{"$regex": name}}, {"status":{"$eq":status}}]} + if roles is not None: + object_search = {"$and":[{"username":{"$regex": name}}, {"roles":{"$eq":roles}}]} + for user_index in user_repository.find_by(object_search, limit=limit, skip=skip): user = users.UserOut(id=user_index.id, username=user_index.username, email=user_index.email, status=user_index.status, roles=user_index.roles, firstName=user_index.firstName, name=user_index.name) listUsers.append(user) return listUsers -@router.get("/users/search", tags=["users"], response_model=list[users.UserOut]) -async def read_users_search(authorize: Annotated[bool, Depends(permissions_checker.PermissionChecker(roles=["Admin", "User"]))], skip: int = 0, limit: int = 20): - if limit < 1 or skip < 0 or limit < skip: - raise HTTPException( - status_code=status.HTTP_400_BAD_REQUEST, - detail="skip should be greater than 0 and limit should be greater than 1. Limit should be greater than skip" - ) - if key is None or value is None: - raise HTTPException( - status_code=status.HTTP_400_BAD_REQUEST, - detail="Key or/and value parameter is empty" - ) - limit = limit + skip - listUsers = [] - user_repository = users.UserRepository(database=database.database) - for user_index in user_repository.find_by({key: {'$regex': value}}, limit=limit, skip=skip): - user = users.UserOut(id=user_index.id, username=user_index.username, status=user_index.status, roles=user_index.roles, email=user_index.email, firstName=user_index.firstName, name=user_index.name) - listUsers.append(user) - return listUsers - - @router.get("/users/me",tags=["users"], response_model=users.User, response_model_exclude=["id", "password", "roles", "status"]) async def read_users_me(current_user: Annotated[users.User, Depends(users_token.get_current_active_user)], authorize: Annotated[bool, Depends(permissions_checker.PermissionChecker(roles=["Admin", "User"]))]): return current_user From a85582d200761bb26eb1c5576988feb8bb2d5eb3 Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Wed, 8 Nov 2023 22:43:53 +0100 Subject: [PATCH 07/16] change delete method events --- app/models/events.py | 22 ++++---- app/routers/events.py | 119 ++++++++++++++++++++++++------------------ 2 files changed, 82 insertions(+), 59 deletions(-) diff --git a/app/models/events.py b/app/models/events.py index c6fef02..eb57b6d 100644 --- a/app/models/events.py +++ b/app/models/events.py @@ -7,9 +7,9 @@ class Event(BaseModel): name: str place: str status: int = 0 - participants: list[str] | None = None - organizers: list[str] | None = None - interested: list[str] | None = None + participants: list[str] = [] + organizers: list[str] = [] + interested: list[str] = [] start_date: datetime | None = None end_date: datetime | None = None created_at: datetime = datetime.today() @@ -22,20 +22,24 @@ class EventOut(BaseModel): name: str place: str status: int = 0 - participants: list[str] | None = None - organizers: list[str] | None = None - interested: list[str] | None = None + participants: list[str] = [] + organizers: list[str] = [] + interested: list[str] = [] start_date: datetime | None = None + end_date: datetime | None = None class EventIn(BaseModel): name: str place: str status: int = 0 - participants: list[str] | None = None - organizers: list[str] | None = None - interested: list[str] | None = None + participants: list[str] = [] + organizers: list[str] = [] + interested: list[str] = [] start_date: datetime | None = None + end_date: datetime | None = None +class EventIDS(BaseModel): + ids: list[str] class EventRepository(AbstractRepository[Event]): diff --git a/app/routers/events.py b/app/routers/events.py index 51fd6ab..8dca26f 100644 --- a/app/routers/events.py +++ b/app/routers/events.py @@ -2,7 +2,7 @@ from fastapi import APIRouter, Depends, HTTPException, status, Response from fastapi.responses import JSONResponse from datetime import datetime from ..dependencies import users_token, permissions_checker, database -from ..models import events +from ..models import events, users from pydantic import EmailStr from typing import Annotated from bson import ObjectId @@ -35,88 +35,107 @@ async def read_events(authorize: Annotated[bool, Depends(permissions_checker.Per for event_index in event_repository.find_by(object_search, limit=limit, skip=skip): - event = users.UserOut(id=event_index.id, name=event_index.name, place=event_index.place, status=event_index.status, start_date=event_index.start_date, end_date=event_index.end_date) + event = events.EventOut(id=event_index.id, name=event_index.name, place=event_index.place, status=event_index.status, start_date=event_index.start_date, end_date=event_index.end_date) listEvents.append(event) return listEvents -@router.get("/users/me",tags=["users"], response_model=users.User, response_model_exclude=["id", "password", "roles", "status"]) +@router.get("/events/me",tags=["events"]) async def read_users_me(current_user: Annotated[users.User, Depends(users_token.get_current_active_user)], authorize: Annotated[bool, Depends(permissions_checker.PermissionChecker(roles=["Admin", "User"]))]): - return current_user + event_repository = events.EventRepository(database=database.database) + listOrganizers = [] + listParticipants = [] + listInterested = [] -@router.get("/users/count", tags=["users"]) -async def read_users_count(authorize: Annotated[bool, Depends(permissions_checker.PermissionChecker(roles=["Admin"]))]): - count = database.database.get_collection("users").estimated_document_count() + for event_index in event_repository.find_by({"organizers":{"$eq": current_user.id}}, limit=limit, skip=skip): + event = events.EventOut(id=event_index.id, name=event_index.name, place=event_index.place, status=event_index.status, start_date=event_index.start_date, end_date=event_index.end_date) + listOrganizers.append(event) + + for event_index in event_repository.find_by({"participants":{"$eq": current_user.id}}, limit=limit, skip=skip): + event = events.EventOut(id=event_index.id, name=event_index.name, place=event_index.place, status=event_index.status, start_date=event_index.start_date, end_date=event_index.end_date) + listParticipants.append(event) + + for event_index in event_repository.find_by({"interested":{"$eq": current_user.id}}, limit=limit, skip=skip): + event = events.EventOut(id=event_index.id, name=event_index.name, place=event_index.place, status=event_index.status, start_date=event_index.start_date, end_date=event_index.end_date) + listInterested.append(event) + + content = {"participants":listParticipants, "organizers":listOrganizers, "interested":listInterested} + response = JSONResponse(content=content) + return response + +@router.get("/events/count", tags=["events"]) +async def read_events_count(authorize: Annotated[bool, Depends(permissions_checker.PermissionChecker(roles=["Admin", "User"]))]): + count = database.database.get_collection("events").estimated_document_count() content = {"count":count} response = JSONResponse(content=content) return response -@router.get("/users/{item_id}", tags=["users"], response_model=users.User) -async def read_users_id(item_id : str, authorize: Annotated[bool, Depends(permissions_checker.PermissionChecker(roles=["Admin"]))]): - user_repository = users.UserRepository(database=database.database) - user = user_repository.find_one_by_id(ObjectId(item_id)) - return user +@router.get("/events/{item_id}", tags=["events"], response_model=events.Event) +async def read_events_id(item_id : str, authorize: Annotated[bool, Depends(permissions_checker.PermissionChecker(roles=["Admin"]))]): + event_repository = event.EventRepository(database=database.database) + event = event_repository.find_one_by_id(ObjectId(item_id)) + return event -@router.delete("/users/me",tags=["users"]) -async def delete_users_me(current_user: Annotated[users.User, Depends(users_token.get_current_active_user)], authorize: Annotated[bool, Depends(permissions_checker.PermissionChecker(roles=["Admin", "User"]))], remove: bool = False): - user_repository = users.UserRepository(database=database.database) - +@router.delete("/events/me/{item_id}", tags=["events"]) +async def delete_event_me(item_id: str, current_user: Annotated[users.User, Depends(users_token.get_current_active_user)], authorize: Annotated[bool, Depends(permissions_checker.PermissionChecker(roles=["Admin", "User"]))], remove: bool = False): + event_repository = event.EventRepository(database=database.database) + event = event_repository.find_one_by_id(ObjectId(item_id)) if remove is True: - current_user.deleted_at = datetime.today() - current_user.status = -1 - content = {"message": "users are deleted"} + event.deleted_at = datetime.today() + event.status = -1 + content = {"message": "event is deleted"} else: - current_user.status = 0 - current_user.deleted_at = datetime.today() - content = {"message": "users are disabled"} - user_repository.save(current_user) + event.status = 0 + event.deleted_at = datetime.today() + content = {"message": "event is disabled"} + event_repository.save(event) response = JSONResponse(content=content) return response -@router.delete("/users/groups",tags=["users"]) -async def delete_users_groups(authorize: Annotated[bool, Depends(permissions_checker.PermissionChecker(roles=["Admin"]))], remove: bool = False, userids: users.UserIDS | None = None): - if len(userids.ids) == 0: +@router.delete("/events/groups",tags=["events"]) +async def delete_events_groups(authorize: Annotated[bool, Depends(permissions_checker.PermissionChecker(roles=["Admin"]))], remove: bool = False, eventids: events.EventIDS | None = None): + if len(eventids.ids) == 0: raise HTTPException( status_code=status.HTTP_400_BAD_REQUEST, - detail="userids should be greater than 0" + detail="eventids should be greater than 0" ) - user_repository = users.UserRepository(database=database.database) - for i in userids.ids: - user = user_repository.find_one_by_id(ObjectId(i)) + event_repository = event.EventRepository(database=database.database) + for i in eventids.ids: + event = event_repository.find_one_by_id(ObjectId(i)) if remove is True: - user.status = -1 - user.deleted_at = datetime.today() - content = {"message": "users are deleted "} + event.status = -1 + event.deleted_at = datetime.today() + content = {"message": "events are deleted "} else: - user.status = 0 - user.disabled_at = datetime.today() - content = {"message": "users are disabled"} - user_repository.save(user) + event.status = 0 + event.disabled_at = datetime.today() + content = {"message": "events are disabled"} + event_repository.save(event) response = JSONResponse(content=content) return response -@router.delete("/users/{item_id}", tags=["users"]) -async def delete_users_id(item_id : str, authorize: Annotated[bool, Depends(permissions_checker.PermissionChecker(roles=["Admin"]))], remove : bool = False): - user_repository = users.UserRepository(database=database.database) - user = user_repository.find_one_by_id(ObjectId(item_id)) +@router.delete("/events/{item_id}", tags=["events"]) +async def delete_events_id(item_id : str, authorize: Annotated[bool, Depends(permissions_checker.PermissionChecker(roles=["Admin"]))], remove : bool = False): + event_repository = event.EventRepository(database=database.database) + event = event_repository.find_one_by_id(ObjectId(item_id)) if remove is True: - user.status = -1 - user.deleted_at = datetime.today() - content = {"message": "users are deleted"} + event.status = -1 + event.deleted_at = datetime.today() + content = {"message": "events are deleted"} else: - user.status = 0 - user.disabled_at = datetime.today() - content = {"message": "users are disabled"} - user_repository.save(user) + event.status = 0 + event.disabled_at = datetime.today() + content = {"message": "events are disabled"} + event.save(user) response = JSONResponse(content=content) return response -@router.put("/users/me",tags=["users"]) -async def update_users_me(current_user: Annotated[users.User, Depends(users_token.get_current_active_user)], authorize: Annotated[bool, Depends(permissions_checker.PermissionChecker(roles=["Admin", "User"]))], userSingle: users.UserIn | None = None): +@router.put("/events/me/{item_id}",tags=["users"]) +async def update_users_me(item_id: str, current_user: Annotated[users.User, Depends(users_token.get_current_active_user)], authorize: Annotated[bool, Depends(permissions_checker.PermissionChecker(roles=["Admin", "User"]))], userSingle: users.UserIn | None = None): user_repository = users.UserRepository(database=database.database) current_user.username = userSingle.username current_user.password = user_token.get_password_hash(userSingle.password) From c7b209030e677da58eabef62575a5904abc3db8d Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Wed, 8 Nov 2023 23:10:45 +0100 Subject: [PATCH 08/16] put and patch events --- app/routers/events.py | 142 +++++++++++++++++++++--------------------- 1 file changed, 70 insertions(+), 72 deletions(-) diff --git a/app/routers/events.py b/app/routers/events.py index 8dca26f..ba11686 100644 --- a/app/routers/events.py +++ b/app/routers/events.py @@ -134,111 +134,109 @@ async def delete_events_id(item_id : str, authorize: Annotated[bool, Depends(per response = JSONResponse(content=content) return response -@router.put("/events/me/{item_id}",tags=["users"]) -async def update_users_me(item_id: str, current_user: Annotated[users.User, Depends(users_token.get_current_active_user)], authorize: Annotated[bool, Depends(permissions_checker.PermissionChecker(roles=["Admin", "User"]))], userSingle: users.UserIn | None = None): - user_repository = users.UserRepository(database=database.database) - current_user.username = userSingle.username - current_user.password = user_token.get_password_hash(userSingle.password) - current_user.roles = userSingle.roles - current_user.email = userSingle.email - user_repository.save(current_user) - content = {"message": "user is updated"} +@router.put("/events/me/{item_id}",tags=["events"]) +async def update_events_me(item_id: str, current_user: Annotated[users.User, Depends(users_token.get_current_active_user)], authorize: Annotated[bool, Depends(permissions_checker.PermissionChecker(roles=["Admin", "User"]))], eventSingle: events.EventIn | None = None): + event_repository = event.EventRepository(database=database.database) + event = event_repository.find_one_by_id(ObjectId(item_id)) + event.name = EventSingle.name + event.place = EventSingle.place + event.start_date = eventSingle.start_date + event.end_date = eventSingle.end_date + event.organizers = eventSingle.organizers + event.participants = eventSingle.participants + event.interested = eventSingle.interested + event.updated_at = datetime.today() + event_repository.save(event) + content = {"message": "event is updated"} response = JSONResponse(content=content) - return current_user + return response -@router.put("/users", tags=["users"], status_code=status.HTTP_201_CREATED) -async def update_users(authorize: Annotated[bool, Depends(permissions_checker.PermissionChecker(roles=["Admin"]))], userSingle: users.UserIn | None = None): - if userSingle is None: +@router.put("/events", tags=["events"], status_code=status.HTTP_201_CREATED) +async def update_events(authorize: Annotated[bool, Depends(permissions_checker.PermissionChecker(roles=["Admin"]))], eventSingle: events.EventIn | None = None): + if eventSingle is None: raise HTTPException( status_code=status.HTTP_400_BAD_REQUEST, detail="Body request is empty" ) - user_repository = users.UserRepository(database=database.database) + event_repository = event.EventRepository(database=database.database) - user = user_repository.find_one_by({"$or":[{"username": {'$eq': userSingle.username}}, {"email": {"$eq": userSingle.email}}]}) - if user is not None: - if user.username == userSingle.username: - raise HTTPException( - status_code=status.HTTP_204_NO_CONTENT, - detail="username" - ) - if user.email == userSingle.email: - raise HTTPException( - status_code=status.HTTP_204_NO_CONTENT, - detail="email" - ) + event = event_repository.find_one_by({"name": {'$eq': eventSingle.name}}) + if event is not None: + raise HTTPException( + status_code=status.HTTP_204_NO_CONTENT, + detail="name" + ) + - user = users.User(username=userSingle.username, password=users_token.get_password_hash(userSingle.password), email=userSingle.email) - user.roles = userSingle.roles - user.firstName = userSingle.firstName - user.name = userSingle.name - user.birth = userSingle.birth - user.created_at = datetime.today() - user_repository.save(user) - content = {"message": "user is created"} + event = events.Event(name=eventSingle.name, place=eventSigle.place) + event.start_date = eventSingle.start_date + event.end_date = eventSingle.end_date + event.created_at = datetime.today() + event_repository.save(event) + content = {"message": "event is created"} response = JSONResponse(content=content, status_code=status.HTTP_201_CREATED) return response -@router.put("/users/{item_id}", tags=["users"], status_code=status.HTTP_200_OK) -async def update_users_id(item_id: str, authorize: Annotated[bool, Depends(permissions_checker.PermissionChecker(roles=["Admin"]))], userSingle: users.UserIn | None = None, response: Response = Response): - if userSingle is None: +@router.put("/events/{item_id}", tags=["events"], status_code=status.HTTP_200_OK) +async def update_events_id(item_id: str, authorize: Annotated[bool, Depends(permissions_checker.PermissionChecker(roles=["Admin"]))], eventSingle: events.EventIn | None = None, response: Response = Response): + if eventSingle is None: raise HTTPException( status_code=status.HTTP_400_BAD_REQUEST, detail="Body request is empty" ) - user_repository = users.UserRepository(database=database.database) + event_repository = users.EventRepository(database=database.database) - user = user_repository.find_one_by({"id": {'$eq': ObjectId(item_id)}}) - if user is None: + event = event_repository.find_one_by({"id": {'$eq': ObjectId(item_id)}}) + if event is None: raise HTTPException( status_code=status.HTTP_404_NOT_FOUND, - detail="User not found" + detail="Event not found" ) - user.username = userSingle.username - user.password = users_token.get_password_hash(userSingle.password) - user.roles = userSingle.roles - user.email = userSingle.email - user.firstName = userSingle.firstName - user.name = userSingle.name - user.birth = userSingle.birth - user.updated_at = datetime.today() - user_repository.save(user) - content = {"message": "user is updated"} + event.name = EventSingle.name + event.place = EventSingle.place + event.start_date = eventSingle.start_date + event.end_date = eventSingle.end_date + event.organizers = eventSingle.organizers + event.participants = eventSingle.participants + event.interested = eventSingle.interested + event.updated_at = datetime.today() + event_repository.save(event) + content = {"message": "event is updated"} response = JSONResponse(content=content) return response -@router.patch("/users/groups",tags=["users"]) -async def patch_users_groups(authorize: Annotated[bool, Depends(permissions_checker.PermissionChecker(roles=["Admin"]))], userids: users.UserIDS | None = None): - if len(userids.ids) == 0: +@router.patch("/events/groups",tags=["events"]) +async def patch_events_groups(authorize: Annotated[bool, Depends(permissions_checker.PermissionChecker(roles=["Admin"]))], eventids: events.EventIDS | None = None): + if len(eventids.ids) == 0: raise HTTPException( status_code=status.HTTP_400_BAD_REQUEST, - detail="userids should be greater than 0" + detail="eventids should be greater than 0" ) - user_repository = users.UserRepository(database=database.database) - content = {"message": "users are enabled"} - for i in userids.ids: - user = user_repository.find_one_by_id(ObjectId(i)) - user.status = 1 - user.disabled_at = None - user.deleted_at = None - user_repository.save(user) + event_repository = users.EventRepository(database=database.database) + content = {"message": "events are enabled"} + for i in eventids.ids: + event = event_repository.find_one_by_id(ObjectId(i)) + event.status = 1 + event.disabled_at = None + event.deleted_at = None + event_repository.save(event) response = JSONResponse(content=content) return response -@router.patch("/users/{item_id}", tags=["users"], response_model=users.User) -async def patch_users_id(item_id : str, authorize: Annotated[bool, Depends(permissions_checker.PermissionChecker(roles=["Admin"]))]): - user_repository = users.UserRepository(database=database.database) - user = user_repository.find_one_by_id(ObjectId(item_id)) - user.status = 1 - user.disabled_at = None - user.deleted_at = None - user_repository.save(user) - content = {"message": "user is enabled"} +@router.patch("/events/{item_id}", tags=["users"]) +async def patch_events_id(item_id : str, authorize: Annotated[bool, Depends(permissions_checker.PermissionChecker(roles=["Admin"]))]): + event_repository = events.EventRepository(database=database.database) + event = event_repository.find_one_by_id(ObjectId(item_id)) + event.status = 1 + event.disabled_at = None + event.deleted_at = None + event_repository.save(event) + content = {"message": "event is enabled"} response = JSONResponse(content=content) return response \ No newline at end of file From 322c00e18dcf5a12572692bfc95f81c6e7463151 Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Thu, 9 Nov 2023 21:03:13 +0100 Subject: [PATCH 09/16] remove list --- app/models/events.py | 6 ------ app/routers/events.py | 6 ------ 2 files changed, 12 deletions(-) diff --git a/app/models/events.py b/app/models/events.py index eb57b6d..f788e08 100644 --- a/app/models/events.py +++ b/app/models/events.py @@ -22,9 +22,6 @@ class EventOut(BaseModel): name: str place: str status: int = 0 - participants: list[str] = [] - organizers: list[str] = [] - interested: list[str] = [] start_date: datetime | None = None end_date: datetime | None = None @@ -32,9 +29,6 @@ class EventIn(BaseModel): name: str place: str status: int = 0 - participants: list[str] = [] - organizers: list[str] = [] - interested: list[str] = [] start_date: datetime | None = None end_date: datetime | None = None diff --git a/app/routers/events.py b/app/routers/events.py index ba11686..c89ed21 100644 --- a/app/routers/events.py +++ b/app/routers/events.py @@ -142,9 +142,6 @@ async def update_events_me(item_id: str, current_user: Annotated[users.User, Dep event.place = EventSingle.place event.start_date = eventSingle.start_date event.end_date = eventSingle.end_date - event.organizers = eventSingle.organizers - event.participants = eventSingle.participants - event.interested = eventSingle.interested event.updated_at = datetime.today() event_repository.save(event) content = {"message": "event is updated"} @@ -198,9 +195,6 @@ async def update_events_id(item_id: str, authorize: Annotated[bool, Depends(perm event.place = EventSingle.place event.start_date = eventSingle.start_date event.end_date = eventSingle.end_date - event.organizers = eventSingle.organizers - event.participants = eventSingle.participants - event.interested = eventSingle.interested event.updated_at = datetime.today() event_repository.save(event) content = {"message": "event is updated"} From e7d13bda96d6338bae0a5e9d3cd064c0a33d345b Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Sun, 12 Nov 2023 18:42:49 +0100 Subject: [PATCH 10/16] fix events --- app/routers/events.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/app/routers/events.py b/app/routers/events.py index c89ed21..2395b4b 100644 --- a/app/routers/events.py +++ b/app/routers/events.py @@ -155,7 +155,7 @@ async def update_events(authorize: Annotated[bool, Depends(permissions_checker.P status_code=status.HTTP_400_BAD_REQUEST, detail="Body request is empty" ) - event_repository = event.EventRepository(database=database.database) + event_repository = events.EventRepository(database=database.database) event = event_repository.find_one_by({"name": {'$eq': eventSingle.name}}) if event is not None: @@ -165,7 +165,7 @@ async def update_events(authorize: Annotated[bool, Depends(permissions_checker.P ) - event = events.Event(name=eventSingle.name, place=eventSigle.place) + event = events.Event(name=eventSingle.name, place=eventSingle.place) event.start_date = eventSingle.start_date event.end_date = eventSingle.end_date event.created_at = datetime.today() @@ -210,7 +210,7 @@ async def patch_events_groups(authorize: Annotated[bool, Depends(permissions_che detail="eventids should be greater than 0" ) - event_repository = users.EventRepository(database=database.database) + event_repository = events.EventRepository(database=database.database) content = {"message": "events are enabled"} for i in eventids.ids: event = event_repository.find_one_by_id(ObjectId(i)) From ecc19cf09abf145d046a9a610f0d85d8deaee4e2 Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Sun, 12 Nov 2023 21:18:49 +0100 Subject: [PATCH 11/16] fix events delete --- app/routers/events.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/app/routers/events.py b/app/routers/events.py index 2395b4b..60f5be2 100644 --- a/app/routers/events.py +++ b/app/routers/events.py @@ -73,14 +73,14 @@ async def read_events_count(authorize: Annotated[bool, Depends(permissions_check @router.get("/events/{item_id}", tags=["events"], response_model=events.Event) async def read_events_id(item_id : str, authorize: Annotated[bool, Depends(permissions_checker.PermissionChecker(roles=["Admin"]))]): - event_repository = event.EventRepository(database=database.database) + event_repository = events.EventRepository(database=database.database) event = event_repository.find_one_by_id(ObjectId(item_id)) return event @router.delete("/events/me/{item_id}", tags=["events"]) async def delete_event_me(item_id: str, current_user: Annotated[users.User, Depends(users_token.get_current_active_user)], authorize: Annotated[bool, Depends(permissions_checker.PermissionChecker(roles=["Admin", "User"]))], remove: bool = False): - event_repository = event.EventRepository(database=database.database) + event_repository = events.EventRepository(database=database.database) event = event_repository.find_one_by_id(ObjectId(item_id)) if remove is True: event.deleted_at = datetime.today() @@ -120,7 +120,7 @@ async def delete_events_groups(authorize: Annotated[bool, Depends(permissions_ch @router.delete("/events/{item_id}", tags=["events"]) async def delete_events_id(item_id : str, authorize: Annotated[bool, Depends(permissions_checker.PermissionChecker(roles=["Admin"]))], remove : bool = False): - event_repository = event.EventRepository(database=database.database) + event_repository = events.EventRepository(database=database.database) event = event_repository.find_one_by_id(ObjectId(item_id)) if remove is True: event.status = -1 @@ -130,7 +130,7 @@ async def delete_events_id(item_id : str, authorize: Annotated[bool, Depends(per event.status = 0 event.disabled_at = datetime.today() content = {"message": "events are disabled"} - event.save(user) + event_repository.save(event) response = JSONResponse(content=content) return response @@ -182,7 +182,7 @@ async def update_events_id(item_id: str, authorize: Annotated[bool, Depends(perm status_code=status.HTTP_400_BAD_REQUEST, detail="Body request is empty" ) - event_repository = users.EventRepository(database=database.database) + event_repository = events.EventRepository(database=database.database) event = event_repository.find_one_by({"id": {'$eq': ObjectId(item_id)}}) if event is None: @@ -191,8 +191,8 @@ async def update_events_id(item_id: str, authorize: Annotated[bool, Depends(perm detail="Event not found" ) - event.name = EventSingle.name - event.place = EventSingle.place + event.name = eventSingle.name + event.place = eventSingle.place event.start_date = eventSingle.start_date event.end_date = eventSingle.end_date event.updated_at = datetime.today() @@ -223,7 +223,7 @@ async def patch_events_groups(authorize: Annotated[bool, Depends(permissions_che response = JSONResponse(content=content) return response -@router.patch("/events/{item_id}", tags=["users"]) +@router.patch("/events/{item_id}", tags=["events"]) async def patch_events_id(item_id : str, authorize: Annotated[bool, Depends(permissions_checker.PermissionChecker(roles=["Admin"]))]): event_repository = events.EventRepository(database=database.database) event = event_repository.find_one_by_id(ObjectId(item_id)) From e7e4326a316d4ae12fffaa0beaa87dc097542b20 Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Tue, 14 Nov 2023 00:21:15 +0100 Subject: [PATCH 12/16] remove participants and interested --- app/models/events.py | 2 -- app/routers/events.py | 14 ++------------ 2 files changed, 2 insertions(+), 14 deletions(-) diff --git a/app/models/events.py b/app/models/events.py index f788e08..d8b14cd 100644 --- a/app/models/events.py +++ b/app/models/events.py @@ -7,9 +7,7 @@ class Event(BaseModel): name: str place: str status: int = 0 - participants: list[str] = [] organizers: list[str] = [] - interested: list[str] = [] start_date: datetime | None = None end_date: datetime | None = None created_at: datetime = datetime.today() diff --git a/app/routers/events.py b/app/routers/events.py index 60f5be2..451467c 100644 --- a/app/routers/events.py +++ b/app/routers/events.py @@ -44,22 +44,12 @@ async def read_events(authorize: Annotated[bool, Depends(permissions_checker.Per async def read_users_me(current_user: Annotated[users.User, Depends(users_token.get_current_active_user)], authorize: Annotated[bool, Depends(permissions_checker.PermissionChecker(roles=["Admin", "User"]))]): event_repository = events.EventRepository(database=database.database) listOrganizers = [] - listParticipants = [] - listInterested = [] - for event_index in event_repository.find_by({"organizers":{"$eq": current_user.id}}, limit=limit, skip=skip): + for event_index in event_repository.find_by({"organizers":{"$eq": current_user.username}}, limit=limit, skip=skip): event = events.EventOut(id=event_index.id, name=event_index.name, place=event_index.place, status=event_index.status, start_date=event_index.start_date, end_date=event_index.end_date) listOrganizers.append(event) - - for event_index in event_repository.find_by({"participants":{"$eq": current_user.id}}, limit=limit, skip=skip): - event = events.EventOut(id=event_index.id, name=event_index.name, place=event_index.place, status=event_index.status, start_date=event_index.start_date, end_date=event_index.end_date) - listParticipants.append(event) - for event_index in event_repository.find_by({"interested":{"$eq": current_user.id}}, limit=limit, skip=skip): - event = events.EventOut(id=event_index.id, name=event_index.name, place=event_index.place, status=event_index.status, start_date=event_index.start_date, end_date=event_index.end_date) - listInterested.append(event) - - content = {"participants":listParticipants, "organizers":listOrganizers, "interested":listInterested} + content = {"organizers":listOrganizers} response = JSONResponse(content=content) return response From d6219779554d1b807314f4e6268d228ef4d6b188 Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Thu, 16 Nov 2023 21:40:49 +0100 Subject: [PATCH 13/16] add organizers --- app/models/events.py | 1 + app/routers/events.py | 2 ++ 2 files changed, 3 insertions(+) diff --git a/app/models/events.py b/app/models/events.py index d8b14cd..fd187fd 100644 --- a/app/models/events.py +++ b/app/models/events.py @@ -27,6 +27,7 @@ class EventIn(BaseModel): name: str place: str status: int = 0 + organizers: list[str] = [] start_date: datetime | None = None end_date: datetime | None = None diff --git a/app/routers/events.py b/app/routers/events.py index 451467c..987fca6 100644 --- a/app/routers/events.py +++ b/app/routers/events.py @@ -158,6 +158,7 @@ async def update_events(authorize: Annotated[bool, Depends(permissions_checker.P event = events.Event(name=eventSingle.name, place=eventSingle.place) event.start_date = eventSingle.start_date event.end_date = eventSingle.end_date + event.organizers = eventSingle.organizers event.created_at = datetime.today() event_repository.save(event) content = {"message": "event is created"} @@ -185,6 +186,7 @@ async def update_events_id(item_id: str, authorize: Annotated[bool, Depends(perm event.place = eventSingle.place event.start_date = eventSingle.start_date event.end_date = eventSingle.end_date + event.organizers = eventSingle.organizers event.updated_at = datetime.today() event_repository.save(event) content = {"message": "event is updated"} From afd2dec01df17b6b99825c29fbc7efdadb028a23 Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Sun, 19 Nov 2023 21:17:41 +0100 Subject: [PATCH 14/16] test toto --- toto.txt | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 toto.txt diff --git a/toto.txt b/toto.txt new file mode 100644 index 0000000..e69de29 From 6edc87fb1dbc65c65fc2d6b57a93b95cc58cfe42 Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Sun, 19 Nov 2023 21:29:42 +0100 Subject: [PATCH 15/16] remove file --- toto.txt | 0 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 toto.txt diff --git a/toto.txt b/toto.txt deleted file mode 100644 index e69de29..0000000 From fb302ca63eccca0dd29d1819588c11ffd04bece6 Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Mon, 20 Nov 2023 22:40:42 +0100 Subject: [PATCH 16/16] add latitude and longitude --- app/models/events.py | 4 ++++ app/routers/events.py | 6 ++++++ 2 files changed, 10 insertions(+) diff --git a/app/models/events.py b/app/models/events.py index fd187fd..9e1f7af 100644 --- a/app/models/events.py +++ b/app/models/events.py @@ -7,6 +7,8 @@ class Event(BaseModel): name: str place: str status: int = 0 + latitude: float = 0.0 + longitude: float = 0.0 organizers: list[str] = [] start_date: datetime | None = None end_date: datetime | None = None @@ -30,6 +32,8 @@ class EventIn(BaseModel): organizers: list[str] = [] start_date: datetime | None = None end_date: datetime | None = None + latitude: float = 0.0 + longitude: float = 0.0 class EventIDS(BaseModel): ids: list[str] diff --git a/app/routers/events.py b/app/routers/events.py index 987fca6..68099d5 100644 --- a/app/routers/events.py +++ b/app/routers/events.py @@ -132,6 +132,8 @@ async def update_events_me(item_id: str, current_user: Annotated[users.User, Dep event.place = EventSingle.place event.start_date = eventSingle.start_date event.end_date = eventSingle.end_date + event.latitude = eventSingle.latitude + event.longitude = eventSingle.longitude event.updated_at = datetime.today() event_repository.save(event) content = {"message": "event is updated"} @@ -159,6 +161,8 @@ async def update_events(authorize: Annotated[bool, Depends(permissions_checker.P event.start_date = eventSingle.start_date event.end_date = eventSingle.end_date event.organizers = eventSingle.organizers + event.latitude = eventSingle.latitude + event.longitude = eventSingle.longitude event.created_at = datetime.today() event_repository.save(event) content = {"message": "event is created"} @@ -187,6 +191,8 @@ async def update_events_id(item_id: str, authorize: Annotated[bool, Depends(perm event.start_date = eventSingle.start_date event.end_date = eventSingle.end_date event.organizers = eventSingle.organizers + event.latitude = eventSingle.latitude + event.longitude = eventSingle.longitude event.updated_at = datetime.today() event_repository.save(event) content = {"message": "event is updated"}