def delete_entity(): user_id = g.user['id'] subgraph_id = request.json.get('subgraph_id') entity_uri = request.json.get('entity_uri') property_uri = request.json.get('property_uri') if subgraph_id is None: return jsonify(error='{} id cannot be empty.'.format( current_app.config['FRONTEND_CONFIG']['Subgraph_term'])) try: subgraph_id = int(subgraph_id) except ValueError: return jsonify(error='{} id has to be an integer.'.format( current_app.config['FRONTEND_CONFIG']['Subgraph_term'])) if not entity_uri or entity_uri.isspace(): return jsonify(error='Entity URI cannot be empty.') if not subgraph_access(user_id, subgraph_id): return jsonify(error=MSG_SUBGRAPH_ACCESS.format(subgraph_id, user_id)) subgraph_knowledge = get_subgraph_knowledge(subgraph_id) if not subgraph_knowledge.is_individual_deletable(entity_uri): return jsonify(error='You are not allowed to delete this entity.') deleted = subgraph_knowledge.delete_individual_recursive(entity_uri) if property_uri: is_functional = get_ontology().is_property_functional(property_uri) return jsonify(deleted=deleted, functional=is_functional) return jsonify(deleted=deleted)
def download_ontology(): filename = '{}_ontology_{}.ttl'.format( current_app.config['FRONTEND_CONFIG']['tool_name'], strftime('%Y-%m-%d-%H-%M-%S')) content = get_ontology().get_serialization(clean=True) return Response( content, mimetype='text/plain', headers={'Content-disposition': 'attachment; filename=' + filename})
def change_value(): user_id = g.user['id'] subgraph_id = request.json.get('subgraph_id') entity_uri = request.json.get('entity_uri') property_uri = request.json.get('property_uri') index = request.json.get('index') value = request.json.get('value').strip() if subgraph_id is None: return jsonify(error='Subgraph id cannot be empty.') try: subgraph_id = int(subgraph_id) except ValueError: return jsonify(error='{} id has to be an integer.'.format( current_app.config['FRONTEND_CONFIG']['Subgraph_term'])) if not entity_uri or entity_uri.isspace(): return jsonify(error='Entity URI cannot be empty.') if not property_uri or property_uri.isspace(): return jsonify(error='Property URI cannot be empty.') if index is None: return jsonify(error='Index cannot be empty.') try: index = int(index) except ValueError: return jsonify(error='Index has to be an integer.') if value is None: return jsonify(error='Value cannot be empty.') if not subgraph_access(user_id, subgraph_id): return jsonify(error=MSG_SUBGRAPH_ACCESS.format(subgraph_id, user_id)) subgraph_knowledge = get_subgraph_knowledge(subgraph_id) if get_ontology().is_property_described(property_uri): return jsonify( error= 'You have to use /_change_label to change the label of a described individual' ) if index == subgraph_knowledge.get_property_free_index( entity_uri, property_uri): subgraph_knowledge.new_value(entity_uri, property_uri) validity = subgraph_knowledge.change_value(entity_uri, property_uri, index, value.strip()) if validity: return jsonify(validity=validity) return jsonify()
def add_user(): user_name = request.json.get('name') user_password = request.json.get('password') user_is_admin = request.json.get('admin') is not None if not user_name or user_name.isspace(): return jsonify(error='Username cannot be empty.') user_name = user_name.strip() if not user_password or user_password.isspace(): return jsonify(error='Password cannot be empty.') user_password = generate_password_hash(user_password) user_uri = get_ontology().get_new_uri_user().n3() db = get_db() db_cursor = db.cursor() if user_name in (r['username'] for r in db_cursor.execute( 'SELECT username FROM user').fetchall()): return jsonify( error='There is already a (potentially deleted) user of that name.' ) db_cursor.execute( 'INSERT INTO user (username, password, admin, uri) VALUES (?, ?, ?, ?)', (user_name, user_password, user_is_admin, user_uri)) user_id = db_cursor.lastrowid user = db_cursor.execute( 'SELECT id, username, admin, uri FROM user WHERE id = ?', (user_id, )).fetchone() db.commit() render_user_row = get_template_attribute('tool/admin_macros.html', 'user_row') user_row = render_user_row(user) return jsonify(user_row=user_row)
def __init__(self): self.graph = Graph() self.graph.parse(data=get_filter_description(), format='ttl') self.uri = RATIO.Filter self.label = 'Filter' self.comment = '' self.is_deletable = False self.knowledge = Graph() db = get_db() for row in db.execute('SELECT * FROM namespace').fetchall(): self.knowledge.namespace_manager.bind(row['prefix'], parse_n3_term(row['uri'])) rows = db.execute( 'SELECT subject, predicate, object FROM knowledge WHERE subgraph_id IN (' ' SELECT id FROM subgraph WHERE finished = 1 AND deleted = 0' ')', ).fetchall() for row in rows: self.knowledge.add(row_to_rdf(row)) self.fields = [ FilterField(property_uri, self.graph, get_ontology().graph, self.knowledge) for property_uri in set(self.graph.subjects()) ] self.fields.sort(key=lambda field: field.order) # todo just for debugging, should be tested when uploading a new filter.ttl # also, check that there are no described fields if [f.order for f in self.fields] != list(range(1, len(self.fields) + 1)): print('There is an error in the order of the fields of the filter') print([(f.order, f.label) for f in self.fields])
def add_entity(): user_id = g.user['id'] subgraph_id = request.json.get('subgraph_id') property_uri = request.json.get('property_uri') parent_uri = request.json.get('entity_uri') entity_label = request.json.get('label') if subgraph_id is None: return jsonify(error='{} id cannot be empty.'.format( current_app.config['FRONTEND_CONFIG']['Subgraph_term'])) try: subgraph_id = int(subgraph_id) except ValueError: return jsonify(error='{} id has to be an integer.'.format( current_app.config['FRONTEND_CONFIG']['Subgraph_term'])) if not property_uri or property_uri.isspace(): return jsonify(error='Property URI cannot be empty.') if not parent_uri or parent_uri.isspace(): return jsonify(error='Parent URI cannot be empty.') if not entity_label or entity_label.isspace(): return jsonify(error='Entity label cannot be empty.') if not subgraph_access(user_id, subgraph_id): return jsonify(error=MSG_SUBGRAPH_ACCESS.format(subgraph_id, user_id)) subgraph_knowledge = get_subgraph_knowledge(subgraph_id) ontology = get_ontology() field_is_deletable = ontology.is_property_deletable(property_uri) field_is_functional = ontology.is_property_functional(property_uri) field_values = subgraph_knowledge.get_property_values( parent_uri, property_uri) field_range = ontology.get_property_range(property_uri) if not field_is_deletable: return jsonify( error='You are not allowed to add entities to this field.') if field_is_functional and field_values: return jsonify( error='You cannot add more than one value to this field.') index = subgraph_knowledge.new_value(parent_uri, property_uri) entity, option_fields = subgraph_knowledge.new_individual( field_range, entity_label, True) subgraph_knowledge.change_value(parent_uri, property_uri, index, entity.uri) render_entity_div = get_template_attribute('tool/edit_macros.html', 'entity_div') entity_div = render_entity_div(entity, index=index, is_deletable=field_is_deletable, collapsed=False) if option_fields: option_fields = [str(f.property_uri) for f in option_fields] render_option_div = get_template_attribute('tool/edit_macros.html', 'option_div') option_div = render_option_div(entity, True) return jsonify(entity_div=entity_div, remove_plus=field_is_functional, option_fields=option_fields, option_div=option_div) return jsonify(entity_div=entity_div, remove_plus=field_is_functional)
def add_option(): # If an index is given, the corresponding value is changed to the new option user_id = g.user['id'] user_uri = g.user['uri'] subgraph_id = request.json.get('subgraph_id') entity_uri = request.json.get('entity_uri') property_uri = request.json.get('property_uri') label = request.json.get('label') index = request.json.get('index') if subgraph_id is None: return jsonify(error='Subgraph id cannot be empty.') try: subgraph_id = int(subgraph_id) except ValueError: return jsonify(error='{} id has to be an integer.'.format( current_app.config['FRONTEND_CONFIG']['Subgraph_term'])) if not property_uri or property_uri.isspace(): return jsonify(error='Property URI cannot be empty.') if not label or label.isspace(): return jsonify(error='Label cannot be empty.') if not subgraph_access(user_id, subgraph_id): return jsonify(error=MSG_SUBGRAPH_ACCESS.format(subgraph_id, user_id)) subgraph_knowledge = get_subgraph_knowledge(subgraph_id) ontology = get_ontology() if ontology.is_property_described(property_uri): return jsonify( error='Use _add_entity to add entities to a described field.') if not ontology.is_property_add_custom_option_allowed(property_uri): return jsonify(error='Adding options to this field is not allowed.') option, option_fields = ontology.new_option( ontology.get_property_range(property_uri), label, user_uri) if index is not None: try: index = int(index) except ValueError: return jsonify(error='Index has to be an integer.') # not only add the option to the ontology but also to the list of values of the field if not entity_uri or entity_uri.isspace(): return jsonify(error='Entity URI cannot be empty.') if index == subgraph_knowledge.get_property_free_index( entity_uri, property_uri): index = subgraph_knowledge.new_value(entity_uri, property_uri) subgraph_knowledge.change_value(entity_uri, property_uri, index, option.uri) render_option_div = get_template_attribute('tool/edit_macros.html', 'option_div') option_div = render_option_div(option, True) return jsonify(option_div=option_div, option_fields=list(option_fields), option_label=option.label, option_uri=option.uri, index=index)