diff --git a/parts/server.py b/parts/server.py index 6173990..cdcff6f 100644 --- a/parts/server.py +++ b/parts/server.py @@ -30,24 +30,24 @@ def getContainers(): return containers def check_auth(username, password): - admin_list = [] - with open('edit_admin.json', 'r') as admin: - admin_list = json.load(admin) - for user in admin_list: - if username == user['username']: - return password == user['password'] + admin_list = [] + with open('edit_admin.json', 'r') as admin: + admin_list = json.load(admin) + for user in admin_list: + if username == user['username']: + return password == user['password'] def authenticate(): - return Response('Could not verify access level. Please retry', 401, {'WWW-Authenticate' : 'Basic realm="Login Required"'}) + return Response('Could not verify access level. Please retry', 401, {'WWW-Authenticate' : 'Basic realm="Login Required"'}) def requires_auth(f): - @wraps(f) - def decorated(*args, **kwargs): - auth = request.authorization - if not auth or not check_auth(auth.username, auth.password): - return authenticate() - return f(*args, **kwargs) - return decorated + @wraps(f) + def decorated(*args, **kwargs): + auth = request.authorization + if not auth or not check_auth(auth.username, auth.password): + return authenticate() + return f(*args, **kwargs) + return decorated def serveImage(img): img_io = BytesIO() @@ -90,7 +90,7 @@ def locationEditor(): return render_template('locationEditor.html', locations=locations, containers=getContainers()) @app.route('/parts/alterLocation/', methods=['POST']) -# @requires_auth +@requires_auth def alterLocation(locationID): locationID = int(locationID) s = '' @@ -152,11 +152,7 @@ def query(filter_dummy, query): s += 'LOWER(description) like LOWER(:kw'+ str(i) +') AND ' s = s[:-5] s += ') OR ' - if filter['n'] == 'true': - s = s[:-4] - s += "AND (datasheet='' IS NOT FALSE) IS FALSE OR " s = s[:-4] + ';' - print(s) s = text(s) r = db_engine.execute(s, kw_dict) l = [] @@ -198,7 +194,7 @@ def getfile(filename): return send_from_directory('/srv/datasheets/', filename + '.pdf') @app.route('/parts/alter/', methods=['POST']) -# @requires_auth +@requires_auth def alter(partID): partID = int(partID) s = '' @@ -212,12 +208,18 @@ def alter(partID): datasheet_file = request.files['datasheet-file'] datasheet_filename = secure_filename(datasheet_file.filename) i = 1 - while os.path.isfile('srv/datasheet/' + datasheet_filename): + while os.path.isfile('srv/datasheets/' + datasheet_filename): datasheet_filename = datasheet_filename[:-4] + str(i) + '.pdf' i += 1 datasheet_file.save('/srv/datasheets/' + datasheet_filename) + datasheet_filename = 'http://elab.kth.se/parts/getfile/' + datasheet_filename + # elif request.form.has_key('datasheet-url'): + # datasheet_filename = request.form['datasheet-url'] else: - datasheet_filename = None + try: + datasheet_filename = request.form['datasheet-url'] + except: + datasheet_filename = None r = db_engine.execute(s, partno=request.form['partno'], description=request.form['description'], datasheet=datasheet_filename, @@ -235,14 +237,20 @@ def alter(partID): datasheet_file = request.files['datasheet-file'] datasheet_filename = secure_filename(datasheet_file.filename) i = 1 - while os.path.isfile('srv/datasheet/' + datasheet_filename): + while os.path.isfile('srv/datasheets/' + datasheet_filename): datasheet_filename = datasheet_filename[:-4] + str(i) + '.pdf' i += 1 datasheet_file.save('/srv/datasheets/' + datasheet_filename) + datasheet_filename = 'http://elab.kth.se/parts/getfile/' + datasheet_filename if l[0]['datasheet'] != None: os.remove('/srv/datasheets/' + l[0]['datasheet']) + # elif request.form.has_key('datasheet-url'): + # datasheet_filename = request.form['datasheet-url'] else: - datasheet_filename = l[0]['datasheet'] + try: + datasheet_filename = request.form['datasheet-url'] + except: + datasheet_filename = l[0]['datasheet'] s += 'where id=:id returning id;' s = text(s) r = db_engine.execute(s, partno=request.form['partno'], diff --git a/parts/static/script.js b/parts/static/script.js index d52a1b0..42aacec 100644 --- a/parts/static/script.js +++ b/parts/static/script.js @@ -4,18 +4,21 @@ function init_edit(partID) { $('#container-dropdown').show(); $('table#details tr#partno td p').hide(); - $('table#details tr#partno td input').show(); + $('input[name=partno-input]').show(); $('#magical_autofill').show(); $('table#details tr#description td p').hide(); - $('table#details tr#description td input').show(); + $('input[name=description-input]').show(); + $('table#details tr#datasheet td input').closest().show(); $('table#details tr#datasheet td input').show(); + // $('input[name=datasheet-url-input]').closest().show(); $('#duplicate-button').closest('div').hide(); - var datasheet_input = $('') - $('#datasheet-info').replaceWith(datasheet_input); + $('tr#datasheet').show(); + // var datasheet_input = $('') + // $('#datasheet-info').replaceWith(datasheet_input); var newButton = '
'; $('.round-button-left').replaceWith(newButton); @@ -27,6 +30,7 @@ function new_entry() { $('table#details tr#description td p').text(''); $('table#details tr#partno td input').val(''); $('table#details tr#description td input').val(''); + $('input[name=datasheet-url-input]').val(''); container_onchange(); init_edit(-1); overlay_in(); @@ -49,7 +53,10 @@ function end_edit() { $('table#details tr#description td p').show(); $('table#details tr#description td input').hide(); - $('table#details tr#datasheet td input').hide(); + $('tr#datasheet').hide(); + // $('table#details tr#datasheet td input').closest().hide(); + // $('table#details tr#datasheet td input').hide(); + // $('input[name=datasheet-url-input]').closest().hide(); $('#duplicate-button').closest('div').show(); @@ -58,10 +65,15 @@ function end_edit() { } function save(partID) { + if (!$('#location-dropdown').val()) { + alert('Please select a location.'); + return; + } var location_id_v = $('#location-dropdown').val(); var partno_v = $('input[name=partno-input]').val(); var description_v = $('input[name=description-input]').val(); var datasheet = $('table#details tr#datasheet td input')[0].files; + var datasheet_url_v = $('input[name=datasheet-url-input]').val(); if(partno_v.length == 0){ alert('Please enter a part number.'); return; @@ -86,6 +98,8 @@ function save(partID) { return; } data.append('datasheet-file', datasheet[0]); + } else if (datasheet_url_v.length > 0) { + data.append('datasheet-url', datasheet_url_v); } data.append('partno', partno_v); data.append('location_id', location_id_v) @@ -159,8 +173,8 @@ function show_part_info(partID) { $('table#details tr#description td input').val(text_filter(data.description)); container_onchange(); if (data.datasheet != null) { - $('tr#datasheet-head').html($('DATASHEET: ')); - $('#datasheet-input').val(data.datasheet); + $('tr#datasheet-head').html($('DATASHEET: ')); + $('input[name=datasheet-url-input]').val(data.datasheet); } else $('tr#datasheet-head td').text('DATASHEET: '); @@ -183,8 +197,7 @@ function perform_query() { var data = { l:$('#location').is(':checked'), p:$('#partno').is(':checked'), - d:$('#description').is(':checked'), - n:$('#has-docs').is(':checked') + d:$('#description').is(':checked') }; filter = '0'; $("#results").find("tr:not(:first)").remove(); // Delete all table rows @@ -213,7 +226,7 @@ function container_onchange() { $('#location-dropdown').empty(); $.getJSON('parts/getlocationsInContainer/' + selected_container_id, function(data) { $.each(data, function(location_id, location_name) { - $('#location-dropdown').append(''); + $('#location-dropdown').append(''); }); }); } diff --git a/parts/static/style.css b/parts/static/style.css index 77b4320..ff0aa90 100644 --- a/parts/static/style.css +++ b/parts/static/style.css @@ -68,7 +68,7 @@ input[type=text].search-bar { color: #D7E2E2; font-size: 10pt; margin-top: 4pt; - margin-left: 16pt; + margin-left: 3pt; margin-bottom: 4pt; } @@ -152,7 +152,7 @@ div label input { height: 100%; left: 0; opacity: 0.0; - position: absolute; + position: fixed; top: 0; width: 100%; z-index: 200; @@ -172,7 +172,6 @@ div label input { min-width: 60%; width:900px; z-index: 201; - border-radius: 2pt; text-align: center; opacity: 0.0; } @@ -308,7 +307,7 @@ table#results td { } table#results #location { - width: 15%; + width: 18%; } table#results #partno { diff --git a/parts/templates/partsearch.html b/parts/templates/partsearch.html index e541601..8e4eca3 100644 --- a/parts/templates/partsearch.html +++ b/parts/templates/partsearch.html @@ -23,11 +23,10 @@
-

Search in:

+

SEARCH IN

-
@@ -62,7 +61,8 @@ - + +

DATASHEET: