def put(cls, event_id: str): parser = reqparse.RequestParser() parser.add_argument('name', type=str, required=False) parser.add_argument('description', type=str, required=False) parser.add_argument('start_date', type=datetime.fromisoformat, required=False) parser.add_argument('end_date', type=datetime.fromisoformat, required=False) parser.add_argument('duration_time', type=int, required=False) parser.add_argument('image_url', type=str, required=False) data = parser.parse_args() if not ObjectId.is_valid(event_id): return {"status": Status.INVALID_FORMAT, "message": "Id is not valid ObjectId"}, 400 current_userid = get_jwt_identity() current_user = UserModel.find_by_id(current_userid) if not current_user: return {"status": Status.USER_NOT_FOUND, "message": "Current user not found"}, 403 event: EventModel = EventModel.find_by_id_and_admin_id(ObjectId(event_id), current_userid) if not event: return {"status": Status.NOT_FOUND, "message": "Event not found with admin as current user"}, 404 if data['name']: if len(data['name']) < 3: return {"status": Status.INVALID_FORMAT, "message": "name must be at least 3 chars long"}, 400 event.name = data['name'] if data['description']: event.description = data['description'] start_date: datetime = data['start_date'] end_date: datetime = data['end_date'] if start_date and end_date: if start_date > end_date: return {"status": Status.INVALID_DATA, "message": "End_date cannot be before start_date"}, 403 event.start_date = start_date event.end_date = end_date elif end_date: if event.start_date and event.start_date > end_date: return {"status": Status.INVALID_DATA, "message": "End_date cannot be before start_date"}, 403 event.end_date = end_date elif start_date: if event.end_date and event.end_date < start_date: return {"status": Status.INVALID_DATA, "message": "End_date cannot be before start_date"}, 403 event.start_date = start_date duration_time = data['duration_time'] if duration_time and duration_time < 0: return {"status": Status.INVALID_DATA, "message": "Duration_time cannot be less than 0"}, 403 event.duration_time = duration_time if data['image_url']: event.image_url = data['image_url'] event.save_to_db() return {"status": Status.SUCCESS, "event": event.json()}, 200
def post(cls, event_id: str): parser = reqparse.RequestParser() parser.add_argument('username', type=str, required=True) data = parser.parse_args() if not ObjectId.is_valid(event_id): return { "status": Status.INVALID_FORMAT, "message": "Id is not valid ObjectId" }, 400 current_userid = get_jwt_identity() current_user = UserModel.find_by_id(ObjectId(current_userid)) if not current_user: return { "status": Status.USER_NOT_FOUND, "message": "Current user not found" }, 403 user_to_update = UserModel.find_by_username(data['username']) if not user_to_update: return { "status": Status.USER_NOT_FOUND, "message": "User you want to add not found" }, 403 if user_to_update.id == current_user.id: return { "status": Status.INVALID_DATA, "message": "Cannot revoke admin to yourself" }, 403 event = EventModel.find_by_id_and_admin_id(ObjectId(event_id), current_user.id) if not event: return { "status": Status.NO_ADMIN, "message": "Event with admin as current user not found" }, 403 participant_to_update: ParticipantModel = next( filter( lambda participant: participant.user_id == user_to_update.id, event.participants), None) if not participant_to_update: return { "status": Status.USER_NOT_FOUND, "message": "User you want to revoke admin is not in the event" }, 403 participant_to_update.role = Role.MEMBER event.save() return { "status": Status.SUCCESS, "message": "Admin revoked", "participant": participant_to_update.json() }, 200
def post(cls, event_id: str): parser = reqparse.RequestParser() parser.add_argument('username', type=str, required=True) data = parser.parse_args() if not ObjectId.is_valid(event_id): return { "status": Status.INVALID_FORMAT, "message": "Id is not valid ObjectId" }, 400 current_userid = get_jwt_identity() current_user = UserModel.find_by_id(ObjectId(current_userid)) if not current_user: return { "status": Status.USER_NOT_FOUND, "message": "Current user not found" }, 403 user_to_remove = UserModel.find_by_username(data['username']) if not user_to_remove: return { "status": Status.USER_NOT_FOUND, "message": "User you want to add not found" }, 403 event = EventModel.find_by_id_and_admin_id(ObjectId(event_id), current_user.id) if not event: return { "status": Status.USER_NOT_FOUND, "message": "Event with admin as current user not found" }, 403 is_success = event.remove_participant(user_id=user_to_remove.id) if not is_success: return { "status": Status.ERROR, "message": "Some error occured" }, 400 event.reload() if len( list( filter(lambda participant: participant.role == Role.ADMIN, event.participants))) == 0: event.delete() return { "status": Status.SUCCESS, "message": "Event removed because last admin was removed" }, 200 return { "status": Status.SUCCESS, "message": "User removed from an event" }, 200
def post(cls, event_id: str): parser = reqparse.RequestParser() parser.add_argument('username', type=str, required=True) data = parser.parse_args() if not ObjectId.is_valid(event_id): return { "status": Status.INVALID_FORMAT, "message": "Id is not valid ObjectId" }, 400 current_userid = get_jwt_identity() current_user = UserModel.find_by_id(ObjectId(current_userid)) if not current_user: return { "status": Status.USER_NOT_FOUND, "message": "Current user not found" }, 403 user_to_add = UserModel.find_by_username(data['username']) if not user_to_add: return { "status": Status.USER_NOT_FOUND, "message": "User you want to add not found" }, 403 event = EventModel.find_by_id_and_admin_id(ObjectId(event_id), current_user.id) if not event: return { "status": Status.NO_ADMIN, "message": "Event with admin as current user not found" }, 403 if len( list( filter( lambda participant: participant.user_id == user_to_add. id, event.participants))) > 0: return { "status": Status.ALREADY_IN_EVENT, "message": "User you want to add is already in event" }, 403 is_success = event.add_new_participant(user_to_add.id) if not is_success: return { "status": Status.ERROR, "message": "Some error occured" }, 400 return { "status": Status.SUCCESS, "message": "User joined event successfully" }, 200
def delete(cls, event_id: str): if not ObjectId.is_valid(event_id): return {"status": Status.INVALID_FORMAT, "message": "Id is not valid ObjectId"}, 400 current_userid = get_jwt_identity() current_user = UserModel.find_by_id(current_userid) if not current_user: return {"status": Status.USER_NOT_FOUND, "message": "Current user not found"}, 403 event: EventModel = EventModel.find_by_id_and_admin_id(ObjectId(event_id), current_userid) if not event: return {"status": Status.NOT_FOUND, "message": "Event not found with admin as current user"}, 404 event.delete() return {"status": Status.SUCCESS, "message": "Event deleted"}, 200