示例#1
0
def sign_in():
    form = SignInForm()
    err = None
    if form.validate_on_submit():
        name = request.form['username']
        password = request.form['password']

        user = UserModel(db.get_connection())
        exists = user.exists(name)
        if (exists[0]):
            password_model = user.get(exists[1])[2]
            salt = len(user.get(exists[1])[3])

            # Проверка валидности введеных данных
            if check_password_hash(password_model[:len(password_model) - salt],
                                   password):
                photo = user.get(exists[1])[6]
                session['username'] = name
                session['photo'] = photo
                session['id'] = exists[1]

                return redirect('/main')
            else:
                err = 'Ошибка в логине или пароле'
                return render_template('sign_in.html',
                                       title='Авторизация',
                                       form=form,
                                       err=err)
        else:
            err = 'Пользователь не существует'
            return render_template('sign_in.html',
                                   title='Авторизация',
                                   form=form,
                                   err=err)

    return render_template('sign_in.html',
                           title='Авторизация',
                           form=form,
                           err=err)
示例#2
0
def delete():
    if 'username' not in session:
        return redirect('/sign_in')

    um = UserModel(db.get_connection())
    if um.exists(session['username'])[0]:
        photo = um.get(um.exists(session['username'])[1])[6]
        um.delete(um.exists(session['username'])[1])

    way = 'static/img/' + photo
    os.remove(way)

    session.pop('username', 0)
    session.pop('photo', 0)
    session.pop('id', 0)

    return redirect('/login')
示例#3
0
def cabinet_other(user_id):
    if 'username' not in session:
        return redirect('/sign_in')

    if user_id == session['id']:
        return redirect('/cabinet')

    um = UserModel(db.get_connection())
    all = um.get(user_id)

    if all:
        tm = TheoryModel(db.get_connection())
        content = 'Всего теорий: ' + str(len(tm.get_all(user_id=all[0])))

        return render_template('cabinet_other.html',
                               n=all[1],
                               i=all[0],
                               d=all[4],
                               l=content,
                               q=None,
                               s=all[5],
                               p=all[6])
    else:
        return render_template('cabinet_other.html', q='1')
示例#4
0
def cabinet():
    if 'username' not in session:
        return redirect('/sign_in')

    um = UserModel(db.get_connection())
    all = []
    err = None

    if um.exists(session['username'])[0]:
        all = um.get(um.exists(session['username'])[1])

    tm = TheoryModel(db.get_connection())
    content = 'Всего теорий: ' + str(len(tm.get_all(user_id=all[0])))

    form = LoadPhotoForm()

    # Загрузка фотографии
    if form.validate_on_submit():
        f = form.file.data

        if f:
            name = f.filename
            session['photo'] = name
            result = open(name, 'wb')
            result.write(f.read())
            result.close()

            way = 'static/img/' + name

            # Если фотография уже существует в нашей папке, то удаляем ее
            move_photo(way, name)

            # Проверка того, является ли файл фотографией
            if not imghdr.what(way):
                err = 'Файл недопустимого формата'
                os.remove(way)
                return render_template('cabinet.html',
                                       n=session['username'],
                                       i=all[0],
                                       d=all[4],
                                       l=content,
                                       s=all[5],
                                       p=all[6],
                                       err=err)
            else:
                um = UserModel(db.get_connection())
                if um.exists(session['username'])[0]:
                    id = um.exists(session['username'])[1]
                um.change_photo(name, id)

        return redirect('/cabinet')

    else:
        return render_template('cabinet.html',
                               n=session['username'],
                               i=all[0],
                               d=all[4],
                               l=content,
                               s=all[5],
                               p=all[6],
                               err=err,
                               form=form)