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

Added exception handling for item updates

parent 0cdb6afb
No related branches found
No related tags found
1 merge request!38Submit exceptions
......@@ -437,3 +437,47 @@ def test_no_category_when_item_type_not_set(view):
initial = view.get_initial()
assert 'category' not in initial
@pytest.mark.django_db
def test_item_update_transport_exception_logs_message(view, form):
# This could happen if someone else deletes the item when the
# form is open
transport.items.delete(view.item['id'])
view.form_valid(form)
assert_message(view.request,
messages.ERROR,
"Not found.")
@pytest.mark.django_db
def test_item_term_update_transport_exception_logs_message(view, form):
# This shouldn't be possible from the form but we may get other
# TransportException errors
form.cleaned_data['category'] = "A category that doesn't exist"
view.form_valid(form)
assert_message(view.request,
messages.ERROR,
"Term matching query does not exist.")
@pytest.mark.django_db
def test_item_term_delete_transport_exception_logs_message(view, form):
# This shouldn't be possible from the form but we may get other
# TransportException errors
form.cleaned_data['category'] = ''
# Not sure if this is good practice
from ..constants import ITEM_TYPE_CATEGORY
ITEM_TYPE_CATEGORY['question'] = 'unknown-slug'
view.form_valid(form)
ITEM_TYPE_CATEGORY['question'] = 'ebola-questions'
assert_message(view.request,
messages.ERROR,
"Taxonomy with slug 'unknown-slug' does not exist.")
......@@ -162,23 +162,32 @@ class AddEditItemView(FormView):
# TODO: Combine terms into single transaction
category = form.cleaned_data.pop('category', None)
transport.items.update(id, form.cleaned_data)
if taxonomy:
if category:
transport.items.add_term(id, taxonomy, category)
else:
transport.items.delete_all_terms(id, taxonomy)
try:
transport.items.update(id, form.cleaned_data)
if taxonomy:
if category:
transport.items.add_term(id, taxonomy, category)
else:
transport.items.delete_all_terms(id, taxonomy)
message = _("%s %d successfully updated.") % (
item_description,
id,
)
message_code = messages.SUCCESS
msg = _("%s %d successfully updated.") % (
item_description,
id,
)
except transport.exceptions.TransportException as e:
message = e.message.get('detail')
if message is None:
message = e.message
message_code = messages.ERROR
return self._response(
form.cleaned_data['next'],
messages.SUCCESS,
msg)
message_code,
message)
def form_invalid(self, form):
""" Form invalid handler """
......
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