Exemple #1
0
def add_thanks():
    logging.info('REQUEST: ' + str(request))
    thank = request.json
    fields = ['message', 'type']
    if not validate_req(request, fields):
        logging.error(str(JSON_ERROR))
        return add_headers(JSON_ERROR, JSON_ERROR['code'])
    token = request.headers['Authorization']
    from_user = jwt.decode(token, SECRET, algorithms=ALG)
    id = request.args.get('id')
    if id:
       to_user = repo.get_by_id(id)
    else:
        name = request.args.get('name')
        if not name:
            # no name or ID in header
            return add_headers(UNKNOWN_USER, UNKNOWN_USER['code'])
        else:
            to_user = repo.get_by_name(name)
    if not to_user:
        return add_headers(UNKNOWN_USER, UNKNOWN_USER['code'])
    thank['name'] = from_user['username']
    res = bc.add_thank(to_user['_id'], thank)
    SUCCESS['hash'] = res
    return add_headers(SUCCESS, OK)
Exemple #2
0
def register():
    logging.info('REQUEST: ' + str(request))
    fields = ['username', 'password', 'email']
    if not validate_req(request, fields):
        logging.error(str(JSON_ERROR))
        return add_headers(JSON_ERROR, JSON_ERROR['code'])
    user = request.json
    passw = user['password']
    user['password'] = str(flask_bcrypt.generate_password_hash(passw, ROUNDS))
    existing_user = repo.get_by_name_and_pw(user['username'], user['password'])
    if not existing_user:
        existing_user = repo.get_by_email(user['email'])
    if existing_user:
        return add_headers(EXISTING_USER, EXISTING_USER['code'])
    # create user with not validated flag
    user['validated'] = False
    user['created_date'] = str(datetime.datetime.now())
    id = repo.create_one(user)
    #trx =  "111"
    trx = bc.register_bc(str(id))
    # if all goes well send email with activation link:
    link = ts.generate_reg_token(user['email'])
    email.send_reg_mail(user['email'], user['username'], link)
    user['_id'] = str(id)
    user['hash'] = trx
    print(user)
    return add_headers(user, OK)
Exemple #3
0
def post_heating_settings():
    logging.info(request.args)
    logging.debug(request)
    fields = ['day', 'start_hour', 'start_min', 'end_hour', 'end_min', 'desired_temp']
    if not validate_req(request, fields):
        logging.error(JSON_ERROR)
        return add_headers(JSON_ERROR, JSON_ERROR['code'])
    setting = request.json
    server.change_timer_setting(**setting)
    return add_headers({"status": "changed heating settings"}, HTTP_OK)
Exemple #4
0
def post_settings():
    logging.info(request.args)
    logging.debug(request)
    current_settings_fields = list(server.get_main_settings().keys())
    if not validate_req(request, current_settings_fields):
        logging.error(JSON_ERROR)
        return add_headers(JSON_ERROR, JSON_ERROR['code'])
    server.change_main_setting(request.json)
    res = copy.copy(request.json)
    res["status"] = "changed settings"
    return add_headers(res, HTTP_OK)
def is_admin():
    token_bearer = request.headers.get('Authorization')
    # Ceci est ignoble
    token = token_bearer[9:len(token_bearer)-1]
    # Mais ca fonctionne
    user_id = models.decode_auth_token(token)
    print('user_id: ' + user_id)
    user = userCollection.find_one({"_id":ObjectId(user_id)})
    if user == None:
        response = jsonify({'isAdmin': 'False'})
        return (utils.add_headers(response), 200)
    else: 
        response = jsonify({'isAdmin': 'True'})
        return (utils.add_headers(response), 200)
Exemple #6
0
def get_thanks():
    logging.info('REQUEST: ' + str(request))
    token = request.headers['Authorization']
    print(token)
    token_user = jwt.decode(token, SECRET, algorithms=ALG)
    print(token_user)
    id = token_user['_id']
    db_user = repo.get_by_id(id)
    if not db_user:
        return add_headers(UNKNOWN_USER, UNKNOWN_USER['code'])
    print(db_user['_id'])
    bc_user = bc.get_thank(db_user['_id'])
    bc_user['name'] = db_user['username']
    return add_headers(bc_user, OK)
Exemple #7
0
def get_by_title():
    if 'key' in request.args:
        key = request.args.get('key')
        matches = db.session.query(VideoEntry).filter(
            VideoEntry.title.like("%" + key + "%")).all()
    elif 'from_id' in request.args and 'to_id' in request.args:
        from_id = request.args.get('from_id')
        to_id = request.args.get('to_id')
        matches = db.session.query(VideoEntry).filter(
            from_id <= VideoEntry.id).filter(VideoEntry.id <= to_id)
    else:
        return add_headers({'result': 'Error in url args'}, 400)
    matches = [x.get_json() for x in matches]
    return add_headers(matches, 200)
Exemple #8
0
def validate():
    id = request.args.get('id')
    email = ts.confirm_token(id)
    user = repo.get_by_email(email)
    user['validated'] = True
    repo.update_one(user['id'], user)
    return add_headers(user, OK)
Exemple #9
0
def login():
    logging.info('REQUEST: ' + str(request))
    fields = ['username', 'password']
    if not validate_req(request, fields):
        logging.error(str(JSON_ERROR))
        return add_headers(JSON_ERROR, JSON_ERROR['code'])
    credentials = request.json
    credentials['password'] = bcrypt.hashpw(credentials['password'], SECURITY_PASSWORD_SALT)
    user = repo.get_by_name_and_pw(credentials['username'], credentials['password'])
    if not user:
        return add_headers(UNKNOWN_USER, UNKNOWN_USER['code'])
    if user['validated'] == False:
        return add_headers(NOT_VALIDATED_USER, NOT_VALIDATED_USER['code'])
    user['exp'] = datetime.datetime.utcnow() + datetime.timedelta(seconds=JWT_EXP_TIME)
    token = jwt.encode(user, SECRET, algorithm=ALG)
    SUCCESS['token'] = token.decode("utf-8")
    return add_headers(SUCCESS, OK)
Exemple #10
0
def switch_heating():
    logging.info(request.args)
    logging.debug(request)
    heating = ForceHeating.OFF if "off" in request.json and request.json["off"] is True else ForceHeating.ON
    force_minutes = FORCE_ON_DEFAULT if "minutes" not in request.json else request.json['minutes']
    server.forced_switch(heating, period=force_minutes)
    result = "Forcing heating: %s for %d minute(s)" % (heating, force_minutes)
    logging.debug(result)
    return add_headers({"status": str(result)}, HTTP_OK)
Exemple #11
0
def get_heating_settings():
    logging.info(request.args)
    logging.debug(request)
    day = request.args.get('day')
    hour = request.args.get('hour')
    minute = request.args.get('minute')
    # if hour is present, day has to be present, if minute is present, hour and day has to be present
    if (day is None and hour is not None) or (minute is not None and (hour is None or day is None)):
        logging.error(PARAM_ERROR)
        return add_headers(PARAM_ERROR, PARAM_ERROR['code'])
    try:
        result = server.current_state.get_setting_for_time(day=day, hour=hour, minute=minute, target_date=None)
        logging.info(result)
        return add_headers(result, HTTP_OK)
    except KeyError:
        logging.error(DAY_ERROR)
        return add_headers(DAY_ERROR, DAY_ERROR['code'])
    except IndexError:
        logging.error(TIME_ERROR)
        return add_headers(TIME_ERROR, TIME_ERROR['code'])
Exemple #12
0
def get_paginated():
    category = request.args.get('category') if request.args.get(
        'category') is not None else ""
    key = request.args.get('key') if request.args.get(
        'key') is not None else ""
    page_no = int(request.args.get('page')) if request.args.get(
        'page') is not None else 1
    descend = desc if request.args.get(
        'desc') is not None else lambda *a, **k: None

    matches = db.session.query(VideoEntry) \
        .filter(VideoEntry.title.like("%" + key + "%")) \
        .filter(VideoEntry.title.like("%" + category + "%")) \
        .order_by(descend(VideoEntry.title)) \
        .paginate(page=page_no, per_page=ENV.DEFAULT_PAGESIZE)

    paginated_result = {
        'elements': list(map(lambda x: x.get_json(), matches.items)),
        'pages': matches.pages,
        'page': matches.page
    }
    return add_headers(paginated_result, 200)
Exemple #13
0
def get_users():
    return add_headers("OK", OK)
Exemple #14
0
def get_weather():
    logging.info(request.args)
    logging.debug(request)
    weather = server.current_state.weather_data
    logging.info(weather)
    return add_headers(weather, HTTP_OK)
Exemple #15
0
def get_sys_info():
    logging.info(request.args)
    logging.debug(request)
    return add_headers({"status": "Future use"}, HTTP_OK)
Exemple #16
0
def get_settings():
    logging.info(request.args)
    logging.debug(request)
    settings = server.get_main_settings()
    return add_headers(settings, HTTP_OK)
Exemple #17
0
def get_state():
    logging.info(request.args)
    logging.debug(request)
    state = server.current_state.get_json_repr()
    logging.info(state)
    return add_headers(state, HTTP_OK)
Exemple #18
0
def add_vid():
    insert_one(VideoEntry(**request.json))
    return add_headers({'result': 'OK'}, 200)