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
|
# # Rupika Dikkala
|
||||||
# January 23, 2019
|
# # January 23, 2019
|
||||||
# File contains serializers needed
|
# # File contains serializers needed
|
||||||
# to set up API end points
|
# # to set up API end points
|
||||||
|
#
|
||||||
from rest_framework import serializers
|
# from rest_framework import serializers
|
||||||
from . import models
|
# from . import models
|
||||||
|
#
|
||||||
# serializer for reports
|
# # serializer for reports
|
||||||
class ReportSerializer(serializers.ModelSerializer):
|
# class ReportSerializer(serializers.ModelSerializer):
|
||||||
# user id is foreign key
|
# # user id is foreign key
|
||||||
user_id = serializers.PrimaryKeyRelatedField(many=False, read_only=True)
|
# user_id = serializers.PrimaryKeyRelatedField(many=False, read_only=True)
|
||||||
|
#
|
||||||
class Meta:
|
# class Meta:
|
||||||
fields = (
|
# fields = (
|
||||||
'user_id',
|
# 'user_id',
|
||||||
'title',
|
# 'title',
|
||||||
'date_created',
|
# 'date_created',
|
||||||
# 'data_submitted',
|
# # 'data_submitted',
|
||||||
'submitted',
|
# 'submitted',
|
||||||
)
|
# )
|
||||||
model = models.Report
|
# model = models.Report
|
||||||
|
#
|
||||||
|
#
|
||||||
# section serializer
|
# # section serializer
|
||||||
class SectionSerializer(serializers.ModelSerializer):
|
# class SectionSerializer(serializers.ModelSerializer):
|
||||||
# report id foriegn key
|
# # report id foriegn key
|
||||||
report_id = serializers.PrimaryKeyRelatedField(many=True, read_only=True)
|
# report_id = serializers.PrimaryKeyRelatedField(many=True, read_only=True)
|
||||||
|
#
|
||||||
|
#
|
||||||
class Meta:
|
# class Meta:
|
||||||
fields = (
|
# fields = (
|
||||||
'report_id',
|
# 'report_id',
|
||||||
'completed',
|
# 'completed',
|
||||||
'title',
|
# 'title',
|
||||||
'html_description',
|
# 'html_description',
|
||||||
'number',
|
# 'number',
|
||||||
)
|
# )
|
||||||
model = models.Section
|
# model = models.Section
|
||||||
|
#
|
||||||
|
#
|
||||||
class FieldSerializer(serializers.ModelSerializer):
|
# class FieldSerializer(serializers.ModelSerializer):
|
||||||
# section_id is foriegn key
|
# # section_id is foriegn key
|
||||||
section_id = serializers.PrimaryKeyRelatedField(many=True, read_only=True)
|
# section_id = serializers.PrimaryKeyRelatedField(many=True, read_only=True)
|
||||||
|
#
|
||||||
class Meta:
|
# class Meta:
|
||||||
fields = (
|
# fields = (
|
||||||
'section_id',
|
# 'section_id',
|
||||||
'label',
|
# 'label',
|
||||||
'number',
|
# 'number',
|
||||||
'type',
|
# 'type',
|
||||||
'completed',
|
# 'completed',
|
||||||
)
|
# )
|
||||||
model = models.Field
|
# model = models.Field
|
||||||
|
#
|
||||||
|
#
|
||||||
class DataSerializer(serializers.ModelSerializer):
|
# class DataSerializer(serializers.ModelSerializer):
|
||||||
field_id = serializers.PrimaryKeyRelatedField(many=False, read_only=True)
|
# field_id = serializers.PrimaryKeyRelatedField(many=False, read_only=True)
|
||||||
|
#
|
||||||
|
#
|
||||||
|
#
|
||||||
|
#
|
||||||
|
#
|
||||||
|
#
|
||||||
|
#
|
||||||
|
|
||||||
|
|
|
@ -5,16 +5,18 @@ from rest_framework.urlpatterns import format_suffix_patterns
|
||||||
from . import views
|
from . import views
|
||||||
|
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
#path('', views.List.as_view()),
|
# path('', views.List.as_view()),
|
||||||
#path('<int:pk>/', views.Detail.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', views.report),
|
||||||
path('report/<int:report_pk>', views.report_detail),
|
# path('reports', views.reports),
|
||||||
path('report/<int:report_pk>/section/<int:section_pk>', views.section),
|
# path('report/<int:report_pk>', views.report_detail),
|
||||||
path('account', views.account),
|
# path('report/<int:report_pk>/section/<int:section_pk>', views.section),
|
||||||
path('account/login', views.account_login),
|
# path('account', views.account),
|
||||||
path('account/logout', views.account_logout),
|
# path('account/login', views.account_login),
|
||||||
|
# path('account/logout', views.account_logout),
|
||||||
]
|
]
|
||||||
|
|
||||||
urlpatterns = format_suffix_patterns(urlpatterns)
|
urlpatterns = format_suffix_patterns(urlpatterns)
|
|
@ -8,13 +8,78 @@ from .serializers import *
|
||||||
|
|
||||||
# Sample view using generics
|
# Sample view using generics
|
||||||
|
|
||||||
class List(generics.ListCreateAPIView):
|
# class List(generics.ListCreateAPIView):
|
||||||
queryset = Report.objects.all()
|
# queryset = Report.objects.all()
|
||||||
serializer_class = ReportSerializer
|
# 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()
|
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
|
# API Endpoints
|
||||||
|
|
||||||
|
@ -85,6 +150,7 @@ def report(request):
|
||||||
}
|
}
|
||||||
return JsonResponse(data)
|
return JsonResponse(data)
|
||||||
|
|
||||||
|
# List of reports
|
||||||
@api_view(['GET'])
|
@api_view(['GET'])
|
||||||
def reports(request):
|
def reports(request):
|
||||||
data = {
|
data = {
|
||||||
|
|
BIN
back/db.sqlite3
BIN
back/db.sqlite3
Binary file not shown.
Loading…
Reference in a new issue