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

Refactored transport layer code for adding terms

add_terms() replaces add_term() and add_free_terms()
parent ab67b3f3
No related branches found
No related tags found
1 merge request!46Tagging frontend
Showing with 41 additions and 73 deletions
...@@ -120,7 +120,7 @@ class Importer(object): ...@@ -120,7 +120,7 @@ class Importer(object):
def save_rows(self, objects, item_type): def save_rows(self, objects, item_type):
for obj in objects: for obj in objects:
item = transport.items.create(obj) item = transport.items.create(obj)
transport.items.add_term(item['id'], 'item-types', item_type) transport.items.add_terms(item['id'], 'item-types', item_type)
return len(objects) return len(objects)
......
...@@ -389,7 +389,7 @@ def _add_items_categories(request, items): ...@@ -389,7 +389,7 @@ def _add_items_categories(request, items):
for item_id, taxonomy_slug, term_name in items: for item_id, taxonomy_slug, term_name in items:
try: try:
if term_name: if term_name:
transport.items.add_term( transport.items.add_terms(
item_id, item_id,
taxonomy_slug, taxonomy_slug,
term_name term_name
......
...@@ -607,8 +607,8 @@ def test_item_category_can_be_updated(view, form): ...@@ -607,8 +607,8 @@ def test_item_category_can_be_updated(view, form):
@pytest.mark.django_db @pytest.mark.django_db
def test_item_category_can_be_unset(view, form): def test_item_category_can_be_unset(view, form):
transport.items.add_term(view.item['id'], 'ebola-questions', transport.items.add_terms(view.item['id'], 'ebola-questions',
'Ebola origins') 'Ebola origins')
form.cleaned_data['category'] = '' form.cleaned_data['category'] = ''
...@@ -744,7 +744,7 @@ def test_free_tags_created_on_item_update(view, form): ...@@ -744,7 +744,7 @@ def test_free_tags_created_on_item_update(view, form):
@pytest.mark.django_db @pytest.mark.django_db
def test_existing_tag_deleted_on_item_update(view, form): def test_existing_tag_deleted_on_item_update(view, form):
transport.items.add_free_terms(view.item['id'], 'tags', ['age 35-40']) transport.items.add_terms(view.item['id'], 'tags', ['age 35-40'])
form.cleaned_data['tags'] = 'Monrovia' form.cleaned_data['tags'] = 'Monrovia'
......
...@@ -90,7 +90,7 @@ def test_add_items_categories_adds_term_to_items(terms, items): ...@@ -90,7 +90,7 @@ def test_add_items_categories_adds_term_to_items(terms, items):
@pytest.mark.django_db @pytest.mark.django_db
def test_add_items_categories_removes_term_from_item(term, item): def test_add_items_categories_removes_term_from_item(term, item):
transport.items.add_term(item['id'], term.taxonomy.slug, term.name) transport.items.add_terms(item['id'], term.taxonomy.slug, term.name)
category_list = [(item['id'], term.taxonomy.slug, ''), ] category_list = [(item['id'], term.taxonomy.slug, ''), ]
......
...@@ -117,11 +117,11 @@ def test_process_items_removes_question_type(): ...@@ -117,11 +117,11 @@ def test_process_items_removes_question_type():
taxonomy = TaxonomyFactory(name="Ebola Questions") taxonomy = TaxonomyFactory(name="Ebola Questions")
term_to_delete = TermFactory(name='term to be deleted', term_to_delete = TermFactory(name='term to be deleted',
taxonomy=taxonomy) taxonomy=taxonomy)
transport.items.add_term( transport.items.add_terms(
item['id'], term_to_delete.taxonomy.slug, term_to_delete.name) item['id'], term_to_delete.taxonomy.slug, term_to_delete.name)
term_to_keep = TermFactory(name='term not to be deleted') term_to_keep = TermFactory(name='term not to be deleted')
transport.items.add_term( transport.items.add_terms(
item['id'], term_to_keep.taxonomy.slug, term_to_keep.name) item['id'], term_to_keep.taxonomy.slug, term_to_keep.name)
url = reverse('data-view-process') url = reverse('data-view-process')
......
...@@ -259,7 +259,7 @@ class AddEditItemView(FormView): ...@@ -259,7 +259,7 @@ class AddEditItemView(FormView):
transport.items.delete_all_terms(item_id, taxonomy) transport.items.delete_all_terms(item_id, taxonomy)
term_names = [t.strip() for t in value.split(self.tag_delimiter)] term_names = [t.strip() for t in value.split(self.tag_delimiter)]
transport.items.add_free_terms(item_id, taxonomy, term_names) transport.items.add_terms(item_id, taxonomy, term_names)
def _update_item(self, item_id, form, taxonomy): def _update_item(self, item_id, form, taxonomy):
""" Update the given item """ Update the given item
...@@ -282,7 +282,7 @@ class AddEditItemView(FormView): ...@@ -282,7 +282,7 @@ class AddEditItemView(FormView):
# TODO: Combine terms into single transaction # TODO: Combine terms into single transaction
if taxonomy: if taxonomy:
if category: if category:
transport.items.add_term(item_id, taxonomy, category) transport.items.add_terms(item_id, taxonomy, category)
else: else:
transport.items.delete_all_terms(item_id, taxonomy) transport.items.delete_all_terms(item_id, taxonomy)
...@@ -309,11 +309,11 @@ class AddEditItemView(FormView): ...@@ -309,11 +309,11 @@ class AddEditItemView(FormView):
created_item = transport.items.create(regular_fields) created_item = transport.items.create(regular_fields)
# TODO: Combine terms into single transaction # TODO: Combine terms into single transaction
transport.items.add_term( transport.items.add_terms(
created_item['id'], 'item-types', self.item_type['name'] created_item['id'], 'item-types', self.item_type['name']
) )
if taxonomy and category: if taxonomy and category:
transport.items.add_term(created_item['id'], taxonomy, category) transport.items.add_terms(created_item['id'], taxonomy, category)
self._add_free_terms(created_item['id'], free_terms) self._add_free_terms(created_item['id'], free_terms)
......
...@@ -94,40 +94,8 @@ def bulk_delete(ids): ...@@ -94,40 +94,8 @@ def bulk_delete(ids):
delete(id) delete(id)
def add_term(item_id, taxonomy_slug, name): def add_terms(item_id, taxonomy_slug, names):
""" Add term named `name` within the Taxonomy with `taxonomy_slug` to the """ Add named terms `names` within the Taxonomy with `taxonomy_slug` to the
Item with id `item_id`
args:
item_id: e.g. 67
taxonomy_slug: e.g. 'ebola-questions'
name: name of a Term in the Taxonomy with given slug
returns:
response from the server
raises:
TransportException on failure
For the moment both the taxonomy and term must already exist.
"""
view = get_view({'post': 'add_terms'})
term = {'taxonomy': taxonomy_slug, 'name': name}
request = request_factory.post("", term)
response = view(request, item_pk=item_id)
if status.is_success(response.status_code):
return response.data
else:
response.data['status_code'] = response.status_code
response.data['term'] = term
response.data['item_id'] = item_id
raise TransportException(response.data)
def add_free_terms(item_id, taxonomy_slug, names):
""" Add term named `name` within the Taxonomy with `taxonomy_slug` to the
Item with id `item_id` Item with id `item_id`
args: args:
...@@ -141,8 +109,8 @@ def add_free_terms(item_id, taxonomy_slug, names): ...@@ -141,8 +109,8 @@ def add_free_terms(item_id, taxonomy_slug, names):
raises: raises:
TransportException on failure TransportException on failure
The taxonomy must already exist. Any terms that do not exist will The taxonomy must already exist. If the taxonomy is open, any terms that
be created do not exist will be created, otherwise an exception will be raised.
""" """
view = get_view({'post': 'add_terms'}) view = get_view({'post': 'add_terms'})
......
...@@ -19,7 +19,7 @@ def test_multiple_new_terms_applied_to_item(taxonomy): ...@@ -19,7 +19,7 @@ def test_multiple_new_terms_applied_to_item(taxonomy):
term_names = ['Monrovia', 'age 40-45', 'pertinent'] term_names = ['Monrovia', 'age 40-45', 'pertinent']
item = items.add_free_terms( item = items.add_terms(
item['id'], taxonomy.slug, term_names) item['id'], taxonomy.slug, term_names)
stored_names = [t['name'] for t in item['terms']] stored_names = [t['name'] for t in item['terms']]
...@@ -28,12 +28,12 @@ def test_multiple_new_terms_applied_to_item(taxonomy): ...@@ -28,12 +28,12 @@ def test_multiple_new_terms_applied_to_item(taxonomy):
@pytest.mark.django_db @pytest.mark.django_db
def test_add_free_terms_raises_transport_exception_if_item_absent(taxonomy): def test_add_terms_raises_transport_exception_if_item_absent(taxonomy):
unknown_item_id = 6 unknown_item_id = 6
term_names = ['Monrovia', 'age 40-45', 'pertinent'] term_names = ['Monrovia', 'age 40-45', 'pertinent']
with pytest.raises(TransportException) as excinfo: with pytest.raises(TransportException) as excinfo:
items.add_free_terms(unknown_item_id, taxonomy.slug, term_names) items.add_terms(unknown_item_id, taxonomy.slug, term_names)
error = excinfo.value.message error = excinfo.value.message
...@@ -45,12 +45,12 @@ def test_add_free_terms_raises_transport_exception_if_item_absent(taxonomy): ...@@ -45,12 +45,12 @@ def test_add_free_terms_raises_transport_exception_if_item_absent(taxonomy):
@pytest.mark.django_db @pytest.mark.django_db
def test_add_free_terms_raises_transport_exception_if_taxonomy_absent(): def test_add_terms_raises_transport_exception_if_taxonomy_absent():
item = items.create({'body': "What is the cuse of ebola?"}) item = items.create({'body': "What is the cuse of ebola?"})
term_names = ['Monrovia', 'age 40-45', 'pertinent'] term_names = ['Monrovia', 'age 40-45', 'pertinent']
with pytest.raises(TransportException) as excinfo: with pytest.raises(TransportException) as excinfo:
items.add_free_terms(item['id'], 'unknown-slug', term_names) items.add_terms(item['id'], 'unknown-slug', term_names)
error = excinfo.value.message error = excinfo.value.message
......
...@@ -29,7 +29,7 @@ def test_terms_can_be_added_to_item(item_data): ...@@ -29,7 +29,7 @@ def test_terms_can_be_added_to_item(item_data):
assert item.terms.count() == 0 assert item.terms.count() == 0
for term in (TermFactory() for i in range(2)): for term in (TermFactory() for i in range(2)):
items.add_term(item_id, term.taxonomy.slug, term.name) items.add_terms(item_id, term.taxonomy.slug, term.name)
assert item.terms.count() == 2 assert item.terms.count() == 2
...@@ -37,7 +37,7 @@ def test_terms_can_be_added_to_item(item_data): ...@@ -37,7 +37,7 @@ def test_terms_can_be_added_to_item(item_data):
@pytest.mark.django_db @pytest.mark.django_db
def test_add_term_fails_if_taxonomy_does_not_exist(item_data): def test_add_term_fails_if_taxonomy_does_not_exist(item_data):
with pytest.raises(TransportException) as excinfo: with pytest.raises(TransportException) as excinfo:
items.add_term( items.add_terms(
item_data['id'], item_data['id'],
"unknown-slug", "unknown-slug",
"unknown term name", "unknown term name",
...@@ -47,13 +47,13 @@ def test_add_term_fails_if_taxonomy_does_not_exist(item_data): ...@@ -47,13 +47,13 @@ def test_add_term_fails_if_taxonomy_does_not_exist(item_data):
assert error['status_code'] == 400 assert error['status_code'] == 400
assert error['detail'] == "Taxonomy matching query does not exist." assert error['detail'] == "Taxonomy matching query does not exist."
assert error['term']['name'] == "unknown term name" assert error['terms']['name'] == "unknown term name"
@pytest.mark.django_db @pytest.mark.django_db
def test_add_term_fails_if_term_does_not_exist(taxonomy, item_data): def test_add_term_fails_if_term_does_not_exist(taxonomy, item_data):
with pytest.raises(TransportException) as excinfo: with pytest.raises(TransportException) as excinfo:
items.add_term( items.add_terms(
item_data['id'], item_data['id'],
taxonomy.slug, taxonomy.slug,
"unknown term name", "unknown term name",
...@@ -63,7 +63,7 @@ def test_add_term_fails_if_term_does_not_exist(taxonomy, item_data): ...@@ -63,7 +63,7 @@ def test_add_term_fails_if_term_does_not_exist(taxonomy, item_data):
assert error['status_code'] == 400 assert error['status_code'] == 400
assert error['detail'] == "Term matching query does not exist." assert error['detail'] == "Term matching query does not exist."
assert error['term']['name'] == "unknown term name" assert error['terms']['name'] == "unknown term name"
@pytest.mark.django_db @pytest.mark.django_db
...@@ -71,7 +71,7 @@ def test_add_term_fails_if_item_does_not_exist(): ...@@ -71,7 +71,7 @@ def test_add_term_fails_if_item_does_not_exist():
with pytest.raises(TransportException) as excinfo: with pytest.raises(TransportException) as excinfo:
term = TermFactory() term = TermFactory()
unknown_item_id = 6 # I am a Free Man unknown_item_id = 6 # I am a Free Man
items.add_term(unknown_item_id, term.taxonomy.slug, term.name) items.add_terms(unknown_item_id, term.taxonomy.slug, term.name)
error = excinfo.value.message error = excinfo.value.message
......
...@@ -22,10 +22,10 @@ def test_terms_can_be_removed_from_item(item_data): ...@@ -22,10 +22,10 @@ def test_terms_can_be_removed_from_item(item_data):
assert item.terms.count() == 0 assert item.terms.count() == 0
term = TermFactory(name='term to be deleted') term = TermFactory(name='term to be deleted')
items.add_term(item_id, term.taxonomy.slug, term.name) items.add_terms(item_id, term.taxonomy.slug, term.name)
term2 = TermFactory(name='term not to be deleted') term2 = TermFactory(name='term not to be deleted')
items.add_term(item_id, term2.taxonomy.slug, term2.name) items.add_terms(item_id, term2.taxonomy.slug, term2.name)
assert item.terms.count() == 2 assert item.terms.count() == 2
......
...@@ -45,9 +45,9 @@ def test_term_itemcount_returns_terms_and_counts(item_data, ...@@ -45,9 +45,9 @@ def test_term_itemcount_returns_terms_and_counts(item_data,
questions_term): questions_term):
# This is tested more comprehensively in the API tests # This is tested more comprehensively in the API tests
transport.items.add_term(item_data['id'], transport.items.add_terms(item_data['id'],
questions_category.slug, questions_category.slug,
questions_term.name) questions_term.name)
terms = transport.taxonomies.term_itemcount(slug=questions_category.slug) terms = transport.taxonomies.term_itemcount(slug=questions_category.slug)
counts = {term['name']: term['count'] for term in terms} counts = {term['name']: term['count'] for term in terms}
...@@ -77,9 +77,9 @@ def test_term_itemcount_returns_terms_and_counts_for_range( ...@@ -77,9 +77,9 @@ def test_term_itemcount_returns_terms_and_counts_for_range(
items = [item_data(timestamp=now - timedelta(days=d)) for d in range(0, 9)] items = [item_data(timestamp=now - timedelta(days=d)) for d in range(0, 9)]
for item in items: for item in items:
transport.items.add_term(item['id'], transport.items.add_terms(item['id'],
questions_category.slug, questions_category.slug,
questions_term.name) questions_term.name)
# 9 8 7 6 5 4 3 2 1 0 # 9 8 7 6 5 4 3 2 1 0
# n y y y y y y y n n # n y y y y y y y n n
...@@ -107,12 +107,12 @@ def test_term_itemcount_returns_zero_term_counts_for_range( ...@@ -107,12 +107,12 @@ def test_term_itemcount_returns_zero_term_counts_for_range(
item_too_recent = item_data(timestamp=now) item_too_recent = item_data(timestamp=now)
item_too_old = item_data(timestamp=eight_days_ago) item_too_old = item_data(timestamp=eight_days_ago)
transport.items.add_term(item_too_recent['id'], transport.items.add_terms(item_too_recent['id'],
questions_category.slug, questions_category.slug,
questions_term.name) questions_term.name)
transport.items.add_term(item_too_old['id'], transport.items.add_terms(item_too_old['id'],
questions_category.slug, questions_category.slug,
questions_term.name) questions_term.name)
[term] = transport.taxonomies.term_itemcount( [term] = transport.taxonomies.term_itemcount(
slug=questions_category.slug, slug=questions_category.slug,
......
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