Skip to content

Commit

Permalink
feat: naive cohort support
Browse files Browse the repository at this point in the history
  • Loading branch information
alubbock committed Jul 4, 2024
1 parent f60dc75 commit f8c9517
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 2 deletions.
18 changes: 18 additions & 0 deletions backend/antigenapi/migrations/0009_naive_cohorts.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 4.2.6 on 2024-07-04 10:36

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("antigenapi", "0008_seq_run_well_offset"),
]

operations = [
migrations.AddField(
model_name="cohort",
name="is_naive",
field=models.BooleanField(default=False),
),
]
6 changes: 6 additions & 0 deletions backend/antigenapi/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from django.db.models import (
CASCADE,
PROTECT,
BooleanField,
FileField,
ForeignKey,
IntegerChoices,
Expand Down Expand Up @@ -79,6 +80,7 @@ class Cohort(Model):

cohort_num = PositiveIntegerField(unique=True)
llama = ForeignKey(Llama, on_delete=PROTECT)
is_naive = BooleanField(default=False)
immunisation_date = DateField(null=True)
blood_draw_date = DateField(null=True)
projects = ManyToManyField(
Expand All @@ -95,6 +97,10 @@ class Cohort(Model):
def __str__(self): # noqa: D105
return f"Cohort No. {self.cohort_num}"

def cohort_num_prefixed(self):
"""Cohort number with 'N' prefix if naive."""
return f"N{self.cohort_num}" if self.is_naive else f"{self.cohort_num}"


class Library(Model):
"""Library model."""
Expand Down
12 changes: 12 additions & 0 deletions backend/antigenapi/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from rest_framework.decorators import action
from rest_framework.response import Response
from rest_framework.serializers import (
BooleanField,
CharField,
FileField,
ModelSerializer,
Expand Down Expand Up @@ -161,6 +162,10 @@ class LibrarySerializer(ModelSerializer):
added_by = StringRelatedField()
# llama_name = CharField(source='llama.name', read_only=True)
cohort_cohort_num = CharField(source="cohort.cohort_num", read_only=True)
cohort_is_naive = BooleanField(source="cohort.is_naive", read_only=True)
cohort_cohort_num_prefixed = CharField(
source="cohort.cohort_num_prefixed", read_only=True
)
project_short_title = CharField(source="project.short_title", read_only=True)

class Meta: # noqa: D106
Expand Down Expand Up @@ -268,6 +273,7 @@ class CohortSerializer(ModelSerializer):
llama_name = CharField(source="llama.name", read_only=True)
antigen_details = AntigenSerializer(source="antigens", many=True, read_only=True)
# project_short_title = CharField(source='project.short_title', read_only=True)
cohort_num_prefixed = CharField(read_only=True)

class Meta: # noqa: D106
model = Cohort
Expand Down Expand Up @@ -316,6 +322,12 @@ class ElisaPlateSerializer(ModelSerializer):
library_cohort_cohort_num = CharField(
source="library.cohort.cohort_num", required=False
)
library_cohort_cohort_num_prefixed = CharField(
source="library.cohort.cohort_num_prefixed", required=False
)
library_cohort_is_naive = BooleanField(
source="library.cohort.is_naive", read_only=True
)
added_by = StringRelatedField()
elisawell_set = NestedElisaWellSerializer(many=True, required=False)
antigen = PrimaryKeyRelatedField(queryset=Antigen.objects.all(), write_only=True)
Expand Down
32 changes: 32 additions & 0 deletions frontend/src/crudtemplates/AddEditObjectPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import React, { useState, useEffect } from "react";
import { useParams, useNavigate, useLocation } from "react-router-dom";
import { OkCancelDialog } from "../OkCancelDialog.js";
import ComboBox from "./ComboBox.js";
import { Switch } from "@headlessui/react";
import * as Sentry from "@sentry/browser";
import SequencingPlateLayout from "./SequencingPlateLayout.js";

Expand Down Expand Up @@ -154,6 +155,11 @@ const AddEditObjectPage = (props) => {
),
);

// deal with boolean manually
props.schema.fields
.filter((field) => field.type === "boolean")
.map((field) => formData.append(field.field, record[field.field]));

// deal with foreignkey manually
props.schema.fields
.filter((field) => field.type === "foreignkey")
Expand Down Expand Up @@ -307,6 +313,32 @@ const AddEditObjectPage = (props) => {
/>
)}

{field.type === "boolean" && (
<Switch
checked={record[field.field] === true}
onChange={(val) => {
setFormValue(field.field, val);
}}
className={classNames(
record[field.field] === true
? "bg-indigo-600"
: "bg-gray-200",
"relative inline-flex h-6 w-11 flex-shrink-0 cursor-pointer rounded-full border-2 border-transparent transition-colors duration-200 ease-in-out focus:outline-none focus:ring-2 focus:ring-indigo-600 focus:ring-offset-2",
)}
>
<span className="sr-only">Use setting</span>
<span
aria-hidden="true"
className={classNames(
record[field.field] === true
? "translate-x-5"
: "translate-x-0",
"pointer-events-none inline-block h-5 w-5 transform rounded-full bg-white shadow ring-0 transition duration-200 ease-in-out",
)}
/>
</Switch>
)}

{field.type === "selectmulti" &&
!relatedTables[field.field] && (
<LoadingSkeleton />
Expand Down
4 changes: 4 additions & 0 deletions frontend/src/crudtemplates/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -262,8 +262,12 @@ export const displayFieldSingle = (field, record, context, props) => {
))}
</ul>
);
} else if (field.type === "boolean") {
return record[field.field] === true ? "Yes" : "No";
} else if (field.viewPageExtLink) {
return makeExtLink(record[field.field], field.viewPageExtLink, context);
} else if (field.field === "cohort_num") {
return record["cohort_num_prefixed"];
} else {
return record[field.field];
}
Expand Down
9 changes: 7 additions & 2 deletions frontend/src/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ const cohortSchema = {
showInTable: true,
tableColWidth: "w-1/5",
},
{
label: "Is Naive?",
field: "is_naive",
type: "boolean",
},
{
label: "Immunisation date",
field: "immunisation_date",
Expand Down Expand Up @@ -91,7 +96,7 @@ const librarySchema = {
field: "cohort",
type: "foreignkey",
apiUrl: "/cohort",
fkDisplayField: "cohort_num",
fkDisplayField: "cohort_num_prefixed",
showInTable: true,
readOnlyOnEdit: true,
tableColWidth: "w-1/5",
Expand Down Expand Up @@ -196,7 +201,7 @@ const elisaSchema = {
field: "library",
type: "foreignkey",
apiUrl: "/library",
fkDisplayField: "cohort_cohort_num",
fkDisplayField: "cohort_cohort_num_prefixed",
showInTable: true,
tableColWidth: "w-1/6",
},
Expand Down

0 comments on commit f8c9517

Please sign in to comment.