From 0ea3751605a8b261074913b366132a0af581ce6f Mon Sep 17 00:00:00 2001
From: Tanner Prestegard <tanner.prestegard@ligo.org>
Date: Thu, 11 Oct 2018 14:18:06 -0500
Subject: [PATCH] Add 'protected' column to labels and apply it

Add a boolean column called 'protected' to the Label model.
Protected labels will not be allowed to be directly applied, but
will be applied as a part of another process (like signoffs).
We also create a data migration which sets some existing labels
as protected (ADV(OK|NO), (H1|L1|V1)(OK|NO))
---
 .../events/migrations/0027_label_protected.py | 20 +++++++++
 .../migrations/0028_make_labels_protected.py  | 44 +++++++++++++++++++
 gracedb/events/models.py                      |  5 +++
 3 files changed, 69 insertions(+)
 create mode 100644 gracedb/events/migrations/0027_label_protected.py
 create mode 100644 gracedb/events/migrations/0028_make_labels_protected.py

diff --git a/gracedb/events/migrations/0027_label_protected.py b/gracedb/events/migrations/0027_label_protected.py
new file mode 100644
index 000000000..c3dfaeea2
--- /dev/null
+++ b/gracedb/events/migrations/0027_label_protected.py
@@ -0,0 +1,20 @@
+# -*- coding: utf-8 -*-
+# Generated by Django 1.11.16 on 2018-10-08 17:42
+from __future__ import unicode_literals
+
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('events', '0026_increase_channel_maxlength'),
+    ]
+
+    operations = [
+        migrations.AddField(
+            model_name='label',
+            name='protected',
+            field=models.BooleanField(default=False),
+        ),
+    ]
diff --git a/gracedb/events/migrations/0028_make_labels_protected.py b/gracedb/events/migrations/0028_make_labels_protected.py
new file mode 100644
index 000000000..b9c64efec
--- /dev/null
+++ b/gracedb/events/migrations/0028_make_labels_protected.py
@@ -0,0 +1,44 @@
+# -*- coding: utf-8 -*-
+# Generated by Django 1.11.16 on 2018-10-08 17:43
+from __future__ import unicode_literals
+
+from django.db import migrations
+
+PROTECTED_LABELS = [
+    'H1OK',
+    'H1NO',
+    'L1OK',
+    'L1NO',
+    'V1OK',
+    'V1NO',
+    'ADVOK',
+    'ADVNO',
+]
+
+def protect_labels(apps, schema_editor):
+    Label = apps.get_model('events', 'Label')
+
+    for label in PROTECTED_LABELS:
+        l = Label.objects.get(name=label)
+        l.protected = True
+        l.save(update_fields=['protected'])
+
+
+def unprotect_labels(apps, schema_editor):
+    Label = apps.get_model('events', 'Label')
+
+    for label in PROTECTED_LABELS:
+        l = Label.objects.get(name=label)
+        l.protected = False
+        l.save(update_fields=['protected'])
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('events', '0027_label_protected'),
+    ]
+
+    operations = [
+        migrations.RunPython(protect_labels, unprotect_labels),
+    ]
diff --git a/gracedb/events/models.py b/gracedb/events/models.py
index 7241f4651..000c450e3 100644
--- a/gracedb/events/models.py
+++ b/gracedb/events/models.py
@@ -89,6 +89,11 @@ class Label(models.Model):
     defaultColor = models.CharField(max_length=20, unique=False,
         default="black")
     description = models.TextField(blank=False)
+    # protected = True means that the Label should not be "writeable": i.e.,
+    # users should not be apply to directly apply or remove it.  This is useful
+    # for labels that are added and removed as part of a process, like
+    # signoffs, for examples.
+    protected = models.BooleanField(default=False)
 
     def __unicode__(self):
         return self.name
-- 
GitLab