From 0eb72913ee0c2cd1dae99d9a93c57367d49038b7 Mon Sep 17 00:00:00 2001 From: erikvw Date: Tue, 12 Dec 2023 14:29:16 -0600 Subject: [PATCH 1/2] update import path from edc_sites, fix signature for add_or_update_django_sites --- edc_registration/admin.py | 4 ++-- edc_registration/auths.py | 4 +++- .../0028_alter_registeredsubject_managers.py | 5 +++-- edc_registration/tests/test_registration.py | 6 +++--- edc_registration/utils.py | 19 +++++++++++++++---- 5 files changed, 26 insertions(+), 12 deletions(-) diff --git a/edc_registration/admin.py b/edc_registration/admin.py index ea7b930..91bc831 100644 --- a/edc_registration/admin.py +++ b/edc_registration/admin.py @@ -1,7 +1,7 @@ from django.contrib import admin from django_audit_fields.admin import audit_fieldset_tuple -from edc_data_manager.modeladmin_mixins import DataManagerSiteModelAdminMixin from edc_model_admin.history import SimpleHistoryAdmin +from edc_sites.admin import SiteModelAdminMixin from .admin_site import edc_registration_admin from .modeladmin_mixins import RegisteredSubjectModelAdminMixin @@ -10,7 +10,7 @@ @admin.register(get_registered_subject_model_cls(), site=edc_registration_admin) class RegisteredSubjectAdmin( - DataManagerSiteModelAdminMixin, RegisteredSubjectModelAdminMixin, SimpleHistoryAdmin + SiteModelAdminMixin, RegisteredSubjectModelAdminMixin, SimpleHistoryAdmin ): ordering = ("subject_identifier",) diff --git a/edc_registration/auths.py b/edc_registration/auths.py index 513a75b..5150613 100644 --- a/edc_registration/auths.py +++ b/edc_registration/auths.py @@ -1,8 +1,10 @@ +from django.apps import apps as django_apps from edc_auth.auth_objects import PII, PII_VIEW from edc_auth.site_auths import site_auths from edc_export.auth_objects import EXPORT -site_auths.update_group("edc_registration.export_registeredsubject", name=EXPORT) +if django_apps.is_installed("edc_export"): + site_auths.update_group("edc_registration.export_registeredsubject", name=EXPORT) site_auths.update_group( "edc_registration.display_dob", "edc_registration.display_firstname", diff --git a/edc_registration/migrations/0028_alter_registeredsubject_managers.py b/edc_registration/migrations/0028_alter_registeredsubject_managers.py index f88ceb1..a3d5a1a 100644 --- a/edc_registration/migrations/0028_alter_registeredsubject_managers.py +++ b/edc_registration/migrations/0028_alter_registeredsubject_managers.py @@ -1,8 +1,9 @@ # Generated by Django 4.2.1 on 2023-07-07 19:32 +import edc_sites.models from django.db import migrations + import edc_registration.models.managers -import edc_sites.model_mixins class Migration(migrations.Migration): @@ -18,7 +19,7 @@ class Migration(migrations.Migration): "objects", edc_registration.models.managers.RegisteredSubjectManager(), ), - ("on_site", edc_sites.model_mixins.CurrentSiteManager()), + ("on_site", edc_sites.models.CurrentSiteManager()), ], ), ] diff --git a/edc_registration/tests/test_registration.py b/edc_registration/tests/test_registration.py index 50e082c..e76e999 100644 --- a/edc_registration/tests/test_registration.py +++ b/edc_registration/tests/test_registration.py @@ -2,8 +2,8 @@ from django.core.exceptions import ObjectDoesNotExist from django.test import TestCase from django.test.utils import override_settings -from edc_sites import add_or_update_django_sites from edc_sites.tests import SiteTestCaseMixin +from edc_sites.utils import add_or_update_django_sites from edc_utils import get_utcnow from multisite import SiteID @@ -112,7 +112,7 @@ def test_cannot_change_subject_identifier(self): @override_settings(SITE_ID=SiteID(10)) def test_site1(self): - add_or_update_django_sites(sites=self.default_sites, verbose=False) + add_or_update_django_sites(single_sites=self.default_sites, verbose=False) obj = SubjectModelOne.objects.create(screening_identifier="12345") rs = RegisteredSubject.objects.get( registration_identifier=obj.to_string(obj.registration_identifier) @@ -121,7 +121,7 @@ def test_site1(self): @override_settings(SITE_ID=SiteID(20)) def test_site2(self): - add_or_update_django_sites(sites=self.default_sites, verbose=False) + add_or_update_django_sites(single_sites=self.default_sites, verbose=False) obj = SubjectModelOne.objects.create(screening_identifier="12345") rs = RegisteredSubject.objects.get( registration_identifier=obj.to_string(obj.registration_identifier) diff --git a/edc_registration/utils.py b/edc_registration/utils.py index 169e7d5..4221849 100644 --- a/edc_registration/utils.py +++ b/edc_registration/utils.py @@ -10,6 +10,10 @@ from edc_registration.models import RegisteredSubject +class RegisteredSubjectDoesNotExist(Exception): + pass + + def get_registered_subject_model_name() -> str: return getattr( settings, @@ -22,11 +26,18 @@ def get_registered_subject_model_cls() -> RegisteredSubject: return django_apps.get_model(get_registered_subject_model_name()) -def get_registered_subject(subject_identifier) -> RegisteredSubject: +def get_registered_subject( + subject_identifier, raise_exception: bool | None = None, **kwargs +) -> RegisteredSubject | None: + opts = dict(subject_identifier=subject_identifier, **kwargs) try: - registered_subject = get_registered_subject_model_cls().objects.get( - subject_identifier=subject_identifier - ) + registered_subject = get_registered_subject_model_cls().objects.get(**opts) except ObjectDoesNotExist: registered_subject = None + if raise_exception and not registered_subject: + raise RegisteredSubjectDoesNotExist( + "Unknown subject. " + f"Searched `{get_registered_subject_model_cls()._meta.label_lower}`. " + f"Got {opts}." + ) return registered_subject From 1c545afdf936ac7a78cbb8be38e5102a24840ec2 Mon Sep 17 00:00:00 2001 From: erikvw Date: Wed, 13 Dec 2023 09:45:26 -0600 Subject: [PATCH 2/2] test setup --- .github/workflows/build.yml | 10 ++++++++-- pyproject.toml | 12 ++++++++---- runtests.py | 24 +++++++----------------- 3 files changed, 23 insertions(+), 23 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 39f9299..4ec2681 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -11,8 +11,14 @@ jobs: strategy: fail-fast: false matrix: - python-version: ['3.11'] - django-version: ['4.2', 'dev'] + python-version: ['3.11', '3.12'] + django-version: ['4.2', '5.0', 'dev'] + + exclude: + - python-version: '3.12' + django-version: '4.2' + - python-version: '3.11' + django-version: 'dev' services: mysql: image: mysql:latest diff --git a/pyproject.toml b/pyproject.toml index 88676a4..9afec50 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -35,19 +35,22 @@ exclude_lines = [ legacy_tox_ini = """ [tox] envlist = - py{311}-dj{41,42,dev}, + py{311}-dj{42,50}, + py{312}-dj{50,dev}, lint isolated_build = true [gh-actions] python = - 3.11: py311, lint + 3.11: py311 + 3.12: py312, lint [gh-actions:env] DJANGO = - 4.2: dj42, lint - dev: djdev + 4.2: dj42 + 5.0: dj50 + dev: djdev, lint [testenv] deps = @@ -56,6 +59,7 @@ deps = -r https://raw.githubusercontent.com/clinicedc/edc/develop/requirements.tests/edc.txt -r https://raw.githubusercontent.com/clinicedc/edc/develop/requirements.tests/third_party_dev.txt dj42: Django>=4.2,<5.0 + dj50: Django>=5.0 djdev: https://github.com/django/django/tarball/main commands = diff --git a/runtests.py b/runtests.py index 21b7d82..595fb7f 100644 --- a/runtests.py +++ b/runtests.py @@ -1,22 +1,18 @@ #!/usr/bin/env python import logging -import sys -from os.path import abspath, dirname, join +from pathlib import Path -import django -from django.conf import settings -from django.test.runner import DiscoverRunner -from edc_test_utils import DefaultTestSettings +from edc_test_utils import DefaultTestSettings, func_main -base_dir = dirname(abspath(__file__)) app_name = "edc_registration" +base_dir = Path(__file__).absolute().parent -DEFAULT_SETTINGS = DefaultTestSettings( +project_settings = DefaultTestSettings( calling_file=__file__, BASE_DIR=base_dir, APP_NAME=app_name, SUBJECT_VISIT_MODEL="edc_visit_tracking.subjectvisit", - ETC_DIR=join(base_dir, app_name, "tests", "etc"), + ETC_DIR=str(base_dir / app_name / "tests" / "etc"), INSTALLED_APPS=[ "django.contrib.admin", "django.contrib.auth", @@ -40,19 +36,13 @@ "edc_visit_tracking.apps.AppConfig", "edc_registration.apps.AppConfig", ], - RANDOMIZATION_LIST_PATH=join(base_dir, app_name, "tests", "test_randomization_list.csv"), + RANDOMIZATION_LIST_PATH=str(base_dir / app_name / "tests" / "test_randomization_list.csv"), add_dashboard_middleware=True, ).settings def main(): - if not settings.configured: - settings.configure(**DEFAULT_SETTINGS) - django.setup() - tags = [t.split("=")[1] for t in sys.argv if t.startswith("--tag")] - failfast = any([True for t in sys.argv if t.startswith("--failfast")]) - failures = DiscoverRunner(failfast=failfast, tags=tags).run_tests([f"{app_name}.tests"]) - sys.exit(failures) + func_main(project_settings, *[f"{app_name}.tests"]) if __name__ == "__main__":