abstracted all of this code into it's own extension from ckanext-ngds/metadata