예제 #1
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)
예제 #2
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)
예제 #3
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)
예제 #4
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')