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

Updated item apply_terms to take multiple terms

parent be880f89
No related branches found
No related tags found
2 merge requests!46Tagging frontend,!45Add multiple terms api
......@@ -23,7 +23,7 @@ class Message(DataLayerModel):
terms = models.ManyToManyField(Term, related_name="items")
network_provider = models.CharField(max_length=200, blank=True)
def apply_terms(self, term):
def apply_terms(self, terms):
# TODO: test this
""" Add or replace value of term.taxonomy for current Item
......@@ -37,10 +37,16 @@ class Message(DataLayerModel):
# This should really be built out with an explicity through model
# in taxonomies, with a generic foreign ken to the content type
# being classified, then this logic could live there.
if term.taxonomy.is_optional:
self.delete_all_terms(term.taxonomy)
if isinstance(terms, Term):
terms = [terms]
self.terms.add(term)
if len(terms) == 1:
[term] = terms
if term.taxonomy.is_optional:
self.delete_all_terms(term.taxonomy)
self.terms.add(*terms)
def delete_all_terms(self, taxonomy):
for term in self.terms.filter(taxonomy=taxonomy):
......
......@@ -143,4 +143,17 @@ def test_apply_terms_adds_term_for_tags():
assert list(item.terms.all()) == [term1]
item.apply_terms(term2)
assert set(item.terms.all()) == set([term1, term2])
assert term1 in item.terms.all()
assert term2 in item.terms.all()
@pytest.mark.django_db
def test_apply_terms_adds_multiple_terms():
item = ItemFactory()
taxonomy = TaxonomyFactory(multiplicity='multiple')
term1 = TermFactory(taxonomy=taxonomy)
term2 = TermFactory(taxonomy=taxonomy)
item.apply_terms((term1, term2))
assert term1 in item.terms.all()
assert term2 in item.terms.all()
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