Skip to content

Commit

Permalink
[#1765] Fix Tag.get() by ID
Browse files Browse the repository at this point in the history
Tag IDs are still unqiue even across vocabularies, so when getting a tag
object by ID there's no need to consider what vocab the tag belongs to.
  • Loading branch information
Sean Hammond committed Feb 8, 2012
1 parent 8853b04 commit a680cd6
Showing 1 changed file with 15 additions and 22 deletions.
37 changes: 15 additions & 22 deletions ckan/model/tag.py
Expand Up @@ -45,26 +45,15 @@ def delete(self):
self.purge()

@classmethod
def by_id(cls, tag_id, autoflush=True, vocab=None):
def by_id(cls, tag_id, autoflush=True):
"""Return the tag object with the given id, or None if there is no
tag with that id.
By default only free tags (tags which do not belong to any vocabulary)
are returned. If the optional argument vocab is given then only tags
from that vocabulary are returned, or None if there is no tag with that
id in that vocabulary.
Arguments:
tag_id -- The id of the tag to return.
vocab -- A Vocabulary object for the vocabulary to look in (optional).
"""
if vocab:
query = Session.query(Tag).filter(Tag.id==tag_id).filter(
Tag.vocabulary_id==vocab.id)
else:
query = Session.query(Tag).filter(Tag.id==tag_id).filter(
Tag.vocabulary_id==None)
query = Session.query(Tag).filter(Tag.id==tag_id)
query = query.autoflush(autoflush)
tag = query.first()
return tag
Expand Down Expand Up @@ -110,17 +99,21 @@ def get(cls, tag_id_or_name, vocab_id_or_name=None):
vocab_id_or_name -- The id or name of the vocabulary to look in.
"""
if vocab_id_or_name:
vocab = vocabulary.Vocabulary.get(vocab_id_or_name)
if vocab is None:
# The user specified an invalid vocab.
return None
# First try to get the tag by ID.
tag = Tag.by_id(tag_id_or_name)
if tag:
return tag
else:
vocab = None
tag = Tag.by_id(tag_id_or_name, vocab=vocab)
if not tag:
# If that didn't work, try to get the tag by name and vocabulary.
if vocab_id_or_name:
vocab = vocabulary.Vocabulary.get(vocab_id_or_name)
if vocab is None:
# The user specified an invalid vocab.
return None
else:
vocab = None
tag = Tag.by_name(tag_id_or_name, vocab=vocab)
return tag
return tag
# Todo: Make sure tag names can't be changed to look like tag IDs?

@classmethod
Expand Down

0 comments on commit a680cd6

Please sign in to comment.