Skip to content

Commit

Permalink
Merge pull request #541 from jefmoura/fix-typo
Browse files Browse the repository at this point in the history
Fix typo in the column name of forms
  • Loading branch information
Rafael Muñoz Cárdenas authored Jul 30, 2018
2 parents af1df38 + a14d7a8 commit 337353a
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 8 deletions.
24 changes: 16 additions & 8 deletions silo/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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)
Expand Down
33 changes: 33 additions & 0 deletions silo/tests/test_customformview.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {}

Expand Down

0 comments on commit 337353a

Please sign in to comment.