-
Notifications
You must be signed in to change notification settings - Fork 0
/
extensions.py
executable file
·71 lines (50 loc) · 2.37 KB
/
extensions.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
#!/usr/bin/env python
import requests
import json
from time import strftime
from datetime import datetime
from custom_httpauth import HTTPTokenAuth
from flask_sqlalchemy import SQLAlchemy
from flask_alembic import Alembic
from flask import jsonify, make_response, abort
db = SQLAlchemy()
auth = HTTPTokenAuth()
alembic = Alembic()
dateFormat = "%Y-%m-%d"
timeFormat = "%H:%M:%S"
dateTimeFormat = dateFormat + "T" + timeFormat
def dataResultSuccess(data, msg="", spuriousParameters=[], count=1, code=200):
return make_response((jsonify({"success": True, "msg": msg, "spuriousparameters": spuriousParameters, "data": data, "count": count}), code))
def resultSuccess(msg="", spuriousParameters=[], code=200):
return make_response((jsonify({"success": True, "msg": msg, "spuriousparameters": spuriousParameters}), code))
def resultFailure(msg, code):
abort(make_response(jsonify({"success": False, "msg": msg}), code))
def getCurrenciesFromAPI():
# call the currency API
response = requests.get('http://openexchangerates.org/api/latest.json?app_id=79cbcd6eacd64256945500440eabcff9')
# remove the b'' and \n in the string (after converting the response to a string)
responseString = str(response.content).replace(r"\n", "")
responseString = responseString.replace(r" ", "")
responseString = responseString.lstrip('b')
responseString = responseString.strip('\'')
# get only the rates object (since we don't care about much else in the response)
responseJson = json.loads(responseString)['rates']
return responseJson
def queryToJson(queryParam):
newQueryParam = "{"
queryParam = queryParam.replace(' ', '') # Remove all whitespaces
queryList = queryParam.split(',') # split the string into queries
for part in queryList:
subParts = part.split(':') # further split things into key, value pairs
newQueryParam += '"' + subParts[0] + '":"' + subParts[1] + '",' # reconstruct the key value pairs in the new parameter
newQueryParam = newQueryParam[:-1] # remove the last trailing comma
newQueryParam += '}' # finish by adding the closing bracket
return json.loads(newQueryParam)
def dateTimeNow():
return strftime(dateTimeFormat)
def timeNow():
return strftime(timeFormat)
def dateNow():
return strftime.strftime(dateFormat)
def stringToDateTime(input):
return datetime.strptime(input, '%Y-%m-%d')