Skip to content

Commit

Permalink
Implements create new ontology
Browse files Browse the repository at this point in the history
  • Loading branch information
qlands committed Aug 17, 2021
1 parent aff4a0a commit b6a121e
Show file tree
Hide file tree
Showing 12 changed files with 133 additions and 4 deletions.
11 changes: 10 additions & 1 deletion cropontology/config/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
SectionRemoveUserView,
)

from ..views.ontology import OntologyView, JSONDataView, OntologyRDFView
from ..views.ontology import OntologyView, JSONDataView, CreateOntology
from ..views.api import APIDocView
from ..views.brapi import (
BRAPITraitsView,
Expand Down Expand Up @@ -251,6 +251,15 @@ def load_routes(config):
)
)

routes.append(
add_route(
"create_ontology",
"/ontologies/create",
CreateOntology,
"ontology/add_ontology.jinja2",
)
)

routes.append(
add_route(
"ontology_owl",
Expand Down
39 changes: 39 additions & 0 deletions cropontology/templates/ontology/add_ontology.jinja2
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{% extends 'page.jinja2' %}
{% import 'macros/form.jinja2' as form %}

{% block breadcrums %}
{% include 'ontology/snippets/breadcrums_add.jinja2' %}
{% endblock breadcrums %}

{% block main_container %}
<div class="row">
<div class="col-lg-12">
<div class="card">
<div class="card-header">
<h5 class="card-title m-0">{{ _('Information for the new ontology') }}</h5>
</div>
{{ form.display_errors(errors) }}
<form role="form" method="post" action="{{ request.url }}">
{{ form.secure_form(request) }}
<div class="card-body">
<div class="form-group">
<label for="exampleInputEmail1">{{ _('ID') }}</label>
<input type="text" class="form-control" name="ontology_id" value="{{ form_data.ontology_id }}" placeholder="{{ _('Enter ontology ID') }}">
</div>
<div class="form-group">
<label for="exampleInputEmail1">{{ _('Name') }}</label>
<input type="text" class="form-control" name="ontology_name" value="{{ form_data.ontology_name }}" placeholder="{{ _('Enter a description') }}">
</div>
<div class="form-group">
<label for="exampleInputEmail1">{{ _('Description') }}</label>
<textarea class="form-control" name="ontology_desc">{{ form_data.ontology_desc }}</textarea>
</div>
</div>
<div class="card-footer">
<button type="submit" class="btn btn-primary">{{ _('Add ontology') }}</button>
</div>
</form>
</div>
</div>
</div>
{% endblock main_container %}
13 changes: 13 additions & 0 deletions cropontology/templates/ontology/snippets/breadcrums_add.jinja2
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<div class="container">
<div class="row mb-2">
<div class="col-sm-6">
<h1 class="m-0 text-dark"> {{ _('Ontologies') }} <br><small>{{ _('Add new ontology') }}</small></h1>
</div>
<div class="col-sm-6">
<ol class="breadcrumb float-sm-right">
<li class="breadcrumb-item"><a href="{{ request.route_url('home') }}">{{ _('Home') }}</a></li>
<li class="breadcrumb-item active">{{ _('Add new ontology') }}</li>
</ol>
</div>
</div>
</div>
1 change: 1 addition & 0 deletions cropontology/templates/pages/add_page.jinja2
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
<div class="card-header">
<h5 class="card-title m-0">{{ _('Information for the new page') }}</h5>
</div>
{{ form.display_errors(errors) }}
<form role="form" method="post" action="{{ request.url }}">
{{ form.secure_form(request) }}
<div class="card-body">
Expand Down
1 change: 1 addition & 0 deletions cropontology/templates/pages/edit_page.jinja2
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
<div class="card-header">
<h5 class="card-title m-0">{{ _('Current content of ') }} {{ form_data.page_desc }}</h5>
</div>
{{ form.display_errors(errors) }}
<form role="form" method="post" action="{{ request.url }}">
{{ form.secure_form(request) }}
<div class="card-body">
Expand Down
1 change: 1 addition & 0 deletions cropontology/templates/sections/add_section.jinja2
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
<div class="card-header">
<h5 class="card-title m-0">{{ _('Information for the new section') }}</h5>
</div>
{{ form.display_errors(errors) }}
<form role="form" method="post" action="{{ request.url }}">
{{ form.secure_form(request) }}
<div class="card-body">
Expand Down
1 change: 1 addition & 0 deletions cropontology/templates/sections/edit_section.jinja2
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
<div class="card-header">
<h5 class="card-title m-0">{{ _('Current content of ') }} {{ form_data.section_desc }}</h5>
</div>
{{ form.display_errors(errors) }}
<form role="form" method="post" action="{{ request.url }}">
{{ form.secure_form(request) }}
<div class="card-body">
Expand Down
3 changes: 3 additions & 0 deletions cropontology/templates/snippets/navbar_main.jinja2
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@
<li class="nav-item dropdown">
<a id="dropdownSubMenu1" href="#" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false" class="nav-link dropdown-toggle">{{ _('Site maintenance') }}</a>
<ul aria-labelledby="dropdownSubMenu1" class="dropdown-menu border-0 shadow">
{% if activeUser.super %}
<li><a href="{{ request.route_url('create_ontology') }}" class="dropdown-item">{{ _('Create new ontology') }}</a></li>
{% endif %}
{% if activeUser.super %}
<li><a href="{{ request.route_url('page_list') }}" class="dropdown-item">{{ _('Pages') }}</a></li>
{% endif %}
Expand Down
53 changes: 51 additions & 2 deletions cropontology/views/ontology.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,60 @@
from .classes import PublicView
from .classes import PublicView, PrivateView
from pyramid.httpexceptions import HTTPNotFound, HTTPFound
import pymongo
import json
from webhelpers2.html import literal
from neo4j import GraphDatabase
import requests
from pyramid.response import Response
import datetime


class CreateOntology(PrivateView):
def process_view(self):
if self.request.method == "POST":
form_data = self.get_post_dict()
if form_data["ontology_id"] != "":
if form_data["ontology_desc"] != "":
if form_data["ontology_name"] != "":
mongo_url = self.request.registry.settings.get("mongo.url")
mongo_client = pymongo.MongoClient(mongo_url)
ontology_db = mongo_client["ontologies"]
ontology_collection = ontology_db["ontologies"]
res = ontology_collection.find_one(
{"ontology_id": form_data["ontology_id"]}
)
if res is None:
ontology_data = {
"ontology_summary": form_data["ontology_desc"],
"ontology_id": form_data["ontology_id"],
"created_at": datetime.datetime.now(),
"user_key": self.userID,
"ontology_name": form_data["ontology_name"],
"category": "300-499 Phenotype and Trait Ontology",
"ontology_type": "trait",
}
ontology_collection.insert_one(ontology_data)
self.request.session.flash(
self._("The ontology was created successfully")
)
self.returnRawViewResult = True
return HTTPFound(location=self.request.route_url("home"))
else:
self.append_to_errors(
self._("The ontology ID already exists")
)
else:
self.append_to_errors(
self._("The ontology name cannot be empty")
)
else:
self.append_to_errors(
self._("The ontology description cannot be empty")
)
else:
self.append_to_errors(self._("The ontology ID cannot be empty"))
else:
form_data = {}
return {"form_data": form_data}


class OntologyView(PublicView):
Expand Down
5 changes: 4 additions & 1 deletion cropontology/views/public_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,10 @@ def process_view(self):
roots = []
for an_item in cursor:
roots.append(an_item["p.id"])
an_ontology["root"] = roots[0]
if len(roots) > 0:
an_ontology["root"] = roots[0]
else:
an_ontology["root"] = "None"
if an_ontology["category"] not in categories:
categories.append(an_ontology["category"])
result = []
Expand Down
6 changes: 6 additions & 0 deletions cropontology/views/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ def process_view(self):
"created_at": date,
"language": row["Language"],
"term_type": "term",
"term_id": ontology_id + ":ROOT",
} # Root
if not term_index.term_exists(ontology_id + ":ROOT"):
term_index.add_term(ontology_id + ":ROOT", es_data)
Expand Down Expand Up @@ -271,6 +272,7 @@ def process_view(self):
"created_at": date,
"language": row["Language"],
"term_type": "variable",
"term_id": var_id,
} # Root
if row["Variable synonyms"]:
es_data["variable_synonyms"] = row["Variable synonyms"]
Expand Down Expand Up @@ -425,6 +427,7 @@ def process_view(self):
"created_at": date,
"language": row["Language"],
"term_type": "trait",
"term_id": trait_id,
}
if row["Trait class"]:
es_data["trait_class"] = row["Trait class"]
Expand Down Expand Up @@ -549,6 +552,7 @@ def process_view(self):
"created_at": date,
"language": row["Language"],
"term_type": "method",
"term_id": method_id,
}
if row["Method class"]:
es_data["method_class"] = row["Method class"]
Expand Down Expand Up @@ -684,6 +688,7 @@ def process_view(self):
"created_at": date,
"language": row["Language"],
"term_type": "scale",
"term_id": scale_id,
}
if row["Scale class"]:
es_data["scale_class"] = row["Scale class"]
Expand Down Expand Up @@ -801,6 +806,7 @@ def process_view(self):
"created_at": date,
"language": row["Language"],
"term_type": "term",
"term_id": ontology_id + ":" + row["Trait class"],
}

if not term_index.term_exists(ontology_id + ":" + row["Trait class"]):
Expand Down
3 changes: 3 additions & 0 deletions cropontology/views/term.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ def process_view(self):
es_query = {"query": {"match_phrase": {"term_id": term_id}}}
res, hits = index_manager.free_query(es_query)
if hits == 0:
print("****************************1111")
print("Not hits for {}".format(term_id))
print("****************************1111")
raise HTTPNotFound()
results = []
ontology_id = None
Expand Down

0 comments on commit b6a121e

Please sign in to comment.