From e0611793f64bf031684941fcba35bbcd76a1246c Mon Sep 17 00:00:00 2001 From: Rupika Date: Sun, 3 Feb 2019 00:47:04 -0800 Subject: [PATCH] Printing the data from the database - need to update the functionality to prevent overwriting --- back/backend/serializers.py | 130 ++++++++++++++++++------------------ back/backend/urls.py | 20 +++--- back/backend/views.py | 76 +++++++++++++++++++-- back/db.sqlite3 | Bin 53248 -> 53248 bytes 4 files changed, 147 insertions(+), 79 deletions(-) diff --git a/back/backend/serializers.py b/back/backend/serializers.py index 1b162f4..16d78e4 100644 --- a/back/backend/serializers.py +++ b/back/backend/serializers.py @@ -1,66 +1,66 @@ -# Rupika Dikkala -# January 23, 2019 -# File contains serializers needed -# to set up API end points - -from rest_framework import serializers -from . import models - -# serializer for reports -class ReportSerializer(serializers.ModelSerializer): - # user id is foreign key - user_id = serializers.PrimaryKeyRelatedField(many=False, read_only=True) - - class Meta: - fields = ( - 'user_id', - 'title', - 'date_created', - # 'data_submitted', - 'submitted', - ) - model = models.Report - - -# section serializer -class SectionSerializer(serializers.ModelSerializer): - # report id foriegn key - report_id = serializers.PrimaryKeyRelatedField(many=True, read_only=True) - - - class Meta: - fields = ( - 'report_id', - 'completed', - 'title', - 'html_description', - 'number', - ) - model = models.Section - - -class FieldSerializer(serializers.ModelSerializer): - # section_id is foriegn key - section_id = serializers.PrimaryKeyRelatedField(many=True, read_only=True) - - class Meta: - fields = ( - 'section_id', - 'label', - 'number', - 'type', - 'completed', - ) - model = models.Field - - -class DataSerializer(serializers.ModelSerializer): - field_id = serializers.PrimaryKeyRelatedField(many=False, read_only=True) - - - - - - - +# # Rupika Dikkala +# # January 23, 2019 +# # File contains serializers needed +# # to set up API end points +# +# from rest_framework import serializers +# from . import models +# +# # serializer for reports +# class ReportSerializer(serializers.ModelSerializer): +# # user id is foreign key +# user_id = serializers.PrimaryKeyRelatedField(many=False, read_only=True) +# +# class Meta: +# fields = ( +# 'user_id', +# 'title', +# 'date_created', +# # 'data_submitted', +# 'submitted', +# ) +# model = models.Report +# +# +# # section serializer +# class SectionSerializer(serializers.ModelSerializer): +# # report id foriegn key +# report_id = serializers.PrimaryKeyRelatedField(many=True, read_only=True) +# +# +# class Meta: +# fields = ( +# 'report_id', +# 'completed', +# 'title', +# 'html_description', +# 'number', +# ) +# model = models.Section +# +# +# class FieldSerializer(serializers.ModelSerializer): +# # section_id is foriegn key +# section_id = serializers.PrimaryKeyRelatedField(many=True, read_only=True) +# +# class Meta: +# fields = ( +# 'section_id', +# 'label', +# 'number', +# 'type', +# 'completed', +# ) +# model = models.Field +# +# +# class DataSerializer(serializers.ModelSerializer): +# field_id = serializers.PrimaryKeyRelatedField(many=False, read_only=True) +# +# +# +# +# +# +# diff --git a/back/backend/urls.py b/back/backend/urls.py index 8e0cada..921ff01 100644 --- a/back/backend/urls.py +++ b/back/backend/urls.py @@ -5,16 +5,18 @@ from rest_framework.urlpatterns import format_suffix_patterns from . import views urlpatterns = [ - #path('', views.List.as_view()), - #path('/', views.Detail.as_view()), + # path('', views.List.as_view()), + # path('/', views.Detail.as_view()), + path('print', views.print_all_reports), - path('report', views.report), - path('reports', views.reports), - path('report/', views.report_detail), - path('report//section/', views.section), - path('account', views.account), - path('account/login', views.account_login), - path('account/logout', views.account_logout), + # + # path('report', views.report), + # path('reports', views.reports), + # path('report/', views.report_detail), + # path('report//section/', views.section), + # path('account', views.account), + # path('account/login', views.account_login), + # path('account/logout', views.account_logout), ] urlpatterns = format_suffix_patterns(urlpatterns) \ No newline at end of file diff --git a/back/backend/views.py b/back/backend/views.py index 1f99059..2c95510 100644 --- a/back/backend/views.py +++ b/back/backend/views.py @@ -8,13 +8,78 @@ from .serializers import * # Sample view using generics -class List(generics.ListCreateAPIView): - queryset = Report.objects.all() - serializer_class = ReportSerializer +# class List(generics.ListCreateAPIView): +# queryset = Report.objects.all() +# serializer_class = ReportSerializer +# +# class Detail(generics.RetrieveUpdateDestroyAPIView): +# queryset = Report.objects.all() +# serializer_class = ReportSerializer -class Detail(generics.RetrieveUpdateDestroyAPIView): + +def print_all_reports(self): + data = {} queryset = Report.objects.all() - serializer_class = ReportSerializer + for i in queryset: + data = { + "title": i.title, + "date_created": i.date_created, + "submitted": i.submitted, + "date_submitted": i.date_submitted, + "sections": get_sections(i.id), + } + + return JsonResponse(data) + +def get_sections(r_id): + section_set = {"section_set": []} + queryset = Section.objects.filter(report_id=r_id) + # queryset = Section.objects.all() + for i in queryset: + inner_section = { + "id": i.id, + "completed": i.completed, + "title": i.title, + "html_description": i.html_description, + "fields": get_fields(i.id), + } + # section_set.update(inner_section) + section_set["section_set"].append(inner_section.copy()) + + return section_set + # return JsonResponse(full) + + +def get_fields(s_id): + field_set = {"fields": []} + queryset = Field.objects.filter(section_id=s_id) + # queryset = Field.objects.all() + count = 0 + for i in queryset: + temp = "field" + str(count) + inner_field = {i.label: { + "label": i.label, + "type": i.type, + "value": i.number, + }} + print("PRINT FIELD") + print(i.label) + print(i.type) + print(i.number) + # field_set.append(inner_field) + field_set["fields"].append(inner_field.copy()) + # field_set.update(inner_field) + count += 1 + + print("COUNT = {}".format(count)) + return field_set + # return JsonResponse(field_set) + + + + + + # API Endpoints @@ -85,6 +150,7 @@ def report(request): } return JsonResponse(data) +# List of reports @api_view(['GET']) def reports(request): data = { diff --git a/back/db.sqlite3 b/back/db.sqlite3 index bfc38f728890df0c057a06cad169fe8f1804510a..42abd451cc1aa0043c727d3948c91babae07e215 100644 GIT binary patch delta 407 zcmZozz}&Ead4e>f-$WT_M!$^-iN=D)3I=9YMkZD!mU;$87KWB4n=cs~)o?H|wlFXq zVr<#0$Wg&q-(=0i>f6HVSXyk6ZdRIIVPIaGo@kPuY;KraUYwMgpJQ2>Tb^W8;_s4e z?C;{18JX*q73JcT?VIJ6;g{u?6=@Wf?dJ|;h5H&r<@#3ohj89wMR=tpy1P{+I(wRVX1N;p<^~!VMY+2gMrB1B`sR8XM7cOt`58r4 zL{_DnMrC=HM}`1pvZAtlvm#9*vm%Xw^yG<~g}HLFJ+tyNeSE*4JF~nTvm+yJYne7b+Ak*n0O|{T=>Px# delta 106 zcmZozz}&Ead4e>f_e2?IM(>RYiN=D43I+yNMn+bK#(I{<7KWxqn=cs~)vz%#9b#ZQ zv{{j32NOq=H6yEU3#;Sg2ip}kzuF|l$i~Qghk^OdW