Enforce AlertSubscription uniqueness; update AlertSubscription delete handler

This commit is contained in:
Alex DeBrie 2016-03-14 18:21:04 +00:00
parent 3cfbb9855b
commit 82b7146216
2 changed files with 9 additions and 7 deletions

View File

@ -106,8 +106,10 @@ class AlertSubscriptionListResource(BaseResource):
class AlertSubscriptionResource(BaseResource):
def delete(self, alert_id, subscriber_id):
models.AlertSubscription.unsubscribe(alert_id, subscriber_id)
require_admin_or_owner(subscriber_id)
subscription = get_object_or_404(models.AlertSubscription.get_by_id, subscriber_id)
require_admin_or_owner(subscription.id)
subscription.delete_instance()
self.record_event({
'action': 'unsubscribe',

View File

@ -1105,8 +1105,13 @@ class AlertSubscription(ModelTimestampsMixin, BaseModel):
class Meta:
db_table = 'alert_subscriptions'
indexes = (
(('destination', 'alert'), True),
)
def to_dict(self):
d = {
'id': self.id,
'user': self.user.to_dict(),
'alert_id': self.alert_id
}
@ -1120,11 +1125,6 @@ class AlertSubscription(ModelTimestampsMixin, BaseModel):
def all(cls, alert_id):
return AlertSubscription.select(AlertSubscription, User).join(User).where(AlertSubscription.alert==alert_id)
@classmethod
def unsubscribe(cls, alert_id, user_id):
query = AlertSubscription.delete().where(AlertSubscription.alert==alert_id).where(AlertSubscription.user==user_id)
return query.execute()
all_models = (Organization, Group, DataSource, DataSourceGroup, User, QueryResult, Query, Alert, Dashboard, Visualization, Widget, Event, NotificationDestination, AlertSubscription)