def schema(): """ Returns the basic schema of :class:`.JobGroup` .. http:get:: /api/v1/jobgroups/schema HTTP/1.1 **Request** .. sourcecode:: http GET /api/v1/jobgroups/schema HTTP/1.1 Accept: application/json **Response** .. sourcecode:: http HTTP/1.1 200 OK Content-Type: application/json { "main_jobtype": "VARCHAR(64)", "title": "VARCHAR(255)", "user": "******", "id": "INTEGER" } :statuscode 200: no error """ schema_dict = JobGroup.to_schema() # In the database, we are storing the user by id, but over the wire, we are # using the username to identify the user instead. schema_dict["user"] = "******" % config.get("max_username_length") del schema_dict["user_id"] schema_dict["main_jobtype"] = "VARCHAR(%s)" % config.get("job_type_max_name_length") del schema_dict["main_jobtype_id"] return jsonify(schema_dict), OK
def post(self): """ A ``POST`` to this endpoint will create a new job group. .. http:post:: /api/v1/jobgroups/ HTTP/1.1 **Request** .. sourcecode:: http POST /api/v1/jobgroups/ HTTP/1.1 Accept: application/json { "title": "Test Group", "user": "******", "main_jobtype": "Test JobType" } **Response** .. sourcecode:: http HTTP/1.1 201 CREATED Content-Type: application/json { "id": 2, "jobs": [], "user": "******", "main_jobtype": "Test JobType", "title": "Test Group" } :statuscode 201: a new job group was created :statuscode 400: there was something wrong with the request (such as invalid columns being included) """ username = g.json.pop("user") user = User.query.filter_by(username=username).first() if not user and AUTOCREATE_USERS: user = User(username=username) if AUTO_USER_EMAIL: user.email = AUTO_USER_EMAIL.format(username=username) db.session.add(user) logger.warning("User %s was autocreated on job group create", username) elif not user: return (jsonify( error="User %s not found" % username), NOT_FOUND) jobtype_name = g.json.pop("main_jobtype") jobtype = JobType.query.filter_by(name=jobtype_name).first() if not jobtype: return (jsonify( error="Jobtype %s not found" % jobtype_name), NOT_FOUND) jobgroup = JobGroup(user=user, main_jobtype=jobtype, **g.json) db.session.add(jobgroup) db.session.commit() jobgroup_data = jobgroup.to_dict() jobgroup_data.pop("user_id", None) jobgroup_data.pop("main_jobtype_id", None) logger.info("Created job group %s: %r", jobgroup.title, jobgroup_data) return jsonify(jobgroup_data), CREATED
def post(self): """ A ``POST`` to this endpoint will create a new job group. .. http:post:: /api/v1/jobgroups/ HTTP/1.1 **Request** .. sourcecode:: http POST /api/v1/jobgroups/ HTTP/1.1 Accept: application/json { "title": "Test Group", "user": "******", "main_jobtype": "Test JobType" } **Response** .. sourcecode:: http HTTP/1.1 201 CREATED Content-Type: application/json { "id": 2, "jobs": [], "user": "******", "main_jobtype": "Test JobType", "title": "Test Group" } :statuscode 201: a new job group was created :statuscode 400: there was something wrong with the request (such as invalid columns being included) """ username = g.json.pop("user") user = User.query.filter_by(username=username).first() if not user and AUTOCREATE_USERS: user = User(username=username) if AUTO_USER_EMAIL: user.email = AUTO_USER_EMAIL.format(username=username) db.session.add(user) logger.warning("User %s was autocreated on job group create", username) elif not user: return (jsonify(error="User %s not found" % username), NOT_FOUND) jobtype_name = g.json.pop("main_jobtype") jobtype = JobType.query.filter_by(name=jobtype_name).first() if not jobtype: return (jsonify(error="Jobtype %s not found" % jobtype_name), NOT_FOUND) jobgroup = JobGroup(user=user, main_jobtype=jobtype, **g.json) db.session.add(jobgroup) db.session.commit() jobgroup_data = jobgroup.to_dict() jobgroup_data.pop("user_id", None) jobgroup_data.pop("main_jobtype_id", None) logger.info("Created job group %s: %r", jobgroup.title, jobgroup_data) return jsonify(jobgroup_data), CREATED