Ejemplo n.º 1
0
def install_post(db):
    """Install view with POST data"""
    error = None
    try:
        if db.query(database.User).all():
            redirect('/')
    except OperationalError:
        error = {"title": "Connexion à la base de données impossible",
                 "content": ("Impossible d'établir une connexion avec la " +
                             "base de données.")}
        install_json = install(db)
        install_json.update({"err": error})
        return install_json

    login = request.forms.get("login").strip()
    password = request.forms.get("password")
    password_confirm = request.forms.get("password_confirm")
    provider = request.forms.get("provider")
    raw_start_night_rate = request.forms.get("start_night_rate")
    raw_end_night_rate = request.forms.get("end_night_rate")
    raw_base_address = request.forms.get("base_address")
    raw_aes_key = request.forms.get("aes_key")

    ret = {"login": login,
           "providers": update_providers(False, db),
           "start_night_rate": raw_start_night_rate,
           "end_night_rate": raw_end_night_rate,
           "base_address": raw_base_address,
           "aes_key": raw_aes_key}

    try:
        base_address_int = int(raw_base_address.strip("L"), 16)
        base_address = str(hex(base_address_int)).upper() + "LL"
    except ValueError:
        error = {"title": "Format invalide",
                 "content": ("L'adresse de la base entrée est invalide.")}
        ret.update({"err": error})
        return ret
    tools.update_base_address(base_address_int)
    try:
        aes_key = [int(i.strip()) for i in raw_aes_key.split("-")]
        if len(aes_key) != 16:
            raise ValueError
    except ValueError:
        error = {"title": "Format invalide",
                 "content": ("La clé AES doit être constituée de 16 " +
                             "chiffres entre 0 et 255, séparés " +
                             "par des tirets.")}
        ret.update({"err": error})
        return ret
    (db.query(database.Sensor)
     .filter_by(name="CitizenWatt")
     .update({"base_address": base_address, "aes_key": json.dumps(aes_key)}))
    db.commit()

    try:
        start_night_rate = raw_start_night_rate.split(":")
        assert(len(start_night_rate) == 2)
        start_night_rate = [int(i) for i in start_night_rate]
        assert(start_night_rate[0] >= 0 and start_night_rate[0] <= 23)
        assert(start_night_rate[1] >= 0 and start_night_rate[1] <= 59)
        start_night_rate = 3600 * start_night_rate[0] + 60*start_night_rate[1]
    except (AssertionError, ValueError):
        error = {"title": "Format invalide",
                 "content": ("La date de début d'heures creuses " +
                             "doit être au format hh:mm.")}
        ret.update({"err": error})
        return ret

    try:
        end_night_rate = raw_end_night_rate.split(":")
        assert(len(end_night_rate) == 2)
        end_night_rate = [int(i) for i in end_night_rate]
        assert(end_night_rate[0] >= 0 and end_night_rate[0] <= 23)
        assert(end_night_rate[1] >= 0 and end_night_rate[1] <= 59)
        end_night_rate = 3600 * end_night_rate[0] + 60*end_night_rate[1]
    except (AssertionError, ValueError):
        error = {"title": "Format invalide",
                 "content": ("La date de fin d'heures creuses " +
                             "doit être au format hh:mm.")}
        ret.update({"err": error})
        return ret

    if login and password and password == password_confirm:
        password = (config.get("salt") +
                    hashlib.sha256(password.encode('utf-8')).hexdigest())
        admin = database.User(login=login,
                              password=password,
                              is_admin=1,
                              start_night_rate=start_night_rate,
                              end_night_rate=end_night_rate)
        db.add(admin)

        provider = (db.query(database.Provider)
                    .filter_by(name=provider)
                    .update({"current": 1}))

        session = session_manager.get_session()
        session['valid'] = True
        session['login'] = login
        session['is_admin'] = 1
        session_manager.save(session)

        redirect('/')
    else:
        ret.update({"err": {"title": "Champs obligatoires manquants",
                            "content": ("Vous devez renseigner tous " +
                                        "les champs obligatoires.")}})
        return ret
Ejemplo n.º 2
0
def settings_post(db):
    """Settings view with POST data"""
    error = None

    password = request.forms.get("password").strip()
    password_confirm = request.forms.get("password_confirm")

    if password:
        if password == password_confirm:
            password = (config.get("salt") +
                        hashlib.sha256(password.encode('utf-8')).hexdigest())
            session = session_manager.get_session()
            (db.query(database.User)
             .filter_by(login=session["login"])
             .update({"password": password},
                     synchronize_session=False))
        else:
            error = {"title": "Les mots de passe ne sont pas identiques.",
                     "content": ("Les deux mots de passe doient " +
                                 "être identiques.")}
            settings_json = settings(db)
            settings_json.update({"err": error})
            return settings_json

    provider = request.forms.get("provider")
    provider = (db.query(database.Provider)
                .filter_by(name=provider)
                .update({"current": 1}))

    raw_start_night_rate = request.forms.get("start_night_rate")
    raw_end_night_rate = request.forms.get("end_night_rate")

    raw_base_address = request.forms.get("base_address")
    raw_aes_key = request.forms.get("aes_key")

    try:
        base_address_int = int(raw_base_address.strip("L"), 16)
        base_address = str(hex(base_address_int)).upper() + "LL"
    except ValueError:
        error = {"title": "Format invalide",
                 "content": ("L'adresse de la base entrée est invalide.")}
        settings_json = settings(db)
        settings_json.update({"err": error})
        return settings_json

    sensor = db.query(database.Sensor).filter_by(name="CitizenWatt").first()
    if base_address != sensor.base_address:
        tools.update_base_address(base_address_int)

    try:
        aes_key = [int(i.strip()) for i in raw_aes_key.split("-")]
        if len(aes_key) != 16:
            raise ValueError
    except ValueError:
        error = {"title": "Format invalide",
                 "content": ("La clé AES doit être constituée de 16 " +
                             "chiffres entre 0 et 255, séparés " +
                             "par des tirets.")}
        settings_json = settings(db)
        settings_json.update({"err": error})
        return settings_json
    (db.query(database.Sensor)
     .filter_by(name="CitizenWatt")
     .update({"base_address": base_address, "aes_key": json.dumps(aes_key)}))
    db.commit()

    try:
        start_night_rate = raw_start_night_rate.split(":")
        assert(len(start_night_rate) == 2)
        start_night_rate = [int(i) for i in start_night_rate]
        assert(start_night_rate[0] >= 0 and start_night_rate[0] <= 23)
        assert(start_night_rate[1] >= 0 and start_night_rate[1] <= 59)
        start_night_rate = 3600 * start_night_rate[0] + 60*start_night_rate[1]
    except (AssertionError, ValueError):
        error = {"title": "Format invalide",
                 "content": ("La date de début d'heures " +
                             "creuses doit être au format hh:mm.")}
        settings_json = settings(db)
        settings_json.update({"err": error})
        return settings_json
    try:
        end_night_rate = raw_end_night_rate.split(":")
        assert(len(end_night_rate) == 2)
        end_night_rate = [int(i) for i in end_night_rate]
        assert(end_night_rate[0] >= 0 and end_night_rate[0] <= 23)
        assert(end_night_rate[1] >= 0 and end_night_rate[1] <= 59)
        end_night_rate = 3600 * end_night_rate[0] + 60*end_night_rate[1]
    except (AssertionError, ValueError):
        error = {"title": "Format invalide",
                 "content": ("La date de fin d'heures " +
                             "creuses doit être au format hh:mm.")}
        settings_json = settings(db)
        settings_json.update({"err": error})
        return settings_json

    session = session_manager.get_session()
    (db.query(database.User)
     .filter_by(login=session["login"])
     .update({"start_night_rate": start_night_rate,
              "end_night_rate": end_night_rate}))

    redirect("/settings")