def test_civ_post_objects_do_not_exist(civ, error_message): # test serializer = ComponentInterfaceValuePostSerializer(data=civ) # verify assert not serializer.is_valid() assert error_message in str(serializer.errors)
def test_civ_post_image_or_upload_required_validation(kind): # setup interface = ComponentInterfaceFactory(kind=kind) civ = {"interface": interface.slug} # test serializer = ComponentInterfaceValuePostSerializer(data=civ) # verify assert not serializer.is_valid() assert (f"upload_session or image are required for interface kind {kind}" in serializer.errors["non_field_errors"])
def test_civ_post_value_required(kind): # setup interface = ComponentInterfaceFactory(kind=kind) civ = {"interface": interface.slug} # test serializer = ComponentInterfaceValuePostSerializer(data=civ) # verify assert not serializer.is_valid() assert "JSON does not fulfill schema: None is not of type" in str( serializer.errors["__all__"][0])
def test_civ_post_image_valid(kind, rf): # setup user = UserFactory() upload = UploadSessionFactory(status=RawImageUploadSession.PENDING, creator=user) interface = ComponentInterfaceFactory(kind=kind) civ = {"interface": interface.slug, "upload_session": upload.api_url} # test request = rf.get("/foo") request.user = user serializer = ComponentInterfaceValuePostSerializer( data=civ, context={"request": request}) # verify assert serializer.is_valid()
def test_civ_post_upload_permission_validation(kind, rf): # setup user = UserFactory() upload = UploadSessionFactory() interface = ComponentInterfaceFactory(kind=kind) civ = {"interface": interface.slug, "upload_session": upload.api_url} # test request = rf.get("/foo") request.user = user serializer = ComponentInterfaceValuePostSerializer( data=civ, context={"request": request}) # verify assert not serializer.is_valid() assert ("Invalid hyperlink - Object does not exist" in serializer.errors["upload_session"][0])
def test_civ_post_value_validation(kind): # setup interface = ComponentInterfaceFactory(kind=kind) for test in TEST_DATA: civ = {"interface": interface.slug, "value": TEST_DATA[test]} # test serializer = ComponentInterfaceValuePostSerializer(data=civ) # verify assert serializer.is_valid() == ( kind == test or ( # Ints are valid for float types kind == "FLT" and test == "INT")) if not serializer.is_valid(): assert "JSON does not fulfill schema: " in str( serializer.errors["__all__"][0])
def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.fields["values"] = ComponentInterfaceValuePostSerializer( many=True, context=self.context) if "request" in self.context: user = self.context["request"].user self.fields["archive"].queryset = get_objects_for_user( user, "archives.use_archive", accept_global_perms=False)
class DisplaySetPostSerializer(DisplaySetSerializer): reader_study = SlugRelatedField( slug_field="slug", queryset=ReaderStudy.objects.none(), required=False ) values = ComponentInterfaceValuePostSerializer(many=True, required=False) def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) if "request" in self.context: user = self.context["request"].user self.fields["reader_study"].queryset = get_objects_for_user( user, "reader_studies.change_readerstudy", accept_global_perms=False, ) self.fields["values"].queryset = get_objects_for_user( user, "reader_studies.change_displayset", accept_global_perms=False, )
def partial_update(self, request, pk=None): instance = self.get_object() if not instance.is_editable: return HttpResponseBadRequest( "This display set cannot be changed, " "as answers for it already exist." ) assigned_civs = [] civ_values = request.data.pop("civ_values", []) values = request.data.pop("values", None) civs = instance.reader_study.display_sets.values_list( "values", flat=True ) assigned_civs = [] for value in civ_values: ci, civ = value.split("::") if civ == "": current_value = instance.values.filter(interface=ci).first() if current_value: assigned_civs.append(current_value) instance.values.remove(current_value) continue # Get the provided civ from the current reader study's display sets. civ = ComponentInterfaceValue.objects.filter(id__in=civs).get( id=civ ) # If there is already a value for the provided civ's interface in # this display set, remove it from this display set. Cast to list # to evaluate immediately. assigned_civs += list( instance.values.exclude(pk=civ.pk).filter(interface=ci) ) # Add the provided civ to the current display set instance.values.add(civ) if values: serialized_data = ComponentInterfaceValuePostSerializer( many=True, data=values ) if serialized_data.is_valid(): civs = serialized_data.create(serialized_data.validated_data) assigned_civs = instance.values.filter( interface__in=[civ.interface for civ in civs] ) instance.values.remove(*assigned_civs) instance.values.add(*civs) # Create a new display set for any civs that have been replaced by a # new value in this display set, to ensure it remains connected to # the reader study. for assigned in assigned_civs: if not instance.reader_study.display_sets.filter( values=assigned ).exists(): ds = DisplaySet.objects.create( reader_study=instance.reader_study ) ds.values.add(assigned) instance.values.remove(assigned) return super().partial_update(request, pk)