about summary refs log tree commit diff
diff options
context:
space:
mode:
authorAdam Novak2020-04-17 11:27:26 -0700
committerAdam Novak2020-04-17 11:27:34 -0700
commite823bf1b464fcb8f52a91dcaebaf5b82cafc06dc (patch)
treee611842328bfba7a3d9d1cb6ed5dc000755c52f9
parent23526cb2215d3f6554ab4227c5931211153ff4e4 (diff)
downloadbh20-seq-resource-e823bf1b464fcb8f52a91dcaebaf5b82cafc06dc.tar.gz
bh20-seq-resource-e823bf1b464fcb8f52a91dcaebaf5b82cafc06dc.tar.lz
bh20-seq-resource-e823bf1b464fcb8f52a91dcaebaf5b82cafc06dc.zip
Don't use @id as a URL if no URL is found
-rw-r--r--bh20simplewebuploader/main.py34
1 files changed, 30 insertions, 4 deletions
diff --git a/bh20simplewebuploader/main.py b/bh20simplewebuploader/main.py
index 13c3fef..0b1616b 100644
--- a/bh20simplewebuploader/main.py
+++ b/bh20simplewebuploader/main.py
@@ -49,6 +49,17 @@ def name_to_label(field_name):
 
     return string.capwords(field_name.replace('_', ' '))
 
+def is_url(string):
+    """
+    Return True if the given string looks like a URL, and False otherwise.
+
+    Used for finding type URLs in the schema.
+
+    Right now only supports http(s) URLs because that's all we have in our schema.
+    """
+
+    return string.startswith('http')
+
 def generate_form(schema):
     """
     Linearize the schema and send a bunch of dicts.
@@ -93,13 +104,28 @@ def generate_form(schema):
             ref_url = None
             if not isinstance(field_type, str):
                 # If the type isn't a string
+
                 # See if it has a more info/what goes here URL
                 predicate = field_type.get('jsonldPredicate', {})
-                if not isinstance(predicate, str):
-                    ref_url = predicate.get('_id', None)
+                # Predicate may be a URL, a dict with a URL in _id, maybe a
+                # dict with a URL in _type, or a dict with _id and _type but no
+                # URLs anywhere. Some of these may not technically be allowed
+                # by the format, but if they occur, we might as well try to
+                # handle them.
+                if isinstance(predicate, str):
+                    if is_url(predicate):
+                        ref_url = predicate
                 else:
-                    ref_url = predicate # not sure this is correct
-                # Grab out its type field
+                    # Assume it's a dict. Look at the fields we know about.
+                    for field in ['_id', 'type']:
+                        field_value = predicate.get(field, None)
+                        if isinstance(field_value, str) and is_url(field_value) and ref_url is None:
+                            # Take the first URL-looking thing we find
+                            ref_url = field_value
+                            break
+
+
+                # Now overwrite the field type with the actual type string
                 field_type = field_type.get('type', '')
 
             # Decide if the field is optional (type ends in ?)