diff --git a/parts/octopartAPIkey.json b/parts/octopartAPIkey.json new file mode 100644 index 0000000..27e6045 --- /dev/null +++ b/parts/octopartAPIkey.json @@ -0,0 +1 @@ +{"URL":"http://octopart.com/api/v3/parts/search?apikey=", "key":""} \ No newline at end of file diff --git a/parts/server.py b/parts/server.py index 1108b5e..cdcff6f 100644 --- a/parts/server.py +++ b/parts/server.py @@ -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);" @@ -280,6 +281,26 @@ def deleteLocation(locationID): r = db_engine.execute(s, id=locationID) return '{"status":"ok"}' +@app.route('/parts/fetchOctopartSnippet/') +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 @@ -299,6 +320,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): diff --git a/parts/static/script.js b/parts/static/script.js index 02bd755..42aacec 100644 --- a/parts/static/script.js +++ b/parts/static/script.js @@ -5,6 +5,7 @@ function init_edit(partID) { $('table#details tr#partno td p').hide(); $('input[name=partno-input]').show(); + $('#magical_autofill').show(); $('table#details tr#description td p').hide(); $('input[name=description-input]').show(); @@ -46,6 +47,8 @@ function end_edit() { $('table#details tr#partno td p').show(); $('table#details tr#partno td input').hide(); + $('#magical_autofill').hide(); + $('table#details tr#description td p').text($('table#details tr#description td input').val()); $('table#details tr#description td p').show(); $('table#details tr#description td input').hide(); @@ -229,6 +232,18 @@ function container_onchange() { } } + +function octopartFetch(){ + $.getJSON(rootURL + 'fetchOctopartSnippet/' + $('#partno-input').val()).done(function(json){ + if (json['result']=='ok'){ + $('#description-input').val(json['snippet']); + }else{ + $('#description-input').val(''); + $('#description-input').attr('placeholder', json['result']); + } + }); +} + $(document).ready(function() { $.ajaxSetup({ cache: false }); $('.search-bar').on('keyup', function() { diff --git a/parts/templates/partsearch.html b/parts/templates/partsearch.html index 7165845..8e4eca3 100644 --- a/parts/templates/partsearch.html +++ b/parts/templates/partsearch.html @@ -56,9 +56,10 @@ PART NUMBER -

+

DESCRIPTION -

+

+ DATASHEET: OR: