class AnnotationsAPI(Resource): def __init__(self): super(AnnotationsAPI, self).__init__() user_args = { "project_id": fields.Int(required=True), "tup_id": fields.Int(required=True), "answer_best": fields.String(required=True), "answer_worst": fields.String(required=True) } # Add annotation into db @fresh_jwt_required @use_args(user_args, location="json") def post(self, args): email = get_jwt_identity() project_id = args['project_id'] tup_id = args['tup_id'] answer_best = args['answer_best'] answer_worst = args['answer_worst'] rst, msg = check_authorization(project_id, email) if not rst: raise UnauthorizedProjectError annotate(project_id, email, tup_id, answer_best, answer_worst) return jsonify({"Result" : "True"})
class ProgressAPI(Resource): def __init__(self): super(ProgressAPI, self).__init__() # Return progress annotation @jwt_required @use_args({"project_id": fields.Int(required=True)}, location="query") def get(self, args): email = get_jwt_identity() project_id = args['project_id'] rst, msg = check_authorization(project_id, email) if not rst: raise UnauthorizedProjectError done, total = progress(project_id, email) if done is None and total is None: raise ResourceNotFoundError return jsonify({'done': done, 'total': total})
class TuplesAPI(Resource): def __init__(self): super(TuplesAPI, self).__init__() # Get next tuple to annotate @jwt_required @use_args({"project_id": fields.Int(required=True)}, location="query") def get(self, args): email = get_jwt_identity() project_id = args['project_id'] rst, msg = check_authorization(project_id, email) if not rst: raise UnauthorizedProjectError tup_id, tup, done, total = next_tuple(project_id, email) if tup is None: return {"Error": "No tuple to annotate"} return jsonify({'tup_id': tup_id, 'tup': tup, 'done': done, 'total': total})
class GoldAPI(Resource): def __init__(self): super(GoldAPI, self).__init__() @fresh_jwt_required @use_args({"project_id": fields.Int(required=True)}, location="query") def get(self, args): email = get_jwt_identity() project_id = args['project_id'] rst, msg = check_authorization(project_id, email) if not rst: raise UnauthorizedProjectError rst, file = generate_gold(project_id) if not rst: raise EmptyAnnotationsError response = make_response(file) response.headers['content-type'] = 'application/octet-stream' return response
400, "{0} is not a valid company type. Please choose buyer or seller.". format(company_type)) company = Company( name=name, description=description, company_type=company_type, ) db.session.add(company) db.session.commit() db.session.refresh(company) return company @app.route("/companies/<int:company_id>", methods=["PUT"]) @marshal_with(CompanySchema()) @use_kwargs({ "company_id": fields.Int(), "name": fields.Str(), "description": fields.Str(), }) def route_company_update(company_id, **kwargs): company = Company.query.filter(Company.id == company_id).first() for key, val in kwargs.items(): if val is not None: setattr(company, key, val) db.session.add(company) db.session.commit() db.session.refresh(company) return company
class AuthorizationAPI(Resource): def __init__(self): super(AuthorizationAPI, self).__init__() user_args = { "project_id": fields.Int(required=True), "user_to": fields.String(required=True), } # Add authorization @fresh_jwt_required @use_args(user_args, location="json") def post(self, args): email = get_jwt_identity() project_id = args['project_id'] user_to = args['user_to'] rst, msg = check_owner(project_id, email) if not rst: raise UnauthorizedProjectError rst, msg = get_authorization(project_id, user_to) if not rst: return {'result': 'False'} with open('templates/auth.html', 'r') as f: body = f.read() body = Template(body).safe_substitute(user=email,user_to=user_to) mail = { 'from' : '*****@*****.**', 'object' : 'LITESCALE - NEW PROJECT!', 'message' : body } # Connect to server server = smtplib.SMTP_SSL('out.virgilio.it', 465) # Login server.login(mail['from'], "6$ki7M!n8y3a2zc") # Send mail msg = MIMEText(mail['message'], 'html', 'utf-8') msg['Subject'] = mail['object'] msg['From'] = mail['from'] msg['To'] = user_to server.sendmail(mail['from'], user_to, msg.as_string()) return {'result': 'True'} # Remove authorization @fresh_jwt_required @use_args(user_args, location="json") def delete(self, args): email = get_jwt_identity() project_id = args['project_id'] user_to = args['user_to'] rst, msg = check_owner(project_id, email) if not rst: raise UnauthorizedProjectError if email == user_to: return {'result': 'False'} rst, msg = remove_authorization(project_id, user_to) if not rst: return {'result': 'False'} return {'result': 'True'}
class ProjectsAPI(Resource): def __init__(self): super(ProjectsAPI, self).__init__() # Return project info @jwt_required @use_args({"project_id": fields.Int(required=True)}, location="query") def get(self, args): email = get_jwt_identity() project_id = args['project_id'] rst, project_dict = get_project_info(project_id) if not rst: raise ResourceNotFoundError rst, msg = check_authorization(project_id, email) if not rst: raise UnauthorizedProjectError return project_dict # Delete project @fresh_jwt_required @use_args({"project_id": fields.Int(required=True)}, location="query") def delete(self, args): email = get_jwt_identity() project_id = args['project_id'] rst, msg = check_owner(project_id, email) if not rst: raise UnauthorizedProjectError rst, msg = delete_project(project_id) if not rst: raise ResourceNotFoundError return {"result": "True"} # Create a new project @fresh_jwt_required def post(self): email = get_jwt_identity() if not request.files or not request.form: raise MissingProjectInfoError if not request.files['file']: raise MissingProjectInfoError if not request.form['json']: raise MissingProjectInfoError info = json.loads((request.form['json'])) if not 'project_name' in info or not 'tuple_size' in info or not 'phenomenon' in info or not 'replication' in info: raise MissingProjectInfoError instance_file = request.files['file'] #file_name, extension = os.path.splitext(instance_file.filename) #if extension != ".tsv": # raise InvalidFileUploadError instance_file.save('instance_file.tsv') project_name = info['project_name'] tuple_size = info['tuple_size'] phenomenon = info['phenomenon'] replication = info['replication'] project_id, msg = new_project( email, project_name, phenomenon, tuple_size, replication, 'instance_file.tsv' ) try: os.remove('instance_file.tsv') except: pass if (not project_id): # project not created raise InvalidFileUploadError return {"result": "True", "id": project_id}
@app.route("/orders/<int:order_id>", methods=["GET"]) def route_order_get_by_id(order_id): order = Order.query.filter(Order.id == order_id).first() if not order: return abort(400, "The order with id: {0} does not exists".format(order_id)) return jsonify(order.as_dict()) @app.route("/orders", methods=["POST"]) @marshal_with(OrderSchema()) @use_kwargs( { "name": fields.Str(), "description": fields.Str(), "buyer_id": fields.Int(), "seller_id": fields.Int(), "lineitems": fields.List(fields.Dict(), required=True), } ) def route_order_create(name, description, buyer_id, seller_id, lineitems): buyer = Company.query.filter(Company.id == buyer_id).first() seller = Company.query.filter(Company.id == seller_id).first() order_items = [] order = Order( name=name, description=description, buyer=buyer, seller=seller, ) for l in lineitems:
@app.route("/products/<int:product_id>", methods=["GET"]) def route_product_get_by_id(product_id): product = Product.query.filter(Product.id == product_id).first() if not product: return abort( 400, "The product with id: {0} does not exists".format(product_id)) return jsonify(product.as_dict()) @app.route("/products", methods=["POST"]) @marshal_with(ProductSchema()) @use_kwargs({ "name": fields.Str(), "description": fields.Str(), "company_id": fields.Int(), }) def route_product_create(name, description, company_id): company = Company.query.filter(Company.id == company_id).first() product = Product(name=name, description=description, company=company) db.session.add(product) db.session.commit() db.session.refresh(product) return product @app.route("/products/<int:product_id>", methods=["PUT"]) @marshal_with(ProductSchema()) @use_kwargs({ "product_id": fields.Int(), "name": fields.Str(),