In the middle of some major changes
authortilly-Q <nattilypigeonfowl@gmail.com>
Mon, 21 Apr 2014 23:07:28 +0000 (19:07 -0400)
committertilly-Q <nattilypigeonfowl@gmail.com>
Mon, 21 Apr 2014 23:07:28 +0000 (19:07 -0400)
mediagoblin/gmg_commands/batchaddmedia.py
mediagoblin/templates/mediagoblin/utils/metadata_table.html

index 61a068d21fd9e870d37bdb081cba7775013a0452..43c24f6d85053d3da92c9793d568acd23dec5608 100644 (file)
@@ -132,7 +132,8 @@ zip files and directories"
         contents = all_metadata.read()
         media_metadata = parse_csv_file(contents)
 
-    dcterms_context = { 'dcterms':'http://purl.org/dc/terms/' }
+    metadata_context = { 'dcterms':'http://purl.org/dc/terms/',
+                         'xsd': 'http://www.w3.org/2001/XMLSchema#'}
 
     for media_id in media_locations.keys():
         files_attempted += 1
@@ -141,13 +142,14 @@ zip files and directories"
         sanitized_metadata = check_metadata_format(file_metadata)
         if sanitized_metadata == {}: continue
 
-        json_ld_metadata = jsonld.compact(file_metadata, dcterms_context)
+        json_ld_metadata = jsonld.compact(build_json_ld_metadata(file_metadata), 
+                                            metadata_context)
         original_location = media_locations[media_id]['media:original']
         url = urlparse(original_location)
 
-        title = file_metadata.get('dcterms:title')
-        description = file_metadata.get('dcterms:description')
-        license = file_metadata.get('dcterms:rights')
+        title = sanitized_metadata.get('dcterms:title')
+        description = sanitized_metadata.get('dcterms:description')
+        license = sanitized_metadata.get('dcterms:rights')
         filename = url.path.split()[-1]
 
         if url.scheme == 'http':
@@ -218,6 +220,19 @@ def teardown(temp_files):
     for temp_file in temp_files:
         subprocess.call(['rm','-r',temp_file])
 
+def build_json_ld_metadata(metadata_dict):
+    output_dict = {}
+    for p in metadata_dict.keys():
+        if p in ["dcterms:rights", "dcterms:relation"]:
+            m_type = "xsd:uri"
+        elif p in ["dcterms:date", "dcterms:created"]:
+            m_type = "xsd:date"
+        else:
+            m_type = "xsd:string"
+        description = {"@value": metadata_dict[p],
+                       "@type" : m_type}
+        output_dict[p] = description
+    return output_dict
 
 def check_metadata_format(metadata_dict):
     schema = {
@@ -250,6 +265,7 @@ def check_metadata_format(metadata_dict):
     try:
         validate(metadata_dict, schema)
         output_dict = metadata_dict
+        # "media:id" is only for internal use, so we delete it for the output
         del output_dict['media:id']
 
     except ValidationError, exc:
index 166d57161e37e1c634ae1ba7c034e15092179ba4..2eb57af39d14289fefe441030ea86ff1cdd47c77 100644 (file)
   {%- set metadata=media_entry.media_metadata %}
   {%- set metadata_context=metadata['@context'] %}
   {%- if metadata %}
-      <table prefix="{%- for prefix in metadata_context -%}
-                     {{ prefix }} {{ metadata_context[prefix] }}
-                     {%- endfor %}">
-        {%- for key, value in metadata.iteritems() if not key=='@context' %}
-          {% if value -%}
+      <table prefix="{% for p in metadata_context %}{% if not loop.first %}
+                    {% endif %}{{ p }} {{ metadata_context[p] }}{% endfor -%}">
+        {%- for key, value_dict in metadata.iteritems() if not key=='@context' %}
+          {% if value_dict['@value'] -%}
             <tr>
               <td>{{ format_predicate(key) }}</td>
-              <td property="{{ key }}">{{ value }}</td>
+              <td property="{{ key }}" typeof="{{ value_dict['@type'] }}">
+                {{ value_dict['@value'] }}</td>
             </tr>
           {%- endif -%}
         {%- endfor %}