def get_json_object(object_id): """ Export the JSON part of a JsonObject as a clean JSON file. """ json_object = JsonObject.query.filter(JsonObject.id == object_id).first() if json_object is None: abort(404) # Log the event new_event = Event( scope="JsonObject", subject=f"id={object_id}", action="object_bp.get_json_object:GET", initiator=request.headers.get("User-Agent"), ) db.session.add(new_event) db.session.commit() result = json.dumps(json_object.json_object, sort_keys=True, indent=4, separators=(",", ": ")) return Response( result, mimetype="application/json", headers={ "Content-Disposition": "attachment;filename={}.json".format( json_object.name.replace(" ", "_")) }, )
def view(object_id=None): """ Display the JSON part of a JsonObject object and some related informations. """ # Examples: # res = JsonObject.query.filter(JsonObject.json_object.contains( # {'predicates': [{'value': 'source-type'}]} # )).all() # res = JsonObject.query.filter(JsonObject.json_object.contains( # {'values': [{'entry': [{'value': 'news-report'}]}]} # )).all() # res = JsonObject.query.filter( # JsonObject.json_object[("namespace")].astext == "osint" # ) # res = JsonObject.query.filter( # JsonObject.json_object[("father-uuid")].astext == "fdsfsf" # ) # res = JsonObject.query.filter( # JsonObject.json_object[("mapping", "father-uuid")].astext == "fdsfsf" # ) # res = JsonObject.query.filter( # JsonObject.json_object[("mapping"), [("father-uuid")]].astext == "fdsfsf" # ) json_object = JsonObject.query.filter(JsonObject.id == object_id).first() if json_object is None: abort(404) try: uuid = json_object.json_object["uuid"] except Exception: uuid = None # Log the event new_event = Event( scope="JsonObject", subject=f"id={object_id} uuid={uuid}", action="object_bp.view:GET", initiator=request.headers.get("User-Agent"), ) db.session.add(new_event) db.session.commit() result = json.dumps( json_object.json_object, ensure_ascii=False, sort_keys=True, indent=4, separators=(",", ": "), ) return render_template( "view_object.html", uuid=uuid, json_object=json_object, json_object_pretty=result, )
def get(self, id): obj = JsonObject.query.filter(JsonObject.id == id).first() if obj: # Log the event new_event = Event( scope="JsonObject", subject=f"id={id}", action="apiv2.object_object_item:GET", initiator=request.headers.get("User-Agent"), ) db.session.add(new_event) db.session.commit() return JsonObject.query.filter(JsonObject.id == id).all(), 200
def delete(self, id): obj = JsonObject.query.filter(JsonObject.id == id).first() if obj: # Log the event new_event = Event( scope="JsonObject", subject=f"id={id}", action="apiv2.object_object_item:DELETE", initiator="{} user-id={}".format( request.headers.get("User-Agent"), current_user.id), ) db.session.add(new_event) db.session.commit() db.session.delete(obj) db.session.commit() return {}, 204
def delete(object_id=None): """ Delete the requested JsonObject. """ json_object = JsonObject.query.filter(JsonObject.id == object_id).first() schema_id = json_object.schema_id # Log the event new_event = Event( scope="JsonObject", subject=object_id, action="object_bp.delete:GET", initiator="{} user-id={}".format(request.headers.get("User-Agent"), current_user.id), ) db.session.add(new_event) db.session.commit() db.session.delete(json_object) db.session.commit() return redirect(url_for("schema_bp.get", schema_id=schema_id))
def patch(self, id): obj = JsonObject.query.filter(JsonObject.id == id).first() if obj: # Log the event new_event = Event( scope="JsonObject", subject=f"id={id}", action="apiv2.object_object_item:PATCH", initiator="{} user-id={}".format( request.headers.get("User-Agent"), current_user.id), ) db.session.add(new_event) db.session.commit() data = { "org_id": obj.org_id, "schema_id": obj.schema_id, "object_id": id, "object_is_locked": obj.is_locked, "object_creator_id": obj.creator_id, "json_object": object_ns.payload["json_object"], } try: # check the submitted object check_submitted_object(data) # create a new version of the object to update create_new_version(obj.id) obj.editor_id = current_user.id # update the object obj.json_object = object_ns.payload["json_object"] db.session.commit() except Exception as e: raise (e) return obj, 201
def get(per_page, schema_id=None): """Return the schema given in parameter with the objects validated by this schema. Also returns the objects validated by this schema. It is possible to filter the objects based on some properties of the schema. """ schema = Schema.query.filter(Schema.id == schema_id).first() if schema is None: abort(404) # Log the event new_event = Event( scope="Schema", subject=f"id={schema_id}", action="schema_bp.get:GET", initiator=request.headers.get("User-Agent"), ) db.session.add(new_event) db.session.commit() # Loads all objects related to the schema query = JsonObject.query.filter(JsonObject.schema_id == schema.id) # Search on the fields of the JSONB object # 1. Look for the searchable properties of the current schema. Actuallay we # accept string, boolean and integer search_keys = { key: ( key in schema.json_schema["required"], schema.json_schema["properties"][key].get("type", False), ) for key in schema.json_schema.get("properties", []) if schema.json_schema["properties"][key].get("type", False) in ["string", "boolean", "integer"] } # 2. Get the query of the user search_key = request.args.get("search_key", "") search_term = request.args.get("search_term", "") # 3. Prepare the filter (query on a JSONB object) based on the type of the # attribute if search_key and search_term: if search_keys[search_key][1] == "string": # string: ilike search query = query.filter( JsonObject.json_object[(search_key)].astext.ilike("%" + search_term + "%")) elif search_keys[search_key][1] == "boolean": # boolean query = query.filter( JsonObject.json_object[(search_key)].astext.cast(Boolean).is_( ast.literal_eval(search_term))) elif search_keys[search_key][1] == "integer": # integer query = query.filter( JsonObject.json_object[(search_key)].astext.cast( Integer) == ast.literal_eval(search_term)) # Pagination page, per_page, offset = get_page_args() pagination = Pagination( page=page, total=query.count(), css_framework="bootstrap4", search=False, record_name="objects", per_page=per_page, ) return render_template( "schema.html", schema=schema, objects=query.offset(offset).limit(per_page), pagination=pagination, search_keys=search_keys, search_key=search_key, search_term=search_term, )
def process_form(object_id=None): """Process the form to edit an object.""" form = AddObjectForm() form.org_id.choices = [(0, "")] form.org_id.choices.extend([(org.id, org.name) for org in current_user.organizations]) if not form.validate(): return render_template("edit_object.html", form=form) # Edit an existing JsonObject if object_id is not None: # Load the object to edit and create a new Version instance for the versioning. json_object = JsonObject.query.filter( JsonObject.id == object_id).first() json_object.create_new_version() json_object.editor_id = current_user.id form.schema_id.data = json_object.schema_id # Licenses new_licenses = [] for license_id in form.licenses.data: license = License.query.filter(License.id == license_id).first() new_licenses.append(license) json_object.licenses = new_licenses del form.licenses # refers_to relationship new_json_objects_to_link = [] for cur_json_object_id in form.refers_to.data: json_object_dep = JsonObject.query.filter( JsonObject.id == cur_json_object_id).first() new_json_objects_to_link.append(json_object_dep) json_object.refers_to = new_json_objects_to_link del form.refers_to # referred_to_by relationship new_json_objects_to_link = [] for cur_json_object_id in form.referred_to_by.data: json_object_dep = JsonObject.query.filter( JsonObject.id == cur_json_object_id).first() new_json_objects_to_link.append(json_object_dep) json_object.referred_to_by = new_json_objects_to_link del form.referred_to_by form.populate_obj(json_object) try: db.session.commit() flash( gettext("%(object_name)s successfully updated.", object_name=form.name.data), "success", ) # Log the event new_event = Event( scope="JsonObject", subject=f"id={object_id}", action="object_bp.process_form:POST", initiator="{} user-id={}".format( request.headers.get("User-Agent"), current_user.id), ) db.session.add(new_event) db.session.commit() except Exception as e: print(e) form.name.errors.append("Name already exists.") return redirect(url_for("object_bp.form", object_id=json_object.id)) # Create a new JsonObject new_object = JsonObject( name=form.name.data, description=form.description.data, schema_id=form.schema_id.data, org_id=form.org_id.data, creator_id=current_user.id, editor_id=current_user.id, ) db.session.add(new_object) try: db.session.commit() except Exception: return redirect(url_for("object_bp.form", object_id=new_object.id)) # Licenses new_licenses = [] for license_id in form.licenses.data: license = License.query.filter(License.id == license_id).first() new_licenses.append(license) new_object.licenses = new_licenses del form.licenses # refers_to relationship new_json_objects_to_link = [] for cur_json_object_id in form.refers_to.data: json_object_dep = JsonObject.query.filter( JsonObject.id == cur_json_object_id).first() new_json_objects_to_link.append(json_object_dep) new_object.refers_to = new_json_objects_to_link del form.refers_to # referred_to_by relationship new_json_objects_to_link = [] for cur_json_object_id in form.referred_to_by.data: json_object_dep = JsonObject.query.filter( JsonObject.id == cur_json_object_id).first() new_json_objects_to_link.append(json_object_dep) new_object.referred_to_by = new_json_objects_to_link del form.referred_to_by try: db.session.commit() flash( gettext("%(object_name)s successfully created.", object_name=new_object.name), "success", ) except Exception: return redirect(url_for("object_bp.form", object_id=new_object.id)) return redirect(url_for("object_bp.form", object_id=new_object.id))
def get(self): """List all objects.""" args = parser.parse_args() offset = args.pop("page", 1) - 1 limit = args.pop("per_page", 10) object_uuid = args.pop("uuid", None) object_language = args.pop("language", None) object_organization = args.pop("organization", None) object_schema = args.pop("schema", None) object_schema_uuid = args.pop("schema_uuid", None) name_ilike = args.pop("name_ilike") args = {k: v for k, v in args.items() if v is not None} result: ResultType = { "data": [], "metadata": { "count": 0, "offset": offset, "limit": limit, }, } # Log the event new_event = Event( scope="JsonObject", subject="id={} uuid={} schema_uuid={}".format( "", str(object_uuid), str(object_schema_uuid)), action="apiv2.object_objects_list:GET", initiator=request.headers.get("User-Agent"), ) db.session.add(new_event) db.session.commit() results = [] count = 0 query = JsonObject.query # Filter on attribute of the object for arg in args: if hasattr(JsonObject, arg): try: query = query.filter(getattr(JsonObject, arg) == args[arg]) except Exception: pass # Filter on other attributes if name_ilike: query = query.filter(JsonObject.name.ilike("%" + name_ilike + "%")) if object_organization is not None: query = query.filter( JsonObject.organization.has(name=object_organization)) if object_schema is not None: # Schema name of the object query = query.filter(JsonObject.schema.has(name=object_schema)) if object_schema_uuid is not None: # Schema UUID of the object query = query.join(Schema).filter(Schema.json_schema[( "$id")].astext.like("%" + str(object_schema_uuid) + "%")) if object_uuid is not None: query = query.filter( JsonObject.json_object[("uuid")].astext == str(object_uuid)) if object_language is not None: query = query.filter( JsonObject.json_object[("language")].astext == object_language) query = query.order_by(JsonObject.last_updated.desc()) total = query.count() query = query.limit(limit) results = query.offset(offset * limit) count = total result["data"] = results result["metadata"]["count"] = count return result, 200