Advertisement
Guest User

CKAN Extension of custom tags using a vocabulary

a guest
Apr 5th, 2014
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.41 KB | None | 0 0
  1. # This Helper function creates the region vocabulary from a text file
  2. def createRegionsVocab():
  3.     user = toolkit.get_action('get_site_user')({'ignore_auth': True}, {})
  4.     context = {'user': user['name']}
  5.  
  6.     try:
  7.         data = {'id': 'ILRI_prjregions'}
  8.         toolkit.get_action('vocabulary_show')(context, data)
  9.         print "ILRI_prjregions vocabulary already exists, skipping."
  10.     except toolkit.ObjectNotFound:
  11.         print "Creating vocab 'ILRI_prjregions'"
  12.         data = {'name': 'ILRI_prjregions'}
  13.         vocab = toolkit.get_action('vocabulary_create')(context, data)
  14.         for tag in getArrayFromFile("/opt/ckan/lib/default/src/ckanext-ilriapi/ckanext/sourcetxt/ilri-regions.txt"):
  15.             logging.info("Adding tag {0} to vocab 'ILRI_prjregions'".format(tag))
  16.             data = {'name': tag, 'vocabulary_id': vocab['id']}
  17.             toolkit.get_action('tag_create')(context, data)
  18.     return True
  19.  
  20. #This is almost the same as CKAN but instead of passing key we pass the new tag
  21. def convertToTags(vocab,newtag,data,error,context):
  22.  
  23.     new_tags = newtag
  24.  
  25.     if not new_tags:
  26.         return
  27.     if isinstance(new_tags, basestring):
  28.         new_tags = [new_tags]
  29.  
  30.     # get current number of tags
  31.     n = 0
  32.     for k in data.keys():
  33.         if k[0] == 'tags':
  34.             n = max(n, k[1] + 1)
  35.  
  36.     v = model.Vocabulary.get(vocab)
  37.     if not v:
  38.         raise df.Invalid(_('Tag vocabulary "%s" does not exist') % vocab)
  39.     context['vocabulary'] = v
  40.  
  41.     for tag in new_tags:
  42.         validators.tag_in_vocabulary_validator(tag, context)
  43.  
  44.     for num, tag in enumerate(new_tags):
  45.         data[('tags', num + n, 'name')] = tag
  46.         data[('tags', num + n, 'vocabulary_id')] = v.id
  47.  
  48. #Separate a string of tags into individual tags using coma and then adds each tag using convertToTags
  49. #Used in datasets to store Countries, Regions and Species as vocabulary tags
  50. def comasToTags(key,data,error,context):
  51.     #print key[0]
  52.     tag_string = data.get(key)
  53.     #print tag_string
  54.     tags = tag_string.split(",")
  55.     vocab = "None"
  56.     if key[0] == "ILRI_prjregions":
  57.         vocab = "ILRI_prjregions"
  58.  
  59.     if key[0] == "ILRI_prjcountries":
  60.         vocab = "ILRI_prjcountries"
  61.  
  62.     if key[0] == "ILRI_prjspecies":
  63.         vocab = "ILRI_prjspecies"
  64.  
  65.     if vocab == "None":
  66.         raise df.Invalid(_('Tag vocabulary for key "%s" does not exist') % key[0])
  67.  
  68.     for tag in tags:
  69.         convertToTags(vocab,tag,data,error,context)
  70.  
  71. # Combines a series of tags into a string of tags separated by separator
  72. # Used in dataset to show the tags with the proper javascript
  73. def tagsToString(tags,separator):
  74.     tag_string = ""
  75.     for tag in tags:
  76.         tag_string = tag_string + tag + separator
  77.     pprint.pprint(tag_string)
  78.     tag_string = tag_string[:len(tag_string)-1]
  79.     pprint.pprint(tag_string)
  80.  
  81.     return tag_string
  82.  
  83.  
  84. def _add_custom_metadata_to_schema(self, schema):
  85.  
  86.         #Project Level metadata        
  87.         schema.update({'ILRI_prjregions': [comasToTags]}) #Here is the custom handling of tags
  88.  
  89.  
  90. def show_package_schema(self):
  91.         schema = super(ILRIMetadataPlugin, self).show_package_schema()
  92.  
  93.         schema['tags']['__extras'].append(toolkit.get_converter('free_tags_only'))
  94.  
  95.         #Project Level metadata
  96.         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