Exemplo n.º 1
0
def new_post():
    if request.method == 'POST':
        jsonStr = request.get_data()
        data = json.loads(jsonStr)
        if hasattr(data, 'key') or data['key'] != "wysj3910":
            return "密钥错误!"
        new_post = Post(data['title'])  # 创建帖子
        new_post.text = data['text']  # 内容
        new_post.cover = data['cover']  # 封面
        new_post.publish_date = time.strftime(
            "%Y-%m-%d %H:%M:%S", time.localtime(data['publish_date']))  # 发布日期
        new_post.update_date = datetime.datetime.now()  #更新的日期
        user = User.query.filter_by(username=data['username']).first()  # 获取用户
        if not user:
            user = User(data['username'])
            if hasattr(data, 'nickname'):
                user.nickname = data['nickname']
        new_post.user = user  # 用户
        new_post.type = data['type']  # 类型
        new_post.summary = data['summary']  # 简介
        new_post.video = data['video']  # 视频地址
        new_post.read = data['read']  # 阅读量
        new_post.post_hash = data['post_hash']  #贴的hash值
        for photo_url in data['photos']:  # 图片列表
            if photo_url != '':
                photo = Photo(photo_url)
                new_post.photos.append(photo)

        for tagStr in data['tags']:  # 标签
            tagStr = tagStr.strip()
            tag = Tag.query.filter_by(title=tagStr).first()
            if not tag:
                tag = Tag(tagStr)
            new_post.tags.append(tag)
        db.session.add(new_post)
        db.session.commit()
        return json.dumps({'status': 0})