Merge branch 'master' of git.elab.kth.se:davide/ELAB-partsearch
# Conflicts: # parts/static/script.js
This commit is contained in:
@@ -0,0 +1 @@
|
||||
{"URL":"http://octopart.com/api/v3/parts/search?apikey=", "key":""}
|
||||
+32
-1
@@ -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/<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
|
||||
@@ -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):
|
||||
|
||||
@@ -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() {
|
||||
|
||||
@@ -56,9 +56,10 @@
|
||||
</select>
|
||||
</td></tr>
|
||||
<tr id="partno-head"><td>PART NUMBER</td></tr>
|
||||
<tr id="partno"><td><p></p><input type="text" name="partno-input" placeholder="Part Number" class="pinfo-input"/></td></tr>
|
||||
<tr id="partno"><td><p></p><input type="text" name="partno-input" placeholder="Part Number" class="pinfo-input" id="partno-input"/></td></tr>
|
||||
<tr id="description-head"><td>DESCRIPTION</td></tr>
|
||||
<tr id="description"><td><p></p><input type="text" name="description-input" placeholder="Description" class="pinfo-input"/></td></tr>
|
||||
<tr id="description"><td><p></p><input type="text" name="description-input" placeholder="Description" class="pinfo-input" id="description-input"/></td></tr>
|
||||
<tr id="magical_autofill" style="display:none"><td><input type="button" id="magic-autofill-button" onclick="octopartFetch()" value="magical auto-fill" /></td></tr>
|
||||
<tr id="datasheet-head"><td>DATASHEET: </td></tr>
|
||||
<tr id="datasheet" style="display: none"><td><input type="file" accept=".pdf" class="pinfo-input" id="datasheet-finput"></td></tr>
|
||||
<tr id="datasheet" style="display: none"><td>OR: <input type="text" class="pinfo-input" name="datasheet-url-input" style="width:80%" placeholder="Datasheet URL"></td></tr>
|
||||
|
||||
Reference in New Issue
Block a user