Skip to content
Snippets Groups Projects
Commit 5402eb41 authored by Martin Burchell's avatar Martin Burchell
Browse files

Changed by_taxonomy to create term if open

If the taxonomy has an open vocabulary, we create missing terms
rather than throw an exception
parent d55d152f
No related branches found
No related tags found
1 merge request!44Added multiplicity field to Taxonomy
...@@ -60,7 +60,7 @@ class Taxonomy(models.Model): ...@@ -60,7 +60,7 @@ class Taxonomy(models.Model):
class TermManager(models.Manager): class TermManager(models.Manager):
def by_taxonomy(self, taxonomy, name): def by_taxonomy(self, taxonomy, name):
""" Fetch an existing Term by its name and its """ Fetch a Term by its name and its
Taxonomy slug which, together should be unique together. Taxonomy slug which, together should be unique together.
args: args:
...@@ -73,21 +73,30 @@ class TermManager(models.Manager): ...@@ -73,21 +73,30 @@ class TermManager(models.Manager):
The term object with the given name in the given Taxonomy. The term object with the given name in the given Taxonomy.
throws: throws:
DoesNotExist if no Term matches the given combination DoesNotExist if Taxonomy with the given slug does not exist
DoesNotExist if named Term does not exist, unless the Taxonomy
vocabulary is open - in this case the Term will be created
ValueError if taxonomy is not one of the allowed types ValueError if taxonomy is not one of the allowed types
""" """
if isinstance(taxonomy, basestring): if isinstance(taxonomy, basestring):
taxonomy_slug = taxonomy taxonomy = Taxonomy.objects.get(slug=taxonomy)
elif isinstance(taxonomy, Taxonomy): elif not isinstance(taxonomy, Taxonomy):
taxonomy_slug = taxonomy.slug
else:
raise ValueError( raise ValueError(
"taxonomy must be a Taxonomy instance " "taxonomy must be a Taxonomy instance "
"or a valid taxonomy slug") "or a valid taxonomy slug")
return self.select_related('taxonomy').get(
taxonomy__slug=taxonomy_slug, if taxonomy.is_open:
name=name term, _ = self.select_related('taxonomy').get_or_create(
) taxonomy=taxonomy,
name=name
)
else:
term = self.select_related('taxonomy').get(
taxonomy=taxonomy,
name=name
)
return term
class Term(models.Model): class Term(models.Model):
......
...@@ -35,3 +35,29 @@ def test_term_by_taxonomy_with_taxonomies_with_taxonomy(term_with_context): ...@@ -35,3 +35,29 @@ def test_term_by_taxonomy_with_taxonomies_with_taxonomy(term_with_context):
assert term.name == term_with_context.name assert term.name == term_with_context.name
assert term.taxonomy == term_with_context.taxonomy assert term.taxonomy == term_with_context.taxonomy
@pytest.mark.django_db
def test_unknown_term_by_taxonomy_creates_term_if_open():
taxonomy = TaxonomyFactory(vocabulary='open')
term = Term.objects.by_taxonomy(
taxonomy=taxonomy,
name="a term that doesn't exist",
)
assert term.name == "a term that doesn't exist"
assert term.taxonomy == taxonomy
@pytest.mark.django_db
def test_unknown_term_by_taxonomy_throws_exception_if_not_open():
taxonomy = TaxonomyFactory(vocabulary='closed')
with pytest.raises(Term.DoesNotExist) as excinfo:
Term.objects.by_taxonomy(
taxonomy=taxonomy,
name="a term that doesn't exist",
)
assert excinfo.value.message == "Term matching query does not exist."
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment