summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--archaeological_operations/tests.py50
-rw-r--r--ishtar_common/wizards.py10
2 files changed, 55 insertions, 5 deletions
diff --git a/archaeological_operations/tests.py b/archaeological_operations/tests.py
index 6a96ebdf4..40e50f42b 100644
--- a/archaeological_operations/tests.py
+++ b/archaeological_operations/tests.py
@@ -42,7 +42,7 @@ from ishtar_common.models import OrganizationType, Organization, ItemKey, \
Town, ImporterColumn, Person, Author, SourceType, AuthorType, \
DocumentTemplate, PersonType, TargetKeyGroup, JsonDataField, \
JsonDataSection, ImportTarget, FormaterType, CustomForm, ExcludedField, \
- UserProfile, ProfileType, Area
+ UserProfile, ProfileType, Area, CustomFormJsonField
from archaeological_files.models import File, FileType
from archaeological_context_records.models import Unit, ContextRecord
@@ -1362,6 +1362,54 @@ class CustomFormTest(TestCase, OperationInitTest):
response = c.post(url, data)
self.assertEqual(response.status_code, 404)
+ def test_json(self):
+ operation = self.operations[0]
+ operation.data = {"groundhog": {"number": 53444,
+ "awake_state": u"réveillée",
+ "with_feather": "Oui"},
+ "frog_number": 32303}
+ operation.save()
+
+ content_type = ContentType.objects.get_for_model(operation)
+ field = JsonDataField.objects.create(
+ name=u"État d'éveil", key='groundhog__awake_state',
+ content_type=content_type, value_type='C')
+ form = CustomForm.objects.create(
+ name="Test", form="operation-010-general", available=True,
+ apply_to_all=True)
+ CustomFormJsonField.objects.create(
+ custom_form=form,
+ json_field=field,
+ label="Le beau",
+ )
+
+ c = Client()
+ c.login(username=self.username, password=self.password)
+
+ cls_wiz = OperationWizardModifTest
+ url = reverse(cls_wiz.url_name)
+ # first wizard step
+ step = 'selec-operation_modification'
+ cls_wiz.wizard_post(c, url, step, {'pk': self.operations[0].pk})
+
+ step = 'general-operation_modification'
+ data = {
+ '{}{}-current_step'.format(cls_wiz.url_name,
+ cls_wiz.wizard_name): [step],
+ }
+ response = c.post(url, data)
+ self.assertIn(
+ "Le beau", response.content,
+ msg="json field not displayed on modification wizard"
+ )
+
+ cls_wiz.wizard_post(c, url, step, {'pk': self.operations[1].pk})
+ response = c.post(url, data)
+ self.assertIn(
+ "Le beau", response.content,
+ msg="json field form: existing value should be presented in select"
+ )
+
class OperationSearchTest(TestCase, OperationInitTest):
fixtures = FILE_FIXTURES
diff --git a/ishtar_common/wizards.py b/ishtar_common/wizards.py
index 40044f100..a6d0befd5 100644
--- a/ishtar_common/wizards.py
+++ b/ishtar_common/wizards.py
@@ -600,7 +600,7 @@ class Wizard(NamedUrlWizardView):
dct = self.get_extra_model(dct, form_list)
obj = self.get_current_saved_object()
data = {}
- if obj:
+ if obj and hasattr(obj, 'data'):
data = obj.data
# manage dependant items and json fields
@@ -647,7 +647,8 @@ class Wizard(NamedUrlWizardView):
elif type(dct[k]) not in (list, tuple):
dct[k] = [dct[k]]
setattr(obj, k, dct[k])
- obj.data = data
+ if hasattr(obj, 'data'):
+ obj.data = data
if hasattr(obj, 'pre_save'):
obj.pre_save()
try:
@@ -732,7 +733,8 @@ class Wizard(NamedUrlWizardView):
except ValidationError as e:
logger.warning(unicode(e))
return self.render(form_list[-1])
- obj.data = data
+ if hasattr(obj, 'data'):
+ obj.data = data
obj.save(**saved_args)
for k in adds:
getattr(obj, k).add(adds[k])
@@ -1267,7 +1269,7 @@ class Wizard(NamedUrlWizardView):
initial = MultiValueDict()
# manage json field
- if hasattr(self, 'json_fields') \
+ if hasattr(obj, 'data') and obj.data and hasattr(self, 'json_fields') \
and getattr(c_form, 'form_slug', None) \
and c_form.form_slug in self.json_fields \
and obj.data: