def post(self): args = self.post_request.load(request.get_json()) if args.errors: raise exceptions.InvalidRequest(args.errors) uid = request.headers['UID'] try: user_object = User.objects.get(uid=uid) except User.DoesNotExist: raise exceptions.UserDoesNotExist("User Does Not Exist") if "url" in args.data: post_object = Post.objects.create( user=user_object, title=args.data['title'], url=args.data['url'], description=args.data['description'], tags=[Tag.get_or_create(tag['text']) for tag in args.data['tags']] ) else: post_object = Post.objects.create( user=user_object, title=args.data['title'], description=args.data['description'], tags=[Tag.get_or_create(tag['text']) for tag in args.data['tags']] ) response_post = self.post_schema.dump(post_object) return jsonify({"post": response_post.data})
def patch(self, id): args = self.post_request.load(request.get_json()) if args.errors: raise exceptions.InvalidRequest(args.errors) try: post_object = Post.objects.get(pid=id) except Post.DoesNotExist: raise exceptions.PostDoesNotExist("Post Does Not Exist") post_object.update(title=args.data['title']) post_object.update(description=args.data['description']) post_object.update(tags=[Tag.get_or_create(tag['text']) for tag in args.data['tags']]) post_object.update(url=args.data['url']) post_object.save() post_object.reload() response_post = self.post_schema.dump(post_object) return jsonify({"post": response_post.data})