def borrowedBooks(): globalVars = tools.initApp() if globalVars['arduino_map'] != None: addresses = db.get_borrowed_books(session.get('app_id')) data = {} data['list_title'] = "Borrowed books" books = [] if addresses: #for address in addresses: for i in range(len(addresses)): book = db.get_book(addresses[i]['id_item'], globalVars['arduino_map']['user_id']) books.append(book) hasRequest = db.get_request_for_position(session.get('app_id'), addresses[i]['position'], addresses[i]['row']) books[i]['address'] = addresses[i] books[i]['hasRequest'] = hasRequest books[i]['arduino_name'] = globalVars['arduino_map']['arduino_name'] books[i]['app_id'] = session.get('app_id') books[i]['color'] = '255,0,0' 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('borrowed.html', books=books, user_login=globalVars['user_login'], \ shelf_infos=globalVars['arduino_map']) abort(404)
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 bookDelete(): globalVars = tools.initApp() book_id = request.form['id'] book = db.get_book(book_id, globalVars['arduino_map']['user_id']) if book: db.clean_tag_for_node(book_id, 1)#clean tags for categories db.clean_tag_for_node(book_id, 2)#clean tags for authors db.del_book(book_id, globalVars['arduino_map']['user_id']) flash('Book "{}" is deleted'.format(book['title']), 'warning') return redirect(url_for('myBookShelf', _scheme='https', _external=True))
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 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 ajaxSetPosition(): globalVars = tools.initApp() if request.method == 'POST' and session.get('app_id'): book_id = request.form.get('book_id') #leds_range = request.form.get('range') #update book width book = db.get_book(book_id, globalVars['arduino_map']['user_id']) book_width = request.form.get('new_book_width') book['width'] = round(float(book_width)) db.set_book(book, globalVars['arduino_map']['user_id']) leds_range = tools.led_range( book, globalVars['arduino_map']['leds_interval']) #update position column = request.form.get('column') row = request.form.get('row') app_id = session.get('app_id') db.set_position(app_id, book_id, column, row, leds_range, 'book', 0) #reinit led column led_column = db.get_led_column(app_id, book_id, row, column) db.set_led_column(app_id, book_id, row, led_column) ret = {'led_column': int(led_column)} response = app.response_class(response=json.dumps(ret), mimetype='application/json') return response
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