From 00a2d6f0701a0887849724f649a3db70d27ef632 Mon Sep 17 00:00:00 2001 From: Marek Baczynski Date: Sun, 11 Nov 2018 00:52:30 +0100 Subject: [PATCH] location editor (MVP - can only add locations) --- parts/server.py | 39 +++++++++++++++- parts/static/locationEditorScript.js | 67 ++++++++++++++++++++++++++++ parts/templates/locationEditor.html | 60 +++++++++++++++++++++++++ 3 files changed, 164 insertions(+), 2 deletions(-) create mode 100644 parts/static/locationEditorScript.js create mode 100644 parts/templates/locationEditor.html diff --git a/parts/server.py b/parts/server.py index 9efaac6..1cf7ed4 100644 --- a/parts/server.py +++ b/parts/server.py @@ -9,6 +9,8 @@ from flask import Flask from flask import render_template, send_from_directory, request, Response, send_file from PIL import Image, ImageDraw from io import BytesIO +from os import listdir +from os.path import isfile, join from werkzeug.utils import secure_filename app = Flask(__name__) @@ -67,6 +69,41 @@ def get_locationURL(locationID): r.close() return l[0][0] +@app.route('/parts/locationEditor') +def locationEditor(): + query = 'select * from locations order by name;' + r = db_engine.execute(text(query)) + locations = [] + for row in r: + locations.append(dict(row)) + r.close() + know_not_map_files = ['here.png', '404.png', '.DS_Store'] + mapfiles = [f for f in listdir('maps') if isfile(join('maps', f)) and f not in know_not_map_files] + return render_template('locationEditor.html', locations=locations, mapfiles=mapfiles, defaultMapfile="elab.png") + +@app.route('/parts/alterLocation/', methods=['POST']) +# @requires_auth +def alterLocation(locationID): + locationID = int(locationID) + s = '' + if locationID < 0: + # New entry + s = 'insert into locations (name, map) ' + s += 'values (:name, :map);' + s = text(s) + r = db_engine.execute(s,name=request.form['name'],map=request.form['map']); + r.close() + return '{"status":"ok"}' + else: + # Modify entry + s = 'update locations ' + s += 'set name=:name, map=:map, ' + s += 'where id=:locationID;' + s = text(s) + r = db_engine.execute(s, name=request.form['name'],map=request.form['map'],locationID=locationID); + r.close() + return '{"status":"ok"}' + @app.route('/parts/getpartinfo/') def get_part_info(partID): s = 'select * from parts as p inner join locations as l on p.location_id=l.id where p.id = :id;' @@ -196,8 +233,6 @@ def alter(partID): description=request.form['description'], datasheet=datasheet_filename, location_id=request.form['location_id']) - - return '{"status":"ok"}' @app.route('/parts/delete/') diff --git a/parts/static/locationEditorScript.js b/parts/static/locationEditorScript.js new file mode 100644 index 0000000..8511890 --- /dev/null +++ b/parts/static/locationEditorScript.js @@ -0,0 +1,67 @@ +function new_location_entry() { + $('#location-name-input').text(''); + $('#location-name-input').show(); + $('#mapfile-input').show(); + init_Location_edit(-1); + overlay_in(); +} + +function init_Location_edit(locationID) { + + $('table#details tr#datasheet td input').show(); + + var newButton = '
'; + $('.round-button-left').replaceWith(newButton); +} + +function update_clickable_map() { + var selected_map_file = $('#mapfile-input').val(); + $('#clickablemap').attr('src', 'map/' + selected_map_file); +} + + +function saveLocation(locationID) { + var map_v = $('#clickablemap').attr('src').substring(4); + var name_v = $('#location-name-input').val(); + + if(name_v.length > 100) { + alert('Name too long (max 100 characters).') + return; + } + var data = new FormData(); + + data.append('map', map_v); + data.append('name', name_v) + + $.ajax({ + url: rootURL + 'alterLocation/' + locationID, + type: 'POST', + data: data, + cache: false, + contentType: false, + processData: false, + success: function(data) { + alert("k."); + }, + error: function() { + alert("Couldn't update the part information. Please retry."); + } + }); + + end_edit(); + $('#edit-button').click(function() { + init_edit(partID); + }); +} + +function placeMarker(locationID){ + var $img = $('#clickablemap'); + var currentClickPosX = event.pageX - $img.offset().left; + var currentClickPosY = event.pageY - $img.offset().top; + + var correctX = (($img.prop('naturalWidth') / $img.width()) * currentClickPosX).toFixed(0); + var correctY = (($img.prop('naturalHeight') / $img.height()) * currentClickPosY).toFixed(0); + + // $("#mapURL").html("elab.png?x=" + correctX + "&y=" + correctY); + $("#clickablemap").attr("src", "map/" + $("#mapfile-input").val() + "?x=" + correctX + "&y=" + correctY); +} \ No newline at end of file diff --git a/parts/templates/locationEditor.html b/parts/templates/locationEditor.html new file mode 100644 index 0000000..973b1c4 --- /dev/null +++ b/parts/templates/locationEditor.html @@ -0,0 +1,60 @@ + + + + + ELAB Part Search Engine + + + + + + + + +

ELAB Part Search Engine - LOCATION EDITOR

+

Looking for a place to store your obsolete ICs discontinued years ago? Just toss them anywhere and mark that location here!

+ + + + + + + {% for location in locations %} + + + + + + {% endfor %} +
IDLocation nameMap URL
{{location['id']}}{{location['name']}}{{location['map']}}
+
+ +
+

Part Details

+ + + + + + + + + +
Name

MAP
+ +
And click on the map to place the marker
+ +
+
+
+
+
+ +