Skip to content

Commit

Permalink
feat: add vocabs app
Browse files Browse the repository at this point in the history
  • Loading branch information
b1rger committed Jan 15, 2024
1 parent 3dc7168 commit 2a09ed9
Show file tree
Hide file tree
Showing 12 changed files with 275 additions and 0 deletions.
1 change: 1 addition & 0 deletions apis_core/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,5 @@
),
path("api/dumpdata", Dumpdata.as_view()),
path("", include("apis_core.generic.urls", namespace="generic")),
path("vocabs/", include("apis_core.vocabs.urls")),
]
6 changes: 6 additions & 0 deletions apis_core/vocabs/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from django.apps import AppConfig


class VocabsConfig(AppConfig):
default_auto_field = "django.db.models.AutoField"
name = "apis_core.vocabs"
101 changes: 101 additions & 0 deletions apis_core/vocabs/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
# Generated by Django 4.2.8 on 2023-12-21 11:54

from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

initial = True

dependencies = [
("contenttypes", "0002_remove_content_type_name"),
]

operations = [
migrations.CreateModel(
name="SkosCollection",
fields=[
(
"id",
models.AutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
(
"name",
models.CharField(
help_text="Collection label or name",
max_length=300,
verbose_name="skos:prefLabel",
),
),
(
"label_lang",
models.CharField(
blank=True,
default="en",
help_text="Language of preferred label given above",
max_length=3,
verbose_name="skos:prefLabel language",
),
),
(
"creator",
models.TextField(
blank=True,
help_text="Person or organisation that created this collectionIf more than one list all using a semicolon ;",
verbose_name="dc:creator",
),
),
(
"contributor",
models.TextField(
blank=True,
help_text="Person or organisation that made contributions to the collectionIf more than one list all using a semicolon ;",
verbose_name="dc:contributor",
),
),
(
"parent",
models.ForeignKey(
null=True,
on_delete=django.db.models.deletion.CASCADE,
to="vocabs.skoscollection",
),
),
],
),
migrations.CreateModel(
name="SkosCollectionContentObject",
fields=[
(
"id",
models.AutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
("object_id", models.PositiveIntegerField()),
(
"collection",
models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE,
to="vocabs.skoscollection",
),
),
(
"content_type",
models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE,
to="contenttypes.contenttype",
),
),
],
),
]
19 changes: 19 additions & 0 deletions apis_core/vocabs/migrations/0002_alter_skoscollection_parent.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Generated by Django 4.2.8 on 2024-01-15 17:20

from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

dependencies = [
('vocabs', '0001_initial'),
]

operations = [
migrations.AlterField(
model_name='skoscollection',
name='parent',
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, to='vocabs.skoscollection'),
),
]
Empty file.
56 changes: 56 additions & 0 deletions apis_core/vocabs/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
from django.contrib.contenttypes.fields import GenericForeignKey
from django.contrib.contenttypes.models import ContentType
from django.db import models


class SkosCollection(models.Model):
"""
SKOS collections are labeled and/or ordered groups of SKOS concepts.
Collections are useful where a group of concepts shares something in common,
and it is convenient to group them under a common label, or
where some concepts can be placed in a meaningful order.
Miles, Alistair, and Sean Bechhofer. "SKOS simple knowledge
organization system reference. W3C recommendation (2009)."
"""

parent = models.ForeignKey("self", null=True, on_delete=models.CASCADE, blank=True)
name = models.CharField(
max_length=300,
verbose_name="skos:prefLabel",
help_text="Collection label or name",
)
label_lang = models.CharField(
max_length=3,
blank=True,
default="en",
verbose_name="skos:prefLabel language",
help_text="Language of preferred label given above",
)
creator = models.TextField(
blank=True,
verbose_name="dc:creator",
help_text="Person or organisation that created this collection"
"If more than one list all using a semicolon ;",
)
contributor = models.TextField(
blank=True,
verbose_name="dc:contributor",
help_text="Person or organisation that made contributions to the collection"
"If more than one list all using a semicolon ;",
)

def __str__(self):
return self.name


class SkosCollectionContentObject(models.Model):
collection = models.ForeignKey(SkosCollection, on_delete=models.CASCADE)

content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE)
object_id = models.PositiveIntegerField()
content_object = GenericForeignKey("content_type", "object_id")

def __str__(self):
return f"{self.content_object} -> {self.collection}"
6 changes: 6 additions & 0 deletions apis_core/vocabs/templates/vocabs/toggle_skoscollection.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<a href="{% url "apis_core:togglecollectionobject" content_type_id object_id collection.id %}?to={{ request.get_full_path }}"
hx-get="{% url "apis_core:togglecollectionobject" content_type_id object_id collection.id %}"
hx-swap="outerHTML"
class="btn btn-sm {% if exists %}btn-success{% else %}btn-outline-secondary{% endif %}">
{{ collection.name }}
</a>
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{% load vocabs %}
{% for child in children %}
{% toggle_skoscollection model child %}
{% endfor %}
39 changes: 39 additions & 0 deletions apis_core/vocabs/templatetags/vocabs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
from django import template
from django.contrib.contenttypes.models import ContentType

from apis_core.vocabs.models import SkosCollectionContentObject, SkosCollection


register = template.Library()


@register.inclusion_tag("vocabs/toggle_skoscollection.html", takes_context=True)
def toggle_skoscollection(context, model, collection):
content_type = ContentType.objects.get_for_model(model)
context["content_type_id"] = content_type.id
context["object_id"] = model.id
context["collection"] = collection
context["exists"] = SkosCollectionContentObject.objects.filter(
object_id=model.id, content_type=content_type, collection=collection
).exists()
return context


@register.inclusion_tag("vocabs/toggle_skoscollection.html", takes_context=True)
def toggle_skoscollection_by_name(context, model, collectionname):
collection = SkosCollection.objects.get(name=collectionname)
return toggle_skoscollection(context, model, collection)


@register.inclusion_tag("vocabs/toggle_skoscollection_children.html", takes_context=True)
def toggle_skoscollection_children(context, model, collection):
context["children"] = SkosCollection.objects.filter(parent=collection)
context["model"] = model
context["collection"] = collection
return context


@register.inclusion_tag("vocabs/toggle_skoscollection_children.html", takes_context=True)
def toggle_skoscollection_children_by_name(context, model, collectionname):
collection = SkosCollection.objects.get(name=collectionname)
return toggle_skoscollection_children(context, model, collection)
11 changes: 11 additions & 0 deletions apis_core/vocabs/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from django.urls import path

from . import views

urlpatterns = [
path(
"togglecollectionobject/<int:content_type_id>/<int:object_id>/<int:collection>",
views.ToggleCollectionObject.as_view(),
name="togglecollectionobject",
),
]
Empty file added apis_core/vocabs/utils.py
Empty file.
32 changes: 32 additions & 0 deletions apis_core/vocabs/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
from django.contrib.auth.mixins import LoginRequiredMixin
from django.contrib.contenttypes.models import ContentType
from django.views.generic.base import TemplateView
from django.shortcuts import redirect

from .models import SkosCollection, SkosCollectionContentObject


class ToggleCollectionObject(LoginRequiredMixin, TemplateView):
template_name = "vocabs/toggle_skoscollection.html"

def get_context_data(self, *args, **kwargs):
context = super().get_context_data(*args, **kwargs)
context["exists"] = self.created
context["collection"] = SkosCollection.objects.get(pk=kwargs["collection"])
context["content_type_id"] = kwargs["content_type_id"]
context["object_id"] = kwargs["object_id"]
return context

def get(self, *args, **kwargs):
collection = SkosCollection.objects.get(pk=kwargs["collection"])
content_type = ContentType.objects.get(pk=kwargs["content_type_id"])
scco, self.created = SkosCollectionContentObject.objects.get_or_create(
collection=collection,
content_type=content_type,
object_id=kwargs["object_id"],
)
if not self.created:
scco.delete()
if redirect_to := self.request.GET.get("to", False):
return redirect(redirect_to)
return super().get(*args, **kwargs)

0 comments on commit 2a09ed9

Please sign in to comment.