Printing the data from the database - need to update the functionality to prevent overwriting
This commit is contained in:
parent
78c9ec522d
commit
e0611793f6
4 changed files with 147 additions and 79 deletions
|
@ -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)
|
||||
#
|
||||
#
|
||||
#
|
||||
#
|
||||
#
|
||||
#
|
||||
#
|
||||
|
||||
|
|
|
@ -5,16 +5,18 @@ from rest_framework.urlpatterns import format_suffix_patterns
|
|||
from . import views
|
||||
|
||||
urlpatterns = [
|
||||
#path('', views.List.as_view()),
|
||||
#path('<int:pk>/', views.Detail.as_view()),
|
||||
# path('', views.List.as_view()),
|
||||
# path('<int:pk>/', views.Detail.as_view()),
|
||||
path('print', views.print_all_reports),
|
||||
|
||||
path('report', views.report),
|
||||
path('reports', views.reports),
|
||||
path('report/<int:report_pk>', views.report_detail),
|
||||
path('report/<int:report_pk>/section/<int:section_pk>', 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/<int:report_pk>', views.report_detail),
|
||||
# path('report/<int:report_pk>/section/<int:section_pk>', views.section),
|
||||
# path('account', views.account),
|
||||
# path('account/login', views.account_login),
|
||||
# path('account/logout', views.account_logout),
|
||||
]
|
||||
|
||||
urlpatterns = format_suffix_patterns(urlpatterns)
|
|
@ -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 = {
|
||||
|
|
BIN
back/db.sqlite3
BIN
back/db.sqlite3
Binary file not shown.
Loading…
Reference in a new issue