Replace notifications by default (#1084)

This commit is contained in:
bmc-msft
2021-07-20 18:39:31 -04:00
committed by GitHub
parent 2691d91d8f
commit 065272191e
7 changed files with 66 additions and 47 deletions

View File

@ -57,30 +57,26 @@ class Notification(models.Notification, ORMMixin):
notification = notifications[0]
return notification
@classmethod
def get_existing(
cls, container: Container, config: NotificationTemplate
) -> Optional["Notification"]:
notifications = Notification.search(query={"container": [container]})
for notification in notifications:
if notification.config == config:
return notification
return None
@classmethod
def key_fields(cls) -> Tuple[str, str]:
return ("notification_id", "container")
@classmethod
def create(
cls, container: Container, config: NotificationTemplate
cls, container: Container, config: NotificationTemplate, replace_existing: bool
) -> Result["Notification"]:
if not container_exists(container, StorageType.corpus):
return Error(code=ErrorCode.INVALID_REQUEST, errors=["invalid container"])
existing = cls.get_existing(container, config)
if existing is not None:
return existing
if replace_existing:
existing = cls.search(query={"container": [container]})
for entry in existing:
logging.info(
"replacing existing notification: %s",
entry.notification_id,
container,
)
entry.delete()
entry = cls(container=container, config=config)
entry.save()