From d8585207a48edc48a01dc39116141a9e4ef9c688 Mon Sep 17 00:00:00 2001 From: Davide Bongiovanni Date: Tue, 6 Jun 2017 20:14:47 +0200 Subject: [PATCH] Implemented db modifications. Added manufacturer line to part info view --- server.py | 58 +++++++++++++++++++++++++++------------ static/script.js | 14 ++++++++++ templates/partsearch.html | 7 ++++- 3 files changed, 61 insertions(+), 18 deletions(-) diff --git a/server.py b/server.py index b68aa2e..fc4ab54 100644 --- a/server.py +++ b/server.py @@ -42,6 +42,8 @@ def query(filter, query): s += 'LOWER(partnoalt2) like LOWER(:name) or ' if filter & (1 << 4) != 0: s += 'LOWER(description) like LOWER(:name) or ' + if filter & (1 << 5) != 0: + s += 'LOWER(notes) like LOWER(:name) or ' s = s[:-4] + ';' s = text(s) r = db_engine.execute(s, name=query) @@ -60,24 +62,46 @@ def getfile(filename): @app.route('/parts/alter/', methods=['POST']) def alter(partID): - '''if 'file' not in request.files: - pass + s = '' + if partID < 0: + # New entry + s = 'insert into parts (block, type, partno, partnoalt, partnoalt2, description, quantity, datasheet, notes) ' + s += 'values (:block, :type, :partno, :partnoalt, :partnoalt2, :description, :quantity, :datasheet, :notes);' + s = text(s) + if len(request.files) != 0: + datasheet_file = request.files['datasheet-file'] + datasheet_filename = secure_filename(datasheet_file.filename) + datasheet_file.save('/srv/datasheets/' + filename) + else: + datasheet_filename = '' else: - file = request.files['file'] - filename = secure_filename(file.filename) - file.save(os.path.join('/srv/datasheets', filename))''' - print 'Form:', request.form - print ' ' - print 'Files:', request.files - if len(request.files) == 0: - print 'No file :(' - else: - print 'Oooh look, a file! :)' - somefile = request.files['datasheet-file'] - print somefile.filename - filename = secure_filename(somefile.filename) - somefile.save('/home/davide/' + filename) - print "Now it's mine" + # Modify entry + r = db_engine.execute(text('select * from parts where id=:id;'), id=partID) + l = [] + for row in r: + l.append(dict(row)) + r.close() + s = 'update parts ' + s += 'set block=:block, type=:type, partno=:partno, partnoalt=:partnoalt, partnoalt2=:partnoalt2, description=:description, quantity=:quantity, datasheet=:datasheet, notes=:notes ' + if len(request.files) != 0: + datasheet_file = request.files['datasheet-file'] + datasheet_filename = secure_filename(datasheet_file.filename) + datasheet_file.save('/srv/datasheets/' + filename) + # TODO: Remove old datasheet + else: + datasheet_filename = l['datasheet'] + s += 'where id=:id;' + s = text(s) + + r = db_engine.execute(s, block=request.form['block'], + type=request.form['type'], + partno=request.form['partno'], + partnoalt=request.form['partnoalt'], + partnoalt2=request.form['partnoalt2'], + description=request.form['description'], + quantity=request.form['quantity'], + datasheet=datasheet_filename, + notes=request.form['notes']) return '{"status":"ok"}' def connect(user, password, db, host='localhost', port=5432): diff --git a/static/script.js b/static/script.js index 32346a1..0506409 100644 --- a/static/script.js +++ b/static/script.js @@ -20,6 +20,10 @@ function init_edit(partID) { block_input.val($('#block-info').text()); $('#block-info').replaceWith(block_input); + var type_input = $('') + type_input.val($('#type-info').text()); + $('#type-info').replaceWith(type_input); + var partno_input = $('') partno_input.val($('#partno-info').text()); $('#partno-info').replaceWith(partno_input); @@ -45,6 +49,7 @@ function init_edit(partID) { function new_entry() { $('#block-info').text(''); + $('#type-info').text(''); $('#partno-info').text(''); $('#description-info').text(''); $('#quantity-info').text(''); @@ -57,6 +62,9 @@ function end_edit() { var block_par = '

' + $('#block-input').val() + '

'; $('#block-input').replaceWith(block_par); + var type_par = '

' + $('#type-input').val() + '

'; + $('#type-input').replaceWith(type_par); + var partno_par = '

' + $('#partno-input').val() + '

'; $('#partno-input').replaceWith(partno_par); @@ -78,6 +86,7 @@ function end_edit() { function save(partID) { var block_v = $('#block-input').val(); + var type_v = $('#type-input').val(); var partno_v = $('#partno-input').val(); var description_v = $('#description-input').val(); var quantity_v = $('#quantity-input').val(); @@ -99,6 +108,7 @@ function save(partID) { if (datasheet.length == 1) data.append('datasheet-file', datasheet[0]); data.append('block', block_v); + data.append('type', type_v); data.append('partno', partnos[0]); if (partnos.length > 1) data.append('partnoalt', partnos[1]); @@ -124,6 +134,9 @@ function save(partID) { var datasheet_par = '

'; $('#datasheet-info').replaceWith(datasheet_par); }, + error: function() { + alert("Couldn't update the part information. Please retry."); + } }); @@ -141,6 +154,7 @@ function text_filter(string) { function show_part_info(partID) { $.getJSON('https://www.elab.kth.se/parts/getpartinfo/' + partID, function(data) { $('#block-info').text(text_filter(data.block)); + $('#type-info').text(text_filter(data.type)); $('#partno-info').text(text_filter(data.partno) + ' ' + text_filter(data.partnoalt) + ' ' + text_filter(data.partnoalt2)); $('#description-info').text(text_filter(data.description)); $('#quantity-info').text(text_filter(data.quantity)); diff --git a/templates/partsearch.html b/templates/partsearch.html index 89fba24..89949ed 100644 --- a/templates/partsearch.html +++ b/templates/partsearch.html @@ -40,7 +40,12 @@
Location: block
-

1

+

+
+ +
+
Manufacturer
+