From 90c8b2c88d78a4794c048e7baecaa7461a759885 Mon Sep 17 00:00:00 2001
From: Tanner Prestegard <tanner.prestegard@ligo.org>
Date: Thu, 7 Jun 2018 15:42:06 -0500
Subject: [PATCH] Restrict tag name characters

If a tag's name has some "uncommon" characters in it, like
parentheses (or probably others), it will cause errors for the
javascript which renders the event pages, since it tries to use
the tag name in the element id for the delete button.  We now require
that tags only use 0-9a-zA-Z_\- in their names. May have to go through
and delete some tags which use them.
---
 .../0022_restrict_tag_name_chars.py           | 21 +++++++++++++++++++
 gracedb/events/models.py                      |  9 +++++++-
 2 files changed, 29 insertions(+), 1 deletion(-)
 create mode 100644 gracedb/events/migrations/0022_restrict_tag_name_chars.py

diff --git a/gracedb/events/migrations/0022_restrict_tag_name_chars.py b/gracedb/events/migrations/0022_restrict_tag_name_chars.py
new file mode 100644
index 000000000..a6073f009
--- /dev/null
+++ b/gracedb/events/migrations/0022_restrict_tag_name_chars.py
@@ -0,0 +1,21 @@
+# -*- coding: utf-8 -*-
+# Generated by Django 1.11.5 on 2018-06-07 18:57
+from __future__ import unicode_literals
+
+import django.core.validators
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('events', '0021_emobservation_N_not_editable'),
+    ]
+
+    operations = [
+        migrations.AlterField(
+            model_name='tag',
+            name='name',
+            field=models.CharField(max_length=100, unique=True, validators=[django.core.validators.RegexValidator(code=b'invalid_tag_name', message=b'Tag names can only include [0-9a-zA-z_-]', regex=b'^[0-9a-zA-Z_\\-]*$')]),
+        ),
+    ]
diff --git a/gracedb/events/models.py b/gracedb/events/models.py
index c116df492..25ee5c16a 100644
--- a/gracedb/events/models.py
+++ b/gracedb/events/models.py
@@ -808,7 +808,14 @@ class Tag(CleanSaveModel):
     track those things?  Doesn't seem like it.
     """
     name = models.CharField(max_length=100, null=False, blank=False,
-        unique=True)
+        unique=True,
+        validators=[
+            models.fields.validators.RegexValidator(
+                regex=r'^[0-9a-zA-Z_\-]*$',
+                message="Tag names can only include [0-9a-zA-z_-]",
+                code="invalid_tag_name",
+            )
+        ])
     displayName = models.CharField(max_length=200, null=True, blank=True)
 
     def __unicode__(self):
-- 
GitLab