Django AJAX looping data - django

In my view I'm trying to loop all pages and extract the name for each using the code below. But it does not appear to work.
How can I achieve this?
view.py
json_dict = json.loads(request.POST['site'])
for item in json_dict['pages']:
item.json_dict['name']
the JSON data
{
"someitem": "xaAX",
"pages": [
{
"id": "1364484811734",
"name": "Page Name",
"type": "basic",
"components": {
"img": "",
"text": ""
}
},
{
"id": "1364484812918",
"name": "Contact",
"type": "contact",
"components": {
"text": "Send us an email using the form below.",
"contact-form": {
"contact-name": "zzz",
"contact-email": "zz"
}
}
},
]
}

This should work:
json_dict = json.loads(request.POST['site'])
for item in json_dict['pages']:
print item['name']

Related

PayPal API integration with Django

I am integrating PayPal API with Django back-end (Reactcjs for front-end) in my project with the help of sandbox accounts (#business & #personal account). What I did till now is I have a client_id and a secret used for generating access_token from paypal.
import requests
import base64
def PaypalToken(client_ID, client_Secret):
url = "https://api.sandbox.paypal.com/v1/oauth2/token"
data = {
"client_id":client_ID,
"client_secret":client_Secret,
"grant_type":"client_credentials"
}
headers = {
"Content-Type": "application/x-www-form-urlencoded",
"Authorization": "Basic {0}".format(base64.b64encode((client_ID + ":" + client_Secret).encode()).decode())
}
token = requests.post(url, data, headers=headers)
return token
x = PaypalToken(my_client_ID, my_client_Secret)
print(x.text)
<code>
This gonna provide me a token then I use that token to post json object on paypal.
<pre>
headers = {"Content-Type": "application/json", "Authorization": 'Bearer' + token}
url = https://api.sandbox.paypal.com/v2/checkout/orders
data = '{
"intent": "CAPTURE",
"application_context": {
"return_url": "https://www.example.com",
"cancel_url": "https://www.example.com",
"brand_name": "EXAMPLE INC",
"landing_page": "BILLING",
"shipping_preference": "SET_PROVIDED_ADDRESS",
"user_action": "CONTINUE"
},
"purchase_units": [
{
"reference_id": "PUHF",
"description": "Sporting Goods",
"custom_id": "CUST-HighFashions",
"soft_descriptor": "HighFashions",
"amount": {
"currency_code": "USD",
"value": "220.00",
"breakdown": {
"item_total": {
"currency_code": "USD",
"value": "180.00"
},
"shipping": {
"currency_code": "USD",
"value": "20.00"
},
"handling": {
"currency_code": "USD",
"value": "10.00"
},
"tax_total": {
"currency_code": "USD",
"value": "20.00"
},
"shipping_discount": {
"currency_code": "USD",
"value": "10"
}
}
},
"items": [
{
"name": "T-Shirt",
"description": "Green XL",
"sku": "sku01",
"unit_amount": {
"currency_code": "USD",
"value": "90.00"
},
"tax": {
"currency_code": "USD",
"value": "10.00"
},
"quantity": "1",
"category": "PHYSICAL_GOODS"
},
{
"name": "Shoes",
"description": "Running, Size 10.5",
"sku": "sku02",
"unit_amount": {
"currency_code": "USD",
"value": "45.00"
},
"tax": {
"currency_code": "USD",
"value": "5.00"
},
"quantity": "2",
"category": "PHYSICAL_GOODS"
}
],
"shipping": {
"method": "United States Postal Service",
"name": {`enter code here`
"full_name":"John Doe"
},
"address": {
"address_line_1": "123 Townsend St",
"address_line_2": "Floor 6",
"admin_area_2": "San Francisco",
"admin_area_1": "CA",
"postal_code": "94107",
"country_code": "US"
}
}
}
]
}'
result = requests.post(url, data, headers=header)
<code>
this gonna give me 4 links
<pre>
{
"id": "5O190127TN364715T",
"status": "CREATED",
"links": [
{
"href": "https://api.paypal.com/v2/checkout/orders/5O190127TN364715T",
"rel": "self",
"method": "GET"
},
{
"href": "https://www.paypal.com/checkoutnow?token=5O190127TN364715T",
"rel": "approve",
"method": "GET"
},
{
"href": "https://api.paypal.com/v2/checkout/orders/5O190127TN364715T/capture",
"rel": "capture",
"method": "POST"
}
]
}
<code>
What Next ?
First, implement two routes that return only JSON data. One for creating the order and one for capturing it after approval.
For the interim payer approval step, ignore those 4 links which are for old integration patterns that redirect away from your site. In place of that, use the PayPal JS SDK for in-context approval (keeping your site loaded in the background) and have it fetch from those two routes on your server.
Here's the approval flow in pure JS: https://developer.paypal.com/demo/checkout/#/pattern/server
For the same with react, see #paypal/react-papal-js and its storybook examples.

How can I make carousel Flex Message in Line Bot with Django?

I know how to show carousel message with using carousel template, however ,it can't show in Line when using computer. So I want to try not to use carousel template, I have seen these code for carousel Flex Message in JSON ,how can I switch these code to Django format?
{
"type": "carousel",
"contents": [
{
"type": "bubble",
"body": {
"type": "box",
"layout": "vertical",
"contents": [
{
"type": "text",
"text": "First bubble"
}
]
}
},
{
"type": "bubble",
"body": {
"type": "box",
"layout": "vertical",
"contents": [
{
"type": "text",
"text": "Second bubble"
}
]
}
}
]
}

Store list or collection in Apache Avro schema

I'm currently creating Avro schema to store twitter data streams.
My data source in JSON:
{
'id': '123456789',
'text': 'bla bla bla...',
'entities': {
'hashtags': [{'text':'hashtag1'},{'text':'hashtag2'}]
}
}
in Cassandra, I can define collection (sets or lists) to store hashtags data.
But I have no idea how to define this structure in Apache Avro.
Here's my best try:
{"namespace": "ln.twitter",
"type": "record",
"name": "main",
"fields": [
{"name": "id","type": "string"},
{"name": "text","type": "string"},
{"name": "hashtags","type": "string"} // is there any better format for this ?
]
}
Need your advice please.
Thanks,
Yusata.
The entities field needed explicit records (or maps) inside. Here's a schema that should work:
{
"type": "record",
"name": "Main",
"fields": [
{
"name": "id",
"type": "string"
},
{
"name": "text",
"type": "string"
},
{
"name": "entities",
"type": {
"type": "record",
"name": "Entities",
"fields": [
{
"name": "hashtags",
"type": {
"type": "array",
"items": {
"type": "record",
"name": "Hashtag",
"fields": [
{
"name": "text",
"type": "string"
}
]
}
}
}
]
}
}
]
}
In case it's helpful, you can use this tool to generate an (anonymous) Avro schema from any valid JSON record. You'll then just need to add names to the record types.
You can try it on your example after switching its ' to ":
{
"id": "123456789",
"text": "bla bla bla...",
"entities": {"hashtags": [{"text": "hashtag1"}, {"text": "hashtag2"}]}
}

ElasticSearch (AWS): How to use another index as a query/match parameter?

Basically I am trying to implement this strategy.
Sample Data:
PUT /newsfeed_consumer_network/consumer_network/urn:viadeo:member:me
{
"producerIds": [
"urn:viadeo:member:ned",
"urn:viadeo:member:john",
"urn:viadeo:member:mary"
]
}
PUT /newsfeed/news/urn:viadeo:news:33
{
"producerId": "urn:viadeo:member:john",
"published": "2014-12-17T12:45:00.000Z",
"actor": {
"id": "urn:viadeo:member:john",
"objectType": "member",
"displayName": "John"
},
"verb": "add",
"object": {
"id": "urn:viadeo:position:10",
"objectType": "position",
"displayName": "Software Engineer # Viadeo"
},
"target": {
"id": "urn:viadeo:profile:john",
"objectType": "profile",
"displayName": "John's profile"
}
}
Sample Query:
POST /newsfeed/news/_search
{
"query": {
"bool": {
"must": [{
"match": {
"actor.id": {
"producerId": {
"index": "newsfeed_consumer_network",
"type": "consumer_network",
"id": "urn:viadeo:network:me",
"path": "producerIds"
}
}
}
}]
}
}
}
I am getting the following error:
"type": "query_parsing_exception",
"reason": "[match] query does not support [index]"
How can I use an index to support a matching query? Is there any way to implement this?
Basically I just want to use another document as the source of the matching parameter for my query. Is this even possible with ElasticSearch?

How to post model together with embedded model using Loopback.js?

Here is the form model schema:
{
"startTime": "",
"stopTime": "",
"id": "objectid",
"formQuestions": [
{
"type": 0,
"label": "",
"content": [
""
],
"id": "objectid"
}
]
}
relations of form.json:
"relations": {
"questions": {
"type": "embedsMany",
"model": "FormQuestion",
"option": {
"validate": true,
"autoId": true
}
}
}
If I post to http://localhost:3000/api/Forms as follow,
{
"startTime": "",
"stopTime": "",
"formQuestions": [
{
"type": 0,
"label": "a label",
"content": ["the content"]
}
]
}
it returns:
{
"startTime": "",
"stopTime": "",
"id": "54ccf7ae6159f1bc0bc6b430",
"formQuestions": []
}
But I want the embedded model formQuestion also to be inserted into the database, how can I do it? I would be grateful if anyone could help me.
Nobody knows, but I find the way myself.
It is because there is some bug about auto id generating with embedded document. It is useless even I set autoId to true. So if I post with manual id, it will success.
Just like this:
{
"startTime": "",
"stopTime": "",
"formQuestions": [
{
"id": 1,
"type": 0,
"label": "a label",
"content": ["the content"]
}
]
}