From a14d7a83ad42aa5a058100e5755ce757bec082f0 Mon Sep 17 00:00:00 2001 From: Jeferson Moura Date: Tue, 24 Jul 2018 14:09:36 +0200 Subject: [PATCH] Fix typo in the column name of forms --- silo/api.py | 24 ++++++++++++++-------- silo/tests/test_customformview.py | 33 +++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 8 deletions(-) diff --git a/silo/api.py b/silo/api.py index 8041841..126f00e 100644 --- a/silo/api.py +++ b/silo/api.py @@ -144,8 +144,8 @@ class CustomFormViewSet(mixins.CreateModelMixin, queryset = Silo.objects.all() _default_columns = [ {'name': 'submitted_by', 'type': 'text'}, - {'name': 'submission_data', 'type': 'date'}, - {'name': 'submission_time', 'type': 'date'} + {'name': 'submission_date', 'type': 'date'}, + {'name': 'submission_time', 'type': 'time'} ] def create(self, request, *args, **kwargs): @@ -271,18 +271,26 @@ def save_data(self, request): status=status.HTTP_400_BAD_REQUEST) if data: - # add the timestamp + submitted_by = None + + # get timestamp now = datetime.now() submission_date = now.strftime('%Y-%m-%d') submission_time = now.strftime('%H:%M:%S') - data.update({'submission_date': submission_date}) - data.update({'submission_time': submission_time}) - # if there's a submitted_by uuid, add the user's name to the data + # if there's a submitted_by uuid, get user's name from db if submitted_by_uuid: - submitted_by_username = TolaUser.objects.values_list( + submitted_by = TolaUser.objects.values_list( 'name', flat=True).get(tola_user_uuid=submitted_by_uuid) - data.update({'submitted_by': submitted_by_username}) + + # add values to the default columns + for default_col in self._default_columns: + if default_col['type'] == 'text' and submitted_by: + data.update({'submitted_by': submitted_by}) + elif default_col['type'] == 'date': + data.update({'submission_date': submission_date}) + elif default_col['type'] == 'time': + data.update({'submission_time': submission_time}) try: silo = Silo.objects.get(pk=silo_id) diff --git a/silo/tests/test_customformview.py b/silo/tests/test_customformview.py index 46339ce..54a39e7 100644 --- a/silo/tests/test_customformview.py +++ b/silo/tests/test_customformview.py @@ -605,6 +605,39 @@ def test_save_data_customform_normaluser(self): self.assertIn('submitted_by', data) self.assertEqual(data['submitted_by'], sender_tola_user.name) + def test_save_data_customform_default_columns(self): + user = factories.User(first_name='Homer', last_name='Simpson') + sender_tola_user = factories.TolaUser(user=user) + + data = { + 'silo_id': self.silo.id, + 'data': { + 'name': 'John Lennon', + 'age': 40, + 'city': 'Liverpool' + }, + 'submitted_by': sender_tola_user.tola_user_uuid.__str__(), + } + + request = self.factory.post('api/customform/save_data', + data=json.dumps(data), + content_type='application/json') + request.user = self.tola_user.user + view = CustomFormViewSet.as_view({'post': 'save_data'}) + response = view(request) + + self.assertEqual(response.status_code, 200) + self.assertEqual(response.data['detail'], 'It was successfully saved.') + self.assertEqual(self.silo.data_count, 1) + + # check if the default columns were created + customform_silo = LabelValueStore.objects.get(silo_id=self.silo.id) + table_column_names = customform_silo._dynamic_fields.keys() + for default_col in CustomFormViewSet._default_columns: + self.assertIn(default_col['name'], table_column_names) + + self.assertEqual(len(table_column_names), 6) + def test_save_data_customform_no_data_normaluser(self): data = {}