Example #1
0
    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
Example #2
0
 def editArduino(app_id):
   globalVars = tools.initApp()
   module = db.get_arduino_map(flask_login.current_user.id, app_id) #globalVars['arduino_map']
   if module:
     session['app_id'] = module['id']
     session['app_name'] = module['arduino_name']
     if request.method == 'POST':
       if 'module_name' in request.form:
         if db.set_module(request.form):
           return redirect(url_for('editArduino', _scheme='https', _external=True, app_id=request.form.get('module_id')))
       if request.is_json:
         mode = request.args.get('mode')
         if mode == 'preview':
           db.clean_request(app_id) #clean request for preview
         data = request.get_json()
         for numrow in data:
           positions = data[numrow]
           #print(positions)
           for i in range(len(positions)):
             pos = i+1        
             if mode == 'save': 
               db.set_position(app_id, pos, pos, numrow, 1, 'static', positions[i])            
             if mode == 'preview': #set distant request for preview
               db.set_request(app_id, pos, numrow, pos, 1, positions[i], 'static', 'server', 'add')
             if mode == 'remove': #remove all static 
               db.del_item_position(int(app_id), pos, 'static', numrow)        
     return render_template('module.html', user_login=flask_login.current_user.name, module=module, db=db, shelf_infos=globalVars['arduino_map'])
   abort(404) 
Example #3
0
    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
Example #4
0
 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
Example #5
0
    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
Example #6
0
  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