Skip to content

Commit

Permalink
fix: migrate management commands from apis-webpage to apis-rdf-core
Browse files Browse the repository at this point in the history
This commit introduces a manangement command that was part of the
`webpage` module but is better suited to be part of `apis_core`.
  • Loading branch information
b1rger committed Oct 16, 2023
1 parent 872386e commit 09fac5e
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions apis_core/management/commands/create_relationships.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
from django.core.management.base import BaseCommand, CommandError
from apis_core.apis_relations.models import Property
from apis_ontology.models import construct_properties


class Command(BaseCommand):
help = (
"Create and label relationships between entities by running "
"the construct_properties() function in your app's models.py file."
)

def handle(self, *args, **options):
try:
construct_properties()
self.stdout.write(
self.style.SUCCESS("Successfully ran construct_properties()")
)
except Exception as e:
raise CommandError(
f"construct_properties() raised the " f"following error(s):\n{e}"
)

try:
props = Property.objects.all()
except Property.DoesNotExist:
raise CommandError("No entity relationships exist.")

if len(props) > 0:
# TODO would ideally verify existence/validity of relevant fields
# as relationship names alone are useless w/o subj/obj references;
# below msg. purposefully only mentions "labels".
self.stdout.write("The following labels now exist:")
for p in props:
self.stdout.write(self.style.SUCCESS(f"{p.name}"), ending="")
self.stdout.write(f" ... {p.name_reverse}")
else:
# construct_properties() starts out by deleting all existing
# Property objects
self.stdout.write(
self.style.ERROR("No entity relationships found nor created.")
)

0 comments on commit 09fac5e

Please sign in to comment.