set field rdf_terms on models instead of fields
@@ -95,14 +95,16 @@ class RDF_Term_MixIn(object):
def rdf_triples(self, subject, graph):
"""Default implementation that parses by convention."""
- term = self.rdf_term if self.rdf_term else getattr(HSTERMS, self.__class__.__name__)
+ term = self.rdf_term if hasattr(self, 'rdf_term') else getattr(HSTERMS, self.__class__.__name__)
metadata_node = BNode()
graph.add((subject, term, metadata_node))
for field in self.__class__._meta.fields:
if self.ignored_fields and field.name in self.ignored_fields:
continue
- field_term = getattr(field, 'rdf_term')
- if not field_term:
+ field_term_attr = field.name + '_rdf_term'
+ if hasattr(self, field_term_attr):
+ field_term = getattr(self, field_term_attr)
+ else:
field_term = getattr(HSTERMS, field.name)
field_value = getattr(self, field.name)
# urls should be a URIRef term, all others should be a Literal term
@@ -115,7 +117,7 @@ class RDF_Term_MixIn(object):
@classmethod
def ingest_rdf(cls, graph, content_object):
"""Default implementation that ingests by convention"""
- term = cls.rdf_term if cls.rdf_term else getattr(HSTERMS, cls.__name__)
+ term = cls.rdf_term if hasattr(cls, 'rdf_term') else getattr(HSTERMS, cls.__name__)
value_dict = {}
subject = content_object.rdf_subject()
metadata_nodes = graph.objects(subject=subject, predicate=term)
@@ -123,8 +125,10 @@ class RDF_Term_MixIn(object):
for field in cls._meta.fields:
if cls.ignored_fields and field.name in cls.ignored_fields:
continue
- field_term = getattr(field, 'rdf_term')
- if not field_term:
+ field_term_attr = field.name + '_rdf_term'
+ if hasattr(cls, field_term_attr):
+ field_term = getattr(cls, field_term_attr)
+ else:
field_term = getattr(HSTERMS, field.name)
val = graph.value(metadata_node, field_term)
value_dict[field.name] = val.value if isinstance(val, Literal) else str(val)
@@ -136,9 +140,8 @@ def rdf_terms(class_term, **field_terms):
def decorator(obj):
obj.rdf_term = class_term
for k, v in field_terms.items():
- field = getattr(obj, k)
- if not field:
+ if not hasattr(obj, k):
raise Exception("field {} not found".format(k))
- field.rdf_term = v
+ setattr(obj, k + '_rdf_term', v)
return obj
return decorator
