From 116e500c142d3fc5cc5f0c996e39a75edab1f3ba Mon Sep 17 00:00:00 2001
From: Jaden Diefenbaugh <blakcap@users.noreply.github.com>
Date: Tue, 11 Apr 2017 15:58:44 +0200
Subject: [PATCH] add notes to migration script and convert aliases
 appropriately

---
 .../migrations/0004_auto_20170410_1121.py     | 27 ++++++++++++++++---
 1 file changed, 24 insertions(+), 3 deletions(-)

diff --git a/beat/web/reports/migrations/0004_auto_20170410_1121.py b/beat/web/reports/migrations/0004_auto_20170410_1121.py
index aae102538..b895fde7e 100644
--- a/beat/web/reports/migrations/0004_auto_20170410_1121.py
+++ b/beat/web/reports/migrations/0004_auto_20170410_1121.py
@@ -6,6 +6,8 @@ from django.db import migrations
 import json
 import re
 
+# parses a table from the old representation (a list of column objects)
+# and returns a table in the new representation
 def parse_table(table, precision):
     conv_table = {
             'fields': ['Experiment'],
@@ -34,6 +36,8 @@ def parse_table(table, precision):
 
     return conv_table
 
+# parses a plot from the old representation
+# and returns a plot in the new representation
 def parse_plot(plot):
     conv_plot = {
         'name': plot['data']['output'][0],
@@ -41,20 +45,27 @@ def parse_plot(plot):
             }
     return conv_plot
 
+# helper func to build the experiment's full name
 def experiment_fullname(exp):
     return '%s/%s/%s/%s/%s' % (exp.author.username, exp.toolchain.author.username, exp.toolchain.name, exp.toolchain.version, exp.name)
 
+# helper func to build the analyzer's full name
 def analyzer_fullname(report):
     return '%s/%s/%s' % (report.analyzer.author.username, report.analyzer.name, report.analyzer.version)
 
+# converts an old report into the new report format
 def move_content_to_groups_format(apps, schema_editor):
     Report = apps.get_model('reports', 'Report')
 
     for report in Report.objects.all():
+        # all of the changes are in the report's content field
         report_content = json.loads(report.content)
-        # convert to groups format
+
+        # convert to groups format, but don't touch any report thats
+        # already using the new system
         if 'groups' not in report_content:
-            exps = report.experiments.all()
+            # format:
+            # ----
             # groups: {
             # 	group1 : {
             # 		experiments: [],
@@ -65,17 +76,27 @@ def move_content_to_groups_format(apps, schema_editor):
             # 		},
             # 	...
             # }
+            # ----
+            exps = report.experiments.all()
             obj = {}
             groups = {}
+            # default to just one group that contains all experiments/items
             group1 = {
+                    # list of experiments in the group
                     'experiments': [ experiment_fullname(e) for e in exps ],
                     'reportItems': [],
+                    # analyzer of the report
                     'analyzer': analyzer_fullname(report) if report.analyzer else '',
                     'aliases': {},
                     'idx': 1
                     }
+
+            old_aliases = report_content['alias_experiments'] if 'alias_experiments' in report_content else {}
+            # assign aliases
+            get_alias = lambda exp_name: old_aliases[exp_name] if exp_name in old_aliases else None
             for e in exps:
-                group1['aliases'][experiment_fullname(e)] = e.name
+                fullname = experiment_fullname(e)
+                group1['aliases'][fullname] = get_alias(fullname) or e.name
 
             count_tables = 0
             count_plots = 0
-- 
GitLab