2023-11-06 23:27:47 +01:00
|
|
|
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
|
2023-11-08 22:43:53 +01:00
|
|
|
from ..models import events, users
|
2023-11-06 23:27:47 +01:00
|
|
|
from pydantic import EmailStr
|
|
|
|
from typing import Annotated
|
|
|
|
from bson import ObjectId
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@router.get("/events", tags=["events"], response_model=list[events.EventOut])
|
2023-11-07 21:08:58 +01:00
|
|
|
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):
|
2023-11-06 23:27:47 +01:00
|
|
|
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}}]}
|
2023-11-07 21:08:58 +01:00
|
|
|
if name is not None:
|
|
|
|
object_search = {"name": {"$regex": name}}
|
|
|
|
if status is not None:
|
|
|
|
object_search = {"$and":[{"name":{"$regex": name}}, {"status":{"$eq":status}}]}
|
2023-11-06 23:27:47 +01:00
|
|
|
|
|
|
|
|
|
|
|
for event_index in event_repository.find_by(object_search, limit=limit, skip=skip):
|
2023-11-08 22:43:53 +01:00
|
|
|
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)
|
2023-11-06 23:27:47 +01:00
|
|
|
listEvents.append(event)
|
|
|
|
return listEvents
|
|
|
|
|
|
|
|
|
2023-11-08 22:43:53 +01:00
|
|
|
@router.get("/events/me",tags=["events"])
|
2023-11-06 23:27:47 +01:00
|
|
|
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"]))]):
|
2023-11-08 22:43:53 +01:00
|
|
|
event_repository = events.EventRepository(database=database.database)
|
|
|
|
listOrganizers = []
|
|
|
|
|
2023-11-14 00:21:15 +01:00
|
|
|
for event_index in event_repository.find_by({"organizers":{"$eq": current_user.username}}, limit=limit, skip=skip):
|
2023-11-08 22:43:53 +01:00
|
|
|
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)
|
|
|
|
|
2023-11-14 00:21:15 +01:00
|
|
|
content = {"organizers":listOrganizers}
|
2023-11-08 22:43:53 +01:00
|
|
|
response = JSONResponse(content=content)
|
|
|
|
return response
|
2023-11-06 23:27:47 +01:00
|
|
|
|
2023-11-08 22:43:53 +01:00
|
|
|
@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()
|
2023-11-06 23:27:47 +01:00
|
|
|
content = {"count":count}
|
|
|
|
response = JSONResponse(content=content)
|
|
|
|
return response
|
|
|
|
|
|
|
|
|
2023-11-08 22:43:53 +01:00
|
|
|
@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"]))]):
|
2023-11-12 21:18:49 +01:00
|
|
|
event_repository = events.EventRepository(database=database.database)
|
2023-11-08 22:43:53 +01:00
|
|
|
event = event_repository.find_one_by_id(ObjectId(item_id))
|
|
|
|
return event
|
2023-11-06 23:27:47 +01:00
|
|
|
|
|
|
|
|
2023-11-08 22:43:53 +01:00
|
|
|
@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):
|
2023-11-12 21:18:49 +01:00
|
|
|
event_repository = events.EventRepository(database=database.database)
|
2023-11-08 22:43:53 +01:00
|
|
|
event = event_repository.find_one_by_id(ObjectId(item_id))
|
2023-11-06 23:27:47 +01:00
|
|
|
if remove is True:
|
2023-11-08 22:43:53 +01:00
|
|
|
event.deleted_at = datetime.today()
|
|
|
|
event.status = -1
|
|
|
|
content = {"message": "event is deleted"}
|
2023-11-06 23:27:47 +01:00
|
|
|
else:
|
2023-11-08 22:43:53 +01:00
|
|
|
event.status = 0
|
|
|
|
event.deleted_at = datetime.today()
|
|
|
|
content = {"message": "event is disabled"}
|
|
|
|
event_repository.save(event)
|
2023-11-06 23:27:47 +01:00
|
|
|
response = JSONResponse(content=content)
|
|
|
|
return response
|
|
|
|
|
2023-11-08 22:43:53 +01:00
|
|
|
@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:
|
2023-11-06 23:27:47 +01:00
|
|
|
raise HTTPException(
|
|
|
|
status_code=status.HTTP_400_BAD_REQUEST,
|
2023-11-08 22:43:53 +01:00
|
|
|
detail="eventids should be greater than 0"
|
2023-11-06 23:27:47 +01:00
|
|
|
)
|
|
|
|
|
2023-11-08 22:43:53 +01:00
|
|
|
event_repository = event.EventRepository(database=database.database)
|
|
|
|
for i in eventids.ids:
|
|
|
|
event = event_repository.find_one_by_id(ObjectId(i))
|
2023-11-06 23:27:47 +01:00
|
|
|
if remove is True:
|
2023-11-08 22:43:53 +01:00
|
|
|
event.status = -1
|
|
|
|
event.deleted_at = datetime.today()
|
|
|
|
content = {"message": "events are deleted "}
|
2023-11-06 23:27:47 +01:00
|
|
|
else:
|
2023-11-08 22:43:53 +01:00
|
|
|
event.status = 0
|
|
|
|
event.disabled_at = datetime.today()
|
|
|
|
content = {"message": "events are disabled"}
|
|
|
|
event_repository.save(event)
|
2023-11-06 23:27:47 +01:00
|
|
|
|
|
|
|
response = JSONResponse(content=content)
|
|
|
|
return response
|
|
|
|
|
2023-11-08 22:43:53 +01:00
|
|
|
@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):
|
2023-11-12 21:18:49 +01:00
|
|
|
event_repository = events.EventRepository(database=database.database)
|
2023-11-08 22:43:53 +01:00
|
|
|
event = event_repository.find_one_by_id(ObjectId(item_id))
|
2023-11-06 23:27:47 +01:00
|
|
|
if remove is True:
|
2023-11-08 22:43:53 +01:00
|
|
|
event.status = -1
|
|
|
|
event.deleted_at = datetime.today()
|
|
|
|
content = {"message": "events are deleted"}
|
2023-11-06 23:27:47 +01:00
|
|
|
else:
|
2023-11-08 22:43:53 +01:00
|
|
|
event.status = 0
|
|
|
|
event.disabled_at = datetime.today()
|
|
|
|
content = {"message": "events are disabled"}
|
2023-11-12 21:18:49 +01:00
|
|
|
event_repository.save(event)
|
2023-11-06 23:27:47 +01:00
|
|
|
response = JSONResponse(content=content)
|
|
|
|
return response
|
|
|
|
|
2023-11-08 23:10:45 +01:00
|
|
|
@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.updated_at = datetime.today()
|
|
|
|
event_repository.save(event)
|
|
|
|
content = {"message": "event is updated"}
|
2023-11-06 23:27:47 +01:00
|
|
|
response = JSONResponse(content=content)
|
2023-11-08 23:10:45 +01:00
|
|
|
return response
|
2023-11-06 23:27:47 +01:00
|
|
|
|
2023-11-08 23:10:45 +01:00
|
|
|
@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:
|
2023-11-06 23:27:47 +01:00
|
|
|
raise HTTPException(
|
|
|
|
status_code=status.HTTP_400_BAD_REQUEST,
|
|
|
|
detail="Body request is empty"
|
|
|
|
)
|
2023-11-12 18:42:49 +01:00
|
|
|
event_repository = events.EventRepository(database=database.database)
|
2023-11-08 23:10:45 +01:00
|
|
|
|
|
|
|
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"
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2023-11-12 18:42:49 +01:00
|
|
|
event = events.Event(name=eventSingle.name, place=eventSingle.place)
|
2023-11-08 23:10:45 +01:00
|
|
|
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"}
|
2023-11-06 23:27:47 +01:00
|
|
|
response = JSONResponse(content=content, status_code=status.HTTP_201_CREATED)
|
|
|
|
return response
|
|
|
|
|
|
|
|
|
2023-11-08 23:10:45 +01:00
|
|
|
@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:
|
2023-11-06 23:27:47 +01:00
|
|
|
raise HTTPException(
|
|
|
|
status_code=status.HTTP_400_BAD_REQUEST,
|
|
|
|
detail="Body request is empty"
|
|
|
|
)
|
2023-11-12 21:18:49 +01:00
|
|
|
event_repository = events.EventRepository(database=database.database)
|
2023-11-06 23:27:47 +01:00
|
|
|
|
2023-11-08 23:10:45 +01:00
|
|
|
event = event_repository.find_one_by({"id": {'$eq': ObjectId(item_id)}})
|
|
|
|
if event is None:
|
2023-11-06 23:27:47 +01:00
|
|
|
raise HTTPException(
|
|
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
2023-11-08 23:10:45 +01:00
|
|
|
detail="Event not found"
|
2023-11-06 23:27:47 +01:00
|
|
|
)
|
|
|
|
|
2023-11-12 21:18:49 +01:00
|
|
|
event.name = eventSingle.name
|
|
|
|
event.place = eventSingle.place
|
2023-11-08 23:10:45 +01:00
|
|
|
event.start_date = eventSingle.start_date
|
|
|
|
event.end_date = eventSingle.end_date
|
|
|
|
event.updated_at = datetime.today()
|
|
|
|
event_repository.save(event)
|
|
|
|
content = {"message": "event is updated"}
|
2023-11-06 23:27:47 +01:00
|
|
|
response = JSONResponse(content=content)
|
|
|
|
return response
|
|
|
|
|
|
|
|
|
2023-11-08 23:10:45 +01:00
|
|
|
@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:
|
2023-11-06 23:27:47 +01:00
|
|
|
raise HTTPException(
|
|
|
|
status_code=status.HTTP_400_BAD_REQUEST,
|
2023-11-08 23:10:45 +01:00
|
|
|
detail="eventids should be greater than 0"
|
2023-11-06 23:27:47 +01:00
|
|
|
)
|
|
|
|
|
2023-11-12 18:42:49 +01:00
|
|
|
event_repository = events.EventRepository(database=database.database)
|
2023-11-08 23:10:45 +01:00
|
|
|
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)
|
2023-11-06 23:27:47 +01:00
|
|
|
|
|
|
|
|
|
|
|
response = JSONResponse(content=content)
|
|
|
|
return response
|
|
|
|
|
2023-11-12 21:18:49 +01:00
|
|
|
@router.patch("/events/{item_id}", tags=["events"])
|
2023-11-08 23:10:45 +01:00
|
|
|
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"}
|
2023-11-06 23:27:47 +01:00
|
|
|
response = JSONResponse(content=content)
|
|
|
|
return response
|