forked from webrecorder/browsertrix
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add slugs to org backend (webrecorder#1250)
- Add slug field with uniqueness constraint to Organization - Use python-slugify to generate slug from name and import that in migration - Require name in all /rename and org creation requests - Auto-generate slug for new org with no slug or when /rename is called w/o a slug - Auto-generate slug for 'default-org' based on name - Add /api/orgs/slugs GET endpoint to return all slugs in use - tests: extend backend test-requirements.txt from requirements to allow testing slugify - tests: move get_redis_crawl_stats() to avoid extra dependency in utils
- Loading branch information
Showing
14 changed files
with
138 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
""" | ||
Migration 0019 - Organization slug | ||
""" | ||
from btrixcloud.migrations import BaseMigration | ||
from btrixcloud.utils import slug_from_name | ||
|
||
|
||
MIGRATION_VERSION = "0019" | ||
|
||
|
||
class Migration(BaseMigration): | ||
"""Migration class.""" | ||
|
||
def __init__(self, mdb, migration_version=MIGRATION_VERSION): | ||
super().__init__(mdb, migration_version) | ||
|
||
async def migrate_up(self): | ||
"""Perform migration up. | ||
Add slug to all existing orgs. | ||
""" | ||
# pylint: disable=duplicate-code | ||
mdb_orgs = self.mdb["organizations"] | ||
async for org in mdb_orgs.find({"slug": {"$eq": None}}): | ||
oid = org["_id"] | ||
slug = slug_from_name(org["name"]) | ||
try: | ||
await mdb_orgs.find_one_and_update( | ||
{"_id": oid}, {"$set": {"slug": slug}} | ||
) | ||
# pylint: disable=broad-exception-caught | ||
except Exception as err: | ||
print(f"Error adding slug to org {oid}: {err}", flush=True) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
-r requirements.txt | ||
|
||
pytest | ||
requests |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
"""utils tests""" | ||
import pytest | ||
|
||
from btrixcloud.utils import slug_from_name | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"name,expected_slug", | ||
[ | ||
("Default org", "default-org"), | ||
("User's org", "users-org"), | ||
("User's @ org", "users-org"), | ||
("Org with åccénted charactêrs", "org-with-accented-characters"), | ||
("Org with åccénted! charactêrs@!", "org-with-accented-characters"), | ||
("cATs! 🐈🐈⬛", "cats"), | ||
], | ||
) | ||
def test_slug_from_name(name: str, expected_slug: str): | ||
assert slug_from_name(name) == expected_slug |