Esempio n. 1
0
def create_post():
    '''添加一篇文章'''
    # 获取json
    data = request.get_json()
    if not data:
        return bad_request('You must post JSON data.')
    message = {}
    # 如果任意两种方式都没拿到title
    if 'title' not in data or not data.get('title'):
        message['title'] = 'Title is required.'
    # 标题字符串长度超过255
    elif len(data.get('title')) > 255:
        message['title'] = 'Title must less than 255 characters.'
    # 验证data中的body数据
    if 'body' not in data or not data.get('body'):
        message['body'] = 'Body is required.'
    # 最后验证一下messge列表是否为真
    if message:
        return bad_request(message)
    # 生成post实例
    post = Post()
    # 将表单中的数据变成post的属性
    post.from_dict(data)
    post.author = g.current_user
    # 通过auth.py 中 verify_token()传递过来的 (同一个request中,
    # 需要先进行Token 认证
    db.session.add(post)
    db.session.commit()
    # todict使post的属性变成字典 再变成json格式赋值给response
    response = jsonify(post.to_dict())
    response.status_code = 201
    # HTTP协议要求201响应包含一个值为新资源URL的Location头部
    response.headers['Location'] = url_for('api.get_post', id=post.id)
    return response
Esempio n. 2
0
def admin_index():
    if request.method == 'GET':
        user = request.args.get('user')

        if user == app.config['LEIHUNAG_ADMIN_KEY']:
            categories = Category.query.all()
            return render_template('admin/index.html', categories=categories)
        else:
            return render_template('404.html')
    else:
        post = Post()
        post.title = request.values.get("title")
        post.path_name = request.values.get("path")

        post.category_id = request.values.get("category")
        post.author = request.values.get("author")
        category = Category.query.filter_by(
            category_id=post.category_id).first()
        category.post_num += 1
        post.content = request.values.get("content")

        post.create_time = datetime.now()
        if exist_post(post.title):
            return '已经存在此博客'
        if exist_path_name(post.path_name):
            return '已经存在此路径的博客'

        db.session.add(post)
        db.session.flush()
        db.session.commit()
        return 'ok'
Esempio n. 3
0
 def test_user_post_group(self):
     u = User()
     g = Group()
     p = Post()
     u.my_group = g
     p.author = g
     self.assertTrue(p.author.owner[0])
Esempio n. 4
0
def create_post():
    '''添加一篇新文章'''
    data = request.get_json()
    if not data:
        return bad_request("You must post JSON data.")
    message = {}
    if 'title' not in data or not data.get('title'):
        message['title'] = 'Title is required.'
    elif len(data.get('title')) > 255:
        message['title'] = 'Title must less than 255 characters.'
    if 'body' not in data or not data.get('body'):
        message['body'] = 'Body is required.'
    if message:
        return bad_request(message)

    post = Post()
    post.from_dict(data)
    post.author = g.current_user
    db.session.add(post)
    db.session.commit()
    response = jsonify(post.to_dict())
    response.status_code = 201
    # HTTP协议要求201响应包含一个值为新资源URL的Location头部
    response.headers['Location'] = url_for('api.get_post', id=post.id)
    return response
Esempio n. 5
0
def create_post():
    data = request.get_json()
    if not data:
        return bad_request('You must post JSON data.')
    # print(data)
    message = {}
    # 暂时不需要检查title
    # if 'title' not in data or not data.get('title'):
    #     message['title'] = 'Title is required.'
    # elif len(data.get('title')) > 255:
    #     message['title'] = 'Title must less than 255 characters.'
    if 'content' not in data or not data.get('content'):
        message['content'] = 'Content is required.'
    if 'type' not in data or not data.get('type'):
        message['type'] = 'Type is required.'
    if message:
        return message
    post = Post()
    post.from_dict(data)
    post.author = g.current_user  # 通过auth.py中verify_token()传递过来的(同一个request中,需要先进行 Token 认证)
    db.session.add(post)
    db.session.commit()
    response = jsonify(post.to_dict())
    response.status_code = 201
    # HTTP协议要求201响应包含一个值为新资源URL的Location头部
    response.headers['Location'] = url_for('api.get_post', id=post.id)
    return response
def create_post():
    """创建一篇文章"""
    json_data = request.json
    if not json_data:
        return bad_request('You must post Json data')
    message = {}
    if 'title' not in json_data and not json_data.get('title'):
        message['title'] = 'Title is required.'
    elif len(json_data.get('title')) > 255:
        message['title'] = 'Title must less than 255 characters.'
    if 'body' not in json_data and not json_data.get('body'):
        message['body'] = 'Body is required'

    if message:
        return bad_request(message)

    # 构建post对象

    post = Post()
    post.from_dict(json_data)
    post.author = g.current_user  # 通过 auth.py 中 verify_token() 传递过来的(同一个request中,需要先进行 Token 认证)
    db.session.add(post)
    db.session.commit()
    response = jsonify(post.to_dict())
    response.status_code = 201
    # HTTP协议要求201响应包含一个值为新资源URL的Location头部
    response.headers['Location'] = url_for('api.get_post', id=post.id)
    return response
Esempio n. 7
0
def create_post():
    '''添加一篇新文章'''
    data = request.get_json()
    if not data:
        return bad_request('You must post JSON data.')
    message = {}
    if 'title' not in data or not data.get('title').strip():
        message['title'] = 'Title is required.'
    elif len(data.get('title')) > 255:
        message['title'] = 'Title must less than 255 characters.'
    if 'body' not in data or not data.get('body').strip():
        message['body'] = 'Body is required.'
    if message:
        return bad_request(message)

    post = Post()
    post.from_dict(data)
    post.author = g.current_user  # 通过 auth.py 中 verify_token() 传递过来的(同一个request中,需要先进行 Token 认证)
    db.session.add(post)
    # 给文章作者的所有粉丝发送新文章通知
    for user in post.author.followers:
        user.add_notification('unread_followeds_posts_count',
                              user.new_followeds_posts())
    db.session.commit()
    response = jsonify(post.to_dict())
    response.status_code = 201
    # HTTP协议要求201响应包含一个值为新资源URL的Location头部
    response.headers['Location'] = url_for('api.get_post', id=post.id)
    return response
Esempio n. 8
0
def publish(request):
	post = Post()
	post.author = User.objects.get(id=request.POST['author'])
	post.title = request.POST['title']
	post.text = request.POST['text']
	post.created_date = request.POST['created_date']
	post.published_date = request.POST['published_date']
	post.save()
Esempio n. 9
0
    def mutate(self, info, title, body, username):
        user = User.query.filter_by(username=username).first()
        post = Post(title=title, body=body)

        if user is not None:
            post.author = user

        db.session.add(post)
        db.session.commit()

        return CreatePost(post=post)
Esempio n. 10
0
def create_post():
    form = PostForm()
    if form.validate_on_submit():
        post = Post(title=form.title.data,
                    pre_body=form.pre_body.data,
                    body=form.body.data)
        post.author = current_user
        db.session.add(post)
        db.session.commit()
        return redirect(url_for('posts.news'))
    return render_template('posts/create_post.html', create_post_form=form)
Esempio n. 11
0
def create_testpost():
    # 执行一次生成20篇测试帖子
    for i in range(2, 20):
        theme = "测试帖子%s" % i
        content = "这只是测试数据,后期会删除 %s" % i
        area = Area.query.first()
        author = FrontUser.query.first()
        post = Post(theme=theme, content=content)
        post.area = area
        area.number = area.number + 1
        post.author = author
        db.session.add(post)
        db.session.commit()
    print("测试数据添加完成")
Esempio n. 12
0
def addPost():
    """Return add post form or process new blog post"""
    form = PostForm()

    if form.validate_on_submit():
        post = Post(title=form.title.data)
        form.populate_obj(post)
        post.author = current_user
        db.session.add(post)
        db.session.commit()

        return redirect(url_for('blog.post', post_slug=post.slug))

    return render_template('blog/compose.html', form=form)
Esempio n. 13
0
def addPost():
    """Return add post form or process new blog post"""
    form = PostForm()

    if form.validate_on_submit():
        post = Post(title=form.title.data)
        form.populate_obj(post)
        post.author = current_user
        db.session.add(post)
        db.session.commit()

        return redirect(url_for('blog.post', post_slug=post.slug))

    return render_template('blog/compose.html', form=form)
Esempio n. 14
0
def new_post():
    form = PostForm()
    post = Post()
    if form.validate_on_submit():
        post.title = form.title.data
        post.body = form.body.data
        post.author = current_user
        db.session.add(post)
        db.session.commit()
        tag_ids = form.tags.data
        for tag_id in tag_ids:
            post_tags = PostTags(post_id=post.id, tag_id=tag_id)
            db.session.add(post_tags)
        flash('文章已发布.')
        return redirect(url_for('.post', title=post.url_title))
    return render_template('edit_post.html', form=form, is_new=True)
Esempio n. 15
0
def save():
    form = BlogPostForm()
    if form.validate_on_submit():
        if request.form['action'] == 'draft':
            print('Saving to redis')
            redis_client.set(form.title.data, form.body.data)
        else:
            print('Saving to postgres')
            model = Post()
            model.title = form.title.data
            model.body = form.body.data
            model.date = form.date.data
            model.author = form.author.data
            db.session.add(model)
            db.session.commit()
    return render_template('new.html', form=form)
Esempio n. 16
0
def new_post():
    form = PostForm()
    post = Post()
    if form.validate_on_submit():
        post.title = form.title.data
        post.body = form.body.data
        post.author = current_user
        db.session.add(post)
        db.session.commit()
        tag_ids = form.tags.data
        for tag_id in tag_ids:
            post_tags = PostTags(post_id=post.id, tag_id=tag_id)
            db.session.add(post_tags)
        flash('文章已发布.')
        return redirect(url_for('.post', title=post.url_title))
    return render_template('edit_post.html', form=form, is_new=True)
Esempio n. 17
0
def submit():
    form = PostForm()
    loadform = LoadPostForm()
    category_form = CatgoryForm()
    if form.validate_on_submit():
        post = Post()
        form.populate_obj(post)
        post.author = current_user
        post.save()
        flash(u"多谢你的提交", "successfully")
        # save the action
        save_action(u"提交了条目 " + u'"' + post.title + u'"')
        return redirect(url_for("home.latest"))
    return render_template("home/submit.html",
                           form=form,
                           loadform=loadform,
                           category_form=category_form)
Esempio n. 18
0
def fake_post(count):
    for i in range(count):
        url = "https://www.v2ex.com/api/topics/latest.json"
        r = requests.get(url).text
        datas = json.loads(r)
        for data in datas:
            title = data['title']
            content = data['content_rendered']
            if title:
                post = Post(title=title,content=content)
                post.tag = Tag.query.get(random.randint(1, Tag.query.count()))
                post.author = random.choice(User.query.all())
                post.publish_time = fake.date_time_between(start_date="-1d", end_date="now", tzinfo=None)
                #post.publish_time = fake.date_time_this_year()
                db.session.add(post)
                db.session.commit()
    return 'Done'
def create_post():
    """ post a new article """
    data = request.get_json()
    if not data:
        return bad_request('You must post JSON data')
    message = {}
    if 'title' not in data or not data.get('title', None):
        message['title'] = 'Title is required.'
    elif len(data.get('title')) > 255:
        message['title'] = 'Title must less than 255 characters.'
    if 'body' not in data or not data.get('body'):
        message['body'] = 'Body is required.'
    if message:
        return bad_request(message)
    post = Post()
    post.from_dict(data)
    post.author = g.current_user  # 通过 auth.py 中 verify_token() 传递过来的(同一个request中,需要先进行 Token 认证)
    db.session.add(post)
    db.session.commit()
    reponse = jsonify(post.to_dict())
    reponse.status_code = 201
    reponse.headers['Location'] = url_for('api.get_post', id=post.id)
    return reponse
Esempio n. 20
0
def create_post():
    '''添加一篇新文章'''
    data = request.get_json()
    if not data:
        return bad_request('You must post JSON data.')
    message = {}
    if 'title' not in data or not data.get('title', None):
        message['title'] = 'Title is required.'
    elif len(data.get('title')) > 255:
        message['title'] = 'Title must less than 255 characters.'
    if 'body' not in data or not data.get('body'):
        message['body'] = 'Body is required.'
    if message:
        return bad_request(message)

    post = Post()
    post.from_dict(data)
    post.author = g.current_user
    post.save()
    response = jsonify(post.to_dict())
    response.status_code = 201
    response.headers['Location'] = url_for('api.get_post', id=post.id)
    return response
Esempio n. 21
0
def create_post():
    data = request.get_json()
    if not data:
        return bad_request('You must post a JSON data')
    message = {}
    if 'title' not in data or not data.get('title'):
        message['title'] = 'Title is required'
    elif len(data.get('title')) > 255:
        message['title'] = 'Title must be less than 255 characters'
    if 'body' not in data or not data.get('body'):
        message['body'] = 'Body is needed'
    if message:
        return bad_request(message)

    post = Post()
    post.from_dict(data)
    post.author = g.current_user
    db.session.add(post)
    db.session.commit()
    response = jsonify(post.to_dict())
    response.status_code = 201
    response.headers['Location'] = url_for('api.get_post', id=post.id)
    return response