Example #1
0
def email():
    form = EmailForm()
    if form.validate_on_submit():
        user = User.query.filter_by(email=form.email.data.lower()).first()
        if user:
            token = generate_token(user=user,
                                   operation=Operations.RESET_PASSWORD)
            send_reset_password_email(user=user, token=token)
            flash('重置密码的邮件已发送,请检查你的邮箱', 'info')
            return redirect(url_for('user.login'))
        else:
            flash('该邮箱尚未注册,请检查邮箱地址重新输入', 'danger')
            redirect_back()
    return render_template('user/email.html', form=form)
Example #2
0
def can_comment(note_id):
    note = Note.query.get(note_id)
    if note.can_comment:
        note.can_comment = False
        flash('评论区已关闭', 'info')
    else:
        note.can_comment = True
        flash('评论区已开启', 'info')
    db.session.commit()
    return redirect_back()
Example #3
0
def change_password():
    form = ChangePasswordForm()
    user = User.query.filter_by(username=current_user.username).first()
    if form.validate_on_submit():
        if user.validate_password(form.password.data):
            user.set_password(form.change_password.data)
            db.session.commit()
            flash('密码修改成功', 'info')
            return redirect(
                url_for('user.index', username=current_user.username))
        flash('原密码错误,请再试一次', 'danger')
        return redirect_back()
    return render_template('user/change_password.html', form=form)
Example #4
0
def new_category():
    form = CategoryForm()
    category = Category.query.all()
    if form.validate_on_submit():
        name = form.name.data
        category = Category(name=name)
        db.session.add(category)
        db.session.commit()
        flash('成功添加一条分类', 'info')
        return redirect_back()
    return render_template('admin/new_category.html',
                           form=form,
                           category=category)
Example #5
0
def login():
    if current_user.is_authenticated:
        return redirect(url_for('index.index'))

    form = LoginForm()
    if form.validate_on_submit():
        user = User.query.filter_by(username=form.username.data).first()
        b = form.b.data
        if user is not None and user.validate_password(form.password.data):
            login_user(user, b)
            flash('登陆成功', 'info')
            return redirect_back()
        flash('用户名或密码不匹配', 'danger')
    return render_template('user/login.html', form=form)
Example #6
0
def retrieve_password(token):
    if current_user.is_authenticated:
        return redirect(url_for('index.index'))

    form = RetrievePasswordForm()
    user = User.query.filter_by(email=form.email.data).first()
    if form.validate_on_submit():
        if user:
            if validate_token(user=user,
                              token=token,
                              operation=Operations.RESET_PASSWORD,
                              new_password=form.password.data):
                user.set_password(form.password.data)
                db.session.commit()
                flash('重置密码成功', 'info')
                return redirect(url_for('user.login'))
            else:
                flash('令牌无效', 'danger')
                return redirect(url_for('user.login'))
        else:
            flash('邮箱不匹配', 'danger')
            redirect_back()
    return render_template('user/retrieve_password.html', form=form)
Example #7
0
def show_note(note_id):
    note = Note.query.get(note_id)
    form = CommentForm()
    if form.validate_on_submit():
        body = form.body.data
        comment = Comment(body=body,
                          note_id=note_id,
                          user=current_user._get_current_object())
        db.session.add(comment)
        db.session.commit()
        return redirect_back()
    page = request.args.get('page', 1, type=int)
    per_page = current_app.config['ALBUMY_PHOTO_PER_PAGE']
    pagination = Comment.query.with_parent(note).order_by(
        Comment.timestamp.desc()).paginate(page, per_page)
    comments = pagination.items
    return render_template('user/show_note.html',
                           note=note,
                           form=form,
                           comments=comments,
                           pagination=pagination)
Example #8
0
def validate_email():
    token = generate_token(user=current_user, operation=Operations.CONFIRM)
    send_confirm_email(user=current_user, token=token)
    flash('验证账户的邮件已发送,请检查你的邮箱', 'info')
    return redirect_back()
Example #9
0
def delete_category(category_id):
    categorys = Category.query.get(category_id)
    db.session.delete(categorys)
    db.session.commit()
    flash('成功删除一条分类', 'info')
    return redirect_back()
Example #10
0
def delete_comment(comment_id):
    comments = Comment.query.get(comment_id)
    db.session.delete(comments)
    db.session.commit()
    flash('成功删除一条评论', 'info')
    return redirect_back()
Example #11
0
def delete_note(note_id):
    notes = Note.query.get(note_id)
    db.session.delete(notes)
    db.session.commit()
    flash('成功删除了一篇文章', 'info')
    return redirect_back()