From 1afcd626ea815745c5169ba7f4131d3da102cc8a Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Sat, 21 Dec 2024 20:59:43 +0100 Subject: [PATCH 1/4] add models tags --- app/models/tags.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 app/models/tags.py diff --git a/app/models/tags.py b/app/models/tags.py new file mode 100644 index 0000000..db00299 --- /dev/null +++ b/app/models/tags.py @@ -0,0 +1,24 @@ +from pydantic import BaseModel, EmailStr +from pydantic_mongo import AbstractRepository, ObjectIdField +from datetime import datetime, date + +class Tags(BaseModel): + id: ObjectIdField = None + name: str + created_at: datetime = datetime.today() + +class TagsOut(BaseModel): + id: ObjectIdField = None + name: str + + +class TagsIn(BaseModel): + name: str + +class EventIDS(BaseModel): + ids: list[str] + + +class TagsRepository(AbstractRepository[Tags]): + class Meta: + collection_name = "tags" \ No newline at end of file From ca983f1199d2dcba1df94db81c25426eca5ab5bc Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Sat, 21 Dec 2024 21:38:14 +0100 Subject: [PATCH 2/4] add routers tags --- app/routers/tags.py | 230 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 230 insertions(+) create mode 100644 app/routers/tags.py diff --git a/app/routers/tags.py b/app/routers/tags.py new file mode 100644 index 0000000..c0eab93 --- /dev/null +++ b/app/routers/tags.py @@ -0,0 +1,230 @@ +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 tags, users +from pydantic import EmailStr +from typing import Annotated, Union +from bson import ObjectId +from datetime import datetime +router = APIRouter() + + +@router.get("/tags", tags=["tags"], response_model=list[tags.TagsOut]) +async def read_tags( + authorize: Annotated[bool, Depends(permissions_checker.PermissionChecker(roles=["Admin", "User"]))], + skip: int = 0, + limit: int = 20, + id_tags: str | None = None, + name: str | None = None +): + # Validate `skip` and `limit` + if limit < 1 or skip < 0 or limit < skip: + raise HTTPException( + status_code=status.HTTP_400_BAD_REQUEST, + detail="`skip` should be >= 0 and `limit` should be > 0 and greater than `skip`.", + ) + limit = limit + skip + + # Initialize filters + filters = [] + + + if name: + filters.append({"name": {"$eq": name, "$options": "i"}}) + + # Add ID filter + if id_tags: + try: + tags_id = ObjectId(id_tags) + filters.append({"_id": {"$eq": event_id}}) + except Exception: + raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail="Invalid event ID format.") + + # Combine all filters + object_search = {"$and": filters} if filters else {} + + # Fetch and return results + tags_repository = tags.TagsRepository(database=database.database) + list_tags = [] + for tag_index in tags_repository.find_by(object_search, limit=limit, skip=skip): + tag = tags.TagsOut( + id=tag_index.id, + name=tag_index.name + ) + list_tags.append(event) + + return list_tags + +@router.get("/tags/count", tags=["tags"]) +async def read_tags_count(authorize: Annotated[bool, Depends(permissions_checker.PermissionChecker(roles=["Admin", "User"]))]): + count = database.database.get_collection("tags").estimated_document_count() + content = {"count":count} + response = JSONResponse(content=content) + return response + + +@router.get("/tags/{item_id}", tags=["tags"], response_model=tags.Tags) +async def read_tags_id(item_id : str, authorize: Annotated[bool, Depends(permissions_checker.PermissionChecker(roles=["Admin"]))]): + tags_repository = tags.TagsRepository(database=database.database) + tag = tags_repository.find_one_by_id(ObjectId(item_id)) + return tag + + +@router.delete("/tags/groups",tags=["tags"]) +async def delete_tags_groups(authorize: Annotated[bool, Depends(permissions_checker.PermissionChecker(roles=["Admin"]))], remove: bool = False, tagsids: tags.TagsIDS | None = None): + if len(eventids.ids) == 0: + raise HTTPException( + status_code=status.HTTP_400_BAD_REQUEST, + detail="eventids should be greater than 0" + ) + + 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: + event.status = -1 + event.deleted_at = datetime.today() + content = {"message": "events are deleted "} + else: + 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("/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 = events.EventRepository(database=database.database) + event = event_repository.find_one_by_id(ObjectId(item_id)) + if remove is True: + event.status = -1 + event.deleted_at = datetime.today() + content = {"message": "events are deleted"} + else: + event.status = 0 + event.disabled_at = datetime.today() + content = {"message": "events are disabled"} + event_repository.save(event) + response = JSONResponse(content=content) + return response + +@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.description = eventSingle.description + event.place = eventSingle.place + event.start_date = eventSingle.start_date + event.tags = eventSingle.tags + event.end_date = eventSingle.end_date + event.latitude = eventSingle.latitude + event.longitude = eventSingle.longitude + event.updated_at = datetime.today() + event.imgUrl = eventSingle.imgUrl + event_repository.save(event) + content = {"message": "event is updated"} + response = JSONResponse(content=content) + return response + +@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" + ) + event_repository = events.EventRepository(database=database.database) + + 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" + ) + + + event = events.Event(name=eventSingle.name, description=eventSingle.description, place=eventSingle.place) + 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.imgUrl = eventSingle.imgUrl + event.tags = eventSingle.tags + event.status = 1 + 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("/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" + ) + event_repository = events.EventRepository(database=database.database) + + 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="Event not found" + ) + + event.name = eventSingle.name + event.place = eventSingle.place + event.description = eventSingle.description + event.start_date = eventSingle.start_date + event.end_date = eventSingle.end_date + event.organizers = eventSingle.organizers + event.tags = eventSingle.tags + event.latitude = eventSingle.latitude + event.longitude = eventSingle.longitude + event.updated_at = datetime.today() + event.imgUrl = eventSingle.imgUrl + event_repository.save(event) + content = {"message": "event is updated"} + response = JSONResponse(content=content) + return response + + +@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="eventids should be greater than 0" + ) + + 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)) + event.status = 1 + event.disabled_at = None + event.deleted_at = None + event_repository.save(event) + + + response = JSONResponse(content=content) + return response + +@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)) + 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 a893e86cdf6d91eb4b30fd4f32583758a113bc3f Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Sat, 21 Dec 2024 22:38:21 +0100 Subject: [PATCH 3/4] add tags rouger work --- app/main.py | 3 +- app/models/tags.py | 2 +- app/routers/tags.py | 159 +++++++------------------------------------- 3 files changed, 27 insertions(+), 137 deletions(-) diff --git a/app/main.py b/app/main.py index be6f657..0d88ef2 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, events +from .routers import users, token, mail, events, tags from .dependencies import user_add import os @@ -29,6 +29,7 @@ app.include_router(users.router) app.include_router(token.router) app.include_router(mail.router) app.include_router(events.router) +app.include_router(tags.router) @app.on_event("startup") diff --git a/app/models/tags.py b/app/models/tags.py index db00299..1fcf75d 100644 --- a/app/models/tags.py +++ b/app/models/tags.py @@ -15,7 +15,7 @@ class TagsOut(BaseModel): class TagsIn(BaseModel): name: str -class EventIDS(BaseModel): +class TagsIDS(BaseModel): ids: list[str] diff --git a/app/routers/tags.py b/app/routers/tags.py index c0eab93..aa965ec 100644 --- a/app/routers/tags.py +++ b/app/routers/tags.py @@ -72,159 +72,48 @@ async def read_tags_id(item_id : str, authorize: Annotated[bool, Depends(permiss @router.delete("/tags/groups",tags=["tags"]) -async def delete_tags_groups(authorize: Annotated[bool, Depends(permissions_checker.PermissionChecker(roles=["Admin"]))], remove: bool = False, tagsids: tags.TagsIDS | None = None): - if len(eventids.ids) == 0: +async def delete_tags_groups(authorize: Annotated[bool, Depends(permissions_checker.PermissionChecker(roles=["Admin"]))], tagsids: tags.TagsIDS | None = None): + if len(tagsids.ids) == 0: raise HTTPException( status_code=status.HTTP_400_BAD_REQUEST, detail="eventids should be greater than 0" ) - 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: - event.status = -1 - event.deleted_at = datetime.today() - content = {"message": "events are deleted "} - else: - event.status = 0 - event.disabled_at = datetime.today() - content = {"message": "events are disabled"} - event_repository.save(event) - + tag_repository = tags.TagsRepository(database=database.database) + for i in tagsids.ids: + tag = tag_repository.find_one_by_id(ObjectId(i)) + tag_repository.delete_one(tag) + content = {"message": "tags removed"} response = JSONResponse(content=content) return response -@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 = events.EventRepository(database=database.database) - event = event_repository.find_one_by_id(ObjectId(item_id)) - if remove is True: - event.status = -1 - event.deleted_at = datetime.today() - content = {"message": "events are deleted"} - else: - event.status = 0 - event.disabled_at = datetime.today() - content = {"message": "events are disabled"} - event_repository.save(event) +@router.delete("/tags/{item_id}", tags=["tags"]) +async def delete_tags_id(item_id : str, authorize: Annotated[bool, Depends(permissions_checker.PermissionChecker(roles=["Admin"]))]): + tag_repository = tags.TagsRepository(database=database.database) + tag = tag_repository.find_one_by_id(ObjectId(item_id)) + event_repository.delete_one(event) + content = {"message": "tags delete"} response = JSONResponse(content=content) - return response + return responsed -@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.description = eventSingle.description - event.place = eventSingle.place - event.start_date = eventSingle.start_date - event.tags = eventSingle.tags - event.end_date = eventSingle.end_date - event.latitude = eventSingle.latitude - event.longitude = eventSingle.longitude - event.updated_at = datetime.today() - event.imgUrl = eventSingle.imgUrl - event_repository.save(event) - content = {"message": "event is updated"} - response = JSONResponse(content=content) - return response - -@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: +@router.put("/tags", tags=["tags"], status_code=status.HTTP_201_CREATED) +async def update_tags(authorize: Annotated[bool, Depends(permissions_checker.PermissionChecker(roles=["Admin"]))], tagSingle: tags.TagsIn | None = None): + if tagSingle is None: raise HTTPException( status_code=status.HTTP_400_BAD_REQUEST, detail="Body request is empty" ) - event_repository = events.EventRepository(database=database.database) + tag_repository = tags.TagsRepository(database=database.database) - event = event_repository.find_one_by({"name": {'$eq': eventSingle.name}}) - if event is not None: + tag = tag_repository.find_one_by({"name": {'$eq': tagSingle.name}}) + if tag is not None: raise HTTPException( status_code=status.HTTP_204_NO_CONTENT, detail="name" ) - - - event = events.Event(name=eventSingle.name, description=eventSingle.description, place=eventSingle.place) - 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.imgUrl = eventSingle.imgUrl - event.tags = eventSingle.tags - event.status = 1 - event.created_at = datetime.today() - event_repository.save(event) - content = {"message": "event is created"} + tag = tags.Tags(name=tagSingle.name) + tag.created_at = datetime.today() + tag_repository.save(event) + content = {"message": "tags created"} response = JSONResponse(content=content, status_code=status.HTTP_201_CREATED) return response - - -@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" - ) - event_repository = events.EventRepository(database=database.database) - - 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="Event not found" - ) - - event.name = eventSingle.name - event.place = eventSingle.place - event.description = eventSingle.description - event.start_date = eventSingle.start_date - event.end_date = eventSingle.end_date - event.organizers = eventSingle.organizers - event.tags = eventSingle.tags - event.latitude = eventSingle.latitude - event.longitude = eventSingle.longitude - event.updated_at = datetime.today() - event.imgUrl = eventSingle.imgUrl - event_repository.save(event) - content = {"message": "event is updated"} - response = JSONResponse(content=content) - return response - - -@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="eventids should be greater than 0" - ) - - 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)) - event.status = 1 - event.disabled_at = None - event.deleted_at = None - event_repository.save(event) - - - response = JSONResponse(content=content) - return response - -@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)) - 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 2a194c163a04c3bfd6cc2a2e19669d48acc263a5 Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Sat, 21 Dec 2024 23:05:24 +0100 Subject: [PATCH 4/4] add tag in put endpiint --- app/routers/events.py | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/app/routers/events.py b/app/routers/events.py index bd18654..585f115 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, users +from ..models import events, users, tags from pydantic import EmailStr from typing import Annotated, Union from bson import ObjectId @@ -330,6 +330,7 @@ async def update_events(authorize: Annotated[bool, Depends(permissions_checker.P detail="Body request is empty" ) event_repository = events.EventRepository(database=database.database) + tags_repository = tags.TagsRepository(database=database.database) event = event_repository.find_one_by({"name": {'$eq': eventSingle.name}}) if event is not None: @@ -347,6 +348,11 @@ async def update_events(authorize: Annotated[bool, Depends(permissions_checker.P event.longitude = eventSingle.longitude event.imgUrl = eventSingle.imgUrl event.tags = eventSingle.tags + for tag_name in eventSingle: + tag = tags_repository.find_one_by({"name": {'$eq': tag_name}}) + if tag is None: + tag = Tags(name=tag_name) + tags_repository.save(tag) event.status = 1 event.created_at = datetime.today() event_repository.save(event) @@ -363,7 +369,7 @@ async def update_events_id(item_id: str, authorize: Annotated[bool, Depends(perm detail="Body request is empty" ) event_repository = events.EventRepository(database=database.database) - + tags_repository = tags.TagsRepository(database=database) event = event_repository.find_one_by({"id": {'$eq': ObjectId(item_id)}}) if event is None: raise HTTPException( @@ -378,6 +384,11 @@ async def update_events_id(item_id: str, authorize: Annotated[bool, Depends(perm event.end_date = eventSingle.end_date event.organizers = eventSingle.organizers event.tags = eventSingle.tags + for tag_name in eventSingle: + tag = tags_repository.find_one_by({"name": {'$eq': tag_name}}) + if tag is None: + tag = Tags(name=tag_name) + tags_repository.save(tag) event.latitude = eventSingle.latitude event.longitude = eventSingle.longitude event.updated_at = datetime.today()