From 864ebae6dea9bb960c171cfe572c1ca9e816aa1b Mon Sep 17 00:00:00 2001 From: Mark Powers Date: Mon, 11 Nov 2024 14:52:02 -0600 Subject: [PATCH] Fix slug generation Slugs are intended to be human-readable and unique for an artifact. However when a user submitted multiple versions they often got duplicated slugs. This was because our check for an existing version was based on the artifacts date, not the version date. We attempted to fix in #133, but the issue persisted. I just happened to notice this filter code seemed wrong. --- trovi/models.py | 2 +- trovi/settings.py | 4 +++- util/test.py | 2 ++ 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/trovi/models.py b/trovi/models.py index c997e6f..59736c0 100644 --- a/trovi/models.py +++ b/trovi/models.py @@ -300,7 +300,7 @@ def generate_slug(instance: "ArtifactVersion", created: bool = False, **_): with transaction.atomic(): if instance.artifact: versions_today_query = instance.artifact.versions.filter( - artifact__created_at__date=instance.created_at.date(), + created_at__date=instance.created_at.date(), ).select_for_update() versions_today = versions_today_query.exclude( slug__exact="" diff --git a/trovi/settings.py b/trovi/settings.py index 63a0628..5a5f581 100644 --- a/trovi/settings.py +++ b/trovi/settings.py @@ -14,6 +14,7 @@ from pathlib import Path # Build paths inside the project like this: BASE_DIR / 'subdir'. +import sys from urllib.parse import urlparse BASE_DIR = Path(__file__).resolve().parent.parent @@ -191,8 +192,9 @@ # Database # https://docs.djangoproject.com/en/3.2/ref/settings/#databases +TESTING = "test" in sys.argv or "test_coverage" in sys.argv -if os.getenv("DB_ENGINE"): +if not TESTING and os.getenv("DB_ENGINE"): DATABASES = { "default": { "ENGINE": os.getenv("DB_ENGINE"), diff --git a/util/test.py b/util/test.py index 56e54c7..cda886c 100644 --- a/util/test.py +++ b/util/test.py @@ -160,6 +160,8 @@ def fake_tag() -> str: is_reproducible=True, repro_access_hours=3, ) +artifact_don_quixote.created_at -= datetime.timedelta(days=2) + version_don_quixote_1 = ArtifactVersion( artifact=artifact_don_quixote, contents_urn=f"urn:trovi:contents:chameleon:{uuid4()}",