Beispiel #1
0
def add_news():
    form = NewsForm()
    if form.validate_on_submit():
        session = db_session.create_session()
        news = News()
        news.title = form.title.data
        news.content = form.content.data
        news.is_private = form.is_private.data
        current_user.news.append(news)
        session.merge(current_user)
        session.commit()
        return redirect('/')
    return render_template('news.html', title='Добавление новости', form=form)
Beispiel #2
0
def login():
    login_form = LoginForm()
    if login_form.validate_on_submit():
        db = db_session.create_session()
        user = db.query(User).filter(
            User.email == login_form.email.data).first()
        if not user:
            return render_template('login.html',
                                   form=login_form,
                                   message="Такого пользователя не существует")
        if user.check_password(login_form.password.data):
            login_user(user, remember=login_form.remember_me.data)
            return redirect(url_for('index'))
        else:
            return render_template('login.html',
                                   form=login_form,
                                   message="Неверный пароль")
    else:
        return render_template('login.html', form=login_form)
Beispiel #3
0
def reqister():
    form = RegisterForm()
    if form.validate_on_submit():
        if form.password.data != form.password_again.data:
            return render_template('register.html',
                                   title='Регистрация',
                                   form=form,
                                   message="Пароли не совпадают")
        db = db_session.create_session()
        if db.query(User).filter(User.email == form.email.data).first():
            return render_template('register.html',
                                   title='Регистрация',
                                   form=form,
                                   message="Такой пользователь уже есть")
        user = User(name=form.name.data, email=form.email.data)
        user.set_password(form.password.data)
        db.add(user)
        db.commit()
        return redirect('/login')
    return render_template('register.html', title='Регистрация', form=form)
Beispiel #4
0
def index():
    db = db_session.create_session()
    news = db.query(News).filter(News.is_private != True)
    return render_template("public_news.html", news=news)
Beispiel #5
0
def load_user(user_id):
    db = db_session.create_session()
    return db.query(User).get(user_id)