コード例 #1
0
ファイル: training.py プロジェクト: konichuvak/rl_experiments
def train(n_intervals):
    q_values = pickle.loads(db.hget('Prioritized Sweeping', 'Q-Values'))
    graph1 = [go.Heatmap(
        z=np.mean(q_values, axis=0),
        showscale=False,
    )]
    layout1 = dict(height=700, width=700, title='Q Values')
    fig1 = {'data': graph1, 'layout': layout1}

    grid = pickle.loads(db.hget('Prioritized Sweeping', 'Grid'))
    graph2 = [go.Heatmap(
        z=grid,
        showscale=False,
        colorscale='Viridis',
    )]
    layout2 = dict(height=700, width=700, title='Grid')
    fig2 = {'data': graph2, 'layout': layout2}

    p_queue = pickle.loads(db.hget('Prioritized Sweeping', 'Priority'))
    priority = np.zeros(
        (q_values.shape[0], q_values.shape[1], q_values.shape[1]))
    for state, action in p_queue:
        priority[action, state[0], state[1]] = p_queue[state, action] * (-1)

    graph3 = [
        go.Heatmap(
            z=np.mean(priority, axis=0),
            showscale=False,
            colorscale='Viridis',
        )
    ]
    layout3 = dict(height=700, width=700, title='Priority')
    fig3 = {'data': graph3, 'layout': layout3}

    return [fig1, fig2, fig3]
コード例 #2
0
def delete_note(note):
    sessionid = request.cookies.get("app_session")
    log.debug(sessionid)
    if sessionid == None:
        log.debug("session id is lost")
        return redirect(url_for('login'))
    current_user = db.hget("sessions_app", sessionid)
    if current_user == None:
        log.debug("session id is bad")
        return redirect(url_for('login'))
    note_owner = db.hget(note, "owner")
    if current_user != note_owner:
        return {'msg': "Lapy precz to nie twoje!"}
    mynotes = json.loads(db.hget(current_user, "notes"))
    mynotes['notes'].remove(note)
    db.hset(current_user, "notes", json.dumps(mynotes))
    users = json.loads(db.hget(note, "users"))
    for user in users['users']:
        notesforuser = json.loads(db.hget(user, "notesforme"))
        notesforuser['notes'].remove(note)
        db.hset(user, "notesforme", json.dumps(notesforuser))
    keys = db.hkeys(note)
    for key in keys:
        db.hdel(note, key)
    db.hdel("public::notes", note)
    return redirect(url_for('notes'))
コード例 #3
0
def login():
    if request.method == POST:
        form = request.form
        username = form["username"]
        password = form.get("password").encode("utf-8")
        res = db.hexists(username, "data")
        if res == 0:
            return render_template("login.html"), 404
        salt = db.hget(username, "passwd_salt").encode("utf-8")
        for i in range(10):
            hashed_passwd = bcrypt.hashpw(password, salt)
            password = hashed_passwd
        password = str(hashed_passwd)
        cor_password = db.hget(username, "passwd_hash")
        if password == cor_password:
            response = make_response(redirect(url_for('home')))
            sessionid = uuid.uuid4().hex
            response.set_cookie("app_session",
                                sessionid,
                                secure=True,
                                httponly=True,
                                samesite="Strict")
            db.hset("sessions_app", sessionid, username)
            log.debug("session is set ")
            threading.Thread(target=removeUserSession, args=(
                sessionid,
                600,
            )).start()
            time.sleep(0.2)
            return response, 200
        else:
            return render_template("login.html"), 403
    else:
        return render_template("login.html"), 200
コード例 #4
0
def profile():
    sessionid = request.cookies.get("app_session")
    log.debug(sessionid)
    if sessionid == None:
        log.debug("session id is lost")
        return redirect(url_for('login'))
    current_user = db.hget("sessions_app", sessionid)
    if current_user == None:
        log.debug("session id is bad")
        return redirect(url_for('login'))
    edit = 'hidden'
    if request.method == POST:
        edit = ''
    user_data = db.hget(current_user, "data")
    user_data = json.loads(user_data)
    return render_template('myprofile.html', user=user_data, edit=edit), 200
コード例 #5
0
def resetpasswd():
    if request.method == POST:
        form = request.form
        username = form["username"]
        res = db.hexists(username, "data")
        if res == 0:
            return {'msg': "Taki uzytkownik nie istnieje"}
        phone = form["phone"]

        user_data = db.hget(username, "data")
        user_data = json.loads(user_data)
        cor_phone = user_data["phone"]
        log.debug(cor_phone)
        log.debug(phone)

        if phone != cor_phone:
            return {'msg': "Nieudalo sie zresetowac hasla!"}
        answer = form.get("answer").encode("utf-8")
        salt = db.hget(username, "answer_salt").encode("utf-8")
        for i in range(10):
            hashed_answer = bcrypt.hashpw(answer, salt)
            answer = hashed_answer
        answer = str(hashed_answer)
        cor_answer = db.hget(username, "answer_hash")
        log.debug(cor_answer)
        log.debug(answer)
        if answer == cor_answer:
            # password hash & save
            generated_passwd = generatePasswd(16)
            password = generated_passwd
            salt = bcrypt.gensalt(12)
            for i in range(10):
                hashed_passwd = bcrypt.hashpw(password, salt)
                password = hashed_passwd
            salt = str(salt)
            password = str(hashed_passwd)
            db.hset(username, "passwd_hash", password)
            db.hset(username, "passwd_salt", salt)
            return {
                'msg':
                "Pomyslnie zresetowano haslo! Zaloguj sie i niezwlocznie zmien haslo na wlasne!",
                'new_password': generated_passwd
            }
        else:
            return {'msg': "Nieudalo sie zresetowac hasla!"}
    else:
        return redirect(url_for("reset"))
コード例 #6
0
    def get(self, proccess):
        if not re.match(REGEX_PROCESS, proccess):
            return make_response(
                jsonify({
                    'message':
                    'Formato inválido, use a seguinte forma 0710802-55.2018.8.02.0001 para realizar pesquisas',
                    'status': 'fail',
                }),
                400,
            )

        if not db.exists(proccess):
            return make_response(
                jsonify({
                    'message': 'Esse processo ainda não existe em nossa base',
                    'status': 'fail'
                }), 400)

        status = db.hget(proccess, 'status')
        data = db.hget(proccess, 'data') or str({})
        return jsonify({'status': status, 'data': json.loads(data)})
コード例 #7
0
def reset():
    if request.method == POST:
        form = request.form
        username = form["username"]
        res = db.hexists(username, "data")
        if res == 0:
            return {'msg': "Taki uzytkownik nie istnieje"}
        question = db.hget(username, "question")
        return render_template("resetpasswd.html",
                               login=username,
                               question=question)
    else:
        return render_template("reset.html")
コード例 #8
0
def changepasswd():
    sessionid = request.cookies.get("app_session")
    log.debug(sessionid)
    if sessionid == None:
        log.debug("session id is lost")
        return redirect(url_for('login'))
    current_user = db.hget("sessions_app", sessionid)
    if current_user == None:
        log.debug("session id is bad")
        return redirect(url_for('login'))
    if request.method == POST:
        form = request.form
        username = current_user
        password = form.get("oldpassword").encode("utf-8")
        salt = db.hget(username, "passwd_salt").encode("utf-8")
        for i in range(10):
            hashed_passwd = bcrypt.hashpw(password, salt)
            password = hashed_passwd
        password = str(hashed_passwd)
        cor_password = db.hget(username, "passwd_hash")
        if password == cor_password:
            isValid, msg = validate_password(form)
            if not isValid:
                return {'msg': msg}, 400
            # password hash & save
            password = form.get("password").encode("utf-8")
            salt = bcrypt.gensalt(12)
            for i in range(10):
                hashed_passwd = bcrypt.hashpw(password, salt)
                password = hashed_passwd
            salt = str(salt)
            password = str(hashed_passwd)
            db.hset(current_user, "passwd_hash", password)
            db.hset(current_user, "passwd_salt", salt)
            return {'msg': "Pomyslnie zmieniono haslo!"}, 200
        else:
            return {'msg': "Nie udalo sie zmienic hasla!"}, 200
    else:
        {'msg': "To nie nie powinno pokazac!"}, 200
コード例 #9
0
def changeprofile():
    sessionid = request.cookies.get("app_session")
    log.debug(sessionid)
    if sessionid == None:
        log.debug("session id is lost")
        return redirect(url_for('login'))
    current_user = db.hget("sessions_app", sessionid)
    if current_user == None:
        log.debug("session id is bad")
        return redirect(url_for('login'))
    form = request.form
    isValid, msg = validate_updateuserform(form)
    if not isValid:
        return {'msg': msg}, 400

    fname = form.get("firstName")
    lname = form.get("lastName")
    phone = form.get("phone")
    street = form.get("street")
    number = form.get("streetNumber")
    postalCode = form.get("postalCode")
    city = form.get("city")
    country = form.get("country")

    user_data = db.hget(current_user, "data")
    user_data = json.loads(user_data)

    user_data['fname'] = fname
    user_data['lname'] = lname
    user_data['phone'] = phone
    user_data['address']['street'] = street
    user_data['address']['number'] = number
    user_data['address']['postalCode'] = postalCode
    user_data['address']['city'] = city
    user_data['address']['country'] = country

    db.hset(current_user, "data", json.dumps(user_data))

    return redirect(url_for("profile")), 200
コード例 #10
0
def notes():
    sessionid = request.cookies.get("app_session")
    log.debug(sessionid)
    if sessionid == None:
        log.debug("session id is lost")
        return redirect(url_for('login'))
    current_user = db.hget("sessions_app", sessionid)
    if current_user == None:
        log.debug("session id is bad")
        return redirect(url_for('login'))
    if request.method == POST:
        form = request.form
        noteid = uuid.uuid4().hex
        title = form.get("title")
        text = form.get("text")
        usersff = form.get("users")
        users = []
        if usersff != "" and usersff != None:
            usersff = usersff.split(";")
            for user in usersff:
                user = user.replace(" ", "")
                res = db.hexists(user, "data")
                if res == 0:
                    return {'msg': "Uzytkownik %s nie istnieje" % user}
            for user in usersff:
                user = user.replace(" ", "")
                users.append(user)
                notesforhim = db.hget(user, "notesforme")
                notesforhim = json.loads(notesforhim)
                notesforhim['notes'].append(noteid)
                db.hset(user, "notesforme", json.dumps(notesforhim))
        public = form["public"]
        if public == "True":
            public = "True"
        else:
            public = "False"
        #create new note (TITLE, TEXT OWNER, PUBLIC, USERS)
        db.hset(noteid, "title", title)

        db.hset(noteid, "text", text)

        db.hset(noteid, "owner", current_user)

        db.hset(noteid, "public", public)

        db.hset(noteid, "users", json.dumps({'users': users}))

        if public == "True":
            db.hset("public::notes", noteid, "True")
        yournotes = db.hget(current_user, "notes")
        yournotes = json.loads(yournotes)
        yournotes["notes"].append(noteid)
        db.hset(current_user, "notes", json.dumps(yournotes))
        notes = []
        if len(yournotes["notes"]) > 0:
            for note in yournotes["notes"]:
                note = {
                    'id': note,
                    'title': db.hget(note, "title"),
                    'text': db.hget(note, "text")
                }
                notes.append(note)
        public_notes = []
        publicnotesid = db.hkeys("public::notes")
        for nid in publicnotesid:
            note = {
                'id': nid,
                'title': db.hget(nid, "title"),
                'text': db.hget(nid, "text")
            }
            public_notes.append(note)
        notes_for_me = []
        notesforme = db.hget(current_user, "notesforme")
        notesforme = json.loads(notesforme)
        for nid in notesforme['notes']:
            note = {
                'id': nid,
                'title': db.hget(nid, "title"),
                'text': db.hget(nid, "text")
            }
            notes_for_me.append(note)
        return render_template("notes.html",
                               notes=notes,
                               public_notes=public_notes,
                               notes_for_me=notes_for_me)
    else:
        yournotes = db.hget(current_user, "notes")
        yournotes = json.loads(yournotes)
        notes = []
        if len(yournotes["notes"]) > 0:
            for note in yournotes["notes"]:
                note = {
                    'id': note,
                    'title': db.hget(note, "title"),
                    'text': db.hget(note, "text")
                }
                notes.append(note)
        public_notes = []
        publicnotesid = db.hkeys("public::notes")
        for nid in publicnotesid:
            note = {
                'id': nid,
                'title': db.hget(nid, "title"),
                'text': db.hget(nid, "text")
            }
            public_notes.append(note)
        notes_for_me = []
        notesforme = db.hget(current_user, "notesforme")
        notesforme = json.loads(notesforme)
        for nid in notesforme['notes']:
            note = {
                'id': nid,
                'title': db.hget(nid, "title"),
                'text': db.hget(nid, "text")
            }
            notes_for_me.append(note)
        return render_template("notes.html",
                               notes=notes,
                               public_notes=public_notes,
                               notes_for_me=notes_for_me)