Here are the snippets of my code,
Model OptionalFieldsByOrganization.py
from django.db import models
from model_utils import Choices
from proposal.models import Organization
from proposal.models.proposal_configurable_fields import ProposalConfigurableFields
class OptionalFieldsByOrganization(models.Model):
STATUS = Choices(
(0, 'hidden', 'Hidden'),
(1, 'optional', 'Optional'),
(2, 'required', 'Required'))
organization = models.ForeignKey(Organization, on_delete=models.PROTECT)
configurable_fields = models.ForeignKey(ProposalConfigurableFields, on_delete=models.PROTECT)
status = models.IntegerField(choices=STATUS, default=STATUS.hidden)
class Meta:
permissions = (
('view_optionalfieldsbyorganization', 'Can view Optional Fields By Organization'),)
Serializer
from rest_framework_json_api import serializers
from proposal.models import (
organization, proposal_configurable_fields)
from rest_framework_json_api.relations import ResourceRelatedField
from proposal.models import OptionalFieldsByOrganization
class OptionalFieldsByOrganizationSerializer(serializers.ModelSerializer):
class Meta:
model = OptionalFieldsByOrganization
#fields = ['status']
fields = "__all__"
ViewSet
from sis_common.views import BaseViewSet
from proposal.models import organization
from proposal.serializers.optional_fields_by_organization import (
OptionalFieldsByOrganizationSerializer)
from proposal.models.optional_fields_by_organization import (
OptionalFieldsByOrganization)
class OptionalFieldsByOrganizationViewSet(BaseViewSet):
queryset = OptionalFieldsByOrganization.objects.all().order_by('pk')
serializer_class = OptionalFieldsByOrganizationSerializer
With this configuration in place, I'm able to fetch multiple records in an array. Like this,
curl -X GET \
http://localhost:8000/v1/optionalfieldsbyorganizations \
-H 'Accept: */*' \
-H 'Authorization: Basic YWRtaW46YWRtaW4xMjM=' \
-H 'Content-Type: application/vnd.api+json' \
{
"links": {
"first": "http://localhost:8000/v1/optionalfieldsbyorganizations?page%5Bnumber%5D=1",
"last": "http://localhost:8000/v1/optionalfieldsbyorganizations?page%5Bnumber%5D=1",
"next": null,
"prev": null
},
"data": [
{
"type": "OptionalFieldsByOrganization",
"id": "1",
"attributes": {
"status": 0
},
"relationships": {
"organization": {
"data": {
"type": "Organization",
"id": "1"
}
},
"configurable_fields": {
"data": {
"type": "ProposalConfigurableFields",
"id": "1"
}
}
}
},
{
"type": "OptionalFieldsByOrganization",
"id": "2",
"attributes": {
"status": 1
},
"relationships": {
"organization": {
"data": {
"type": "Organization",
"id": "1"
}
},
"configurable_fields": {
"data": {
"type": "ProposalConfigurableFields",
"id": "2"
}
}
}
},
{
"type": "OptionalFieldsByOrganization",
"id": "3",
"attributes": {
"status": 0
},
"relationships": {
"organization": {
"data": {
"type": "Organization",
"id": "1"
}
},
"configurable_fields": {
"data": {
"type": "ProposalConfigurableFields",
"id": "1"
}
}
}
}
],
"meta": {
"pagination": {
"page": 1,
"pages": 1,
"count": 3
}
}
}
Now, I want to bulk post and patch for the same resource. The payload will look like this,
{
"data": [
{
"type": "OptionalFieldsByOrganization",
"attributes": {
"status": 2
},
"relationships": {
"organization": {
"data": {
"type": "Organization",
"id": "2"
}
},
"configurable_fields": {
"data": {
"type": "ProposalConfigurableFields",
"id": "2"
}
}
}
},{
"type": "OptionalFieldsByOrganization",
"attributes": {
"status": 1
},
"relationships": {
"organization": {
"data": {
"type": "Organization",
"id": "2"
}
},
"configurable_fields": {
"data": {
"type": "ProposalConfigurableFields",
"id": "3"
}
}
}
}]
}
What all changes should I make in my code to achieve this?
I read about extensions in {json.api} however, not sure how to make use of it?
I believe that one possible approach could as follows. If you insist on sending the object in "data" field of your request.
Create a special view to handle bulk create a do this in post method:
class BulkView(generics.GenericAPIView):
serializer_class = OptionalFieldsByOrganizationSerializer
queryset = OptionalFieldsByOrganization.objects.all()
def post(self, request, *args, **kwargs):
ser = self.get_serializer(data=request.data["data"], many=True)
ser.is_valid(raise_exception=True)
ser.save()
return Response(ser.data)
Related
I have this object:
elements120: {
"data": {
"name": "120",
"type": "120"
},
"children": [
{
"data": {
"name": "120A",
"type": "120A"
},
"children": []
},
{
"data": {
"name": "120B",
"type": "120B"
},
"children": []
},
{
"data": {
"name": "120C",
"type": "120C"
},
"children": []
}
]
}
I need to make some new Json as these below:
Json 1:
filtered120A{
"data": {
"name": "120",
"type": "120"
},
"children": [
{
"data": {
"name": "120A",
"type": "120A"
},
"children": []
}
]
}
Json 2:
filtered120B{
"data": {
"name": "120",
"type": "120"
},
"children": [
{
"data": {
"name": "120B",
"type": "120B"
},
"children": []
}
]
}
Json 3:
filtered120C{
"data": {
"name": "120",
"type": "120"
},
"children": [
{
"data": {
"name": "120C",
"type": "120C"
},
"children": []
}
]
}
I tried to do this in order to get only the first children node and delete the others (it's the only way it works me). For 120B and 120C i put as first child the value of the node that I needed for each case
this.filtered120A = utils.deepClone(this.elements120);
this.filtered120B = utils.deepClone(this.elements120);
this.filtered120C = utils.deepClone(this.elements120);
this.filtered120A = this.filtered120A.filter((element) => {
delete element.children[1];
delete element.children[2];
return true;
});
this.filtered120B = this.filtered120B.filter((element) => {
element.children[0] = element.children[1];
delete element.children[1];
delete element.children[2];
return true;
});
this.filtered120C = this.filtered120C.filter((element) => {
element.children[0] = element.children[2];
delete element.children[1];
delete element.children[2];
return true;
});
It works, but the code is not very elegant. I would like to know if there is any other better alternative.
Here you go:
function getOutputs(input) {
const outputs = [];
for (const child of input.children) {
outputs.push({
"data": {
"name": input.data.name,
"type": input.data.type
},
"children": [child]
});
}
return outputs;
}
const inputs = {
"data": {
"name": "120",
"type": "120"
},
"children": [
{
"data": {
"name": "120A",
"type": "120A"
},
"children": []
},
{
"data": {
"name": "120B",
"type": "120B"
},
"children": []
},
{
"data": {
"name": "120C",
"type": "120C"
},
"children": []
}
]
};
const outputs = getOutputs(inputs);
console.log(outputs[0]); // output1
console.log(outputs[1]); // output2
console.log(outputs[2]); // output3
I'm new in react. Need help to create form with multiple nested field array like I have provide json, I have used react hook form for validation.
Facing issue to create dynamic setting,model array data form elements,
I need to create below Json on form submit
{
"field_data": [
{
"id": 1,
"fields": [
{
"name": "Field 1",
"config": {
"type": "Number",
"setting": {
"model": [
{
"id": 7,
"value": []
},
{
"id": 6,
"value": []
}
]
},
"extra_info": {
"suffix": ""
}
}
},
{
"name": "Field 2",
"config": {
"type": "Any",
"setting": {
"model": []
},
"extra_info": {
"suffix": ""
}
}
},
{
"name": "Field 3",
"config": {
"type": "Any",
"setting": {
"model": []
},
"extra_info": {
"suffix": ""
}
}
}
]
}
],
"name": "sacascasc"
}
In above json input will be "model" and "extra_info"
How to work with multiple nexted formarray
Thank you in advance.
Following is the DATA that needs to parse which can nested arrays and objects i need
help understanding the easiest way to parse this .
const country= [
{
"place": "sikkim",
"location": 2,
"Extension": "",
"Keys": {
"string": [
"Enabled",
"Disabled"
]
},
"ItemValues": {
"ItemData": [
{
"Name": "Enabled",
"Data": {
"Rows": {
"Row": {
"Values": {
"Type": false
}
}
}
}
},
{
"Name": "Value",
"Data": {
"Rows": {
"DataRow": {
"Values": {
"anyType": "100"
}
}
}
}
}
]
}
},
{
"place": "assam",
"location": 1,
"Extension": "",
"Keys": {
"string": "MinValue"
},
"ItemValues": {
"ItemData": {
"Name": "nValue",
"Data": {
"Rows": {
"DataRow": {
"Values": {
"anyType": "1"
}
}
}
}
}
}
}
]
In this array 2 objects are there i need to parse this complex array of object and
show like this -
OUTCOME should be -
sikkim:{
"place": "sikkim",
"location": 2,
"Extension": "",
"string":"Enabled","Disabled",
"Name": "Enabled",
"Type": false,
"Name": "Value",
"anyType": 100
},
assam:{.. so on}
code that i tried is displaying only keys -
const getValues = country =>
country === Object(country)
? Object.values(country).flatMap(getValues)
: [country];
console.log(getValues(country));
OUTPUT from above code =
["sikkim", 2, "", "Enabled", "Disabled", "Enabled", false, "Value", "100",
"assam", 1, "", "MinValue", "nValue", "1"]
Need to write logic which is smart way to write to cover this logic because my data
is huge any leads?I can achieve the output but writing many for and if loops but that
creates lot of confusion.
Please feel free to update fitting title for this question.
I am trying to achieve something like this. An input JSON/data JSON must have an OWNER and a CHARGE_TO Account, but these don't have to be on the same Account (I.e. one Account can be OWNER, another can be CHARGE_TO) and must not contain account with any other roles.
NOTE: Need to define JSON schema which should be simple to maintain. i.e. Should be easy to add a new role in the condition. My valid and invalid JSONs are,
*************************** VALID JSON1 *********************************
{
"user": [
{
"name": "user1",
"roles": ["OWNER"]
},
{
"name": "user2",
"roles": ["ANY_OTHER_ROLE"]
},
{
"name": "user3",
"roles": ["CHARGE_TO"]
}]
}
*************************** VALID JSON2 *********************************
{
"user": [
{
"name": "user1",
"roles": ["OWNER", "CHARGE_TO"]
},
{
"name": "user2",
"roles": ["ANY_OTHER_ROLE"]
}]
}
*************************** INVALID JSON *********************************
{
"user": [
{
"name": "user1",
"roles": ["OWNER"]
},
{
"name": "user2",
"roles": ["ANY_OTHER_ROLE"]
}]
}
A JSON is valid if it has a user with roles ("OWNER" & "CHARGE_TO") or if users with roles (user1 - "OWNER", user3-"CHARGE_TO", other users with any other role).
Below is JSON schema I tried (draft_07). THIS IS NOT A WORKING SCHEMA.
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "Complex inner array",
"type": "object",
"properties": {
"user": {
"type": "array",
"contains": {
"type": "object",
"properties": {
"name": { "type": "string" },
"orderRoles": {
"type": "array",
"minItems": 1,
"items": { "type": "string" }
}
},
"oneOf": [
{ "properties": { "roles": { "enum": ["OWNER", "CHARGE_TO"] }}},
{ "properties": { "roles": { "enum": ["OWNER"] }}},
{ "properties": { "roles": { "enum": ["CHARGE_TO"] }}}
]
},
"items": {
"type": "object",
"properties": {
"name": { "type": "string" },
"orderRoles": {
"type": "array",
"minItems": 1,
"items": { "type": "string" }
}
}
}
}
}
}
Below is my working schema. This is not an elegant solution as schema becomes huge if a new role ("ADMIN") is added, i.e. schema condition would An input JSON/data JSON must have an OWNER, ADMIN and a CHARGE_TO Account, but these don't have to be on the same Account (I.e. one Account can be OWNER, one Account can be ADMIN & another can be CHARGE_TO) and must not contain account with any other roles. Please feel free to improve this. Thanks.
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "Complex inner array",
"type": "object",
"allOf": [
{
"properties": {
"account": {
"type": "array",
"contains": {
"type": "object",
"properties": {
"roles": {
"type": "array",
"minItems": 1,
"contains": { "enum": ["OWNER"] }
}
}
}
}
}
},
{
"properties": {
"account": {
"type": "array",
"contains": {
"type": "object",
"properties": {
"roles": {
"type": "array",
"minItems": 1,
"contains": { "enum": ["CHARGE_TO"] }
}
}
}
}
}
},
{
"properties": {
"account": {
"type": "array",
"items": {
"type": "object",
"properties": {
"roles": {
"type": "array",
"minItems": 1,
"items":{
"enum": ["CHARGE_TO", "OWNER"]
}
}
}
}
}
}
}
]
}
You need to isolate the part of the schema that checks for a specific role. Then you can just combine them with allOf. There's as little duplication as possible and you can easily add another constraint.
{
"type": "object",
"properties": {
"user": {
"type": "array",
"items": {
"type": "object",
"properties": {
"name": { "type": "string" },
"roles": {
"type": "array",
"items": { "type": "string" }
}
}
},
"allOf": [
{ "$ref": "#/definitions/require-role-owner" },
{ "$ref": "#/definitions/require-role-charge-to" }
]
}
},
"definitions": {
"require-role-owner": {
"contains": {
"properties": {
"roles": {
"contains": { "const": "OWNER" },
"allOf": [{ "$ref": "#/definitions/not-other-role" }]
}
}
}
},
"require-role-charge-to": {
"contains": {
"properties": {
"roles": {
"contains": { "const": "CHARGE_TO" },
"allOf": [{ "$ref": "#/definitions/not-other-role" }]
}
}
}
},
"not-other-role": {
"items": { "enum": ["OWNER", "CHARGE_TO"] }
}
}
}
I have data with one parameter which is an array. I know that objects in array are not well supported in Kibana, however I would like to know if there is a way to filter that array with only one value for the key. I mean :
This is a json for exemple :
{
"_index": "index",
"_type": "data",
"_id": "8",
"_version": 2,
"_score": 1,
"_source": {
"envelope": {
"version": "0.0.1",
"submitter": "VF12RBU1D53087510",
"MetaData": {
"SpecificMetaData": [
{
"key": "key1",
"value": "94"
},
{
"key": "key2",
"value": "0"
}
]
}
}
}
}
And I would like to only have the data which contains key1 in my SpecificMetaData array in order to plot them. For now, when I plot SpecificMetaData.value it takes all the values of the array (value of key1 and key2) and doesn't propose SpecificMetaData.value1 and SpecificMetaData.value2.
If you need more information, tell me. Thank you.
you may need to map your data to mappings so as SpecificMetaData should act as nested_type and inner_hits of nested filter can supply you with objects which have key1.
PUT envelope_index
{
"mappings": {
"document_type": {
"properties": {
"envelope": {
"type": "object",
"properties": {
"version": {
"type": "text"
},
"submitter": {
"type": "text"
},
"MetaData": {
"type": "object",
"properties": {
"SpecificMetaData": {
"type": "nested"
}
}
}
}
}
}
}
}
}
POST envelope_index/document_type
{
"envelope": {
"version": "0.0.1",
"submitter": "VF12RBU1D53087510",
"MetaData": {
"SpecificMetaData": [{
"key": "key1",
"value": "94"
},
{
"key": "key2",
"value": "0"
}
]
}
}
}
POST envelope_index/_search
{
"query": {
"bool": {
"must": [
{
"nested": {
"inner_hits": {},
"path": "envelope.MetaData.SpecificMetaData",
"query": {
"bool": {
"must": [
{
"term": {
"envelope.MetaData.SpecificMetaData.key": {
"value": "key1"
}
}
}
]
}
}
}
}
]
}
}
}