Skip to content

Commit

Permalink
refactor: remove unused fields and refactor serializers
Browse files Browse the repository at this point in the history
  • Loading branch information
Hicham committed Jan 23, 2025
1 parent f357442 commit a65d9c3
Show file tree
Hide file tree
Showing 15 changed files with 220 additions and 190 deletions.
11 changes: 3 additions & 8 deletions accesses_perimeters/tests/resources/initial_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,12 @@
'name': 'Request 1',
'description': 'Description for Request 1',
'favorite': False,
'data_type_of_query': 'PATIENT',
'shared_by_id': None
}
]

request_query_snapshots_data = [
{
'title': 'Snapshot 1',
'owner_id': 1,
'serialized_query': '{"sourcePopulation": {"caresiteCohortList": ["1", "2", "3"]}}',
'translated_query': None,
Expand All @@ -95,33 +93,30 @@
'version': 1
},
{
'title': 'Snapshot 2',
'owner_id': 1,
'serialized_query': '{"sourcePopulation": {"caresiteCohortList": ["4", "5", "6"]}}',
'translated_query': None,
'previous_snapshot_id': None,
'shared_by_id': None,
'perimeters_ids': ['4', '5', '6'],
'version': 1
'version': 2
},
{
'title': 'Snapshot 3',
'owner_id': 1,
'serialized_query': '{"sourcePopulation": {"caresiteCohortList": ["7", "8", "9"]}}',
'translated_query': None,
'previous_snapshot_id': None,
'shared_by_id': None,
'perimeters_ids': ['7', '8', '9'],
'version': 1
'version': 3
},
{
'title': 'Snapshot 4',
'owner_id': 1,
'serialized_query': '{"sourcePopulation": {"caresiteCohortList": ["10", "11"]}}',
'translated_query': None,
'previous_snapshot_id': None,
'shared_by_id': None,
'perimeters_ids': ['10', '11'],
'version': 1
'version': 4
}
]
2 changes: 1 addition & 1 deletion accesses_perimeters/tests/test_perimeters_updater.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ def test_perimeters_created_from_existing_care_sites(self, mock_sql_query, mock_
mock_settings_2.ROOT_PERIMETER_ID = ROOT_PERIMETER_ID
perimeters_data_model_objects_update()

updated_query: RequestQuerySnapshot = RequestQuerySnapshot.objects.filter(title='Snapshot 2').first()
updated_query: RequestQuerySnapshot = RequestQuerySnapshot.objects.filter(version=2).first()
self.assertListEqual(updated_query.perimeters_ids, ['5', '6'])
self.assertEqual(updated_query.serialized_query, '{"sourcePopulation": {"caresiteCohortList": ["5", "6"]}}')
count_created_perimeters = Perimeter.objects.count()
Expand Down
1 change: 0 additions & 1 deletion admin_cohort/services/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,6 @@ def authenticate_token(self, token: str, auth_method: str, headers: Dict[str, st
return None
try:
authenticator = self._get_authenticator(auth_method)
_logger.info(f"Authenticating token: {token}")
username = authenticator.authenticate(token=token)
user = User.objects.get(username=username)
for post_auth_hook in self.post_auth_hooks:
Expand Down
30 changes: 30 additions & 0 deletions cohort/migrations/0017_remove_cohortresult_type_and_more.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Generated by Django 5.0.10 on 2025-01-23 13:54

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('cohort', '0016_fhirfilter_auto_generated_and_more'),
]

operations = [
migrations.RemoveField(
model_name='cohortresult',
name='type',
),
migrations.RemoveField(
model_name='request',
name='data_type_of_query',
),
migrations.RemoveField(
model_name='requestquerysnapshot',
name='title',
),
migrations.AddField(
model_name='folder',
name='description',
field=models.TextField(blank=True, null=True),
),
]
16 changes: 8 additions & 8 deletions cohort/models/cohort_result.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,6 @@
from admin_cohort.models import JobModel, User
from cohort.models import CohortBaseModel, RequestQuerySnapshot, DatedMeasure

COHORT_TYPES = [("IMPORT_I2B2", "Previous cohorts imported from i2b2."),
("MY_ORGANIZATIONS", "Organizations in which I work (care sites with pseudo-anonymised reading rights)."),
("MY_PATIENTS", "Patients that passed by all my organizations (care sites with nominative reading rights)."),
("MY_COHORTS", "Cohorts I created in Cohort360")]

MY_COHORTS_TYPE = COHORT_TYPES[3][0]


class CohortResult(CohortBaseModel, JobModel):
owner = models.ForeignKey(User, on_delete=models.CASCADE, related_name='user_cohorts')
Expand All @@ -24,13 +17,20 @@ class CohortResult(CohortBaseModel, JobModel):
dated_measure = models.ForeignKey(DatedMeasure, related_name="cohorts", on_delete=models.CASCADE, null=True)
dated_measure_global = models.ForeignKey(DatedMeasure, related_name="global_cohorts", null=True, on_delete=models.SET_NULL)
create_task_id = models.TextField(blank=True)
type = models.CharField(max_length=20, choices=COHORT_TYPES, default=MY_COHORTS_TYPE)
is_subset = models.BooleanField(default=False)

@property
def result_size(self) -> int:
return self.dated_measure.measure

@property
def measure_min(self) -> int:
return self.dated_measure_global and self.dated_measure_global.measure_min

@property
def measure_max(self) -> int:
return self.dated_measure_global and self.dated_measure_global.measure_max

@property
def exportable(self) -> bool:
cohort_size = self.result_size
Expand Down
1 change: 1 addition & 0 deletions cohort/models/folder.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
class Folder(CohortBaseModel):
owner = models.ForeignKey(User, on_delete=models.CASCADE, related_name='folders')
name = models.CharField(max_length=50)
description = models.TextField(blank=True, null=True)

def __str__(self):
return f"{self.name} ({self.owner})"
Expand Down
5 changes: 0 additions & 5 deletions cohort/models/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,13 @@
from admin_cohort.models import User
from cohort.models import Folder, CohortBaseModel

REQUEST_DATA_TYPES = [('PATIENT', 'FHIR Patient'),
('ENCOUNTER', 'FHIR Encounter')]
PATIENT_DATA_TYPE = REQUEST_DATA_TYPES[0][0]


class Request(CohortBaseModel):
owner = models.ForeignKey(User, on_delete=models.CASCADE, related_name='user_requests')
name = models.CharField(max_length=255)
description = models.TextField(blank=True)
favorite = models.BooleanField(default=False)
parent_folder = models.ForeignKey(Folder, on_delete=models.CASCADE, related_name="requests", null=False)
data_type_of_query = models.CharField(max_length=9, choices=REQUEST_DATA_TYPES, default=PATIENT_DATA_TYPE)
shared_by = models.ForeignKey(User, on_delete=models.SET_NULL, related_name='shared_requests', null=True, default=None)

@property
Expand Down
7 changes: 1 addition & 6 deletions cohort/models/request_query_snapshot.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ def get_queryset(self):


class RequestQuerySnapshot(CohortBaseModel):
title = models.CharField(default="", max_length=50)
owner = models.ForeignKey(User, on_delete=models.CASCADE, related_name='user_request_query_snapshots')
request = models.ForeignKey(Request, on_delete=models.CASCADE, related_name='query_snapshots')
serialized_query = models.TextField(default="{}")
Expand All @@ -29,13 +28,9 @@ class RequestQuerySnapshot(CohortBaseModel):

objects = RequestQuerySnapshotManager()

@property
def has_linked_cohorts(self) -> bool:
return self.cohort_results.exists()

def save(self, *args, **kwargs):
try:
json.loads(str(self.serialized_query))
except json.decoder.JSONDecodeError as e:
raise ValueError(f"serialized_query is not a valid JSON {e}")
super(RequestQuerySnapshot, self).save(*args, **kwargs)
super().save(*args, **kwargs)
Loading

0 comments on commit a65d9c3

Please sign in to comment.