prompt: You will be provided with a partial code base and an issue statement explaining a problem to resolve.
  <issue>
  Deleting and re-adding widgets to a dashboard breaks it
  There's a specific set of steps that has messed up some of our users' dashboards:
  1. Create a new dashboard
  2. Add multiple widgets to it.
  3. Remove all those widgets from the dashboard
  4. Re-add some widgets
  5. Click the "Edit Dashboard (Name/Layout)" button
  6. Click Save without changing anything.
  7. Refresh the page
  
  This makes none of the widgets appear and causes the "Layout" array in the admin panel to contain one or more "null" values (depending on how many widgets you added/deleted):
  ![image](https://cloud.githubusercontent.com/assets/844493/11826038/76c749fa-a348-11e5-9f46-bbae43c4b6f6.png)
  
  The only way to recover from this state is to manually delete the "null" values through the admin interface.
  
  This is on re:dash version 0.8.2
  
  
  </issue>
  <code>
  [start of redash/handlers/widgets.py]
  1 import json
  2 
  3 from flask import request
  4 
  5 from redash import models
  6 from redash.wsgi import api
  7 from redash.permissions import require_permission
  8 from redash.handlers.base import BaseResource
  9 
  10 
  11 class WidgetListAPI(BaseResource):
  12     @require_permission('edit_dashboard')
  13     def post(self):
  14         widget_properties = request.get_json(force=True)
  15         widget_properties['options'] = json.dumps(widget_properties['options'])
  16         widget_properties.pop('id', None)
  17         widget_properties['dashboard'] = widget_properties.pop('dashboard_id')
  18         widget_properties['visualization'] = widget_properties.pop('visualization_id')
  19         widget = models.Widget(**widget_properties)
  20         widget.save()
  21 
  22         layout = json.loads(widget.dashboard.layout)
  23         new_row = True
  24 
  25         if len(layout) == 0 or widget.width == 2:
  26             layout.append([widget.id])
  27         elif len(layout[-1]) == 1:
  28             neighbour_widget = models.Widget.get(models.Widget.id == layout[-1][0])
  29             if neighbour_widget.width == 1:
  30                 layout[-1].append(widget.id)
  31                 new_row = False
  32             else:
  33                 layout.append([widget.id])
  34         else:
  35             layout.append([widget.id])
  36 
  37         widget.dashboard.layout = json.dumps(layout)
  38         widget.dashboard.save()
  39 
  40         return {'widget': widget.to_dict(), 'layout': layout, 'new_row': new_row}
  41 
  42 
  43 class WidgetAPI(BaseResource):
  44     @require_permission('edit_dashboard')
  45     def delete(self, widget_id):
  46         widget = models.Widget.get(models.Widget.id == widget_id)
  47         widget.delete_instance()
  48 
  49 api.add_resource(WidgetListAPI, '/api/widgets', endpoint='widgets')
  50 api.add_resource(WidgetAPI, '/api/widgets/<int:widget_id>', endpoint='widget')
  51
  [end of redash/handlers/widgets.py]
  </code>
  
  I need you to solve this issue by generating a single patch file that I can apply directly to this repository using git apply. Please respond with a single patch file in the following format.
  <patch>
  diff --git a/file.py b/file.py
  --- a/file.py
  +++ b/file.py
  @@ -1,27 +1,35 @@
   def euclidean(a, b):
  -    while b:
  -        a, b = b, a % b
  -    return a
  +    if b == 0:
  +        return a
  +    return euclidean(b, a % b)
   
   
   def bresenham(x0, y0, x1, y1):
       points = []
       dx = abs(x1 - x0)
       dy = abs(y1 - y0)
  -    sx = 1 if x0 < x1 else -1
  -    sy = 1 if y0 < y1 else -1
  -    err = dx - dy
  +    x, y = x0, y0
  +    sx = -1 if x0 > x1 else 1
  +    sy = -1 if y0 > y1 else 1
   
  -    while True:
  -        points.append((x0, y0))
  -        if x0 == x1 and y0 == y1:
  -            break
  -        e2 = 2 * err
  -        if e2 > -dy:
  -            err -= dy
  -            x0 += sx
  -        if e2 < dx:
  -            err += dx
  -            y0 += sy
  +    if dx > dy:
  +        err = dx / 2.0
  +        while x != x1:
  +            points.append((x, y))
  +            err -= dy
  +            if err < 0:
  +                y += sy
  +                err += dx
  +            x += sx
  +    else:
  +        err = dy / 2.0
  +        while y != y1:
  +            points.append((x, y))
  +            err -= dx
  +            if err < 0:
  +                x += sx
  +                err += dy
  +            y += sy
  +
  +    points.append((x, y))
       return points
  
  </patch>
golden_diff: diff --git a/redash/handlers/widgets.py b/redash/handlers/widgets.py
  --- a/redash/handlers/widgets.py
  +++ b/redash/handlers/widgets.py
  @@ -46,5 +46,7 @@
           widget = models.Widget.get(models.Widget.id == widget_id)
           widget.delete_instance()
   
  +        return {'layout': widget.dashboard.layout }
  +
   api.add_resource(WidgetListAPI, '/api/widgets', endpoint='widgets')
   api.add_resource(WidgetAPI, '/api/widgets/<int:widget_id>', endpoint='widget')
  
verification_info: {"golden_diff": "diff --git a/redash/handlers/widgets.py b/redash/handlers/widgets.py\n--- a/redash/handlers/widgets.py\n+++ b/redash/handlers/widgets.py\n@@ -46,5 +46,7 @@\n         widget = models.Widget.get(models.Widget.id == widget_id)\n         widget.delete_instance()\n \n+        return {'layout': widget.dashboard.layout }\n+\n api.add_resource(WidgetListAPI, '/api/widgets', endpoint='widgets')\n api.add_resource(WidgetAPI, '/api/widgets/<int:widget_id>', endpoint='widget')\n", "issue": "Deleting and re-adding widgets to a dashboard breaks it\nThere's a specific set of steps that has messed up some of our users' dashboards:\n1. Create a new dashboard\n2. Add multiple widgets to it.\n3. Remove all those widgets from the dashboard\n4. Re-add some widgets\n5. Click the \"Edit Dashboard (Name/Layout)\" button\n6. Click Save without changing anything.\n7. Refresh the page\n\nThis makes none of the widgets appear and causes the \"Layout\" array in the admin panel to contain one or more \"null\" values (depending on how many widgets you added/deleted):\n![image](https://cloud.githubusercontent.com/assets/844493/11826038/76c749fa-a348-11e5-9f46-bbae43c4b6f6.png)\n\nThe only way to recover from this state is to manually delete the \"null\" values through the admin interface.\n\nThis is on re:dash version 0.8.2\n\n", "before_files": [{"content": "import json\n\nfrom flask import request\n\nfrom redash import models\nfrom redash.wsgi import api\nfrom redash.permissions import require_permission\nfrom redash.handlers.base import BaseResource\n\n\nclass WidgetListAPI(BaseResource):\n    @require_permission('edit_dashboard')\n    def post(self):\n        widget_properties = request.get_json(force=True)\n        widget_properties['options'] = json.dumps(widget_properties['options'])\n        widget_properties.pop('id', None)\n        widget_properties['dashboard'] = widget_properties.pop('dashboard_id')\n        widget_properties['visualization'] = widget_properties.pop('visualization_id')\n        widget = models.Widget(**widget_properties)\n        widget.save()\n\n        layout = json.loads(widget.dashboard.layout)\n        new_row = True\n\n        if len(layout) == 0 or widget.width == 2:\n            layout.append([widget.id])\n        elif len(layout[-1]) == 1:\n            neighbour_widget = models.Widget.get(models.Widget.id == layout[-1][0])\n            if neighbour_widget.width == 1:\n                layout[-1].append(widget.id)\n                new_row = False\n            else:\n                layout.append([widget.id])\n        else:\n            layout.append([widget.id])\n\n        widget.dashboard.layout = json.dumps(layout)\n        widget.dashboard.save()\n\n        return {'widget': widget.to_dict(), 'layout': layout, 'new_row': new_row}\n\n\nclass WidgetAPI(BaseResource):\n    @require_permission('edit_dashboard')\n    def delete(self, widget_id):\n        widget = models.Widget.get(models.Widget.id == widget_id)\n        widget.delete_instance()\n\napi.add_resource(WidgetListAPI, '/api/widgets', endpoint='widgets')\napi.add_resource(WidgetAPI, '/api/widgets/<int:widget_id>', endpoint='widget')\n", "path": "redash/handlers/widgets.py"}]}