Fix bug where deletes failed with the wrong status code

sync
Laurent 9 months ago
parent a219cf1828
commit 269ed59e2a
  1. 23
      sync/views.py

@ -2,7 +2,6 @@
import re
import json
from .serializers import DataAccessSerializer
from django.db.models import Q
from rest_framework import viewsets
from rest_framework.views import APIView
@ -10,9 +9,10 @@ from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response
from rest_framework import status
# from django.apps import apps
from django.utils import timezone
from django.core.exceptions import ObjectDoesNotExist
from django.db import transaction
from django.db.models import Q
from collections import defaultdict
from urllib.parse import unquote
@ -101,12 +101,11 @@ class SynchronizationApi(HierarchyApiView):
for op in operations:
result = None
message = None
try:
model_operation = op.get('operation')
model_name = op.get('model_name')
data = op.get('data')
print(f'{model_operation} : {model_name}')
try:
print(f'{model_operation} : {model_name}, id = {data['id']}')
models.add(model_name)
@ -148,19 +147,19 @@ class SynchronizationApi(HierarchyApiView):
try:
instance = get_data(model_name, data_id)
instance._device_id = device_id
try:
instance.delete()
response_status = status.HTTP_204_NO_CONTENT
except model.DoesNotExist: # POST
except model.DoesNotExist: # a relationship was not found, not good
response_status = status.HTTP_400_BAD_REQUEST
except model.DoesNotExist: # the data was not found, it's ok
response_status = status.HTTP_208_ALREADY_REPORTED
# delete = ModelLog.objects.filter(model_id=data_id, operation=model_operation).first()
# if delete:
# response_status = status.HTTP_208_ALREADY_REPORTED
# else:
# response_status = status.HTTP_404_NOT_FOUND
print(f'{model_name} DoesNotExist, id: {data_id}')
except Exception as e:
response_status = status.HTTP_400_BAD_REQUEST
print(f'other exception: {str(e)}, type: {type(e)}, args: {e.args}, traceback: {e.__traceback__}')
print(f'OTHER exception on {model_operation} {model_name} : {str(e)}, type: {type(e)}, id: {data.get('id')}')
message = str(e)
results.append({

Loading…
Cancel
Save