def save(self, request, pk=None): profile = Profile.objects.get(pk=pk) new_data = request.data.get("specification_data", {}) new_structure = request.data.get("structure", {}) changed_data = (profile.specification_data.keys().sort() == new_data.keys().sort() and profile.specification_data != new_data) changed_structure = profile.structure != new_structure if (changed_data or changed_structure): try: new_profile = profile.copy( specification_data=new_data, new_name=request.data["new_name"], structure=new_structure, ) except ValidationError as e: raise exceptions.ParseError(e) serializer = ProfileSerializer( new_profile, context={'request': request} ) return Response(serializer.data) return Response({'status': 'no changes, not saving'}, status=status.HTTP_400_BAD_REQUEST)
def save(self, request, pk=None): profile = Profile.objects.get(pk=pk) new_data = request.data.get("specification_data", {}) new_structure = request.data.get("structure", {}) changed_data = (profile.specification_data.keys().sort() == new_data.keys().sort() and profile.specification_data != new_data) changed_structure = profile.structure != new_structure if (changed_data or changed_structure): new_profile = profile.copy_and_switch( ip=InformationPackage.objects.get( pk=request.data["information_package"]), specification_data=new_data, new_name=request.data["new_name"], structure=new_structure, ) serializer = ProfileSerializer(new_profile, context={'request': request}) return Response(serializer.data) return Response({'status': 'no changes, not saving'}, status=status.HTTP_400_BAD_REQUEST)
def test_authenticated(self): # get API response self.client.force_authenticate(user=self.user) request = APIRequestFactory().get(self.url) response = self.client.get(self.url) # get data from DB profiles = Profile.objects.all() serializer = ProfileSerializer(profiles, many=True, context={'request': request}) self.assertEqual(response.data, serializer.data) self.assertEqual(response.status_code, status.HTTP_200_OK)
def profiles(self, request, pk=None): sa = self.get_object() profiles = [p for p in sa.get_profiles() if p is not None] return Response(ProfileSerializer([p for p in profiles if p is not None], many=True).data)
def generate(self, request, pk=None): class SimpleProfileSerializer(serializers.ModelSerializer): class Meta: model = Profile fields = ( 'id', 'profile_type', 'name', 'type', 'status', 'label', ) def addExtraAttribute(field, data, attr): """ Adds extra attribute to field if it exists in data Args: field: The field to add to data: The data dictionary to look in attr: The name of the attribute to add Returns: The new field with the attribute added to it if the attribute exists in data. Otherwise the original field. """ field_attr = field['key'] + '_' + attr if field_attr in data: field[attr] = data[field_attr] return field serializer = SimpleProfileSerializer(data=request.data) serializer.is_valid(raise_exception=True) validated_data = serializer.validated_data obj = self.get_object() existingElements = obj.existingElements form = existingElements['root']['form'] formData = existingElements['root']['formData'] for idx, field in enumerate(form): field = addExtraAttribute(field, formData, 'desc') field = addExtraAttribute(field, formData, 'hideExpression') field = addExtraAttribute(field, formData, 'readonly') form[idx] = field existingElements['root']['form'] = form nsmap = obj.nsmap for ext in obj.extensions.iterator(): nsmap.update(ext.nsmap) schemaLocation = ['%s %s' % (obj.targetNamespace, obj.schemaURL)] XSI = 'http://www.w3.org/2001/XMLSchema-instance' if not nsmap.get("xsi"): nsmap["xsi"] = XSI if not nsmap.get(obj.prefix): nsmap[obj.prefix] = obj.targetNamespace for ext in obj.extensions.all(): nsmap[ext.prefix] = ext.targetNamespace schemaLocation.append('%s %s' % (ext.targetNamespace, ext.schemaURL)) jsonString, forms, data = generateElement(existingElements, 'root', nsmap=nsmap) schemaLocation = ({ '-name': 'schemaLocation', '-namespace': 'xsi', '#content': [{ 'text': ' '.join(schemaLocation) }] }) jsonString['-attr'].append(schemaLocation) profile = Profile.objects.create( profile_type=validated_data['profile_type'], name=validated_data['name'], type=validated_data['type'], status=validated_data['status'], label=validated_data['label'], template=forms, specification=jsonString, specification_data=data, structure=obj.structure ) profile_data = ProfileSerializer(profile, context={'request': request}).data return Response(profile_data, status=status.HTTP_201_CREATED)