Exemple #1
0
def login():
    try:
        form = LoginForm(request.form)
        if request.method == 'POST' and form.validate():
            email = form.email.data
            password = form.password.data
            user = User.query.filter_by(email=email).first()
            if  user and sha256_crypt.verify(password, user.password):
                user.last_login_at = datetime.now()
                user.last_login_ip = request.remote_addr
                if user.login_count:
                    user.login_count += 1
                else:
                    user.login_count = 1
                db_session.commit()
                session.clear()
                login_user(user, remember=True)
                session['email'] = user.email
                session['name'] = user.name
                flash(u'登录成功!')
                return redirect(request.referrer or url_for('.index'))
            else:
                flash(u'登录失败!请重试!','error')
        return render_template('general/login.html',form=form)
    except Exception as e:
        app.logger.info('User fails to login, error : %s .',e)
        return render_template('general/login.html',form=form)
Exemple #2
0
def reset():
    try:
        form = ResetForm(request.form)
        if request.method == 'POST' and form.validate():
            email = form.email.data
            newpasswd = form.newpasswd.data
            user = User.query.filter_by(email=email).first()
            if  user:
                new = sha256_crypt.hash(str(newpasswd))
                user.newpasswd = new
                db_session.commit()
                link = "https://www.jdiandian.com/reset_confirm?email="+user.email+"&rank="+new

                msg = Message("今点点-重置密码!",
                    sender=("jdiandian","*****@*****.**"),
                    recipients=[email])
                assert msg.sender == "jdiandian <*****@*****.**>"
                # you can add the nickname in the template
                msg.html = render_template('mail/reset.html',link=link)
                flask_mail.send(msg)

                flash(u'一封确认邮件已经发送到您的邮箱!请检查')
                return redirect(request.referrer or url_for('general.login'))
            else:
                flash(u'这个邮箱还没有注册,请先注册!')
                return redirect(url_for('general.register'))
        return render_template('general/reset.html',form=form)
    except Exception as e:
        # return str(e)
        return render_template('general/reset.html')
Exemple #3
0
def new_subscriber():
    if not current_user.is_authenticated:
        return u'请先登录:3'
    req = request.get_json(force=True)
    if req:
        user_id = req.get('id',None)
        user_name = req.get('name',None)
        if user_id and user_name:
            sub_id = int(current_user.get_id())
            if int(user_id) == sub_id:
                return u'您不能订阅自己!'
            sub = Subscribe.query.filter_by(subscriber_id=sub_id,user_id=int(user_id)).first()
            if sub:
                return u'您已经订阅!若需要取消,请到用户中心。'
            else:
                user = User.query.get(user_id)
                if user.subscribe:
                    user.subscribe += 1
                else:
                    user.subscribe = 1
                db_session.commit()
                new = Subscribe(user_id=user_id,user_name=user.name,subscriber_id=sub_id,update=0)
                db_session.add(new)
                db_session.commit()
                return u'您已订阅成功',200
        else:
            return abort(400)
    else:
        return abort(404)
Exemple #4
0
def user_info():
    try:
        form = UserInfoForm(request.form)
        uid = int(current_user.get_id())
        info = User.query.get(uid)
        track = Tracking.query.filter_by(user_id=uid).first()
        if request.method == 'POST' and form.validate():
            info.name = form.name.data
            info.summary = form.summary.data
            db_session.commit()
            phone = form.phone.data
            sex = form.sex.data
            birthday = form.birthday.data
            marriage = form.marriage.data
            hobby = ','.join(form.hobby.data)
            if phone or sex or birthday or marriage or hobby:
                track.phone = phone
                track.sex = sex
                track.birthday = birthday
                track.marriage = marriage
                track.hobby = hobby
                db_session.commit()
            flash(u'资料已更新!')
        form.name.data = info.name
        form.summary.data = info.summary
        form.phone.data = track.phone
        form.sex.data = track.sex
        form.birthday.data = track.birthday
        form.marriage.data = track.marriage
        form.hobby.data = track.hobby
        return render_template('dashboard/info.html', form=form)
    except Exception as e:
        return str(e)
Exemple #5
0
def add_comment(post_id):
    if not current_user.is_authenticated:
        return u'请先登录!'
    req = request.get_json(force=True)
    if req:
        article_id = post_id
        user_id = current_user.get_id()
        user_name = session.get('name', '')
        date = dt.today()
        praise = req.get('praise', None)
        body = req.get('body', None)
        if not body:
            return u'请填写评论内容!'
        if praise == 'up':
            praise = 'up'
        elif praise == 'down':
            praise = 'down'
        else:
            praise = ''
        new = Comment(article_id=article_id,
                      user_id=user_id,
                      user_name=user_name,
                      date=date,
                      praise=praise,
                      body=body)
        db_session.add(new)
        db_session.commit()
        return u'评论成功!', 200
    else:
        return abort(404)
Exemple #6
0
def edit_post(post_id):
    try:
        form = NewPostForm(request.form)
        post = Article.query.get(post_id)
        if post is None or int(current_user.get_id()) != post.author_id:
            return redirect(url_for('post.index'))
        pt = post.post_type
        post_body = ArticleBody.query.filter_by(article_id=post_id).first()
        if request.method == 'POST' and form.validate():
            if post.location:
                location = post.location
            else:
                location = str(post.author_id) + '_' + str(int(
                    time.time())) + '.html'
            post.location = ''
            post.cover = form.cover.data
            post.title = form.title.data
            post.summary = form.summary.data
            post.tags = form.tags.data
            post_body.date = dt.today()
            post_body.body = form.body.data
            db_session.commit()
            flash('post update !')
            # convert to static html
            file_path = app.config['BASE_POSTS_PATH'] + location
            url = '/post/' + str(post.id)
            response = app.test_client().get(url)
            if response.status_code != 200:
                app.logger.info(
                    'STATIC: Post %s make mistake in create a new static page!',
                    str(post.id))
                return redirect(url_for('post.index'))
            else:
                content = response.data
                with open(file_path, 'wb') as fd:
                    fd.write(content)
            response.close()
            new = Article.query.get(post.id)
            new.location = location
            db_session.commit()
            # return the static page
            return send_from_directory(app.config['BASE_POSTS_PATH'], location)
        form.cover.data = post.cover
        form.title.data = post.title
        form.summary.data = post.summary
        form.tags.data = post.tags
        form.body.data = post_body.body
        if pt == 'text':
            return render_template('post/new_text.html', form=form)
        elif pt == 'md':
            return render_template('post/new_md.html', form=form)
        else:
            return redirect(url_for('post.index'))
    except Exception as e:
        # return str(e)
        return redirect(url_for('post.index'))
Exemple #7
0
def delete_post(post_id):
    post = Article.query.get(post_id)
    if int(current_user.get_id()) != post.author_id:
        return u'permission denied'
    if post:
        pb = ArticleBody.query.filter_by(article_id=post_id).first()
        db_session.delete(pb)
        db_session.delete(post)
        db_session.commit()
    else:
        return abort(404)
    return u'文章已经删除!', 200
Exemple #8
0
def reset_confirm():
    email = request.args.get('email')
    rank = request.args.get('rank')
    if not email or not rank:
        return redirect(url_for('general.index'))
    user = User.query.filter_by(email=email).first()
    if user and user.newpasswd == rank:
        user.password = rank
        db_session.commit()
        flash(u'新密码已设置成功,请登录!')
        return redirect(url_for('general.login'))
    else:
        return redirect(url_for('general.index'))
Exemple #9
0
def new_md():
    try:
        form = NewPostForm(request.form)
        if request.method == 'POST' and form.validate():
            post_type = 'md'
            classify = form.name.data
            cover = form.cover.data
            author_id = current_user.get_id()
            title = form.title.data
            author = session.get('name', '')
            date = dt.today()
            summary = form.summary.data
            tags = form.tags.data
            body = safe_script(form.body.data)
            location = str(author_id) + '_' + str(int(time.time())) + '.html'
            c = Classify.query.filter_by(name=classify).first()
            if not c:
                new_c = Classify(name=classify)
                db_session.add(new_c)
                db_session.commit()

            new_a = Article(post_type=post_type,
                            cover=cover,
                            author_id=author_id,
                            title=title,
                            author=author,
                            date=date,
                            summary=summary,
                            tags=tags,
                            classify=c)
            db_session.add(new_a)
            db_session.commit()

            new_b = ArticleBody(body=body, owner=new_a)
            db_session.add(new_b)
            db_session.commit()
            flash('post add!')
            # convert to static html
            file_path = app.config['BASE_POSTS_PATH'] + location
            url = '/post/' + str(new_a.id)
            response = app.test_client().get(url)
            if response.status_code != 200:
                app.logger.info(
                    'STATIC: Post %s make mistake in create a new static page!',
                    str(new_a.id))
                return redirect(url_for('post.index'))
            else:
                content = response.data
                with open(file_path, 'wb') as fd:
                    fd.write(content)
            response.close()
            new = Article.query.get(new_a.id)
            new.location = location
            db_session.commit()
            # return the static page
            return send_from_directory(app.config['BASE_POSTS_PATH'], location)
        return render_template('post/new_md.html', form=form)
    except Exception as e:
        return str(e)
Exemple #10
0
def show_post(post_id):
    post = Article.query.get(post_id)
    if post and post.location:
        if post.views:
            count = post.views + 1
        else:
            count = 1
        post.views = count
        db_session.commit()
        return send_from_directory(app.config['BASE_POSTS_PATH'],
                                   post.location)
    else:
        post_body = ArticleBody.query.filter_by(article_id=post.id).first()
    app.logger.info('STATIC: Post %s dont have a static page!',
                    str(post.author_id))
    return render_template('post/show_post.html',
                           post=post,
                           post_body=post_body)
Exemple #11
0
def register():
    try:
        form = RegisterForm(request.form)
        if request.method == 'POST' and form.validate():
            name = form.name.data
            email = form.email.data
            password = sha256_crypt.hash(str(form.password.data))
            x = User.query.filter_by(email=email).first()
            if x :
                flash(u'邮箱已被注册,请重试!','error')
            else:
                new_user = User(name=name,email=email,password=password)
                db_session.add(new_user)
                db_session.commit()
                new_tarck = Tracking(confirmed_at=datetime.today(),owner=new_user)
                db_session.add(new_tarck)
                db_session.commit()
                flash(u'注册完成!请登录!')
                return redirect(url_for('general.login'))
        return render_template('general/register.html',form=form)
    except Exception as e:
        app.logger.info('User fails to registered, error : %s .',e)
        return render_template('general/register.html')