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

Implemented updating of category on form submit

parent 304addd6
No related branches found
No related tags found
1 merge request!37Items can be edited when form is submitted
......@@ -31,9 +31,15 @@ def item():
@pytest.fixture
def view(item):
def item_type():
return {'name': 'question', 'long_name': 'Question'}
@pytest.fixture
def view(item, item_type):
view = AddEditItemView()
view.item = item
view.item_type = item_type
url = reverse('edit-item',
kwargs={'item_id': item['id']})
......@@ -53,6 +59,7 @@ def form(view, item):
return form
ReqFactory = RequestFactory()
......@@ -353,8 +360,6 @@ def test_displaying_unknown_item_returns_redirect_response(generic_item):
@pytest.mark.django_db
def test_item_can_be_updated(view, form):
view.item_type = {'long_name': 'Question'}
new_text = "What is the cause of Ebola?"
form.cleaned_data['body'] = new_text,
......@@ -364,9 +369,48 @@ def test_item_can_be_updated(view, form):
assert item['body'] == new_text
@pytest.mark.django_db
def test_item_category_can_be_updated(view, form):
form.cleaned_data['category'] = 'Ebola updates',
view.form_valid(form)
item = transport.items.get(view.item['id'])
terms = {t['taxonomy']: t['name'] for t in item['terms']}
assert terms['ebola-questions'] == 'Ebola updates'
@pytest.mark.django_db
def test_item_category_can_be_unset(view, form):
transport.items.add_term(view.item['id'], 'ebola-questions',
'Ebola origins')
form.cleaned_data['category'] = ''
view.form_valid(form)
item = transport.items.get(view.item['id'])
terms = {t['taxonomy']: t['name'] for t in item['terms']}
assert 'ebola-questions' not in terms
@pytest.mark.django_db
def test_item_category_not_required(view, form):
form.cleaned_data['category'] = ''
view.form_valid(form)
item = transport.items.get(view.item['id'])
terms = {t['taxonomy']: t['name'] for t in item['terms']}
assert 'ebola-questions' not in terms
@pytest.mark.django_db
def test_item_update_logs_message_and_redirects(view, form):
view.item_type = {'long_name': 'Question'}
view.item_type['long_name'] = 'Question'
response = view.form_valid(form)
assert response.url == form.cleaned_data['next']
......
......@@ -154,14 +154,24 @@ class AddEditItemView(FormView):
id = int(form.cleaned_data['id'])
if self.item_type:
item_type = self.item_type['long_name']
item_description = self.item_type['long_name']
taxonomy = ITEM_TYPE_CATEGORY.get(self.item_type['name'])
else:
item_type = 'Item'
item_description = 'Item'
taxonomy = None
# 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)
msg = _("%s %d successfully updated.") % (
item_type,
item_description,
id,
)
......
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