def new(type): form = PostForm() if form.validate_on_submit(): new_post = Post(form.title.data) new_post.text = form.text.data new_post.cover = form.cover.data new_post.publish_date = datetime.now() new_post.update_date = new_post.publish_date new_post.user_id = current_user.id new_post.type = type new_post.summary = form.summary.data new_post.video = form.video.data tagStrList = form.tags.data.split(',') for tagStr in tagStrList: tagStr = tagStr.strip() tag = Tag.query.filter_by(title=tagStr).first() if not tag: tag = Tag(tagStr) new_post.tags.append(tag) for photo_url in form.photos.data: if photo_url != '': photo = Photo(photo_url) new_post.photos.append(photo) db.session.add(new_post) db.session.commit() return redirect(url_for('blog.post', post_id=new_post.id)) form.type.data = type return render_template("edit_{}.html".format(type), form=form, type=type)
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})