Browse Source

Reserve these org slug words

David Burke 4 years ago
parent
commit
6f2fb8cca0
2 changed files with 31 additions and 0 deletions
  1. 24 0
      organizations_ext/models.py
  2. 7 0
      organizations_ext/tests/tests.py

+ 24 - 0
organizations_ext/models.py

@@ -1,4 +1,5 @@
 from django.db import models
+from django.utils.text import slugify
 from django.utils.translation import ugettext_lazy as _
 from organizations.base import (
     OrganizationBase,
@@ -145,6 +146,29 @@ class Organization(SharedBaseModel, OrganizationBase):
         help_text="Default for whether projects should script IP Addresses",
     )
 
+    def slugify_function(self, content):
+        reserved_words = [
+            "login",
+            "register",
+            "app",
+            "profile",
+            "organizations",
+            "settings",
+            "issues",
+            "performance",
+            "_health",
+            "rest-auth",
+            "api",
+            "accept",
+            "stripe",
+            "admin",
+            "__debug__",
+        ]
+        slug = slugify(content)
+        if slug in reserved_words:
+            return slug + "-1"
+        return slug
+
     def add_user(self, user, role=OrganizationUserRole.MEMBER):
         """
         Adds a new user and if the first user makes the user an admin and

+ 7 - 0
organizations_ext/tests/tests.py

@@ -36,6 +36,13 @@ class OrganizationModelTestCase(TestCase):
         callback = settings.DJSTRIPE_SUBSCRIBER_MODEL_REQUEST_CALLBACK
         self.assertEqual(callback(request), organization)
 
+    def test_slug_reserved_words(self):
+        """ Reserve some words for frontend routing needs """
+        word = "login"
+        organization = baker.make("organizations_ext.Organization", name=word)
+        self.assertNotEqual(organization.slug, word)
+        organization = baker.make("organizations_ext.Organization", name=word)
+
 
 class OrganizationsAPITestCase(APITestCase):
     def setUp(self):