Data is not validated in Django Rest Framework - django

I am trying to pass the data to a serializer like the following:
myDict = {
"invoice_date": "2021-02-24T11:44:13+05:30",
"invoice_number": "12",
"vendor": "4",
"amount": "12",
"gst": "12",
"total_amount": "14",
"transaction_type": "Allot",
"status": "Hold",
"transactions": [
{
"t_no": 47,
"f_mile": "45",
"long_haul": "45",
"l_mile": "45",
"labour": "45",
"others": "54",
"a_t_no": 47,
},
{
"t_no": 102,
"f_mile": "12",
"long_haul": "12",
"l_mile": "21",
"labour": "21",
"others": "21",
"a_t_no": 102,
},
],
"owner": 2,
}
But when I check the validated data in the serialzer it shows it without the transactions data:
{'invoice_date': datetime.datetime(2021, 2, 24, 6, 14, 13, tzinfo=<UTC>), 'invoice_number': '12', 'amount': 12, 'gst': 12, 'total_amount': 14, 'status': 'Hold', 'transaction_type': 'Allot', 'vendor': <Vendor: Vendor object (4)>, 'owner': <User: yantraksh>}
so I tried to check the initial data that is being passed to the serializer :
<QueryDict: {
"invoice_date": ["2021-02-24T11:44:13+05:30"],
"invoice_number": ["12"],
"vendor": ["4"],
"amount": ["12"],
"gst": ["12"],
"total_amount": ["14"],
"transaction_type": ["Allot"],
"status": ["Hold"],
"transactions": [
'[{"t_no":47,"f_mile":"45","long_haul":"45","l_mile":"45","labour":"45","others":"54","a_t_no":47},{"t_no":102,"f_mile":"12","long_haul":"12","l_mile":"21","labour":"21","others": "21","a_t_no":102}]'
],
"owner": [2],
}>
It shows that the transaction data is being passed as a string, what should I change it to in order to get it as validated data ?

Based on the "evidence" in that QueryDict (namely strings wrapped in lists) it sounds like you're not submitting the data as JSON.
You can't use regular HTML form data to submit structured data, you'll need to post JSON data.

Related

how to put two data in a title?

I want to display a list of products that uses an API to retrieve the data.
the array I use to display data looks like this :
Array [
Object {
"amount": 2671.25,
"balance": 0,
"client_id": 1,
"created_at": "2020-05-06T17:42:26Z",
"discount": 0,
"discount_type": 0,
"id": 19,
"items": Array [
Object {
"cost": 2400,
"currency": "EUR",
"description": "",
"name": "Apple MacBook '' LED 500 Go SSD 32 Go",
"product_id": 5,
"quantity": 1,
"tax_rate_id": 1,
},
Object {
"cost": 54.25,
"currency": "EUR",
"description": "blablabla",
"product_id": 2,
"quantity": 5,
"tax_rate_id": 4,
},
],
"po_number": "",
"public_notes": "TEST 6 : Acomptes",
"quote_date": "2020-05-06",
"quote_number": "D0019",
"quote_status": 40,
"terms": "",
"updated_at": "2020-05-06T18:08:06Z",
},
It works, it's great. But I would like to improve the display.
For example, when I unroll my lilste to get the detail, I have the price.
<List.Item title={item.amount}/>
Ok, it works, cool, but I would like to add the currency (€ or other) How can I do it? I tried this but it doesn't work:
<List.Item title={item.amount}{item.currency}/>
<List.Item title={item.amount} + {item.currency}/>
<List.Item title={item.amount} + '€'/>
<List.Item title='{item.amount} + €'/>
Use Template Literals
Template literals are string literals allowing embedded expressions.
It takes that form:
`string text ${expression} string text`
So you could do something like
<List.Item title={`${item.amount} ${item.currency}`} />

How to pass json data as queryset object to django template?

I have this json data in views.py
[
{
"id": 6,
"first_name": "Star",
"last_initial": "W",
"phone": "+918893972142",
"email": "star#gmail.com",
"date_created": "2020-10-12T17:17:17.629123Z",
"orders": []
},
{
"id": 4,
"first_name": "Sam",
"last_initial": "0",
"phone": "+918766897214",
"email": "sam#gmail.com",
"date_created": "2020-10-12T17:13:33.435065Z",
"orders": []
},
{
"id": 3,
"first_name": "Gaara",
"last_initial": "W",
"phone": "+918668972789",
"email": "gaara#gmail.com",
"date_created": "2020-10-12T17:08:44.906809Z",
"orders": [
{
"order_id": "9",
"customer_id": "3",
"customer_name": "Gaara W",
"product_id": "3",
"product_name": "Jet",
"date_created": "2020-10-12T17:18:18.823289Z",
"status": "Delivered"
}
]
}
]
I want to pass this data as a context object to the template from views using render function. so I can display this data using {% for %} loop in my template.
Pass the data into the template.
return render(request, "app/template.html", {
"data": data
})
Then you can use for loops to render the data you need.

Can I get all active ARB subscription in a single API call

Current I'm exporting all ARB data by calling API to get all active ARB ids then go through each ARB id to get info stored in each ID. But this process is too long and it makes lots of requests. Is there any way so that I can get all active ARB ids data in one request like that of any database?
https://developer.authorize.net/api/reference/index.html#recurring-billing-get-a-list-of-subscriptions
This function gives only small amount data while I need complete data stored in a profile like this one: https://developer.authorize.net/api/reference/index.html#recurring-billing-get-subscription
But this function only works for single ID.
New Answer
No. The ARBGetSubscriptionListRequest only returns a limited amount of information. If you want detailed information you would need to call ARBGetSubscriptionListRequest and then loop through the results and make an API call for each subscription to get the more granular data.
Due to the potentially large amount of results, you probably should store the results in a database and then have a bunch of scheduled scripts make the subsequent API calls.
Old Answer
Yes. You can call ARBGetSubscriptionListRequest.
Request:
{
"ARBGetSubscriptionListRequest": {
"merchantAuthentication": {
"name": "5KP3u95bQpv",
"transactionKey": "346HZ32z3fP4hTG2"
},
"refId": "123456",
"searchType": "subscriptionActive",
"sorting": {
"orderBy": "id",
"orderDescending": "false"
},
"paging": {
"limit": "1000",
"offset": "1"
}
}
}
Response:
{
"totalNumInResultSet": 1273,
"totalNumInResultSetSpecified": true,
"subscriptionDetails": [
{
"id": 100188,
"name": "subscription",
"status": "canceled",
"createTimeStampUTC": "2004-04-28T23:59:47.33",
"firstName": "Joe",
"lastName": "Tester",
"totalOccurrences": 12,
"pastOccurrences": 6,
"paymentMethod": "creditCard",
"accountNumber": "XXXX5454",
"invoice": "42820041325496571",
"amount": 10,
"currencyCode": "USD"
},
{
"id": 100222,
"name": "",
"status": "canceled",
"createTimeStampUTC": "2004-10-22T21:00:15.503",
"firstName": "asdf",
"lastName": "asdf",
"totalOccurrences": 12,
"pastOccurrences": 0,
"paymentMethod": "creditCard",
"accountNumber": "XXXX1111",
"invoice": "",
"amount": 1,
"currencyCode": "USD"
},
{
"id": 100223,
"name": "",
"status": "canceled",
"createTimeStampUTC": "2004-10-22T21:01:27.69",
"firstName": "asdf",
"lastName": "asdf",
"totalOccurrences": 12,
"pastOccurrences": 1,
"paymentMethod": "eCheck",
"accountNumber": "XXXX3888",
"invoice": "",
"amount": 10,
"currencyCode": "USD"
}
],
"refId": "123456",
"messages": {
"resultCode": "Ok",
"message": [
{
"code": "I00001",
"text": "Successful."
}
]
}
}

format dictionary using python

I am new to python, can someone tell me how can we change the given code into the formatted code given below .
The data is stored in a dictionary and i want to store the formatted data also in a dictionary.
I am using python version 2.7.
Is it possible to do the same?
Can the alterations be made in the same dictionary or we need to create a new dictionary?
The code I have :
{
"roles":
{
"AA":[
{
"date":"20150101",
"total":6.0
},
{
"date":"20150201",
"total":14.5
},
{
"date":"20150301",
"total":14.5
}
],
"AB":[
{
"date":"20150301",
"total":1.6
},
{
"date":"20150401",
"total":1.6
},
{
"date":"20150501",
"total":7.24
}
]
}
}
The code I want :
{
"key" : "roles",
"values" : [
{
"key": "AA",
"values": [
{
"date": "20150101",
"total": 6
},
{
"date": "20150201",
"total": 14.5
},
{
"date": "20150301",
"total": 14.5
}
]
},
{
"key": "AB",
"values": [
{
"date": "20150301",
"total": 1.6
},
{
"date": "20150401",
"total": 1.6
},
{
"date": "20150501",
"total": 7.24
}
]
}
]
}
Not necessarily the most robust but gets the job done as long as the input format stays consistent with the example you provided (using pprint only for formatted output. You don't have to install it for the code to work)
from pprint import pprint
output = {}
for key, value in input_dict.items():
output['key'] = key
output['values'] = [{'key': inner_key, 'values': inner_value}
for inner_key, inner_value in value.items()]
pprint(output)
Outputs
{'key': 'roles',
'values': [{'key': 'AA',
'values': [{'date': '20150101', 'total': 6.0},
{'date': '20150201', 'total': 14.5},
{'date': '20150301', 'total': 14.5}]},
{'key': 'AB',
'values': [{'date': '20150301', 'total': 1.6},
{'date': '20150401', 'total': 1.6},
{'date': '20150501', 'total': 7.24}]}]}

Get Malformed error while patch request to update payment

I want to reduce some items of my order from payment. That's why I'm using replace method of PayPal python API that allows to update or add data after an object of payment has been created.
Error:
{u'message': u'Incoming JSON request does not map to API request', u'debug_id': u'd57742e9d3b03', u'name': u'MALFORMED_REQUEST', u'information_link': u'https://developer.paypal.com/webapps/developer/docs/api/#MALFORMED_REQUEST'}
Code:
create payment:
I added two products while creating a payment object.
payment = Payment({
"intent": "sale",
"payer": {
"payment_method": "paypal"},
"redirect_urls": {
"return_url": "http://localhost:3000/payment/execute",
"cancel_url": "http://localhost:3000/"},
"transactions": [{
"item_list": {
"items": [{
"name": "Ice-cream",
"sku": "I10001",
"price": "50.00",
"currency": "USD",
"quantity": 12},
{
"name": "Ice-cream12",
"sku": "I100012",
"price": "60.00",
"currency": "USD",
"quantity": 12}
]},
"amount": {
"total": "1320.00",
"currency": "USD"},
"description": "This is the payment transaction description."}]})
payment.create()
Update Payment:
update_payment_json = [
{
"op":"replace",
"path":"/transactions/0/item_list",
"values":{"items":[{
"name": "Ice-cream",
"sku": "I10001",
"price": "50.00",
"currency": "USD",
"quantity": 12,
},]}
},
{
"op":"replace",
"path":"/transactions/0/amount",
"value":
{
"total": "600.00",
"currency": "USD",
"details":{
"subtotal": "600.00",
"shipping":"0.0",
}},
},
]
payment.replace(update_payment_json)
I don't believe the below path is sufficient to determine which item are you updating ? there are 2 items in the item_list correct? if you are updating just 1 of it then you need to qualify it to the right path so that you are clear about which item you are replacing.
"path":"/transactions/0/item_list",