def get(cls): user_id = request.args.get('user_id', None) if user_id: posts = PostModel.get_by_user_id(user_id) return {'posts': PostSchema(many=True).dump(posts)}, 200 posts = PostModel.get_all() return {'posts': [PostSchema().dump(post) for post in posts]}, 200
def delete(self, obj_id): post = PostModel.objects(id=obj_id).first() comment_num = int(request.args.get('comment_num')) self.check_is_exist(post, comment_num) if not len(post.comments): return Response('', 204) PostModel.objects(comments__match={"comment_num": comment_num}) \ .update_one(pull__comments_comment_num=comment_num) return '', 200
def delete(self, obj_id, comment_num): post = PostModel.objects(id=obj_id).first() self.check_is_exist(post, comment_num) if not len(post.comments): return Response('', 204) comment = PostModel.objects(comment__match={ "comment_num": comment_num }).first() comment.delete() return Response('', 200)
def create_posts(): """ generate random posts """ for i in range(150): tag = [ Tags[random_number(0,len(Tags)-1)], Tags[random_number(0,len(Tags)-1)] ] post = " ".join(map(''.join, zip(*[iter(id_generator(500))]*random_number()))) description = " ".join(map(''.join, zip(*[iter(id_generator(100))]*random_number()))) title = " ".join(map(''.join, zip(*[iter(id_generator(25))]*random_number()))) slugid = slugify(title) # check if this already exists - if so, throw or pass pm = PostModel( Title = title, Post=post, Tags=tag, Description=description, Published=True, id=slugid ) pm.put() postId = pm.key.id() for tg in tag: TagModel.create_or_update_tag(tg) for i in xrange(random_number(3,15)): comment = " ".join(map(''.join, zip(*[iter(id_generator(100))]*random_number()))) username = id_generator(random_number(6,15)) email = ".".join([ "@".join([username, id_generator(random_number(5,10))]), "com" ]) cm = CommentModel( Name=username, Email=email, Comment=comment, SlugId=slugid, Published=True ) cm.put()
def posteditor(): postform = PostForm() if postform.validate_on_submit(): try: npost = PostModel.create_or_update_post(postform) return render_template('post_success.html', post=npost) except Exception as e: logging.error(str(e)) return render_template('error404.html', error=str(e)) else: return render_template('posteditor.html',postform=postform)
def post(cls, user_id): data = request.get_json() error = PostSchema().validate(data) if error: return {'message': error}, 400 user = UserModel.get_by_id(user_id) if not user: return {'message': 'User not found'}, 404 user.posts.append(PostModel(**data)) user.save() return {'message': 'Post was added to user'}, 201
def post(self, obj_id, comment_num): post = PostModel.objects(id=obj_id).first() self.check_is_exist(post, comment_num) new_comment_num = len(post.comments) + 1 comment = CommentModel(user=g.user, comment=request.json['comment'], comment_num=new_comment_num) post.comments.append(comment) post.save() return Response('', 201)
def create_post(): json_data = request.get_json(force=True) # check validation try: post = PostModel(title=json_data["title"]) db.session.add(post) db.session.commit() ret_data = {"title": json_data["title"]} return jsonify(ret_data) except IntegrityError as e: print(e)
def post(self): payload = request.json if not payload: abort(409) title = payload["title"] content = payload["content"] try: tags = payload['tags'] except KeyError: tags = [] PostModel(title=title, author=g.user, content=content, tags=tags).save() return Response('', 201)
def get(self): all_post = PostModel.objects().all() if not all_post: return Response('', 204) return self.unicode_safe_json_dumps([{ 'obj_id': str(post.id), 'title': post.title, 'author': post.author.nickname, 'content': post.content, 'comments': [{ "name": data.name, "comment": data.comment } for data in post.comments], 'tags': post.tags, 'timestamp': str(post.timestamp) } for post in all_post], 200)
def get(self, obj_id): post = PostModel.objects(id=obj_id).first() self.check_is_exist(post) return self.unicode_safe_json_dumps( { 'obj_id': str(post.id), 'title': post.title, 'author': post.author.nickname, 'content': post.content, 'comments': [{ "user": data.user.nickname, "comment": data.comment } for data in post.comments], 'tags': post.tags, 'timestamp': str(post.timestamp) }, 200)
def _post_tasklet(slug): post, comments = yield PostModel.get_by_id_async(slug), CommentModel.get_comment_by_slug_async(slug) raise ndb.Return((post, comments))
def posts_by_tag(tag, page): """ tag page endpoint """ context = _get_index_context(page, PostModel.get_tags_query(tag)) context.update( _explorerInfo(url_for('posts_by_tag', tag=tag, page=page)) ) #logging.error( context ) return render_page_or_error('index.html', context)
def index(page): """ home page endpoint """ context = _get_index_context(page, PostModel.get_posts_query()) #context.update( _explorerInfo(url_for('index',page=page)) ) #logging.error( context ) return render_page_or_error('index.html', context)
def delete(self, obj_id): post = PostModel.objects(id=obj_id).first() self.check_is_exist(post) post.delete() return Response('', 200)
def get(cls, post_id): post = PostModel.get_by_id(post_id) if not post: return {'message': 'post not found'}, 400 return PostSchema().dump(post), 200