Ejemplo n.º 1
0
def create(*, db_session, notification_in: NotificationCreate) -> Notification:
    """Creates a new notification."""
    filters = [
        search_service.get(db_session=db_session, search_filter_id=f.id)
        for f in notification_in.filters
    ]

    notification = Notification(
        **notification_in.dict(exclude={"filters"}),
        filters=filters,
    )

    db_session.add(notification)
    db_session.commit()
    return notification
Ejemplo n.º 2
0
def create(*, db_session, notification_in: NotificationCreate) -> Notification:
    """Creates a new notification."""
    filters = []
    if notification_in.filters:
        filters = [
            search_service.get(db_session=db_session, search_filter_id=f.id)
            for f in notification_in.filters
        ]

    project = project_service.get_by_name(db_session=db_session,
                                          name=notification_in.project.name)

    notification = Notification(**notification_in.dict(
        exclude={"filters", "project"}),
                                filters=filters,
                                project=project)

    db_session.add(notification)
    db_session.commit()
    return notification
Ejemplo n.º 3
0
def update(*, db_session, notification: Notification,
           notification_in: NotificationUpdate) -> Notification:
    """Updates a notification."""
    notification_data = jsonable_encoder(notification)

    filters = [
        search_service.get(db_session=db_session, search_filter_id=f.id)
        for f in notification_in.filters
    ]

    update_data = notification_in.dict(
        skip_defaults=True,
        exclude={"filters"},
    )

    for field in notification_data:
        if field in update_data:
            setattr(notification, field, update_data[field])

    notification.filters = filters
    db_session.add(notification)
    db_session.commit()
    return notification