Skip to content

Commit

Permalink
allow interventions to be none closes #701
Browse files Browse the repository at this point in the history
  • Loading branch information
janekg89 committed Dec 18, 2020
1 parent 64c3ff9 commit 65cc244
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 43 deletions.
31 changes: 16 additions & 15 deletions backend/pkdb_app/info_nodes/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ def validate_numeric(self, data):
f"for all measurement types except "
f"<{self.CAN_NEGATIVE}>.", "detail": data})

def validate_complete(self, data):
def validate_complete(self, data, time_allowed: bool = True):
"""Complete validation."""

# check unit
Expand All @@ -378,24 +378,25 @@ def validate_complete(self, data):
choice = data.get("choice", None)
d_choice = self.validate_choice(choice)

time_unit = data.get("time_unit", None)
time = data.get("time", None)
if time_allowed:
time_unit = data.get("time_unit", None)
time = data.get("time", None)

if time_unit and time_unit != "NR":
self.validate_time_unit(time_unit)
if time_unit and time_unit != "NR":
self.validate_time_unit(time_unit)

if self.time_required:
details = f"for measurement type `{self.info_node.name}`"
if self.time_required:
details = f"for measurement type `{self.info_node.name}`"

if time != "NR":
_validate_required_key_and_value(data, "time", details=details)
if time_unit != "NR":
_validate_required_key_and_value(data, "time_unit", details=details)
if time != "NR":
_validate_required_key_and_value(data, "time", details=details)
if time_unit != "NR":
_validate_required_key_and_value(data, "time_unit", details=details)

if time == "NR":
data["time"] = None
if time_unit == "NR":
data["time_unit"] = None
if time == "NR":
data["time"] = None
if time_unit == "NR":
data["time_unit"] = None

return {"choice": d_choice}

Expand Down
5 changes: 0 additions & 5 deletions backend/pkdb_app/info_nodes/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,6 @@ class MeasurementTypeableSerializer(EXMeasurementTypeableSerializer):
time = FloatNRField(allow_null=True)


def to_representation(self, instance):
rep = super().to_representation(instance)
return rep


class SynonymSerializer(WrongKeyValidationSerializer):
pk = serializers.IntegerField(read_only=True)
class Meta:
Expand Down
4 changes: 2 additions & 2 deletions backend/pkdb_app/outputs/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ class OutputTypes(models.TextChoices):

group = models.ForeignKey(Group, null=True, blank=True, on_delete=models.CASCADE)
individual = models.ForeignKey(Individual, null=True, blank=True, on_delete=models.CASCADE)
interventions = models.ManyToManyField(Intervention, through="OutputIntervention", related_name="outputs")
interventions = models.ManyToManyField(Intervention, through="OutputIntervention", related_name="outputs", null=True, blank=True)
subset = models.ForeignKey('data.Subset', on_delete=models.CASCADE, null=True, blank=True, related_name="pks")

tissue = models.ForeignKey(Tissue, related_name="outputs", null=True, blank=True, on_delete=models.CASCADE)
Expand All @@ -130,7 +130,7 @@ def null_attr(self, attr):

def is_timecourse(self):
if self.timecourse:
if self.timecourse.data.data_type=="timecourse":
if self.timecourse.data.data_type == "timecourse":
return True
return False

Expand Down
20 changes: 9 additions & 11 deletions backend/pkdb_app/outputs/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
CommentElasticSerializer
from ..interventions.models import Intervention
from ..serializers import (
ExSerializer, StudySmallElasticSerializer, SidNameLabelSerializer)
ExSerializer, StudySmallElasticSerializer, SidNameLabelSerializer, PKField)
from ..subjects.models import Group, DataFile, Individual
from ..subjects.serializers import (
EXTERN_FILE_FIELDS, GroupSmallElasticSerializer, IndividualSmallElasticSerializer)
Expand Down Expand Up @@ -51,6 +51,7 @@
# Outputs
# ----------------------------------


class OutputSerializer(MeasurementTypeableSerializer):

group = serializers.PrimaryKeyRelatedField(
Expand All @@ -62,13 +63,10 @@ class OutputSerializer(MeasurementTypeableSerializer):
required=False,
allow_null=True,
)
interventions = serializers.PrimaryKeyRelatedField(
interventions = PKField(
queryset=Intervention.objects.all(),
many=True,
read_only=False,
required=False,
allow_null=True,
)
many=True, required=True, allow_null=True)

tissue = utils.SlugRelatedField(
slug_field="name",
queryset=InfoNode.objects.filter(ntype=InfoNode.NTypes.Tissue),
Expand All @@ -91,10 +89,12 @@ class Meta:
def to_internal_value(self, data):
data.pop("comments", None)
data.pop("descriptions", None)
data.pop("image", None)
data.pop("source", None)

data = self.retransform_map_fields(data)
data = self.to_internal_related_fields(data)
self.validate_wrong_keys(data, additional_fields=OutputExSerializer.Meta.fields)
self.validate_wrong_keys(data)
return super(serializers.ModelSerializer, self).to_internal_value(data)

def validate(self, attrs):
Expand All @@ -103,7 +103,6 @@ def validate(self, attrs):
self.validate_group_individual_output(attrs)

_validate_required_key(attrs, "measurement_type")

_validate_required_key(attrs, "substance")
_validate_required_key(attrs, "tissue")
_validate_required_key(attrs, "interventions")
Expand Down Expand Up @@ -173,7 +172,6 @@ def to_internal_value(self, data):
for output in temp_outputs:
outputs_from_file = self.entries_from_file(output)
outputs.extend(outputs_from_file)

# ----------------------------------
# finished
# ----------------------------------
Expand All @@ -191,7 +189,7 @@ def to_internal_value(self, data):
[data.pop(field, None) for field in drop_fields]
data["outputs"] = outputs
data = self.transform_map_fields(data)
self.validate_wrong_keys(data, OutputSerializer.Meta.fields)
self.validate_wrong_keys(data)
return super(serializers.ModelSerializer, self).to_internal_value(data)

def validate_label_map(self, value) -> None:
Expand Down
9 changes: 5 additions & 4 deletions backend/pkdb_app/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from django.core.exceptions import ObjectDoesNotExist, MultipleObjectsReturned
from django.db.models import Q
from rest_framework import serializers
from rest_framework.fields import empty
from rest_framework.settings import api_settings

from pkdb_app.behaviours import map_field
Expand Down Expand Up @@ -354,8 +355,6 @@ def make_entry(self, entry, template, data, source):
try:
entry_value = getattr(entry, values[1])



except AttributeError:

raise serializers.ValidationError(
Expand Down Expand Up @@ -464,7 +463,7 @@ def to_representation(self, instance):
request = self.context.get('request')

# url representation of file
for file in ["source","image"]:
for file in ["source", "image"]:
if file in rep:
if "||" not in str(rep[file]):
rep[file] = request.build_absolute_uri(getattr(instance, file).file.url)
Expand Down Expand Up @@ -530,6 +529,8 @@ def to_internal_related_fields(self, data):
f"intervention <{intervention}> does not exist, check interventions."
)
data["interventions"] = interventions
else:
data["interventions"] = []
return data


Expand Down Expand Up @@ -784,4 +785,4 @@ def to_internal_value(self, data):
try:
return float(data)
except (TypeError, ValueError):
self.fail('invalid')
self.fail('invalid')
2 changes: 1 addition & 1 deletion backend/pkdb_app/subjects/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ def validate(self, attrs):
if attrs[info_node] is not None:
attrs[info_node] = getattr(attrs[info_node], info_node)

attrs["choice"] = attrs["measurement_type"].validate_complete(data=attrs)["choice"]
attrs["choice"] = attrs["measurement_type"].validate_complete(data=attrs, time_allowed=False)["choice"]

except ValueError as err:
raise serializers.ValidationError(err)
Expand Down
10 changes: 5 additions & 5 deletions backend/pkdb_app/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,20 +149,20 @@ def _create(validated_data, model_manager=None, model_serializer=None,
return instance, popped_data


def initialize_normed(notnorm_instance):
norm = copy.copy(notnorm_instance)
def initialize_normed(not_norm_instance):
norm = copy.copy(not_norm_instance)
norm.pk = None
norm.normed = True
norm.normalize()
norm.raw_id = notnorm_instance.pk
norm.raw_id = not_norm_instance.pk

try:
norm.individual_id = notnorm_instance.individual.pk
norm.individual_id = not_norm_instance.individual.pk
except AttributeError:
pass

try:
norm.group_id = notnorm_instance.group.pk
norm.group_id = not_norm_instance.group.pk
except AttributeError:
pass

Expand Down

0 comments on commit 65cc244

Please sign in to comment.