Skip to content

Commit

Permalink
Merge branch 'release/0.3.43' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
erikvw committed Jan 24, 2024
2 parents a105f83 + 2874c7b commit ed59c8b
Show file tree
Hide file tree
Showing 15 changed files with 259 additions and 211 deletions.
15 changes: 0 additions & 15 deletions edc_export/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import sys

from django.apps import AppConfig as DjangoApponfig
from django.conf import settings
from django.core.management import color_style

from edc_export.utils import get_export_folder, get_upload_folder
Expand Down Expand Up @@ -31,17 +30,3 @@ def ready(self):
f"See {self.name}.\n"
)
)


if settings.APP_NAME == "edc_export":
from dateutil.relativedelta import FR, MO, SA, SU, TH, TU, WE
from edc_facility.apps import AppConfig as BaseEdcFacilityAppConfig

class EdcFacilityAppConfig(BaseEdcFacilityAppConfig):
definitions = {
"7-day-clinic": dict(
days=[MO, TU, WE, TH, FR, SA, SU],
slots=[100, 100, 100, 100, 100, 100, 100],
),
"5-day-clinic": dict(days=[MO, TU, WE, TH, FR], slots=[100, 100, 100, 100, 100]),
}
7 changes: 0 additions & 7 deletions edc_export/models/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
import sys

from django.conf import settings

from .data_request import DataRequest
from .data_request_history import DataRequestHistory
from .edc_permissions import EdcPermissions
Expand All @@ -15,6 +11,3 @@
export_transaction_history_on_pre_delete,
)
from .upload_export_receipt_file import UploadExportReceiptFile

if settings.APP_NAME == "edc_export" and "makemigrations" not in sys.argv:
from ..tests import models # noqa
61 changes: 61 additions & 0 deletions edc_export/tests/create_crfs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import uuid

from edc_appointment.models import Appointment
from edc_utils import get_utcnow

from export_app.models import Crf, CrfOne, CrfThree, CrfTwo, ListModel, SubjectVisit

from .create_crfs_with_inlines import create_crf_with_inlines


def create_crfs(i) -> None:
j = 0
for appointment in Appointment.objects.all().order_by("timepoint", "visit_code_sequence"):
j += 1
if j == i:
break
SubjectVisit.objects.create(
appointment=appointment,
subject_identifier=appointment.subject_identifier,
report_datetime=get_utcnow(),
)
j = 0
for subject_visit in SubjectVisit.objects.all().order_by(
"appointment__subject_identifier",
"appointment__timepoint",
"appointment__visit_code_sequence",
):
j += 1
ListModel.objects.create(
display_name=(
f"thing_one_{subject_visit.subject_identifier}"
f"{subject_visit.appointment.visit_code}"
),
name=(
f"thing_one_{subject_visit.subject_identifier}"
f"{subject_visit.appointment.visit_code}"
),
)
ListModel.objects.create(
display_name=(
f"thing_two_{subject_visit.appointment.subject_identifier}"
f"{subject_visit.appointment.visit_code}"
),
name=(
f"thing_two_{subject_visit.appointment.subject_identifier}"
f"{subject_visit.appointment.visit_code}"
),
)
Crf.objects.create(
subject_visit=subject_visit,
char1=f"char{subject_visit.appointment.visit_code}",
date1=get_utcnow(),
int1=j,
uuid1=uuid.uuid4(),
)
CrfOne.objects.create(subject_visit=subject_visit, dte=get_utcnow())
CrfTwo.objects.create(subject_visit=subject_visit, dte=get_utcnow())
CrfThree.objects.create(subject_visit=subject_visit, UPPERCASE=get_utcnow())

for subject_visit in SubjectVisit.objects.all():
create_crf_with_inlines(subject_visit)
20 changes: 20 additions & 0 deletions edc_export/tests/create_crfs_with_inlines.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from edc_utils import get_utcnow

from export_app.models import CrfWithInline, ListOne, ListTwo


def create_crf_with_inlines(subject_visit):
list_one = ListOne.objects.create(
display_name=f"list_one{subject_visit.subject_identifier}{subject_visit.visit_code}",
name=f"list_one{subject_visit.subject_identifier}{subject_visit.visit_code}",
)
list_two = ListTwo.objects.create(
display_name=f"list_two{subject_visit.subject_identifier}{subject_visit.visit_code}",
name=f"list_two{subject_visit.subject_identifier}{subject_visit.visit_code}",
)
CrfWithInline.objects.create(
subject_visit=subject_visit,
list_one=list_one,
list_two=list_two,
dte=get_utcnow(),
)
133 changes: 12 additions & 121 deletions edc_export/tests/helper.py
Original file line number Diff line number Diff line change
@@ -1,128 +1,19 @@
import uuid
from __future__ import annotations

from edc_appointment.creators import UnscheduledAppointmentCreator
from edc_appointment.models import Appointment
from edc_consent.site_consents import AlreadyRegistered
from edc_consent.tests.consent_test_utils import consent_definition_factory
from edc_registration.models import RegisteredSubject
from edc_utils import get_utcnow
from edc_visit_schedule.site_visit_schedules import site_visit_schedules
from typing import TYPE_CHECKING

from .models import (
Crf,
CrfOne,
CrfThree,
CrfTwo,
CrfWithInline,
ListModel,
ListOne,
ListTwo,
SubjectConsent,
SubjectVisit,
)
from .visit_schedule import visit_schedule1
from edc_appointment.tests.helper import Helper as BaseHelper

from export_app.models import SubjectScreening

class Helper:
def __init__(self, now=None, subject_identifier=None):
site_visit_schedules._registry = {}
site_visit_schedules.register(visit_schedule1)
for schedule in visit_schedule1.schedules.values():
try:
consent_definition_factory(model=schedule.consent_model)
except AlreadyRegistered:
pass
if TYPE_CHECKING:
from edc_appointment.models import Appointment

self.now = now or get_utcnow()
self.subject_identifier = subject_identifier or uuid.uuid4().hex
self.consent_and_put_on_schedule()
self.subject_visit = None
self.thing_one = None
self.thing_two = None

def consent_and_put_on_schedule(self, subject_identifier=None):
subject_identifier = subject_identifier or self.subject_identifier
RegisteredSubject.objects.create(subject_identifier=self.subject_identifier)
subject_consent = SubjectConsent.objects.create(
subject_identifier=subject_identifier, consent_datetime=self.now
)
visit_schedule = site_visit_schedules.get_visit_schedule("visit_schedule1")
schedule = visit_schedule.schedules.get("schedule1")
schedule.put_on_schedule(
subject_identifier=subject_consent.subject_identifier,
onschedule_datetime=subject_consent.consent_datetime,
)
return subject_consent
class Helper(BaseHelper):
@property
def screening_model_cls(self):
return SubjectScreening

@staticmethod
def add_unscheduled_appointment(appointment=None):
creator = UnscheduledAppointmentCreator(
subject_identifier=appointment.subject_identifier,
visit_schedule_name=appointment.visit_schedule_name,
schedule_name=appointment.schedule_name,
visit_code=appointment.visit_code,
facility=appointment.facility,
suggested_visit_code_sequence=appointment.visit_code_sequence + 1,
)
return creator.appointment

def create_crfs(self, i):
for appointment in Appointment.objects.all().order_by(
"timepoint", "visit_code_sequence"
):
SubjectVisit.objects.create(
appointment=appointment,
subject_identifier=appointment.subject_identifier,
report_datetime=get_utcnow(),
)
for j in range(0, i - 1):
appointment = Appointment.objects.all().order_by(
"timepoint", "visit_code_sequence"
)[j]
self.subject_visit = SubjectVisit.objects.get(appointment=appointment)
self.thing_one = ListModel.objects.create(
display_name=f"thing_one_{appointment.visit_code}",
name=f"thing_one_{appointment.visit_code}",
)
self.thing_two = ListModel.objects.create(
display_name=f"thing_two_{appointment.visit_code}",
name=f"thing_two_{appointment.visit_code}",
)
Crf.objects.create(
subject_visit=self.subject_visit,
char1=f"char{appointment.visit_code}",
date1=get_utcnow(),
int1=j,
uuid1=uuid.uuid4(),
)
CrfOne.objects.create(subject_visit=self.subject_visit, dte=get_utcnow())
CrfTwo.objects.create(subject_visit=self.subject_visit, dte=get_utcnow())
CrfThree.objects.create(subject_visit=self.subject_visit, UPPERCASE=get_utcnow())

for i, appointment in enumerate(
Appointment.objects.all().order_by("timepoint", "visit_code_sequence")
):
if appointment != self.subject_visit.appointment:
self.create_crf_with_inlines(appointment)

@staticmethod
def create_crf_with_inlines(appointment):
# subject_visit = SubjectVisit.objects.create(
# appointment=appointment,
# subject_identifier=appointment.subject_identifier,
# report_datetime=get_utcnow(),
# )
list_one = ListOne.objects.create(
display_name=f"list_one{appointment.visit_code}",
name=f"list_one{appointment.visit_code}",
)
list_two = ListTwo.objects.create(
display_name=f"list_two{appointment.visit_code}",
name=f"list_two{appointment.visit_code}",
)
CrfWithInline.objects.create(
subject_visit=appointment.subjectvisit,
list_one=list_one,
list_two=list_two,
dte=get_utcnow(),
)
def create_unscheduled(self, appointment: Appointment):
return self.add_unscheduled_appointment(appointment)
5 changes: 1 addition & 4 deletions edc_export/tests/tests/test_archive_exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from django.contrib.sites.models import Site
from django.test import TestCase
from django.test.utils import override_settings, tag
from django.test.utils import override_settings
from edc_registration.models import RegisteredSubject
from edc_test_utils.get_user_for_tests import get_user_for_tests

Expand All @@ -14,8 +14,6 @@
@override_settings(EDC_EXPORT_EXPORT_FOLDER=mkdtemp(), EDC_EXPORT_UPLOAD_FOLDER=mkdtemp())
class TestArchiveExporter(TestCase):
def setUp(self):
# self.user = User.objects.create(username="erikvw")
# self.user.userprofile.sites.add(Site.objects.get(id=settings.SITE_ID))
self.user = get_user_for_tests(username="erikvw")
Site.objects.get_current()
RegisteredSubject.objects.create(subject_identifier="12345")
Expand All @@ -34,7 +32,6 @@ def test_request_archive_filename_exists(self):
self.assertIsNotNone(filename)
self.assertTrue(os.path.exists(filename), msg=f"file '{filename}' does not exist")

@tag("1")
def test_requested_with_invalid_table(self):
models = ["auth.blah", "edc_registration.registeredsubject"]
self.assertRaises(
Expand Down
22 changes: 18 additions & 4 deletions edc_export/tests/tests/test_export_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,35 @@
from django.core.exceptions import ObjectDoesNotExist
from django.test import TestCase, override_settings
from edc_appointment.models import Appointment
from edc_facility import import_holidays
from edc_pdutils.model_to_dataframe import ValueGetterInvalidLookup
from edc_utils import get_utcnow
from edc_visit_schedule.site_visit_schedules import site_visit_schedules

from edc_export.constants import EXPORTED, INSERT, UPDATE
from edc_export.model_exporter import ModelExporter
from edc_export.models import FileHistory, ObjectHistory
from export_app.models import Crf, CrfEncrypted, ListModel, SubjectVisit
from export_app.visit_schedule import visit_schedule1

from ..helper import Helper
from ..models import Crf, CrfEncrypted, ListModel, SubjectVisit


@override_settings(EDC_EXPORT_EXPORT_FOLDER=mkdtemp(), EDC_EXPORT_UPLOAD_FOLDER=mkdtemp())
class TestExportModel(TestCase):
helper_cls = Helper

def setUp(self):
self.helper = Helper()
import_holidays()
site_visit_schedules._registry = {}
site_visit_schedules.register(visit_schedule1)
for i in range(0, 7):
helper = self.helper_cls(subject_identifier=f"subject-{i}")
helper.consent_and_put_on_schedule(
visit_schedule_name=visit_schedule1.name,
schedule_name="schedule1",
report_datetime=get_utcnow(),
)
for appointment in Appointment.objects.all().order_by(
"timepoint", "visit_code_sequence"
):
Expand All @@ -44,7 +58,7 @@ def setUp(self):

def test_model(self):
ModelExporter(
model="edc_export.crf",
model="export_app.crf",
lookups={"subject_identifier": "subject_visit__subject_identifier"},
)

Expand All @@ -65,7 +79,7 @@ def test_export_file(self):
)
path = model_exporter.export()
self.assertTrue(os.path.exists(path))
self.assertIn("edc_export_crf_", path)
self.assertIn("export_app_crf_", path)

def test_field_names(self):
Crf.objects.all().delete()
Expand Down
Loading

0 comments on commit ed59c8b

Please sign in to comment.