implement rdf_terms for modelprogram
@@ -5,6 +5,7 @@ from django.db import models, transaction
from django.core.exceptions import ValidationError
from mezzanine.pages.page_processors import processor_for
+from rdflib import Literal, URIRef
from hs_core.hs_rdf import rdf_terms
from hs_core.models import BaseResource, ResourceManager, resource_processor, CoreMetaData, \
@@ -82,6 +83,45 @@ class MpMetadata(AbstractMetaDataElement):
def get_engine_list(self):
return self.modelEngine.split(';')
+ def rdf_triples(self, subject, graph):
+ for field in self._meta.fields:
+ if self.ignored_fields and field.name in self.ignored_fields:
+ continue
+ if field.name in ['modelEngine', 'modelReleaseNotes', 'modelDocumentation', 'modelSoftware']:
+ field_term = self.get_field_term(field.name)
+ field_value = getattr(self, field.name)
+ for f in field_value.split(';'):
+ graph.add((subject, field_term, Literal('/data/contents/' + f)))
+ else:
+ field_term = self.get_field_term(field.name)
+ field_value = getattr(self, field.name)
+ # urls should be a URIRef term, all others should be a Literal term
+ if field_value and field_value != 'None':
+ if isinstance(field_value, str) and field_value.startswith('http'):
+ field_value = URIRef(field_value)
+ else:
+ field_value = Literal(field_value)
+ graph.add((subject, field_term, field_value))
+
+ @classmethod
+ def ingest_rdf(cls, graph, subject, content_object):
+ for field in cls._meta.fields:
+ value_dict = {}
+ if cls.ignored_fields and field.name in cls.ignored_fields:
+ continue
+ if field.name in ['modelEngine', 'modelReleaseNotes', 'modelDocumentation', 'modelSoftware']:
+ values = []
+ for o in graph.objects(subject=subject, predicate=field_term):
+ values.append(o.lstrip('/data/contents/'))
+ value_dict[field.name] = ';'.join(values)
+ else:
+ field_term = cls.get_field_term(field.name)
+ val = graph.value(subject, field_term)
+ if val:
+ value_dict[field.name] = str(val)
+ if value_dict:
+ cls.create(content_object=content_object, **value_dict)
+
class ModelProgramResource(BaseResource):
objects = ResourceManager("ModelProgramResource")
