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
# January 19, 2019
# Add urls and link to the
# views
# Link report and account functionality to views
from django.urls import path
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('create_report/', views.create_report),
path('delete_report/', views.delete_report),
path('get_report/', views.get_report),
path('list_report/', views.list_report),
path('update_report/', views.update_report),
path('submit_report/', views.submit_report),
path('update_section/', views.update_section),
path('create_account/', views.create_account),
path('login/', views.login),
path('logout/', views.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)

View file

@ -1,17 +1,13 @@
# Rupika Dikkala
# January 19, 2019
# Creating views for URL that
# returns JSON data
from rest_framework import generics
from rest_framework import status
from rest_framework.decorators import api_view
from django.shortcuts import render
from django.http import JsonResponse
from .models import *
from .serializers import *
from rest_framework import generics
# Sample view using generics
# create sample view
class List(generics.ListCreateAPIView):
queryset = Report.objects.all()
serializer_class = ReportSerializer
@ -20,11 +16,13 @@ class Detail(generics.RetrieveUpdateDestroyAPIView):
queryset = Report.objects.all()
serializer_class = ReportSerializer
# API Endpoints
#######################################
# Create Report
def create_report(request):
@api_view(['POST'])
def report(request):
'''
Generate a new empty report and return it
'''
data = {
"title": "2018 Portland trip",
"date_created": "2018-05-22T14:56:28.000Z",
@ -87,79 +85,8 @@ def create_report(request):
}
return JsonResponse(data)
# Delete report
def delete_report(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):
@api_view(['GET'])
def reports(request):
data = {
"reports": [
{
@ -184,24 +111,83 @@ def list_report(request):
}
return JsonResponse(data)
# Update Reports
def update_report(request):
data = {
'name': 'update report',
'state': 'SUBMITTED!',
}
return JsonResponse(data)
@api_view(['GET', 'PUT', 'DELETE'])
def report_detail(request, report_pk):
if request.method == 'GET':
data = {
"report_pk": report_pk,
"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)
elif request.method == 'PUT':
return JsonResponse({"message": "Report submitted."})
elif request.method == 'DELETE':
return JsonResponse({"message": "Deleted report {0}.".format(report_pk)})
# Submit Reports
def submit_report(request):
data = {
'name': 'submit report',
}
return JsonResponse(data)
# Update section
def update_section(request):
@api_view(['PUT'])
def section(request, report_pk, section_pk):
'''
Update a section with new data.
'''
data = {
"message": "Updated report {0}, section {1}.".format(report_pk, section_pk),
"fields": {
"international": True,
"travel_date": "2012-04-23T18:25:43.511Z",
@ -211,25 +197,23 @@ def update_section(request):
}
return JsonResponse(data)
@api_view(['POST'])
def account(request):
'''
Create a new user account
'''
return JsonResponse({"message": "Account creation successful."})
# Create account
def create_account(request):
data = {
'name': 'create account',
}
return JsonResponse(data)
# Login
def login(request):
data = {
'name': 'login',
}
return JsonResponse(data)
# Logout
def logout(request):
data = {
'name': 'logout',
}
return JsonResponse(data)
@api_view(['POST'])
def account_login(request):
'''
Log in to a user account
'''
return JsonResponse({"message": "Successfully logged in."})
@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
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'))
"""
reimbursinator URL Configuration
"""
# Rupika Dikkala
@ -23,5 +11,5 @@ from django.urls import path, include
# add urls to this array
urlpatterns = [
path('admin/', admin.site.urls),
path('backend/', include("backend.urls")),
]
path('api/v1/', include("backend.urls")),
]