Skip to content
Snippets Groups Projects
Commit 36423572 authored by Piotr Gawron's avatar Piotr Gawron
Browse files

study introduced as a concept - every StudySubject should be assigned to study

parent 61a73e91
No related branches found
No related tags found
1 merge request!101Resolve "list of subjects should contain columns dependent on the study"
# -*- coding: utf-8 -*-
# Generated by Django 1.10.7 on 2017-11-30 16:07
from __future__ import unicode_literals
import django.db.models.deletion
from django.db import migrations, models
# noinspection PyUnusedLocal
# noinspection PyPep8Naming
def create_default_study(apps, schema_editor):
# We can't import the Study model directly as it may be a newer
# version than this migration expects. We use the historical version.
Study = apps.get_model("web", "Study")
study = Study.objects.create()
study.name = "New study"
study.save()
class Migration(migrations.Migration):
dependencies = [
('web', '0070_auto_20171128_1124'),
]
operations = [
migrations.CreateModel(
name='Study',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(max_length=255, verbose_name=b'Name')),
],
),
migrations.AlterField(
model_name='studysubject',
name='subject',
field=models.ForeignKey(editable=False, on_delete=django.db.models.deletion.CASCADE, to='web.Subject',
verbose_name=b'Subject'),
),
migrations.AddField(
model_name='studysubject',
name='study',
field=models.ForeignKey(editable=False, null=True, on_delete=django.db.models.deletion.CASCADE,
to='web.Study', verbose_name=b'Study'),
),
migrations.RunPython(create_default_study),
migrations.AlterField(
model_name='studysubject',
name='study',
field=models.ForeignKey(default=1, editable=False, on_delete=django.db.models.deletion.CASCADE, to='web.Study', verbose_name=b'Study'),
preserve_default=False,
),
]
...@@ -8,6 +8,7 @@ from flying_team import FlyingTeam ...@@ -8,6 +8,7 @@ from flying_team import FlyingTeam
from location import Location from location import Location
from appointment_type_link import AppointmentTypeLink from appointment_type_link import AppointmentTypeLink
from country import Country from country import Country
from study import Study
from room import Room from room import Room
from visit import Visit from visit import Visit
from worker import Worker from worker import Worker
...@@ -25,6 +26,6 @@ from missing_subject import MissingSubject ...@@ -25,6 +26,6 @@ from missing_subject import MissingSubject
from inconsistent_subject import InconsistentSubject, InconsistentField from inconsistent_subject import InconsistentSubject, InconsistentField
__all__ = [FlyingTeam, Appointment, AppointmentType, Availability, Holiday, Item, Language, Location, Room, Subject, StudySubject, __all__ = [Study, FlyingTeam, Appointment, AppointmentType, Availability, Holiday, Item, Language, Location, Room, Subject, StudySubject,
Visit, Worker, ContactAttempt, ConfigurationItem, MailTemplate, AppointmentTypeLink, MissingSubject, Visit, Worker, ContactAttempt, ConfigurationItem, MailTemplate, AppointmentTypeLink, MissingSubject,
InconsistentSubject, InconsistentField, Country] InconsistentSubject, InconsistentField, Country]
...@@ -76,4 +76,8 @@ REDCAP_TOKEN_CONFIGURATION_TYPE = "REDCAP_TOKEN_CONFIGURATION_TYPE" ...@@ -76,4 +76,8 @@ REDCAP_TOKEN_CONFIGURATION_TYPE = "REDCAP_TOKEN_CONFIGURATION_TYPE"
REDCAP_BASE_URL_CONFIGURATION_TYPE = "REDCAP_BASE_URL_CONFIGURATION_TYPE" REDCAP_BASE_URL_CONFIGURATION_TYPE = "REDCAP_BASE_URL_CONFIGURATION_TYPE"
COUNTRY_OTHER_ID = 1 COUNTRY_OTHER_ID = 1
COUNTRY_AFGHANISTAN_ID = 2 COUNTRY_AFGHANISTAN_ID = 2
\ No newline at end of file
# id of the singleton Study,
# TODO remove after allowing many studies per Smasch instance
GLOBAL_STUDY_ID = 1
# coding=utf-8
from django.db import models
class Study(models.Model):
class Meta:
app_label = 'web'
name = models.CharField(max_length=255, verbose_name='Name')
def __str__(self):
return "%s" % self.name
def __unicode__(self):
return "%s" % self.name
...@@ -33,6 +33,12 @@ class StudySubject(models.Model): ...@@ -33,6 +33,12 @@ class StudySubject(models.Model):
null=False, null=False,
) )
study = models.ForeignKey("web.Study",
verbose_name='Study',
editable=False,
null=False,
)
postponed = models.BooleanField(choices=BOOL_CHOICES, postponed = models.BooleanField(choices=BOOL_CHOICES,
verbose_name='Postponed', verbose_name='Postponed',
default=False default=False
......
...@@ -3,7 +3,7 @@ import logging ...@@ -3,7 +3,7 @@ import logging
from web.forms import StudySubjectAddForm, get_new_screening_number from web.forms import StudySubjectAddForm, get_new_screening_number
from web.models.constants import SUBJECT_TYPE_CHOICES_CONTROL from web.models.constants import SUBJECT_TYPE_CHOICES_CONTROL
from web.tests import LoggedInWithWorkerTestCase from web.tests import LoggedInWithWorkerTestCase
from web.tests.functions import create_study_subject, create_subject from web.tests.functions import create_study_subject, create_subject, get_test_study
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
...@@ -14,6 +14,7 @@ class StudySubjectAddFormTests(LoggedInWithWorkerTestCase): ...@@ -14,6 +14,7 @@ class StudySubjectAddFormTests(LoggedInWithWorkerTestCase):
location = self.worker.locations.all()[0] location = self.worker.locations.all()[0]
self.subject = create_subject() self.subject = create_subject()
self.study = get_test_study()
self.sample_data = { self.sample_data = {
'type': SUBJECT_TYPE_CHOICES_CONTROL, 'type': SUBJECT_TYPE_CHOICES_CONTROL,
'default_location': location.id, 'default_location': location.id,
...@@ -33,6 +34,7 @@ class StudySubjectAddFormTests(LoggedInWithWorkerTestCase): ...@@ -33,6 +34,7 @@ class StudySubjectAddFormTests(LoggedInWithWorkerTestCase):
form = StudySubjectAddForm(data=form_data, user=self.user) form = StudySubjectAddForm(data=form_data, user=self.user)
form.is_valid() form.is_valid()
form.instance.subject_id = self.subject.id form.instance.subject_id = self.subject.id
form.instance.study_id = self.study.id
self.assertTrue(form.is_valid()) self.assertTrue(form.is_valid())
self.assertIsNone(form.fields['year_of_diagnosis'].initial) self.assertIsNone(form.fields['year_of_diagnosis'].initial)
form.save() form.save()
...@@ -50,6 +52,7 @@ class StudySubjectAddFormTests(LoggedInWithWorkerTestCase): ...@@ -50,6 +52,7 @@ class StudySubjectAddFormTests(LoggedInWithWorkerTestCase):
form.is_valid() form.is_valid()
self.assertTrue(form.is_valid()) self.assertTrue(form.is_valid())
form.instance.subject_id = self.subject.id form.instance.subject_id = self.subject.id
form.instance.study_id = self.study.id
form.save() form.save()
form_data['screening_number'] = "2" form_data['screening_number'] = "2"
...@@ -66,6 +69,7 @@ class StudySubjectAddFormTests(LoggedInWithWorkerTestCase): ...@@ -66,6 +69,7 @@ class StudySubjectAddFormTests(LoggedInWithWorkerTestCase):
form.is_valid() form.is_valid()
self.assertTrue(form.is_valid()) self.assertTrue(form.is_valid())
form.instance.subject_id = self.subject.id form.instance.subject_id = self.subject.id
form.instance.study_id = self.study.id
form.save() form.save()
form_data['screening_number'] = "2" form_data['screening_number'] = "2"
......
...@@ -4,7 +4,7 @@ import os ...@@ -4,7 +4,7 @@ import os
from django.contrib.auth.models import User from django.contrib.auth.models import User
from web.models import Location, AppointmentType, StudySubject, Worker, Visit, Appointment, ConfigurationItem, Language, \ from web.models import Location, AppointmentType, StudySubject, Worker, Visit, Appointment, ConfigurationItem, Language, \
ContactAttempt, FlyingTeam, Availability, Subject ContactAttempt, FlyingTeam, Availability, Subject, Study
from web.models.constants import REDCAP_TOKEN_CONFIGURATION_TYPE, REDCAP_BASE_URL_CONFIGURATION_TYPE, \ from web.models.constants import REDCAP_TOKEN_CONFIGURATION_TYPE, REDCAP_BASE_URL_CONFIGURATION_TYPE, \
SEX_CHOICES_MALE, SUBJECT_TYPE_CHOICES_CONTROL, CONTACT_TYPES_PHONE, \ SEX_CHOICES_MALE, SUBJECT_TYPE_CHOICES_CONTROL, CONTACT_TYPES_PHONE, \
MONDAY_AS_DAY_OF_WEEK, COUNTRY_AFGHANISTAN_ID MONDAY_AS_DAY_OF_WEEK, COUNTRY_AFGHANISTAN_ID
...@@ -23,6 +23,10 @@ def create_location(name="test"): ...@@ -23,6 +23,10 @@ def create_location(name="test"):
return Location.objects.create(name=name) return Location.objects.create(name=name)
def create_study(name="test"):
return Study.objects.create(name=name)
def get_test_location(): def get_test_location():
locations = Location.objects.filter(name="test") locations = Location.objects.filter(name="test")
if len(locations) > 0: if len(locations) > 0:
...@@ -31,6 +35,14 @@ def get_test_location(): ...@@ -31,6 +35,14 @@ def get_test_location():
return create_location() return create_location()
def get_test_study():
locations = Study.objects.filter(name="test-study")
if len(locations) > 0:
return locations[0]
else:
return create_study("test-study")
def create_appointment_type(): def create_appointment_type():
return AppointmentType.objects.create( return AppointmentType.objects.create(
code="C", code="C",
...@@ -70,6 +82,7 @@ def create_study_subject(subject_id=1, subject=None): ...@@ -70,6 +82,7 @@ def create_study_subject(subject_id=1, subject=None):
default_location=get_test_location(), default_location=get_test_location(),
type=SUBJECT_TYPE_CHOICES_CONTROL, type=SUBJECT_TYPE_CHOICES_CONTROL,
screening_number="piotr's number" + str(subject_id), screening_number="piotr's number" + str(subject_id),
study=get_test_study(),
subject=subject subject=subject
) )
......
import logging
from django.test import TestCase
from web.tests.functions import create_study
logger = logging.getLogger(__name__)
class StudyTests(TestCase):
def test_image_img(self):
study = create_study()
self.assertTrue(study.name in str(study))
...@@ -4,6 +4,7 @@ import logging ...@@ -4,6 +4,7 @@ import logging
from django.contrib import messages from django.contrib import messages
from django.shortcuts import redirect, get_object_or_404 from django.shortcuts import redirect, get_object_or_404
from ..models.constants import GLOBAL_STUDY_ID
from . import wrap_response from . import wrap_response
from ..forms import StudySubjectAddForm, StudySubjectEditForm, VisitDetailForm, SubjectEditForm, SubjectAddForm from ..forms import StudySubjectAddForm, StudySubjectEditForm, VisitDetailForm, SubjectEditForm, SubjectAddForm
from ..models import StudySubject, MailTemplate, Worker from ..models import StudySubject, MailTemplate, Worker
...@@ -31,6 +32,7 @@ def subject_add(request): ...@@ -31,6 +32,7 @@ def subject_add(request):
if study_subject_form.is_valid() and subject_form.is_valid(): if study_subject_form.is_valid() and subject_form.is_valid():
subject = subject_form.save() subject = subject_form.save()
study_subject_form.instance.subject_id = subject.id study_subject_form.instance.subject_id = subject.id
study_subject_form.instance.study_id = GLOBAL_STUDY_ID
study_subject_form.save() study_subject_form.save()
messages.add_message(request, messages.SUCCESS, 'Subject created') messages.add_message(request, messages.SUCCESS, 'Subject created')
return redirect('web.views.subject_edit', id=study_subject_form.instance.id) return redirect('web.views.subject_edit', id=study_subject_form.instance.id)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment