add tags rouger work

This commit is contained in:
Valentin CZERYBA 2024-12-21 22:38:21 +01:00
parent ca983f1199
commit a893e86cdf
3 changed files with 27 additions and 137 deletions

View File

@ -1,7 +1,7 @@
from fastapi import FastAPI from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware 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 from .dependencies import user_add
import os import os
@ -29,6 +29,7 @@ app.include_router(users.router)
app.include_router(token.router) app.include_router(token.router)
app.include_router(mail.router) app.include_router(mail.router)
app.include_router(events.router) app.include_router(events.router)
app.include_router(tags.router)
@app.on_event("startup") @app.on_event("startup")

View File

@ -15,7 +15,7 @@ class TagsOut(BaseModel):
class TagsIn(BaseModel): class TagsIn(BaseModel):
name: str name: str
class EventIDS(BaseModel): class TagsIDS(BaseModel):
ids: list[str] ids: list[str]

View File

@ -72,159 +72,48 @@ async def read_tags_id(item_id : str, authorize: Annotated[bool, Depends(permiss
@router.delete("/tags/groups",tags=["tags"]) @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): async def delete_tags_groups(authorize: Annotated[bool, Depends(permissions_checker.PermissionChecker(roles=["Admin"]))], tagsids: tags.TagsIDS | None = None):
if len(eventids.ids) == 0: if len(tagsids.ids) == 0:
raise HTTPException( raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST, status_code=status.HTTP_400_BAD_REQUEST,
detail="eventids should be greater than 0" detail="eventids should be greater than 0"
) )
event_repository = event.EventRepository(database=database.database) tag_repository = tags.TagsRepository(database=database.database)
for i in eventids.ids: for i in tagsids.ids:
event = event_repository.find_one_by_id(ObjectId(i)) tag = tag_repository.find_one_by_id(ObjectId(i))
if remove is True: tag_repository.delete_one(tag)
event.status = -1 content = {"message": "tags removed"}
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) response = JSONResponse(content=content)
return response return response
@router.delete("/events/{item_id}", tags=["events"]) @router.delete("/tags/{item_id}", tags=["tags"])
async def delete_events_id(item_id : str, authorize: Annotated[bool, Depends(permissions_checker.PermissionChecker(roles=["Admin"]))], remove : bool = False): async def delete_tags_id(item_id : str, authorize: Annotated[bool, Depends(permissions_checker.PermissionChecker(roles=["Admin"]))]):
event_repository = events.EventRepository(database=database.database) tag_repository = tags.TagsRepository(database=database.database)
event = event_repository.find_one_by_id(ObjectId(item_id)) tag = tag_repository.find_one_by_id(ObjectId(item_id))
if remove is True: event_repository.delete_one(event)
event.status = -1 content = {"message": "tags delete"}
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) response = JSONResponse(content=content)
return response return responsed
@router.put("/events/me/{item_id}",tags=["events"]) @router.put("/tags", tags=["tags"], status_code=status.HTTP_201_CREATED)
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): async def update_tags(authorize: Annotated[bool, Depends(permissions_checker.PermissionChecker(roles=["Admin"]))], tagSingle: tags.TagsIn | None = None):
event_repository = event.EventRepository(database=database.database) if tagSingle is None:
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( raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST, status_code=status.HTTP_400_BAD_REQUEST,
detail="Body request is empty" 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}}) tag = tag_repository.find_one_by({"name": {'$eq': tagSingle.name}})
if event is not None: if tag is not None:
raise HTTPException( raise HTTPException(
status_code=status.HTTP_204_NO_CONTENT, status_code=status.HTTP_204_NO_CONTENT,
detail="name" detail="name"
) )
tag = tags.Tags(name=tagSingle.name)
tag.created_at = datetime.today()
event = events.Event(name=eventSingle.name, description=eventSingle.description, place=eventSingle.place) tag_repository.save(event)
event.start_date = eventSingle.start_date content = {"message": "tags created"}
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) response = JSONResponse(content=content, status_code=status.HTTP_201_CREATED)
return response 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