def project(id: int = None, name: str = None) -> Tuple[Any, int]: project = Project.query.get(id) if id else Project.query.filter( Project.name == name.lower()).first() if not project: return make_resp(NOT_FOUND) if request.method == "GET": return jsonify(data=project_schema.dump(project)), 200 elif request.method == "PUT": if project.user != get_user(): return make_resp(FORBIDDEN) if not request.is_json: return make_resp(NO_JSON) try: project_schema.context["project_id"] = project.id project = project_schema.load(request.get_json(), instance=project) project.last_modified = datetime.datetime.utcnow() project_schema.context.pop("project_id") except ValidationError as errors: return errors.messages, 422 db.session.commit() return jsonify(data=project_schema.dump(project)), 200 elif request.method == "DELETE": if project.user != get_user(): return make_resp(FORBIDDEN) db.session.delete(project) db.session.commit() return make_resp(({"msg": "success"}, 200))
def user(id: int = None, username: str = None) -> Tuple[Any, int]: user = get_user(id if id else username.lower() if username else None) if not user: return make_resp(NOT_FOUND) if request.method == "GET": return jsonify(data=user_schema.dump(user) if get_user() == user else other_user_schema.dump(user)), 200 elif request.method == "PUT": if user != get_user(): return make_resp(FORBIDDEN) if not request.is_json: return make_resp(NO_JSON) try: user_schema.context["user_id"] = user.id user = user_schema.load(request.get_json(), instance=user) user_schema.context.pop("user_id") except ValidationError as errors: return errors.messages, 422 db.session.commit() return jsonify(data=user_schema.dump(user)), 200 elif request.method == "DELETE": if user != get_user(): return make_resp(FORBIDDEN) db.session.delete(user) db.session.commit() return make_resp(({"msg": "success"}, 200))
def projects(user_id: int = None, username: str = None) -> Tuple[Any, int]: user = get_user( user_id if user_id else username.lower() if username else None) if not user: return make_resp(NOT_FOUND) if request.method == "GET": return jsonify(data=project_schema.dump(Project.query.filter( Project.user_id == user.id).all(), many=True)), 200 elif request.method == "POST": if get_user() is None: return make_resp(UNAUTHORIZED) if not request.is_json: return make_resp(NO_JSON) try: project = project_schema.load(request.get_json()) project.user = get_user() project.root_directory = Directory(name="root") project.root_directory.block_files = [ BlockFile(name="app.py", block_xml="") ] project.last_modified = datetime.datetime.utcnow() except ValidationError as errors: return errors.messages, 422 db.session.add(project) db.session.commit() return jsonify(data=project_schema.dump(project)), 200
def directory(id: int) -> Tuple[Any, int]: directory = Directory.query.get(id) if not directory: return make_resp(NOT_FOUND) if request.method == "GET": return jsonify(data=directory_schema.dump(directory)), 200 elif request.method == "PUT": if directory.project.user != get_user(): return make_resp(FORBIDDEN) if not request.is_json: return make_resp(NO_JSON) try: directory = directory_schema.load(request.get_json(), instance=directory, partial=True) directory.project.last_modified = datetime.datetime.utcnow() except ValidationError as errors: return errors.messages, 422 db.session.commit() return jsonify(data=directory_schema.dump(directory)), 200 elif request.method == "DELETE": if directory.project.user != get_user(): return make_resp(FORBIDDEN) db.session.delete(directory) db.session.commit() return make_resp(({"msg": "success"}, 200))
def create_directory_with_path(project_id: int = None, project_name: str = None, parent_dir_path: str = "") -> Tuple[Any, int]: project = Project.query.get( project_id) if project_id else Project.query.filter( Project.name == project_name.lower()).first() if not get_user(): return make_resp(UNAUTHORIZED) if not project: return make_resp(NOT_FOUND) if project.user != get_user(): return make_resp(FORBIDDEN) if not request.is_json: return make_resp(NO_JSON) parent_dir = get_sub_directory_from_path(project.root_directory, parent_dir_path) if not parent_dir: return make_resp(NOT_FOUND) try: directory = in_directory_schema.load(request.get_json()) directory.parent = parent_dir project.last_modified = datetime.datetime.utcnow() except ValidationError as errors: return errors.messages, 422 db.session.add(directory) db.session.commit() return jsonify(data=directory_schema.dump(directory)), 200
def users_post() -> Tuple[Any, int]: if get_user() is not None: return make_resp(FORBIDDEN) if not request.is_json: return make_resp(NO_JSON) try: user = user_schema.load(request.get_json()) except ValidationError as errors: return errors.messages, 422 db.session.add(user) db.session.commit() return jsonify(data=user_schema.dump(user), access_token=create_access_token(identity=user)), 200
def get_dir_contents(project_id: int = None, project_name: str = None, file_path: str = "") -> Tuple[Any, int]: project = Project.query.get( project_id) if project_id else Project.query.filter( Project.name == project_name.lower()).first() if not project: return make_resp(NOT_FOUND) dir = get_sub_directory_from_path(project.root_directory, file_path) if not dir: return make_resp(NOT_FOUND) return jsonify(data=directory_schema.dump(dir)), 200
def custom_user_loader_error(identity): ret = { "msg": "User {} not found".format(identity) } return make_resp(UNAUTHORIZED)