-
Notifications
You must be signed in to change notification settings - Fork 0
/
siteparams.py
76 lines (71 loc) · 2.42 KB
/
siteparams.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
"""Site Params Function"""
import setup # pylint: disable=unused-import, wrong-import-order
import logging
import json
from flask import request
from flask_restful import Resource
from helper_funcs import snake_to_camel
# from flask_restful import reqparse
import pymysql
from params import Params
logging.basicConfig(
filename="kumpe3d-api.log",
filemode="a",
format="%(asctime)s: [%(name)s] [%(levelname)s] %(message)s",
level=Params.log_level(),
)
logger = logging.getLogger("SiteParams")
class SiteParams(Resource):
"""Site Params Functions"""
def get(self):
"""Get Site Params"""
sql_params = Params.SQL
db = pymysql.connect(
db=sql_params.database,
user=sql_params.username,
passwd=sql_params.password,
host=sql_params.server,
port=3306,
)
logger.debug("start get")
base_url = Params.base_url
logger.debug("create cursor")
cursor = db.cursor(pymysql.cursors.DictCursor)
sql = "SELECT parameter, value, type FROM Web_3dprints.site_parameters;"
cursor.execute(sql)
logger.debug(sql)
logger.debug("Get Product Pricing")
params = cursor.fetchall()
cursor.close()
db.close()
response = {}
for param in params:
parameter = snake_to_camel(param["parameter"])
if param["type"] == "int":
response[parameter] = json.loads(param["value"])
elif param["type"] == "bool":
response[parameter] = bool(param["value"])
elif param["type"] == "json":
response[parameter] = json.loads(param["value"])
else:
response[parameter] = param["value"]
logger.debug(response)
try:
referrer = request.environ["HTTP_REFERER"]
except KeyError:
referrer = "none"
logger.info("Referrer: %s", referrer)
if referrer != base_url + "/" and referrer != base_url:
logger.error("Returned 401 Unauthorized")
return (
{"error": "Unauthorized"},
401,
{"Access-Control-Allow-Origin": "*"},
)
else:
logger.info("Returned 200 Success")
return (
{"response": response, "status_code": 200},
200,
{"Access-Control-Allow-Origin": "*"},
)