def delete(self, blog_id): if self.user_is_admin is False: response = failure_response(401, 'Unauthenticated requests') return jsonify(response) blog = Blog.objects(_id=blog_id).first() if blog is not None: blog.delete() response = success_response({'deleted': True}) return jsonify(response)
def put(self, blog_id): if self.user_is_admin is False: response = failure_response(401, 'Unauthenticated requests') return jsonify(response) data = request.get_json() name = data.get('name') content = data.get('content') blog = Blog.objects(_id=blog_id).first() if blog is None: response = failure_response(400, 'Invalid requests') return jsonify(response) blog.name = name blog.content = content blog.updated_date = datetime.now() blog.save()
def get(self, blog_id=None): limit = request.args.get('limit', 0) if blog_id is None: blogs = [] for blog in Blog.objects.order_by('created_date')[int(limit)]: obj = { 'id': blog.id, 'name': blog.name, 'createdAt': blog.created_date.timestamp() } blogs.append(obj) else: blog = Blog.objects(_id=blog_id).first() if blog is None: response = failure_response(404, 'Blog does not exist') else: obj = { 'name': blog.name, 'content': blog.content, 'createdAt': blog.created_date } response = success_response({'blog': obj}) return jsonify(response)