Beispiel #1
0
async def change_owner(
        request: Request,
        event_id: int,
        db: Session = Depends(get_db),
):
    form = await request.form()
    if "username" not in form:
        return RedirectResponse(
            router.url_path_for("eventview", event_id=event_id),
            status_code=status.HTTP_302_FOUND,
        )
    username = form["username"]
    user = db.query(User).filter_by(username=username).first()
    try:
        user_id = user.id
    except AttributeError as e:
        error_message = f"Username does not exist. {form['username']}"
        logger.exception(str(e))
        raise HTTPException(
            status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
            detail=error_message,
        )
    owner_to_update = {"owner_id": user_id}
    _update_event(db, event_id, owner_to_update)
    return RedirectResponse(
        router.url_path_for("eventview", event_id=event_id),
        status_code=status.HTTP_302_FOUND,
    )
Beispiel #2
0
def raise_for_nonexisting_event(event_id: int) -> None:
    error_message = f"Event ID does not exist. ID: {event_id}"
    logger.exception(error_message)
    raise HTTPException(
        status_code=status.HTTP_404_NOT_FOUND,
        detail=error_message,
    )
Beispiel #3
0
def by_id(db: Session, event_id: int) -> Event:
    """Get a single event by id"""
    if not isinstance(db, Session):
        error_message = (f"Could not connect to database. "
                         f"db instance type received: {type(db)}")
        logger.critical(error_message)
        raise HTTPException(
            status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
            detail=error_message,
        )

    try:
        event = db.query(Event).filter_by(id=event_id).one()
    except NoResultFound:
        error_message = f"Event ID does not exist. ID: {event_id}"
        logger.exception(error_message)
        raise HTTPException(
            status_code=status.HTTP_404_NOT_FOUND,
            detail=error_message,
        )
    except MultipleResultsFound:
        error_message = (
            f"Multiple results found when getting event. Expected only one. "
            f"ID: {event_id}")
        logger.critical(error_message)
        raise HTTPException(
            status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
            detail=error_message,
        )
    return event
Beispiel #4
0
def _update_event(db: Session, event_id: int, event_to_update: Dict) -> Event:
    try:
        # Update database
        db.query(Event).filter(Event.id == event_id).update(
            event_to_update, synchronize_session=False)

        db.commit()
        return by_id(db, event_id)
    except (AttributeError, SQLAlchemyError) as e:
        logger.exception(str(e))
        raise HTTPException(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
                            detail="Internal server error")
Beispiel #5
0
def _delete_event(db: Session, event: Event):
    try:
        # Delete event
        db.delete(event)

        # Delete user_event
        db.query(UserEvent).filter(UserEvent.event_id == event.id).delete()

        db.commit()

    except (SQLAlchemyError, AttributeError) as e:
        logger.exception(str(e))
        raise HTTPException(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
                            detail="Deletion failed")
Beispiel #6
0
def add_new_event(values: dict, db: Session) -> Optional[Event]:
    """Get User values and the DB Session insert the values
    to the DB and refresh it exception in case that the keys
    in the dict is not match to the fields in the DB
    return the Event Class item"""

    if not is_date_before(values['start'], values['end']):
        return None
    try:
        new_event = create_model(db, Event, **values)
        create_model(db,
                     UserEvent,
                     user_id=values['owner_id'],
                     event_id=new_event.id)
        return new_event
    except (AssertionError, AttributeError, TypeError) as e:
        logger.exception(e)
        return None