|
|
|
@ -1,6 +1,6 @@
|
|
|
|
|
import os
|
|
|
|
|
import re
|
|
|
|
|
import json
|
|
|
|
|
import json, urllib.parse, urllib.request
|
|
|
|
|
import sqlalchemy
|
|
|
|
|
from functools import wraps
|
|
|
|
|
from sqlalchemy.sql import select
|
|
|
|
@ -18,6 +18,7 @@ app = Flask(__name__)
|
|
|
|
|
db_engine = {}
|
|
|
|
|
db_metadata = {}
|
|
|
|
|
parts = {}
|
|
|
|
|
octopartURL =""
|
|
|
|
|
|
|
|
|
|
def getContainers():
|
|
|
|
|
query = "select id, name from containers order by UPPER(name);"
|
|
|
|
@ -272,6 +273,26 @@ def deleteLocation(locationID):
|
|
|
|
|
r = db_engine.execute(s, id=locationID)
|
|
|
|
|
return '{"status":"ok"}'
|
|
|
|
|
|
|
|
|
|
@app.route('/parts/fetchOctopartSnippet/<searchTerm>')
|
|
|
|
|
def fetchOctopartSnippet(searchTerm):
|
|
|
|
|
if octopartURL == '':
|
|
|
|
|
return '{"result":"octopart integration not enabled"}'
|
|
|
|
|
args = [
|
|
|
|
|
('q', searchTerm),
|
|
|
|
|
('start', 0),
|
|
|
|
|
('limit', 1)
|
|
|
|
|
]
|
|
|
|
|
data = urllib.request.urlopen(octopartURL + '&' + urllib.parse.urlencode(args)).read()
|
|
|
|
|
search_response = json.loads(data)
|
|
|
|
|
|
|
|
|
|
result = '{"result":"no results. sorry :(("}'
|
|
|
|
|
if search_response['hits']>0:
|
|
|
|
|
try:
|
|
|
|
|
result = '{"result":"ok", "snippet":"' + (search_response['results'][0]['snippet']) + '"}';
|
|
|
|
|
except TypeError:
|
|
|
|
|
result = '{"result":"no results?"}'
|
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
|
def connect(user, password, db, host='localhost', port=5432):
|
|
|
|
|
'''Returns a connection and a metadata object'''
|
|
|
|
|
# We connect with the help of the PostgreSQL URL
|
|
|
|
@ -291,6 +312,16 @@ if __name__ == '__main__':
|
|
|
|
|
postgres_credentials = json.load(f)
|
|
|
|
|
db_engine, db_metadata = connect(postgres_credentials['username'], postgres_credentials['password'], 'parts_v2')
|
|
|
|
|
parts = sqlalchemy.Table('parts', db_metadata)
|
|
|
|
|
try:
|
|
|
|
|
with open('octopartAPIkey.json') as f:
|
|
|
|
|
j = json.load(f);
|
|
|
|
|
if j['key'] is not '':
|
|
|
|
|
octopartURL = j['URL'] + j['key']
|
|
|
|
|
print ("Octopart credentials loaded.")
|
|
|
|
|
else:
|
|
|
|
|
raise FileNotFoundError
|
|
|
|
|
except FileNotFoundError:
|
|
|
|
|
print ("NO OCTOPART KEY FOUND. ABANDONING THAT PART")
|
|
|
|
|
# Example query
|
|
|
|
|
'''s = select([parts]).where(parts.c.notes != '')
|
|
|
|
|
for row in db_engine.execute(s):
|
|
|
|
|