diff --git a/back/backend/migrations/0006_auto_20190213_2113.py b/back/backend/migrations/0006_auto_20190213_2113.py new file mode 100644 index 0000000..f472fa1 --- /dev/null +++ b/back/backend/migrations/0006_auto_20190213_2113.py @@ -0,0 +1,18 @@ +# Generated by Django 2.1.5 on 2019-02-14 05:13 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('backend', '0005_field_field_name'), + ] + + operations = [ + migrations.AlterField( + model_name='field', + name='data_date', + field=models.DateField(blank=True, null=True), + ), + ] diff --git a/back/backend/migrations/0007_merge_20190213_2318.py b/back/backend/migrations/0007_merge_20190213_2318.py new file mode 100644 index 0000000..aa0cb2c --- /dev/null +++ b/back/backend/migrations/0007_merge_20190213_2318.py @@ -0,0 +1,14 @@ +# Generated by Django 2.1.5 on 2019-02-14 07:18 + +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('backend', '0006_auto_20190213_1719'), + ('backend', '0006_auto_20190213_2113'), + ] + + operations = [ + ] diff --git a/back/backend/models.py b/back/backend/models.py index 33caa60..0ab711a 100644 --- a/back/backend/models.py +++ b/back/backend/models.py @@ -34,7 +34,7 @@ class Field(models.Model): completed = models.BooleanField(default=False) data_bool = models.BooleanField(default=False) data_decimal = models.DecimalField(max_digits=9, decimal_places=2, null=True, blank=True) - data_date = models.DateField(default=datetime.date.today) + data_date = models.DateField(null=True, blank=True) data_file = models.FileField(upload_to='uploads/%Y/%m/%d/', max_length=512, null=True, blank=True) data_string = models.TextField(default='', blank=True) data_integer = models.IntegerField(default=0, blank=True) diff --git a/back/backend/views.py b/back/backend/views.py index 069aab3..04fc6dd 100644 --- a/back/backend/views.py +++ b/back/backend/views.py @@ -57,7 +57,8 @@ def get_fields(s_id): "label": i.label, "field_type": i.field_type, "number": i.number, - "value": value + "value": value, + "id": i.id, } # append the fields to array # use copy() to avoid overwriting @@ -154,21 +155,62 @@ def report_detail(request, report_pk): # update a section with new data @api_view(['PUT']) def section(request, report_pk, section_pk): - - """ original stub - data = { - "message": "Updated report {0}, section {1}.".format(report_pk, section_pk), - "fields": { - "international": True, - "travel_date": "2012-04-23T18:25:43.511Z", - "fare": "1024.99", - "lowest_fare_screenshot": "image", - } - } - """ + + for key in request.data: + # get the matching field object + update = Field.objects.get(section_id=section_pk, field_name=key) + + if update.field_type == "boolean": + # flight check + if request.data[key] == "on": + update.data_bool = True + elif request.data[key] == "off": + update.data_bool = False + # everything else + else: + update.data_bool = request.data[key] + + if update.field_type == "decimal": + # initialize to 0 + if ( + request.data[key] == "" or + request.data[key] is None + ): + update.data_decimal = 0.0 + else: + update.data_decimal = request.data[key] + + if update.field_type == "date": + # initialize to today's date + if ( + request.data[key] == "" or + request.data[key] is None + ): + update.data_date = None + else: + update.data_date = request.data[key] + + if update.field_type == "file": + update.data_file = request.data[key] + + if update.field_type == "string": + update.data_string = request.data[key] + + if update.field_type == "integer": + # initialize to 0 + if ( + request.data[key] == "" or + request.data[key] is None + ): + update.data_integer = 0 + else: + update.data_integer = request.data[key] + + update.save() data = { "message": "Updated report {0}, section {1}.".format(report_pk, section_pk), + "request_data": request.data } - return JsonResponse(data) + diff --git a/back/db.sqlite3 b/back/db.sqlite3 index 52e2ecd..df940de 100644 Binary files a/back/db.sqlite3 and b/back/db.sqlite3 differ