def ajaxPermutePosition(): globalVars = tools.initApp() from_id = request.args.get('from_id') dest_id = request.args.get('dest_id') pos_from = db.get_position_for_book(session['app_id'], from_id) pos_dest = db.get_position_for_book(session['app_id'], dest_id) #test interval equality before permutation if pos_from['range'] != pos_dest['range']: message = {'success': False, 'dest_range': pos_dest['range']} else: #clean old positions db.del_item_position(session.get('app_id'), from_id, 'book', pos_from['row']) db.del_item_position(session.get('app_id'), dest_id, 'book', pos_dest['row']) #add new position for first book led_column_1 = db.set_position(session.get('app_id'), from_id, pos_dest['position'], pos_dest['row'], \ pos_dest['range'], 'book', pos_dest['led_column']) #add new position for destination book led_column_2 = db.set_position(session.get('app_id'), dest_id, pos_from['position'], pos_from['row'], \ pos_from['range'], 'book', pos_from['led_column']) message = { 'success': True, from_id: led_column_1, dest_id: led_column_2 } response = app.response_class(response=json.dumps(message), mimetype='application/json') return response
def getBook(book_id): globalVars = tools.initApp() if globalVars['arduino_map'] != None: book = db.get_book(book_id, globalVars['arduino_map']['user_id']) if book: book['address']=None book['hasRequest']=None book['categories'] = [] tags = db.get_tag_for_node(book['id'], 1)#book tags for taxonomy categories if tags: for i in range(len(tags)): book['categories'].append(tags[i]['tag']) app_modules = db.get_arduino_for_user(flask_login.current_user.id) for module in app_modules: address = db.get_position_for_book(module['id'], book['id']) if address: hasRequest = db.get_request_for_position(module['id'], address['position'], address['row']) book['address'] = address book['arduino_name'] = module['arduino_name'] book['app_id'] = module['id'] book['hasRequest'] = hasRequest #send json when token mode if('api' in request.path and 'token' in request.args): response = app.response_class( response=json.dumps(book), mimetype='application/json' ) return response return render_template('book.html', user_login=globalVars['user_login'], book=book, \ shelf_infos=globalVars['arduino_map'], biblio_nb_rows=globalVars['arduino_map']['nb_lines']) abort(404)
def ajaxSort(): globalVars = tools.initApp() book_ids = [] if request.method == 'POST' and session.get('app_id'): if request.json and 'book_ids' in request.json: book_ids = request.json['book_ids'] current_row = request.json['row'] elif 'row' in request.form: current_row = request.form['row'] book_ids = request.form.getlist('book[]') if len(book_ids) > 0: app_id = session.get('app_id') i = 0 sortable = [] for book_id in book_ids: position = db.get_position_for_book(app_id, book_id) if position: interval = position['range'] else: book = db.get_book( book_id, globalVars['arduino_map']['user_id']) interval = tools.led_range( book, globalVars['arduino_map']['leds_interval']) i += 1 db.set_position(app_id, book_id, i, current_row, interval, 'book', 0) #reinit led column #update new leds number positions = db.get_positions_for_row(app_id, current_row) for pos in positions: led_column = db.get_led_column(app_id, pos['id_item'], current_row, pos['position']) db.set_led_column(app_id, pos['id_item'], current_row, led_column) sortable.append({ 'book': pos['id_item'], 'position': pos['position'], 'fulfillment': int(led_column + pos['range']), 'shelf': current_row }) #save order for customcodes if 'customcode' in request.form: code_ids = request.form.getlist('code[]') sortable = db.sort_customcodes( globalVars['arduino_map']['user_id'], session.get('app_id'), code_ids) response = app.response_class(response=json.dumps(sortable), mimetype='application/json') return response
def ajaxSearch(): globalVars = tools.initApp() term = request.args.get('term') data = {} if len(term) > 2: results = db.search_book(session['app_id'], term) #search for mobile app if request.method == 'GET' and request.args.get('token'): books = [] #when results if results: #search for tag tag_id = None tag_color = None tag = db.get_tag(term) if tag: tag_id = tag['id'] tag_color = tag['color'] for i in range(len(results)): book = db.get_book(results[i]['id'], globalVars['arduino_map']['user_id']) books.append(book) app_modules = db.get_arduino_for_user(flask_login.current_user.id) for module in app_modules: address = db.get_position_for_book(module['id'], book['id']) if address: books[i]['address'] = address books[i]['hasRequest'] = False #for display mode, force set request if 'display' in request.args and int(request.args.get('display'))==1: db.set_request(session['app_id'], book['id'], address['row'], address['position'], \ address['range'], address['led_column'], 'book', 'server', 'add', tag_id, tag_color) books[i]['hasRequest'] = True data['list_title'] = str(len(results)) data['list_title'] += " books " if len(results) > 1 else " book " data['list_title'] += "for \""+request.args.get('term')+"\"" #no result else: data['list_title'] = "No result for \""+request.args.get('term')+"\"" data['items'] = books #autcomplete for book permutation for server else: data = [] if results: for result in results: data.append({'id':result['id'], 'label':result['author']+' - '+result['title'], \ 'value':result['title']}) response = app.response_class( response=json.dumps(data), mimetype='application/json' ) return response
def locateBooksForTag(tag_id, methods=['GET', 'POST']): globalVars = tools.initApp() nodes = db.get_node_for_tag(tag_id, globalVars['arduino_map']['user_id']) tag = db.get_tag_by_id(tag_id, globalVars['arduino_map']['user_id']) if tag['color'] is not None: colors = tag['color'].split(",") tag['red'] = colors[0] tag['green'] = colors[1] tag['blue'] = colors[2] action = 'add' if ('action' in request.args): #for add or remove action = request.args.get('action') client = 'server' if ('token' in request.args): client = 'mobile' positions = [] for node in nodes: address = db.get_position_for_book(session['app_id'], node['id_node']) if address: book = db.get_book(node['id_node'], globalVars['arduino_map']['user_id']) #manage request db.set_request(session['app_id'], node['id_node'], address['row'], address['position'], address['range'], \ address['led_column'], 'book', client, action, tag_id, tag['color']) if tag['color'] is None: tag['color'] = '' positions.append({'item':book['title'], 'action':action, 'row':address['row'], 'led_column':address['led_column'], \ 'interval':address['range'], 'id_tag':tag_id, 'color':tag['color'], 'id_node':node['id_node'], 'client':client}) '''get elements for block build''' positions.sort(key=tools.sortPositions) blocks = tools.build_block_position(positions, action) #print(blocks) #send json when token mode if ('api' in request.path and 'token' in request.args): response = app.response_class(response=json.dumps(blocks), mimetype='application/json') return response for i, book in enumerate(positions): flash('Location requested for book {}'.format(book['item']), 'info') return redirect(url_for('listAuthors', _scheme='https', _external=True))
def borrowBook(): globalVars = tools.initApp() ret = [] if('api' in request.path and 'token' in request.args): app_id = session['app_id'] book_id = request.args.get('book_id') action = request.args.get('action') db.set_borrow_book(app_id, book_id, action) address = db.get_position_for_book(app_id, book_id) ret.append({'item':book_id,'action':'borrow','address':address}) response = app.response_class( response=json.dumps(ret), mimetype='application/json' ) return response
def listNodesForTag(tag_id): globalVars = tools.initApp() if globalVars['arduino_map'] != None: nodes = db.get_node_for_tag(tag_id, globalVars['arduino_map']['user_id']) tag = db.get_tag_by_id(tag_id, globalVars['arduino_map']['user_id']) data = {} books = [] data['list_title'] = tag['tag'] client = 'server' if('token' in request.args): client = 'mobile' if nodes: #for node in nodes: for i in range(len(nodes)): book = db.get_book(nodes[i]['id_node'], globalVars['arduino_map']['user_id']) books.append(book) app_modules = db.get_arduino_for_user(flask_login.current_user.id) for module in app_modules: address = db.get_position_for_book(module['id'], book['id']) if address: hasRequest = db.get_request_for_position(module['id'], address['position'], address['row']) books[i]['address'] = address books[i]['arduino_name'] = module['arduino_name'] books[i]['uuid_encode'] = tools.uuid_encode(module['id_ble']) books[i]['app_id'] = module['id'] books[i]['app_uuid'] = module['uuid'] books[i]['app_mac'] = module['mac'] books[i]['hasRequest'] = hasRequest if tag['color'] is not None: books[i]['color'] = tag['color'] data['items'] = books #send json when token mode if('api' in request.path and 'token' in request.args): response = app.response_class( response=json.dumps(data), mimetype='application/json' ) return response return render_template('tag.html', books=books, user_login=globalVars['user_login'], \ shelf_infos=globalVars['arduino_map'], tag=tag) abort(404)
def ajaxDelPosition(): globalVars = tools.initApp() if request.method == 'POST' and session.get('app_id'): for elem in request.form: book = elem.split('_') #vars book_[id] item_id = book[1] item_type = book[0] #clean all position for books position = db.get_position_for_book(session.get('app_id'), item_id) sortable = [] if position: db.del_item_position(session.get('app_id'), item_id, item_type, position['row']) has_request = db.get_request_for_position( session.get('app_id'), position['position'], position['row']) #remove request if has_request: db.del_request(session.get('app_id'), position['led_column'], position['row']) #get list for remaining items and sort them again items = db.get_positions_for_row(session.get('app_id'), position['row']) if items: app_id = session.get('app_id') i = 0 sortable = [] for item in items: position = db.get_position_for_book( app_id, item['id_item']) if position: interval = position['range'] else: book = db.get_book(item['id_item'], user_id) interval = tools.led_range( book, globalVars['arduino_map']['leds_interval']) i += 1 db.set_position(app_id, item['id_item'], i, position['row'], interval, 'book', 0) #reinit led column #update new leds number positions = db.get_positions_for_row( app_id, position['row']) for pos in positions: led_column = db.get_led_column( app_id, pos['id_item'], pos['row'], pos['position']) db.set_led_column(app_id, pos['id_item'], pos['row'], led_column) sortable.append({ 'book': pos['id_item'], 'position': pos['position'], 'fulfillment': int(led_column + pos['range']), 'shelf': pos['row'] }) response = app.response_class(response=json.dumps(sortable), mimetype='application/json') return response
def locateBook(): globalVars = tools.initApp() action = 'add' positions = [] color = '' app_id = session['app_id'] book_id = None address = None client = 'server' if ('api' in request.path and 'token' in request.args): client = 'mobile' '''get form request params''' if (request.method == 'POST'): app_id = request.form.get('app_id') column = request.form.get('column') row = request.form.get('row') book_id = request.form.get('book_id') leds_range = request.form.get('range') address = db.get_position_for_book(app_id, book_id) if address['borrowed'] == 1: color = '255,0,0' if 'remove_request' in request.form: action = 'remove' '''get params for location''' if (request.method == 'GET'): app_id = session['app_id'] book_id = request.args.get('book_id') color = request.args.get('color') address = db.get_position_for_book(app_id, book_id) if 'remove_request' in request.args: action = 'remove' #manage request if address != None and book_id != None: db.set_request(app_id, book_id, address['row'], address['position'], address['range'], address['led_column'], \ 'book', client, action, None, color) if action == 'remove': retMsg = 'Location removed for book {}'.format(book_id) else: retMsg = 'Location requested for book {}'.format(book_id) flash(retMsg, 'info') else: retMsg = 'Unable to find book' flash(retMsg, 'danger') #send data for mobile if ('api' in request.path and 'token' in request.args): data = {'action':action, 'row':address['row'], 'start':address['led_column'], 'interval':address['range'], \ 'id_node':book_id, 'borrowed':address['borrowed']} if color != None: data.update({'color': color}) positions.append(data) response = app.response_class(response=json.dumps(positions), mimetype='application/json') return response if (request.referrer and 'tag' in request.referrer): return redirect( url_for('listAuthors', _scheme='https', _external=True)) return redirect(url_for('myBookShelf', _scheme='https', _external=True))
def bookReferencer(): globalVars = tools.initApp() '''save classic data from form''' if request.method == 'POST': book = tools.formatBookApi('localform', request.form, request.form['isbn']) if 'id' in request.form: book['id'] = request.form['id'] db.bookSave(book, globalVars['arduino_map']['user_id'], request.form['tags']) return redirect(url_for('myBookShelf', _scheme='https', _external=True)) #return render_template('bookreferencer.html', user_login=globalVars['user_login']) '''save book from mobile app''' if 'api' in request.path and request.args.get('token') and request.args.get('save_bookapi'): import requests ref = request.args.get('ref') isbn = request.args.get('isbn') source_api = request.args.get('save_bookapi') '''resume detail on api before saving''' if source_api=='googleapis': r = requests.get("https://www.googleapis.com/books/v1/volumes/"+ref) data = r.json() book = tools.formatBookApi('googleapis', data, isbn) if source_api=='openlibrary': r = requests.get("https://openlibrary.org/api/volumes/brief/isbn/"+isbn+".json") data = r.json() book = tools.formatBookApi('openlibrary', data['records'][ref]['data'], isbn) book_width = request.args.get('book_width') book['width'] = round(float(book_width)) #print(book) #save process bookId = db.get_bookapi(isbn, globalVars['arduino_map']['user_id']) message = {} if bookId: message = {'result':'error', 'message':'This book is already in your shelfs'} #add new book else: bookId = db.bookSave(book, globalVars['arduino_map']['user_id']) #force position in current app forcePosition = request.args.get('forcePosition') if forcePosition == 'true': lastPos = db.get_last_saved_position(session.get('app_id')) newInterval = tools.led_range(book, globalVars['arduino_map']['leds_interval']) if lastPos: newPos = lastPos['position']+1 newRow = lastPos['row'] newLedNum = lastPos['led_column']+lastPos['range'] else:#first book in line newPos = 1 newRow = 1 newLedNum = 0 #adjust new led column with static element statics = db.get_static_positions(session.get('app_id'),newPos) if statics: for static in statics: if int(newLedNum) == int(static['led_column']): newLedNum += static['range'] db.set_position(session.get('app_id'), bookId['id'], newPos, newRow, newInterval, 'book', newLedNum) address = db.get_position_for_book(session.get('app_id'), bookId['id']) #confirm message if bookId: message['result'] = 'success' message['message'] = 'Book added with id '+str(bookId['id']) if forcePosition == 'true' and newLedNum != None: message['message'] += ' at position n°' +str(newPos)+ ' and LED n° ' + str(int(newLedNum)+1) + ' in row n°'+str(newRow) message['address'] = [{'action':'add', 'row':address['row'], 'start':address['led_column'], 'interval':address['range'], \ 'id_node':bookId['id'], 'borrowed':address['borrowed']}] else: message = {'result':'error', 'message':'Error saving book'} response = app.response_class( response=json.dumps(message), mimetype='application/json' ) return response