-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
94 lines (82 loc) · 2.92 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
from flask import Flask, render_template, make_response
import requests
from functools import wraps, update_wrapper
from datetime import datetime
app = Flask(__name__,
static_url_path='/resources',
static_folder='static',
template_folder='templates')
store_name = '866e98-8d'
access_token = '1a0f8b8243b980d0407f15535f6518b2'
graphql_endpoint = f'https://{store_name}.myshopify.com/api/2024-07/graphql.json'
def nocache(view):
@wraps(view)
def no_cache(*args, **kwargs):
response = make_response(view(*args, **kwargs))
response.headers['Last-Modified'] = datetime.now()
response.headers['Cache-Control'] = 'no-store, no-cache, must-revalidate, post-check=0, pre-check=0, max-age=0'
response.headers['Pragma'] = 'no-cache'
response.headers['Expires'] = '-1'
return response
return update_wrapper(no_cache, view)
@nocache
@app.route('/')
def home():
return render_template("index.html")
@nocache
@app.route('/product/<id>')
def product(id):
query = """
{
product(id: "gid://shopify/Product/%s") {
id
title
description
productType
variants(first: 10) {
edges {
node {
id
title
priceV2 {
amount
currencyCode
}
quantityAvailable
}
}
}
metafield(namespace: "custom", key: "short_description") {
value
}
images(first: 1) {
edges {
node {
src
}
}
}
}
}
""" % id
headers = {
'Content-Type': 'application/json',
'X-Shopify-Storefront-Access-Token': access_token
}
response = requests.post(graphql_endpoint, json={
'query': query}, headers=headers)
productinfo = response.json()["data"]["product"]
print(productinfo)
variants = [item["node"]
for item in productinfo["variants"]["edges"]]
description = productinfo["description"]
shortdescription = productinfo["metafield"]["value"]
title = productinfo["title"]
stock = str(variants[0]["quantityAvailable"])
imageurl = productinfo["images"]["edges"][0]["node"]["src"]
pricedict = productinfo["variants"]["edges"][0]["node"]["priceV2"]
price = f"${float(pricedict['amount']):.2f} {pricedict['currencyCode']}"
return render_template("product.html", PRODUCT_DESCRIPTION=description, PRODUCT_TITLE=title, PRODUCT_STOCK=stock, PRODUCT_MAINIMAGE=imageurl, SHORT_DESCRIPTION=shortdescription, PRODUCT_PRICE=price)
@app.route('/teapot')
def iamateapot():
return "<img src=\"https://raw.githubusercontent.com/hiroharu-kato/neural_renderer/master/examples/data/example1.gif\">", 418