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

Changed form to update item on submit

parent c0f829aa
No related branches found
No related tags found
1 merge request!37Items can be edited when form is submitted
......@@ -14,7 +14,7 @@ class AddEditItemForm(forms.Form):
widget=forms.Textarea,
required=True
)
timestamp = forms.DateField(required=True)
timestamp = forms.DateTimeField(required=True)
next = forms.CharField(
widget=forms.HiddenInput,
required=True
......
from datetime import datetime
import pytest
from django.contrib import messages
......@@ -14,7 +15,10 @@ from .views_tests import (
@pytest.fixture
def item():
msg = {'body': "What is the cuse of Ebola?"}
msg = {
'body': "What is the cuse of Ebola?",
'timestamp': "2015-02-23 00:00:00",
}
response = transport.items.create(msg)
return response
......@@ -36,23 +40,33 @@ def view(item):
@pytest.fixture
def form(view):
def form(view, item):
form = view.form_class('question')
form.cleaned_data = item
form.cleaned_data['next'] = '/'
return form
@pytest.mark.django_db
def test_item_update_logs_message_and_redirects(view, form):
def test_item_can_be_updated(view, form):
view.item_type = {'long_name': 'Question'}
form.cleaned_data = {
'next': '/',
'id': view.item['id'],
}
new_text = "What is the cause of Ebola?"
form.cleaned_data['body'] = new_text,
view.form_valid(form)
item = transport.items.get(view.item['id'])
assert item['body'] == new_text
@pytest.mark.django_db
def test_item_update_logs_message_and_redirects(view, form):
view.item_type = {'long_name': 'Question'}
response = view.form_valid(form)
assert response.url == '/'
assert response.url == form.cleaned_data['next']
assert_message(view.request,
messages.SUCCESS,
......@@ -63,11 +77,6 @@ def test_item_update_logs_message_and_redirects(view, form):
def test_item_update_without_type_logs_message(view, form):
view.item_type = None
form.cleaned_data = {
'next': '/',
'id': view.item['id'],
}
view.form_valid(form)
assert_message(view.request,
......
......@@ -3,7 +3,10 @@ from django.http import HttpResponseRedirect
from django.utils.translation import ugettext as _
from django.views.generic.edit import FormView
from transport.items import list
from transport.items import (
list,
update,
)
from ..forms.item import AddEditItemForm
from ..constants import ITEM_TYPE_CATEGORY
......@@ -137,14 +140,18 @@ class AddEditItemView(FormView):
def form_valid(self, form):
""" Form submit handler """
id = int(form.cleaned_data['id'])
if self.item_type:
item_type = self.item_type['long_name']
else:
item_type = 'Item'
update(id, form.cleaned_data)
msg = _("%s %d successfully updated.") % (
item_type,
int(form.cleaned_data['id'])
id,
)
return self._response(
......
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