migrating Widgets to Visualizations

This commit is contained in:
Amir Nissim 2014-02-04 16:11:48 +02:00
parent bb096be00c
commit cd838e5a7e
2 changed files with 51 additions and 0 deletions

View File

@ -186,6 +186,7 @@ class Visualization(models.Model):
class Widget(models.Model): class Widget(models.Model):
id = models.AutoField(primary_key=True) id = models.AutoField(primary_key=True)
type = models.CharField(max_length=100) type = models.CharField(max_length=100)
query = models.ForeignKey(Query, related_name='widgets')
visualization = models.ForeignKey(Visualization, related_name='widgets') visualization = models.ForeignKey(Visualization, related_name='widgets')
width = models.IntegerField() width = models.IntegerField()
options = models.TextField() options = models.TextField()

50
rd_service/migrate.py Normal file
View File

@ -0,0 +1,50 @@
import json
import settings
from data.models import *
# first run:
# CREATE TABLE "visualizations" (
# "id" serial NOT NULL PRIMARY KEY,
# "type" varchar(100) NOT NULL,
# "query_id" integer NOT NULL REFERENCES "queries" ("id") DEFERRABLE INITIALLY DEFERRED,
# "name" varchar(255) NOT NULL,
# "description" varchar(4096),
# "options" text NOT NULL
# )
# ;
# ALTER TABLE widgets ADD COLUMN "visualization_id" integer REFERENCES "visualizations" ("id") DEFERRABLE INITIALLY DEFERRED;
if __name__ == '__main__':
print 'migrating Widgets -> Visualizations ...'
for query in Query.objects.filter(name__icontains="cohort"):
vis = Visualization(query=query, name=query.name,
description=query.description,
type="COHORT", options="{}")
vis.save()
for widget in Widget.objects.all():
print 'processing widget %d' % widget.id
query = widget.query
vis_type = widget.type.upper()
vis = query.visualizations.filter(type=vis_type)
if vis:
print 'found'
widget.visualization = vis[0]
widget.save()
else:
options = json.loads(widget.options)
vis_options = {"series": options} if options else {}
vis_options = json.dumps(vis_options)
vis = Visualization(query=query, name=query.name,
description=query.description,
type=vis_type, options=vis_options)
vis.save()
widget.visualization = vis
widget.save()