Skip to content

Commit

Permalink
Merge branch 'feature-1698-tag-taxonomies'
Browse files Browse the repository at this point in the history
  • Loading branch information
Sean Hammond committed Feb 21, 2012
2 parents 277dc15 + abb7317 commit c5a90d8
Show file tree
Hide file tree
Showing 11 changed files with 355 additions and 36 deletions.
3 changes: 3 additions & 0 deletions ckan/lib/dictization/model_dictize.py
Expand Up @@ -462,6 +462,9 @@ def package_to_api2(pkg, context):

def vocabulary_dictize(vocabulary, context):
vocabulary_dict = table_dictize(vocabulary, context)
assert not vocabulary_dict.has_key('tags')
vocabulary_dict['tags'] = [tag_dictize(tag, context) for tag
in vocabulary.tags]
return vocabulary_dict

def vocabulary_list_dictize(vocabulary_list, context):
Expand Down
29 changes: 28 additions & 1 deletion ckan/lib/dictization/model_save.py
Expand Up @@ -478,6 +478,23 @@ def activity_dict_save(activity_dict, context):

return activity_obj

def vocabulary_tag_list_save(new_tag_dicts, vocabulary_obj, context):
model = context['model']
session = context['session']

# First delete any tags not in new_tag_dicts.
for tag in vocabulary_obj.tags:
if tag.name not in [tag['name'] for tag in new_tag_dicts]:
tag.delete()
# Now add any new tags.
for tag_dict in new_tag_dicts:
current_tag_names = [tag.name for tag in vocabulary_obj.tags]
if tag_dict['name'] not in current_tag_names:
# Make sure the tag belongs to this vocab..
tag_dict['vocabulary_id'] = vocabulary_obj.id
# then add it.
tag_dict_save(tag_dict, {'model': model, 'session': session})

def vocabulary_dict_save(vocabulary_dict, context):
model = context['model']
session = context['session']
Expand All @@ -486,6 +503,10 @@ def vocabulary_dict_save(vocabulary_dict, context):
vocabulary_obj = model.Vocabulary(vocabulary_name)
session.add(vocabulary_obj)

if vocabulary_dict.has_key('tags'):
vocabulary_tag_list_save(vocabulary_dict['tags'], vocabulary_obj,
context)

return vocabulary_obj

def vocabulary_dict_update(vocabulary_dict, context):
Expand All @@ -494,7 +515,13 @@ def vocabulary_dict_update(vocabulary_dict, context):
session = context['session']

vocabulary_obj = model.vocabulary.Vocabulary.get(vocabulary_dict['id'])
vocabulary_obj.name = vocabulary_dict['name']

if vocabulary_dict.has_key('name'):
vocabulary_obj.name = vocabulary_dict['name']

if vocabulary_dict.has_key('tags'):
vocabulary_tag_list_save(vocabulary_dict['tags'], vocabulary_obj,
context)

return vocabulary_obj

Expand Down
17 changes: 11 additions & 6 deletions ckan/lib/navl/dictization_functions.py
Expand Up @@ -358,24 +358,29 @@ def unflatten(data):
'state': u'active',
'save': u'Save Changes',
'cancel': u'Cancel'}
'''

unflattened = {}
convert_to_list = []

for flattend_key in sorted(data.keys(), key=flattened_order_key):
current_pos = unflattened

if (len(flattend_key) > 1
and not flattend_key[0] in convert_to_list
and not flattend_key[0] in unflattened):
convert_to_list.append(flattend_key[0])

for key in flattend_key[:-1]:
try:
current_pos = current_pos[key]
except KeyError:
new_pos = []
current_pos[key] = new_pos
current_pos = new_pos
except IndexError:
new_pos = {}
current_pos.append(new_pos)
current_pos[key] = new_pos
current_pos = new_pos
current_pos[flattend_key[-1]] = data[flattend_key]

for key in convert_to_list:
unflattened[key] = [unflattened[key][s] for s in sorted(unflattened[key])]

return unflattened
3 changes: 1 addition & 2 deletions ckan/logic/action/create.py
Expand Up @@ -348,7 +348,6 @@ def group_create_rest(context, data_dict):
def vocabulary_create(context, data_dict):

model = context['model']
user = context['user']
schema = context.get('schema') or default_create_vocabulary_schema()

model.Session.remove()
Expand All @@ -368,7 +367,7 @@ def vocabulary_create(context, data_dict):
model.repo.commit()

log.debug('Created Vocabulary %s' % str(vocabulary.name))

return vocabulary_dictize(vocabulary, context)

def activity_create(context, activity_dict, ignore_auth=False):
Expand Down
5 changes: 5 additions & 0 deletions ckan/logic/action/update.py
Expand Up @@ -624,6 +624,11 @@ def vocabulary_update(context, data_dict):
if vocab is None:
raise NotFound(_('Could not find vocabulary "%s"') % vocab_id)

data_dict['id'] = vocab.id
if data_dict.has_key('name'):
if data_dict['name'] == vocab.name:
del data_dict['name']

check_access('vocabulary_update', context, data_dict)

schema = context.get('schema') or default_update_vocabulary_schema()
Expand Down
10 changes: 4 additions & 6 deletions ckan/logic/converters.py
Expand Up @@ -34,13 +34,11 @@ def date_to_form(value, context):

def free_tags_only(key, data, errors, context):
tag_number = key[1]
to_delete = []
if data.get(('tags', tag_number, 'vocabulary_id')):
to_delete.append(tag_number)
if not data.get(('tags', tag_number, 'vocabulary_id')):
return
for k in data.keys():
for n in to_delete:
if k[0] == 'tags' and k[1] == n:
del data[k]
if k[0] == 'tags' and k[1] == tag_number:
del data[k]

def convert_to_tags(vocab):
def callable(key, data, errors, context):
Expand Down
6 changes: 4 additions & 2 deletions ckan/logic/schema.py
Expand Up @@ -318,8 +318,9 @@ def default_task_status_schema():

def default_vocabulary_schema():
schema = {
'id': [ignore_empty, ignore_missing, unicode],
'id': [ignore_missing, unicode, vocabulary_id_exists],
'name': [not_empty, unicode, vocabulary_name_validator],
'tags': default_tags_schema(),
}
return schema

Expand All @@ -330,7 +331,8 @@ def default_create_vocabulary_schema():

def default_update_vocabulary_schema():
schema = default_vocabulary_schema()
schema['id'] = schema['id'] + [vocabulary_id_not_changed]
schema['id'] = [ignore_missing, vocabulary_id_not_changed]
schema['name'] = [ignore_missing, vocabulary_name_validator]
return schema

def default_create_activity_schema():
Expand Down
2 changes: 1 addition & 1 deletion ckan/model/tag.py
Expand Up @@ -260,7 +260,7 @@ def related_packages(self):
'package_tags': relation(PackageTag, backref='tag',
cascade='all, delete, delete-orphan',
),
'vocabulary': relation(vocabulary.Vocabulary, backref='tags',
'vocabulary': relation(vocabulary.Vocabulary,
order_by=tag_table.c.name)
},
order_by=tag_table.c.name,
Expand Down
5 changes: 5 additions & 0 deletions ckan/model/vocabulary.py
@@ -1,6 +1,7 @@
from meta import Table, types, Session
from core import metadata, Column, DomainObject, mapper
from types import make_uuid
import tag

VOCABULARY_NAME_MIN_LENGTH = 2
VOCABULARY_NAME_MAX_LENGTH = 100
Expand Down Expand Up @@ -30,4 +31,8 @@ def get(cls, id_or_name):
vocab = Vocabulary.by_name(id_or_name)
return vocab

@property
def tags(self):
return Session.query(tag.Tag).filter(tag.Tag.vocabulary_id==self.id)

mapper(Vocabulary, vocabulary_table)

0 comments on commit c5a90d8

Please sign in to comment.