def new_bucketlist(): """ Creates a new bucketlist. """ try: # validates user key/value inputs using a try-catch block bucketlist = Bucketlist() sanitized = bucketlist.import_data(request.json) if sanitized == "Invalid": return jsonify({"Message": "The bucketlist must have a name"}), 400 except ValidationError as e: return jsonify({"Message": str(e)}), 400 # checks for duplicates before creating the new bucketlist duplicate = Bucketlist.query.filter_by(name=bucketlist.name, created_by=g.user.id).first() if not duplicate: bucketlist.created_by = g.user.id db.session.add(bucketlist) db.session.commit() return jsonify( {"Message": bucketlist.name.title() + " has been created"}), 201 return jsonify({ "Message": "A bucketlist with that name already exists. Please try again" }), 400
def setUp(self): """ Add bucketlists and items directly to db to avoid using tokens required for POST request. """ app.config.from_object(TestingConfig) self.client = app.test_client() db.create_all() self.client.post("/auth/register", data=json.dumps( dict(username="******", password="******")), content_type="application/json") response = self.client.post("/auth/login", data=json.dumps( dict(username="******", password="******")), content_type="application/json") response_msg = json.loads(response.data) self.token = response_msg["Token"] bucket = {"name": "testbucketlist"} test_bucket = Bucketlist() test_bucket.import_data(bucket) test_bucket.created_by = 1 bucket2 = {"name": "testbucketlist2"} test_bucket2 = Bucketlist() test_bucket2.import_data(bucket2) test_bucket2.created_by = 1 bucket3 = {"name": "testbucketlist3"} test_bucket3 = Bucketlist() test_bucket3.import_data(bucket3) test_bucket3.created_by = 2 item = {"name": "testitem", "done": ""} test_item = BucketlistItem() test_item.import_data(item) test_item.bucket = 1 test_item.created_by = 1 db.session.add(test_bucket) db.session.add(test_bucket2) db.session.add(test_bucket3) db.session.add(test_item) db.session.commit()