Reworked API endpoints with correct types and some sample JSON.

This commit is contained in:
kououken 2019-01-27 14:22:42 -08:00
parent a4628c5f3c
commit a4dbb3f644
3 changed files with 122 additions and 153 deletions

View file

@ -1,23 +1,20 @@
# Rupika Dikkala # Link report and account functionality to views
# January 19, 2019
# Add urls and link to the
# views
from django.urls import path from django.urls import path
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('create_report/', views.create_report), path('report', views.report),
path('delete_report/', views.delete_report), path('reports', views.reports),
path('get_report/', views.get_report), path('report/<int:report_pk>', views.report_detail),
path('list_report/', views.list_report), path('report/<int:report_pk>/section/<int:section_pk>', views.section),
path('update_report/', views.update_report), path('account', views.account),
path('submit_report/', views.submit_report), path('account/login', views.account_login),
path('update_section/', views.update_section), path('account/logout', views.account_logout),
path('create_account/', views.create_account),
path('login/', views.login),
path('logout/', views.logout),
] ]
urlpatterns = format_suffix_patterns(urlpatterns)

View file

@ -1,17 +1,13 @@
# Rupika Dikkala from rest_framework import generics
# January 19, 2019 from rest_framework import status
# Creating views for URL that from rest_framework.decorators import api_view
# returns JSON data
from django.shortcuts import render from django.shortcuts import render
from django.http import JsonResponse from django.http import JsonResponse
from .models import * from .models import *
from .serializers import * from .serializers import *
from rest_framework import generics # Sample view using generics
# create sample view
class List(generics.ListCreateAPIView): class List(generics.ListCreateAPIView):
queryset = Report.objects.all() queryset = Report.objects.all()
serializer_class = ReportSerializer serializer_class = ReportSerializer
@ -20,11 +16,13 @@ class Detail(generics.RetrieveUpdateDestroyAPIView):
queryset = Report.objects.all() queryset = Report.objects.all()
serializer_class = ReportSerializer serializer_class = ReportSerializer
# API Endpoints
####################################### @api_view(['POST'])
def report(request):
# Create Report '''
def create_report(request): Generate a new empty report and return it
'''
data = { data = {
"title": "2018 Portland trip", "title": "2018 Portland trip",
"date_created": "2018-05-22T14:56:28.000Z", "date_created": "2018-05-22T14:56:28.000Z",
@ -87,79 +85,8 @@ def create_report(request):
} }
return JsonResponse(data) return JsonResponse(data)
# Delete report @api_view(['GET'])
def delete_report(request): def reports(request):
data = {
'name': 'Delete report',
}
return JsonResponse(data)
# Get report
def get_report(request):
data = {
"title": "2018 Portland trip",
"date_created": "2018-05-22T14:56:28.000Z",
"submitted": False,
"date_submitted": "0000-00-00T00:00:00.000Z",
"sections": [
{
"id": 1,
"completed": True,
"title": "Flight Info",
"html_description": "<p>Enter flight details here.</p>",
"fields": {
"international": {
"label": "International flight",
"type": "boolean",
"value": True
},
"travel_date": {
"label": "Travel start date",
"type": "date",
"value": "2016-05-22T14:56:28.000Z"
},
"fare": {
"label": "Fare",
"type": "decimal",
"value": "1024.99"
},
"lowest_fare_screenshot": {
"label": "Lowest fare screenshot",
"type": "file",
"value": "e92h842jiu49f8..."
},
"plane_ticket_invoice": {
"label": "Plane ticket invoice PDF",
"type": "file",
"value": ""
}
},
"rule_violations": [
{
"error_text": "Plane ticket invoice must be submitted."
}
]
},
{
"id": 2,
"completed": False,
"title": "Hotel info",
"html_description": "<p>If you used a hotel, please enter the details.</p>",
"fields": {
"total": {
"label": "Total cost",
"type": "decimal"
}
},
"rule_violations": [
]
}
]
}
return JsonResponse(data)
# List Reports
def list_report(request):
data = { data = {
"reports": [ "reports": [
{ {
@ -184,24 +111,83 @@ def list_report(request):
} }
return JsonResponse(data) return JsonResponse(data)
# Update Reports @api_view(['GET', 'PUT', 'DELETE'])
def update_report(request): def report_detail(request, report_pk):
data = { if request.method == 'GET':
'name': 'update report', data = {
'state': 'SUBMITTED!', "report_pk": report_pk,
} "title": "2018 Portland trip",
return JsonResponse(data) "date_created": "2018-05-22T14:56:28.000Z",
"submitted": False,
"date_submitted": "0000-00-00T00:00:00.000Z",
"sections": [
{
"id": 1,
"completed": True,
"title": "Flight Info",
"html_description": "<p>Enter flight details here.</p>",
"fields": {
"international": {
"label": "International flight",
"type": "boolean",
"value": True
},
"travel_date": {
"label": "Travel start date",
"type": "date",
"value": "2016-05-22T14:56:28.000Z"
},
"fare": {
"label": "Fare",
"type": "decimal",
"value": "1024.99"
},
"lowest_fare_screenshot": {
"label": "Lowest fare screenshot",
"type": "file",
"value": "e92h842jiu49f8..."
},
"plane_ticket_invoice": {
"label": "Plane ticket invoice PDF",
"type": "file",
"value": ""
}
},
"rule_violations": [
{
"error_text": "Plane ticket invoice must be submitted."
}
]
},
{
"id": 2,
"completed": False,
"title": "Hotel info",
"html_description": "<p>If you used a hotel, please enter the details.</p>",
"fields": {
"total": {
"label": "Total cost",
"type": "decimal"
}
},
"rule_violations": [
]
}
]
}
return JsonResponse(data)
elif request.method == 'PUT':
return JsonResponse({"message": "Report submitted."})
elif request.method == 'DELETE':
return JsonResponse({"message": "Deleted report {0}.".format(report_pk)})
# Submit Reports @api_view(['PUT'])
def submit_report(request): def section(request, report_pk, section_pk):
data = { '''
'name': 'submit report', Update a section with new data.
} '''
return JsonResponse(data)
# Update section
def update_section(request):
data = { data = {
"message": "Updated report {0}, section {1}.".format(report_pk, section_pk),
"fields": { "fields": {
"international": True, "international": True,
"travel_date": "2012-04-23T18:25:43.511Z", "travel_date": "2012-04-23T18:25:43.511Z",
@ -211,25 +197,23 @@ def update_section(request):
} }
return JsonResponse(data) return JsonResponse(data)
@api_view(['POST'])
def account(request):
'''
Create a new user account
'''
return JsonResponse({"message": "Account creation successful."})
# Create account @api_view(['POST'])
def create_account(request): def account_login(request):
data = { '''
'name': 'create account', Log in to a user account
} '''
return JsonResponse(data) return JsonResponse({"message": "Successfully logged in."})
# Login
def login(request):
data = {
'name': 'login',
}
return JsonResponse(data)
# Logout
def logout(request):
data = {
'name': 'logout',
}
return JsonResponse(data)
@api_view(['DELETE'])
def account_logout(request):
'''
Log out from a user account
'''
return JsonResponse({"message": "User logged out."})

View file

@ -1,17 +1,5 @@
"""reimbursinator URL Configuration """
reimbursinator URL Configuration
The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/2.1/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: path('', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
Including another URLconf
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
""" """
# Rupika Dikkala # Rupika Dikkala
@ -23,5 +11,5 @@ from django.urls import path, include
# add urls to this array # add urls to this array
urlpatterns = [ urlpatterns = [
path('admin/', admin.site.urls), path('admin/', admin.site.urls),
path('backend/', include("backend.urls")), path('api/v1/', include("backend.urls")),
] ]