Exemplo n.º 1
0
 def listCategories():
   globalVars = tools.initApp()
   #for mobile app
   if('api' in request.path and 'uuid' in request.args):
     categories = db.get_categories_for_app(globalVars['arduino_map']['user_id'], session['app_id'])
     data = {}
     data['list_title'] = session['app_name']
     token = models.get_token('guest',flask_login.current_user.id)
     if categories:
       for i in range(len(categories)):
         hasRequest = db.get_request_for_tag(session['app_id'], categories[i]['id'])
         categories[i]['url'] = url_for('locateBooksForTag',tag_id=categories[i]['id'])
         categories[i]['token'] = token
         categories[i]['hasRequest'] = hasRequest
         if categories[i]['color'] is not None:
           colors = categories[i]['color'].split(",")
           categories[i]['red'] = colors[0]
           categories[i]['green'] = colors[1]
           categories[i]['blue'] = colors[2]
       data['elements']=categories
       response = app.response_class(
         response=json.dumps(data),
         mimetype='application/json'
       )
       return response
     else:
       return ('', 204)
   #for web
   else:
     if globalVars['arduino_map'] != None:
       user_id = globalVars['arduino_map']['user_id']
       categories = db.get_categories_for_user(user_id)    
       return render_template('categories.html', user_login=globalVars['user_login'], categories=categories, \
       shelf_infos=globalVars['arduino_map'], uuid_encode=tools.uuid_encode(globalVars['arduino_map']['id_ble']))
     abort(404)
Exemplo n.º 2
0
 def customCodes():
   globalVars = tools.initApp()
   if globalVars['arduino_map'] != None:
     #print(codes)
     #send json when token mode
     if('api' in request.path and 'token' in request.args):
       codes = db.get_customcodes(globalVars['arduino_map']['user_id'], session['app_id'], True)
       data = {}
       data['list_title'] = 'Your codes for ' + session['app_name']
       token = models.get_token('guest',flask_login.current_user.id)
       for i in range(len(codes)):
         codes[i]['url'] = url_for('customCode',code_id=codes[i]['id'])
         codes[i]['token'] = token
       data['elements']= codes
       response = app.response_class(
         response=json.dumps(data),
         mimetype='application/json'
       )
       return response  
     if request.args.get('saved'):
       flash('Your code is saved', 'success')  
     #manage post data from json request
     if request.method == 'POST':
       if request.is_json:
           jsonr = request.get_json()
           #print(json)
           db.set_customcode(globalVars['arduino_map']['user_id'], session['app_id'], None, jsonr['title'], jsonr['description'], \
             jsonr['published'], json.dumps(jsonr['customvars']), jsonr['customcode'])
           #print(request.data.decode())
     codes = db.get_customcodes(globalVars['arduino_map']['user_id'], session['app_id'])
     maxLeds = globalVars['arduino_map']['nb_cols']*globalVars['arduino_map']['nb_lines']      
     return render_template('customcodes.html', user_login=globalVars['user_login'], customcodes=codes, json=json, \
       max_leds=maxLeds, shelf_infos=globalVars['arduino_map'])
   abort(404)
Exemplo n.º 3
0
 def listAuthors():
   globalVars = tools.initApp()  
   if globalVars['arduino_map'] != None:
     #for mobile app 
     if('api' in request.path and 'uuid' in request.args):
       data = {}
       token = models.get_token('guest',flask_login.current_user.id)
       data['list_title'] = session['app_name']
       data['elements']=[]
       alphabet = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"]
       for j in range(len(alphabet)):
         '''data['elements'][j]={}
         data['elements'][j]['initial']=alphabet[j]'''
         #print(data)
         items = db.get_authors_for_app(session['app_id'], alphabet[j])
         if items:
           '''set url for authenticate requesting location from app'''
           for i in range(len(items)):
             items[i]['url'] = url_for('locateBooksForTag',tag_id=items[i]['id'])
             items[i]['token'] = token
             hasRequest = db.get_request_for_tag(session['app_id'], items[i]['id'])
             items[i]['hasRequest'] = hasRequest
           #data['elements'][j]['items'] = items
         data['elements'].append({'initial':alphabet[j],'items':items})
       response = app.response_class(
             response=json.dumps(data),
             mimetype='application/json'
       )
       return response
     #for web
     else:
       return render_template('authors.html', user_login=flask_login.current_user.name, db=db, \
           user_id=globalVars['arduino_map']['user_id'], shelf_infos=globalVars['arduino_map'], 
           uuid_encode=tools.uuid_encode(globalVars['arduino_map']['id_ble'])) 
   abort(404)    
Exemplo n.º 4
0
def test_new_token():
    """
    GIVEN a jwt token
    WHEN an email is requested
    THEN check if email belongs to the given token
    """
    flask_app = create_app()
    email = '*****@*****.**'
    new_token = models.get_token('test',email)
    verif_token = jwt.decode(new_token, flask_app.config['SECRET_KEY'], algorithms=['HS256'])['test']
    assert verif_token == email
Exemplo n.º 5
0
 def customEffects():
   globalVars = tools.initApp()
   if globalVars['arduino_map'] != None:
     if('token' in request.args):
       effects = tools.get_leds_effects()
       data = {}
       data['list_title'] = 'Effects for ' + session['app_name']
       token = models.get_token('guest',flask_login.current_user.id)
       data['elements']= effects
       response = app.response_class(
         response=json.dumps(data),
         mimetype='application/json'
       )
       return response
   abort(404) 
Exemplo n.º 6
0
 def getModule(uuid):
   uuid = tools.uuid_decode(uuid)
   if uuid:
     user_app = db.get_app_for_uuid(uuid)
     user = db.get_user_for_uuid(uuid)
     if(user_app):
       data = {}
       data = user_app
       data['total_leds'] = user_app['nb_lines']*user_app['nb_cols']
       data['token'] = models.get_token('guest',user['email'])
       response = app.response_class(
             response=json.dumps(data),
             mimetype='application/json'
       )
       return response
   abort(404)  
Exemplo n.º 7
0
 def listModules(hash_email):
     modules = db.get_arduino_for_api(hash_email)
     data = {}
     if modules:
         data['token'] = models.get_token('guest', modules[0]['email'])
         data['bibus'] = []
         for module in modules:
             data['bibus'].append({
                 'uuid':
                 tools.uuid_encode(module['id_ble']),
                 'name':
                 module['arduino_name']
             })
         response = app.response_class(response=json.dumps(data),
                                       mimetype='application/json')
         return response
     abort(401)
Exemplo n.º 8
0
 def forgotPassword():
     mail = Mail(app)
     hasRequest = False
     if flask_login.current_user.is_authenticated:
         return redirect(
             url_for('selectArduino', _scheme='https', _external=True))
     if request.method == 'POST':
         hasRequest = True
         email = request.form.get('uemail')
         exist = db.get_user(email)
         if exist != None:
             token = models.get_token('auth', exist['email'])
             msg = Message('[Biblioapp] Reset Your Password',
                           recipients=[exist['email']])
             msg.body = render_template('email/reset_password.txt',
                                        user=exist,
                                        token=token)
             msg.html = render_template('email/reset_password.html',
                                        user=exist,
                                        token=token)
             mail.send(msg)
             #return "Sent"
     return render_template('forgot_password.html', hasRequest=hasRequest)
Exemplo n.º 9
0
    def login():
        if request.method == 'GET':
            if request.args.get('saved'):
                flash(
                    'Congratulation, your account is saved! You can login now',
                    'success')
            return render_template('login.html')

        if request.json:
            email = request.json['email']
            pwd = request.json['password']
        else:
            email = request.form['email']
            pwd = request.form['password']

        exists = db.get_user(email)
        if exists is not None:
            #hash = generate_password_hash(exists['password'])
            if check_password_hash(exists['password'], pwd):

                user = models.User()
                user.id = email
                user.name = exists['firstname']
                flask_login.login_user(user)
                #return token and user infos when api exists in requested url
                if 'api' in request.url:
                    token = models.get_token('guest', exists['email'])
                    modules = db.get_arduino_for_user(exists['email'])
                    user = {'email': exists['email'], 'firstname': exists['firstname'], \
                    'lastname': exists['lastname'], 'modules': modules}
                    data = [{'user': user, 'token': token}]
                    return app.response_class(response=json.dumps(data),
                                              mimetype='application/json')
                return redirect(
                    url_for('selectArduino', _scheme='https', _external=True))

        return 'Bad login'