Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # This Helper function creates the region vocabulary from a text file
- def createRegionsVocab():
- user = toolkit.get_action('get_site_user')({'ignore_auth': True}, {})
- context = {'user': user['name']}
- try:
- data = {'id': 'ILRI_prjregions'}
- toolkit.get_action('vocabulary_show')(context, data)
- print "ILRI_prjregions vocabulary already exists, skipping."
- except toolkit.ObjectNotFound:
- print "Creating vocab 'ILRI_prjregions'"
- data = {'name': 'ILRI_prjregions'}
- vocab = toolkit.get_action('vocabulary_create')(context, data)
- for tag in getArrayFromFile("/opt/ckan/lib/default/src/ckanext-ilriapi/ckanext/sourcetxt/ilri-regions.txt"):
- logging.info("Adding tag {0} to vocab 'ILRI_prjregions'".format(tag))
- data = {'name': tag, 'vocabulary_id': vocab['id']}
- toolkit.get_action('tag_create')(context, data)
- return True
- #This is almost the same as CKAN but instead of passing key we pass the new tag
- def convertToTags(vocab,newtag,data,error,context):
- new_tags = newtag
- if not new_tags:
- return
- if isinstance(new_tags, basestring):
- new_tags = [new_tags]
- # get current number of tags
- n = 0
- for k in data.keys():
- if k[0] == 'tags':
- n = max(n, k[1] + 1)
- v = model.Vocabulary.get(vocab)
- if not v:
- raise df.Invalid(_('Tag vocabulary "%s" does not exist') % vocab)
- context['vocabulary'] = v
- for tag in new_tags:
- validators.tag_in_vocabulary_validator(tag, context)
- for num, tag in enumerate(new_tags):
- data[('tags', num + n, 'name')] = tag
- data[('tags', num + n, 'vocabulary_id')] = v.id
- #Separate a string of tags into individual tags using coma and then adds each tag using convertToTags
- #Used in datasets to store Countries, Regions and Species as vocabulary tags
- def comasToTags(key,data,error,context):
- #print key[0]
- tag_string = data.get(key)
- #print tag_string
- tags = tag_string.split(",")
- vocab = "None"
- if key[0] == "ILRI_prjregions":
- vocab = "ILRI_prjregions"
- if key[0] == "ILRI_prjcountries":
- vocab = "ILRI_prjcountries"
- if key[0] == "ILRI_prjspecies":
- vocab = "ILRI_prjspecies"
- if vocab == "None":
- raise df.Invalid(_('Tag vocabulary for key "%s" does not exist') % key[0])
- for tag in tags:
- convertToTags(vocab,tag,data,error,context)
- # Combines a series of tags into a string of tags separated by separator
- # Used in dataset to show the tags with the proper javascript
- def tagsToString(tags,separator):
- tag_string = ""
- for tag in tags:
- tag_string = tag_string + tag + separator
- pprint.pprint(tag_string)
- tag_string = tag_string[:len(tag_string)-1]
- pprint.pprint(tag_string)
- return tag_string
- def _add_custom_metadata_to_schema(self, schema):
- #Project Level metadata
- schema.update({'ILRI_prjregions': [comasToTags]}) #Here is the custom handling of tags
- def show_package_schema(self):
- schema = super(ILRIMetadataPlugin, self).show_package_schema()
- schema['tags']['__extras'].append(toolkit.get_converter('free_tags_only'))
- #Project Level metadata
- schema.update({'ILRI_prjregions': [toolkit.get_converter('convert_from_tags')("ILRI_prjregions"),toolkit.get_validator('ignore_missing')]})
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement