SciPost Code Repository

Skip to content
Snippets Groups Projects
Commit 87ea119b authored by Jean-Sébastien Caux's avatar Jean-Sébastien Caux
Browse files

Work on ontology

parent cc8d25d6
No related branches found
No related tags found
No related merge requests found
...@@ -4,7 +4,7 @@ __license__ = "AGPL v3" ...@@ -4,7 +4,7 @@ __license__ = "AGPL v3"
from django.contrib import admin from django.contrib import admin
from .models import Tag, Topic from .models import Tag, Topic, RelationAsym, RelationSym
class TagAdmin(admin.ModelAdmin): class TagAdmin(admin.ModelAdmin):
...@@ -17,3 +17,7 @@ class TopicAdmin(admin.ModelAdmin): ...@@ -17,3 +17,7 @@ class TopicAdmin(admin.ModelAdmin):
pass pass
admin.site.register(Topic, TopicAdmin) admin.site.register(Topic, TopicAdmin)
admin.site.register(RelationAsym)
admin.site.register(RelationSym)
__copyright__ = "Copyright 2016-2018, Stichting SciPost (SciPost Foundation)"
__license__ = "AGPL v3"
IS_INSTANCE_OF = 'is_instance_of'
IS_SPECIALIZATION_OF = 'is_specialization_of'
IS_REFINEMENT_OF = 'is_refinement_of'
IS_USED_IN = 'is_used_in'
IS_EXAMPLE_OF = 'is_example_of'
TOPIC_RELATIONS_ASYM = (
(IS_INSTANCE_OF, 'is an instance of'),
(IS_SPECIALIZATION_OF, 'is a specialization of'),
(IS_REFINEMENT_OF, 'is a refinement of'),
(IS_USED_IN, 'is used in'),
(IS_EXAMPLE_OF, 'is an example of'),
)
ARE_RELATED = 'are_related'
ARE_SYNONYMS = 'are_synonyms'
TOPIC_RELATIONS_SYM = (
(ARE_RELATED, 'are related'),
(ARE_SYNONYMS, 'are synonyms'),
)
# -*- coding: utf-8 -*-
# Generated by Django 1.11.4 on 2018-10-27 18:33
from __future__ import unicode_literals
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('ontology', '0003_auto_20181027_1748'),
]
operations = [
migrations.CreateModel(
name='RelationAsym',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('relation', models.CharField(choices=[('is_instance_of', 'is an instance of'), ('is_specialization_of', 'is a specialization of'), ('is_refinement_of', 'is a refinement of'), ('is_used_in', 'is used in'), ('is_example_of', 'is an example of')], max_length=32)),
('A', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='relation_LHS', to='ontology.Topic')),
('B', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='relation_RHS', to='ontology.Topic')),
],
),
migrations.CreateModel(
name='RelationSym',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('relation', models.CharField(choices=[('are_related', 'are related'), ('are_synonyms', 'are synonyms')], max_length=32)),
('topics', models.ManyToManyField(to='ontology.Topic')),
],
),
]
...@@ -4,6 +4,8 @@ __license__ = "AGPL v3" ...@@ -4,6 +4,8 @@ __license__ = "AGPL v3"
from django.db import models from django.db import models
from .constants import TOPIC_RELATIONS_ASYM, TOPIC_RELATIONS_SYM
class Tag(models.Model): class Tag(models.Model):
""" """
...@@ -32,3 +34,32 @@ class Topic(models.Model): ...@@ -32,3 +34,32 @@ class Topic(models.Model):
def get_abolute_url(self): def get_abolute_url(self):
return reverse('ontology:topic_details', kwargs={'slug': self.slug}) return reverse('ontology:topic_details', kwargs={'slug': self.slug})
class RelationAsym(models.Model):
"""
An asymmetric Relation between two Topics.
"""
A = models.ForeignKey('ontology.Topic', on_delete=models.CASCADE,
related_name='relation_LHS')
relation = models.CharField(max_length=32, choices=TOPIC_RELATIONS_ASYM)
B = models.ForeignKey('ontology.Topic', on_delete=models.CASCADE,
related_name='relation_RHS')
def __str__(self):
return '%s %s %s' % (self.A, self.get_relation_display(), self.B)
class RelationSym(models.Model):
"""
A symmetric relation between multiple Topics.
"""
topics = models.ManyToManyField('ontology.Topic')
relation = models.CharField(max_length=32, choices=TOPIC_RELATIONS_SYM)
def __str__(self):
text = ''
for topic in self.topics.all():
text += '%s, ' % topic
text += self.get_relation_display()
return text
...@@ -15,5 +15,28 @@ ...@@ -15,5 +15,28 @@
{% endfor %} {% endfor %}
</ul> </ul>
{% endif %} {% endif %}
<h4>Relations</h4>
<div class="row">
<div class="col-6">
<h5>asymmetric:</h5>
<ul>
{% for rel in relations_asym %}
<li>{% if rel.A != topic %}<a href="{% url 'ontology:topic_details' slug=rel.A.slug %}">{{ rel.A}}</a>{% else %}{{ rel.A }}{% endif %} {{ rel.get_relation_display }} {% if rel.B != topic %}<a href="{% url 'ontology:topic_details' slug=rel.B.slug %}">{{ rel.B }}</a>{% else %}{{ rel.B }}{% endif %}</li>
{% empty %}
<li>No relations have been defined</li>
{% endfor %}
</ul>
</div>
<div class="col-6">
<h5>symmetric:</h5>
<ul>
{% for rel in topic.relationsym_set.all %}
<li>{% for reltopic in rel.topics.all %}{% if reltopic != topic %}<a href="{% url 'ontology:topic_details' slug=reltopic.slug %}">{{ reltopic }}</a>{% else %}{{ reltopic }}{% endif %}, {% endfor %} {{ rel.get_relation_display }}</li>
{% empty %}
<li>No symmetric relations have been defined</li>
{% endfor %}
</ul>
</div>
</div>
</div> </div>
</div> </div>
...@@ -3,11 +3,12 @@ __license__ = "AGPL v3" ...@@ -3,11 +3,12 @@ __license__ = "AGPL v3"
from django.core.urlresolvers import reverse_lazy from django.core.urlresolvers import reverse_lazy
from django.db.models import Q
from django.views.generic.detail import DetailView from django.views.generic.detail import DetailView
from django.views.generic.edit import CreateView, UpdateView from django.views.generic.edit import CreateView, UpdateView
from django.views.generic.list import ListView from django.views.generic.list import ListView
from .models import Topic from .models import Topic, RelationAsym, RelationSym
from scipost.mixins import PermissionsMixin from scipost.mixins import PermissionsMixin
...@@ -40,3 +41,8 @@ class TopicListView(ListView): ...@@ -40,3 +41,8 @@ class TopicListView(ListView):
class TopicDetailView(DetailView): class TopicDetailView(DetailView):
model = Topic model = Topic
def get_context_data(self, *args, **kwargs):
context = super().get_context_data(*args, **kwargs)
context['relations_asym'] = RelationAsym.objects.filter(Q(A=self.object) | Q(B=self.object))
return context
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment