prompt: We are currently solving the following issue within our repository. Here is the issue text:
  --- BEGIN ISSUE ---
  User should be able to delete an Alert
  Can't remove Alert with UI.
  
  Directly run sql as below.
  
  ``` sql
  delete from alerts where id = 〜
  ```
  
  
  --- END ISSUE ---
  
  Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
  --- BEGIN FILES ---
  Path: `redash/handlers/alerts.py`
  Content:
  ```
  1 import time
  2 
  3 from flask import request
  4 from funcy import project
  5 
  6 from redash import models
  7 from redash.permissions import require_access, require_admin_or_owner, view_only, require_permission
  8 from redash.handlers.base import BaseResource, require_fields, get_object_or_404
  9 
  10 
  11 class AlertResource(BaseResource):
  12     def get(self, alert_id):
  13         alert = get_object_or_404(models.Alert.get_by_id_and_org, alert_id, self.current_org)
  14         require_access(alert.groups, self.current_user, view_only)
  15         return alert.to_dict()
  16 
  17     def post(self, alert_id):
  18         req = request.get_json(True)
  19         params = project(req, ('options', 'name', 'query_id', 'rearm'))
  20         alert = get_object_or_404(models.Alert.get_by_id_and_org, alert_id, self.current_org)
  21         require_admin_or_owner(alert.user.id)
  22 
  23         if 'query_id' in params:
  24             params['query'] = params.pop('query_id')
  25 
  26         alert.update_instance(**params)
  27 
  28         self.record_event({
  29             'action': 'edit',
  30             'timestamp': int(time.time()),
  31             'object_id': alert.id,
  32             'object_type': 'alert'
  33         })
  34 
  35         return alert.to_dict()
  36 
  37 
  38 class AlertListResource(BaseResource):
  39     def post(self):
  40         req = request.get_json(True)
  41         require_fields(req, ('options', 'name', 'query_id'))
  42 
  43         query = models.Query.get_by_id_and_org(req['query_id'], self.current_org)
  44         require_access(query.groups, self.current_user, view_only)
  45 
  46         alert = models.Alert.create(
  47             name=req['name'],
  48             query=query,
  49             user=self.current_user,
  50             options=req['options']
  51         )
  52 
  53         self.record_event({
  54             'action': 'create',
  55             'timestamp': int(time.time()),
  56             'object_id': alert.id,
  57             'object_type': 'alert'
  58         })
  59 
  60         return alert.to_dict()
  61 
  62     @require_permission('list_alerts')
  63     def get(self):
  64         return [alert.to_dict() for alert in models.Alert.all(groups=self.current_user.groups)]
  65 
  66 
  67 class AlertSubscriptionListResource(BaseResource):
  68     def post(self, alert_id):
  69         req = request.get_json(True)
  70 
  71         alert = models.Alert.get_by_id_and_org(alert_id, self.current_org)
  72         require_access(alert.groups, self.current_user, view_only)
  73         kwargs = {'alert': alert, 'user': self.current_user}
  74 
  75         if 'destination_id' in req:
  76             destination = models.NotificationDestination.get_by_id_and_org(req['destination_id'], self.current_org)
  77             kwargs['destination'] = destination
  78 
  79         subscription = models.AlertSubscription.create(**kwargs)
  80 
  81         self.record_event({
  82             'action': 'subscribe',
  83             'timestamp': int(time.time()),
  84             'object_id': alert_id,
  85             'object_type': 'alert',
  86             'destination': req.get('destination_id')
  87         })
  88 
  89         return subscription.to_dict()
  90 
  91     def get(self, alert_id):
  92         alert = models.Alert.get_by_id_and_org(alert_id, self.current_org)
  93         require_access(alert.groups, self.current_user, view_only)
  94 
  95         subscriptions = models.AlertSubscription.all(alert_id)
  96         return [s.to_dict() for s in subscriptions]
  97 
  98 
  99 class AlertSubscriptionResource(BaseResource):
  100     def delete(self, alert_id, subscriber_id):
  101         
  102         subscription = get_object_or_404(models.AlertSubscription.get_by_id, subscriber_id)
  103         require_admin_or_owner(subscription.user.id)
  104         subscription.delete_instance()
  105 
  106         self.record_event({
  107             'action': 'unsubscribe',
  108             'timestamp': int(time.time()),
  109             'object_id': alert_id,
  110             'object_type': 'alert'
  111         })
  112 
  113
  ```
  --- END FILES ---
  
  Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.
  
  Here is an example:
  
  ```diff
  diff --git a/examples/server_async.py b/examples/server_async.py
  --- a/examples/server_async.py
  +++ b/examples/server_async.py
  @@ -313,4 +313,4 @@
   
   
   if __name__ == "__main__":
  -    asyncio.run(run_async_server("."), debug=True)
  +    asyncio.run(run_async_server(), debug=True)
  diff --git a/examples/server_sync.py b/examples/server_sync.py
  --- a/examples/server_sync.py
  +++ b/examples/server_sync.py
  @@ -313,5 +313,5 @@
   
   
   if __name__ == "__main__":
  -    server = run_sync_server(".")
  +    server = run_sync_server()
       server.shutdown()
  
  ```
  
golden_diff: diff --git a/redash/handlers/alerts.py b/redash/handlers/alerts.py
  --- a/redash/handlers/alerts.py
  +++ b/redash/handlers/alerts.py
  @@ -34,6 +34,11 @@
   
           return alert.to_dict()
   
  +    def delete(self, alert_id):
  +        alert = get_object_or_404(models.Alert.get_by_id_and_org, alert_id, self.current_org)
  +        require_admin_or_owner(alert.user.id)
  +        alert.delete_instance(recursive=True)
  +
   
   class AlertListResource(BaseResource):
       def post(self):
  
verification_info: {"golden_diff": "diff --git a/redash/handlers/alerts.py b/redash/handlers/alerts.py\n--- a/redash/handlers/alerts.py\n+++ b/redash/handlers/alerts.py\n@@ -34,6 +34,11 @@\n \n         return alert.to_dict()\n \n+    def delete(self, alert_id):\n+        alert = get_object_or_404(models.Alert.get_by_id_and_org, alert_id, self.current_org)\n+        require_admin_or_owner(alert.user.id)\n+        alert.delete_instance(recursive=True)\n+\n \n class AlertListResource(BaseResource):\n     def post(self):\n", "issue": "User should be able to delete an Alert\nCan't remove Alert with UI.\n\nDirectly run sql as below.\n\n``` sql\ndelete from alerts where id = \u301c\n```\n\n", "before_files": [{"content": "import time\n\nfrom flask import request\nfrom funcy import project\n\nfrom redash import models\nfrom redash.permissions import require_access, require_admin_or_owner, view_only, require_permission\nfrom redash.handlers.base import BaseResource, require_fields, get_object_or_404\n\n\nclass AlertResource(BaseResource):\n    def get(self, alert_id):\n        alert = get_object_or_404(models.Alert.get_by_id_and_org, alert_id, self.current_org)\n        require_access(alert.groups, self.current_user, view_only)\n        return alert.to_dict()\n\n    def post(self, alert_id):\n        req = request.get_json(True)\n        params = project(req, ('options', 'name', 'query_id', 'rearm'))\n        alert = get_object_or_404(models.Alert.get_by_id_and_org, alert_id, self.current_org)\n        require_admin_or_owner(alert.user.id)\n\n        if 'query_id' in params:\n            params['query'] = params.pop('query_id')\n\n        alert.update_instance(**params)\n\n        self.record_event({\n            'action': 'edit',\n            'timestamp': int(time.time()),\n            'object_id': alert.id,\n            'object_type': 'alert'\n        })\n\n        return alert.to_dict()\n\n\nclass AlertListResource(BaseResource):\n    def post(self):\n        req = request.get_json(True)\n        require_fields(req, ('options', 'name', 'query_id'))\n\n        query = models.Query.get_by_id_and_org(req['query_id'], self.current_org)\n        require_access(query.groups, self.current_user, view_only)\n\n        alert = models.Alert.create(\n            name=req['name'],\n            query=query,\n            user=self.current_user,\n            options=req['options']\n        )\n\n        self.record_event({\n            'action': 'create',\n            'timestamp': int(time.time()),\n            'object_id': alert.id,\n            'object_type': 'alert'\n        })\n\n        return alert.to_dict()\n\n    @require_permission('list_alerts')\n    def get(self):\n        return [alert.to_dict() for alert in models.Alert.all(groups=self.current_user.groups)]\n\n\nclass AlertSubscriptionListResource(BaseResource):\n    def post(self, alert_id):\n        req = request.get_json(True)\n\n        alert = models.Alert.get_by_id_and_org(alert_id, self.current_org)\n        require_access(alert.groups, self.current_user, view_only)\n        kwargs = {'alert': alert, 'user': self.current_user}\n\n        if 'destination_id' in req:\n            destination = models.NotificationDestination.get_by_id_and_org(req['destination_id'], self.current_org)\n            kwargs['destination'] = destination\n\n        subscription = models.AlertSubscription.create(**kwargs)\n\n        self.record_event({\n            'action': 'subscribe',\n            'timestamp': int(time.time()),\n            'object_id': alert_id,\n            'object_type': 'alert',\n            'destination': req.get('destination_id')\n        })\n\n        return subscription.to_dict()\n\n    def get(self, alert_id):\n        alert = models.Alert.get_by_id_and_org(alert_id, self.current_org)\n        require_access(alert.groups, self.current_user, view_only)\n\n        subscriptions = models.AlertSubscription.all(alert_id)\n        return [s.to_dict() for s in subscriptions]\n\n\nclass AlertSubscriptionResource(BaseResource):\n    def delete(self, alert_id, subscriber_id):\n        \n        subscription = get_object_or_404(models.AlertSubscription.get_by_id, subscriber_id)\n        require_admin_or_owner(subscription.user.id)\n        subscription.delete_instance()\n\n        self.record_event({\n            'action': 'unsubscribe',\n            'timestamp': int(time.time()),\n            'object_id': alert_id,\n            'object_type': 'alert'\n        })\n\n", "path": "redash/handlers/alerts.py"}], "after_files": [{"content": "import time\n\nfrom flask import request\nfrom funcy import project\n\nfrom redash import models\nfrom redash.permissions import require_access, require_admin_or_owner, view_only, require_permission\nfrom redash.handlers.base import BaseResource, require_fields, get_object_or_404\n\n\nclass AlertResource(BaseResource):\n    def get(self, alert_id):\n        alert = get_object_or_404(models.Alert.get_by_id_and_org, alert_id, self.current_org)\n        require_access(alert.groups, self.current_user, view_only)\n        return alert.to_dict()\n\n    def post(self, alert_id):\n        req = request.get_json(True)\n        params = project(req, ('options', 'name', 'query_id', 'rearm'))\n        alert = get_object_or_404(models.Alert.get_by_id_and_org, alert_id, self.current_org)\n        require_admin_or_owner(alert.user.id)\n\n        if 'query_id' in params:\n            params['query'] = params.pop('query_id')\n\n        alert.update_instance(**params)\n\n        self.record_event({\n            'action': 'edit',\n            'timestamp': int(time.time()),\n            'object_id': alert.id,\n            'object_type': 'alert'\n        })\n\n        return alert.to_dict()\n\n    def delete(self, alert_id):\n        alert = get_object_or_404(models.Alert.get_by_id_and_org, alert_id, self.current_org)\n        require_admin_or_owner(alert.user.id)\n        alert.delete_instance(recursive=True)\n\n\nclass AlertListResource(BaseResource):\n    def post(self):\n        req = request.get_json(True)\n        require_fields(req, ('options', 'name', 'query_id'))\n\n        query = models.Query.get_by_id_and_org(req['query_id'], self.current_org)\n        require_access(query.groups, self.current_user, view_only)\n\n        alert = models.Alert.create(\n            name=req['name'],\n            query=query,\n            user=self.current_user,\n            options=req['options']\n        )\n\n        self.record_event({\n            'action': 'create',\n            'timestamp': int(time.time()),\n            'object_id': alert.id,\n            'object_type': 'alert'\n        })\n\n        return alert.to_dict()\n\n    @require_permission('list_alerts')\n    def get(self):\n        return [alert.to_dict() for alert in models.Alert.all(groups=self.current_user.groups)]\n\n\nclass AlertSubscriptionListResource(BaseResource):\n    def post(self, alert_id):\n        req = request.get_json(True)\n\n        alert = models.Alert.get_by_id_and_org(alert_id, self.current_org)\n        require_access(alert.groups, self.current_user, view_only)\n        kwargs = {'alert': alert, 'user': self.current_user}\n\n        if 'destination_id' in req:\n            destination = models.NotificationDestination.get_by_id_and_org(req['destination_id'], self.current_org)\n            kwargs['destination'] = destination\n\n        subscription = models.AlertSubscription.create(**kwargs)\n\n        self.record_event({\n            'action': 'subscribe',\n            'timestamp': int(time.time()),\n            'object_id': alert_id,\n            'object_type': 'alert',\n            'destination': req.get('destination_id')\n        })\n\n        return subscription.to_dict()\n\n    def get(self, alert_id):\n        alert = models.Alert.get_by_id_and_org(alert_id, self.current_org)\n        require_access(alert.groups, self.current_user, view_only)\n\n        subscriptions = models.AlertSubscription.all(alert_id)\n        return [s.to_dict() for s in subscriptions]\n\n\nclass AlertSubscriptionResource(BaseResource):\n    def delete(self, alert_id, subscriber_id):\n        \n        subscription = get_object_or_404(models.AlertSubscription.get_by_id, subscriber_id)\n        require_admin_or_owner(subscription.user.id)\n        subscription.delete_instance()\n\n        self.record_event({\n            'action': 'unsubscribe',\n            'timestamp': int(time.time()),\n            'object_id': alert_id,\n            'object_type': 'alert'\n        })\n\n", "path": "redash/handlers/alerts.py"}]}