Added PUT, PATCH and DELETE to create, modify and delete model
instances.
This commit is contained in:
parent
f5b329bc17
commit
9cc39c56ab
|
|
@ -15,7 +15,7 @@ class Migration(migrations.Migration):
|
|||
migrations.AddField(
|
||||
model_name='faretransferrule',
|
||||
name='feed_info_id',
|
||||
field=models.ForeignKey(default=django.utils.timezone.now, on_delete=django.db.models.deletion.CASCADE, to='pt_map.feedinfo'),
|
||||
field=models.ForeignKey(default=1, on_delete=django.db.models.deletion.CASCADE, to='pt_map.feedinfo'),
|
||||
preserve_default=False,
|
||||
),
|
||||
]
|
||||
|
|
|
|||
|
|
@ -13,6 +13,7 @@ from django.http import HttpResponse, HttpResponseBadRequest, HttpResponseNotAll
|
|||
from django.core.exceptions import BadRequest, ObjectDoesNotExist
|
||||
from django.core import serializers
|
||||
import django.db.models
|
||||
from MySQLdb import IntegrityError
|
||||
from .models import *
|
||||
from .forms import *
|
||||
import json
|
||||
|
|
@ -133,23 +134,47 @@ def data(request):
|
|||
Successful response is 200 with a list of the primary keys of the modified objects.
|
||||
GET
|
||||
Return json of models identified by primary keys.
|
||||
Responds 400 if any of the requested pks does not exist.
|
||||
DELETE
|
||||
Delete models with given primary keys if they exist.
|
||||
Responds 400 if any of the primary keys given does not exist in the database.
|
||||
Successful response is 200 and the number of deleted models.
|
||||
"""
|
||||
if request.method == "PUT":
|
||||
if request.method in ["PUT", "PATCH", "DELETE"]:
|
||||
new = 0
|
||||
modified = 0
|
||||
if not request.META["CONTENT_TYPE"] == 'application/json':
|
||||
HttpResponseBadRequest('Request must be JSON.')
|
||||
try:
|
||||
bdy = json.loads(request.body)
|
||||
for o in serializers.deserialize('json', request.body):
|
||||
try:
|
||||
obj = o.object.__class__.objects.get(pk=o.object.pk)
|
||||
modified += 1
|
||||
if request.method == "PATCH":
|
||||
for f,v in o.object.__dict__.items():
|
||||
if v:
|
||||
setattr(obj, f, v)
|
||||
obj.save()
|
||||
else:
|
||||
obj.delete()
|
||||
if request.method == "PUT":
|
||||
o.save()
|
||||
except ObjectDoesNotExist:
|
||||
if not request.method == "PUT":
|
||||
return HttpResponseBadRequest("Object(s) not found. {modified} objects touched.")
|
||||
new += 1
|
||||
o.save()
|
||||
except django.db.IntegrityError:
|
||||
return HttpResponseBadRequest("Could not write to database. Probably the objects you were trying to create or update where not compliant with GTFS's or the API's specification.")
|
||||
except IntegrityError:
|
||||
return HttpResponseBadRequest("There was an error while trying to write to the database. Probably a foreign key could not be resolved. Did you specify the objects in the correct order, if they depend on each other?")
|
||||
except serializers.base.DeserializationError:
|
||||
return HttpResponseBadRequest("Could not process the JSON you sent me correctly. Did you comply with the format required by the API and used valid JSON?")
|
||||
except json.JSONDecodeError:
|
||||
return HttpResponseBadRequest("Malformed json.")
|
||||
return HttpResponse(f"received {obj}")
|
||||
elif request.method == "PATCH":
|
||||
if not request.META["CONTENT_TYPE"] == 'application/json':
|
||||
HttpResponseBadRequest('Request must be JSON.')
|
||||
return HttpResponse(f"received {obj}")
|
||||
return HttpResponseBadRequest("Invalid JSON.")
|
||||
except django.db.utils.DataError:
|
||||
return HttpResponseBadRequest("One of your objects has fields that do not fit in their corresponding fields in the database. Did you comply to all data type requirements?")
|
||||
return HttpResponse(f"OK. {new} new objects, {modified} replaced.") if request.method == "PUT" else HttpResponse(f"OK. {'Patched' if request.method == 'PATCH' else 'Deleted'} {modified} objects.")
|
||||
elif request.method == "GET":
|
||||
try:
|
||||
pks = get_pks_from_get(request.GET)
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user