forked from mealie-recipes/mealie
-
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.
feat: Improve Public URL Readability (mealie-recipes#2482)
* added support for group slugs * modified frontend to use links with group slug * fixed test refs * unused import --------- Co-authored-by: Hayden <[email protected]>
- Loading branch information
1 parent
99372aa
commit 095edef
Showing
12 changed files
with
166 additions
and
18 deletions.
There are no files selected for viewing
56 changes: 56 additions & 0 deletions
56
alembic/versions/2023-08-06-21.00.34_04ac51cbe9a4_added_group_slug.py
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,56 @@ | ||
"""added group slug | ||
Revision ID: 04ac51cbe9a4 | ||
Revises: b3dbb554ba53 | ||
Create Date: 2023-08-06 21:00:34.582905 | ||
""" | ||
import sqlalchemy as sa | ||
from slugify import slugify | ||
from sqlalchemy.orm import Session | ||
|
||
from alembic import op | ||
from mealie.db.models.group.group import Group | ||
|
||
# revision identifiers, used by Alembic. | ||
revision = "04ac51cbe9a4" | ||
down_revision = "b3dbb554ba53" | ||
branch_labels = None | ||
depends_on = None | ||
|
||
|
||
def populate_group_slugs(session: Session): | ||
groups: list[Group] = session.query(Group).all() | ||
seen_slugs: set[str] = set() | ||
for group in groups: | ||
original_name = group.name | ||
attempts = 0 | ||
while True: | ||
slug = slugify(group.name) | ||
if slug not in seen_slugs: | ||
break | ||
|
||
attempts += 1 | ||
group.name = f"{original_name} ({attempts})" | ||
|
||
seen_slugs.add(slug) | ||
group.slug = slug | ||
|
||
session.commit() | ||
|
||
|
||
def upgrade(): | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
op.add_column("groups", sa.Column("slug", sa.String(), nullable=True)) | ||
op.create_index(op.f("ix_groups_slug"), "groups", ["slug"], unique=True) | ||
# ### end Alembic commands ### | ||
|
||
session = Session(bind=op.get_bind()) | ||
populate_group_slugs(session) | ||
|
||
|
||
def downgrade(): | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
op.drop_index(op.f("ix_groups_slug"), table_name="groups") | ||
op.drop_column("groups", "slug") | ||
# ### end Alembic commands ### |
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
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
24 changes: 24 additions & 0 deletions
24
tests/unit_tests/repository_tests/test_group_repository.py
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,24 @@ | ||
from mealie.repos.repository_factory import AllRepositories | ||
from tests.utils.factories import random_int, random_string | ||
|
||
|
||
def test_group_resolve_similar_names(database: AllRepositories): | ||
base_group_name = random_string() | ||
groups = database.groups.create_many({"name": base_group_name} for _ in range(random_int(3, 10))) | ||
|
||
seen_names = set() | ||
seen_slugs = set() | ||
for group in groups: | ||
assert group.name not in seen_names | ||
assert group.slug not in seen_slugs | ||
seen_names.add(group.name) | ||
seen_slugs.add(group.slug) | ||
|
||
assert base_group_name in group.name | ||
|
||
|
||
def test_group_get_by_slug_or_id(database: AllRepositories): | ||
groups = [database.groups.create({"name": random_string()}) for _ in range(random_int(3, 10))] | ||
for group in groups: | ||
assert database.groups.get_by_slug_or_id(group.id) == group | ||
assert database.groups.get_by_slug_or_id(group.slug) == group |
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