fixed crud list
fixed payments upload -a
This commit is contained in:
+1
-1
@@ -3,5 +3,5 @@ from django.urls import path
|
||||
from damages_api.views import Damages
|
||||
|
||||
urlpatterns = [
|
||||
path('<int:depot_id>', Damages.as_view(), name='damages_list'),
|
||||
path('<int:depot_id>/', Damages.as_view(), name='damages_list'),
|
||||
]
|
||||
+19
-10
@@ -9,34 +9,43 @@ from rest_framework.response import Response
|
||||
from rest_framework.views import APIView
|
||||
|
||||
from common.utils.owncloud_utls import Owncloud
|
||||
from containers.models import ContainerPhotos
|
||||
from containers.models import ContainerPhotos, Container
|
||||
|
||||
|
||||
class Damages(APIView):
|
||||
|
||||
def post(self, request, depot_id):
|
||||
data = request.get_json()
|
||||
photo = data.pop("photo")
|
||||
extension = data.pop("photo_extension")
|
||||
photo = request.data.get("photo")
|
||||
extension = request.data.get("photo_extension")
|
||||
|
||||
with tempfile.NamedTemporaryFile(suffix=f'.{extension}', delete=False) as temp_file:
|
||||
try:
|
||||
temp_file.write(base64.b64decode(photo.encode("utf-8")))
|
||||
temp_file.flush()
|
||||
temp_file.close() # Close the file before uploading
|
||||
|
||||
own_filename = Owncloud.upload_damage_photo(temp_file.name, depot_id)
|
||||
|
||||
container_photo = ContainerPhotos()
|
||||
container_photo.container.pk = depot_id
|
||||
container_photo.container = Container.objects.get(pk=depot_id)
|
||||
container_photo.photo = own_filename
|
||||
container_photo.uploaded_on = datetime.now()
|
||||
container_photo.uploaded_by = request.user
|
||||
container_photo.save()
|
||||
except Exception as ex:
|
||||
raise BadRequest("Invalid photo encoding")
|
||||
finally:
|
||||
os.unlink(temp_file.name)
|
||||
return Response(status=status.HTTP_201_CREATED)
|
||||
return Response(status=status.HTTP_201_CREATED)
|
||||
|
||||
except Exception as ex:
|
||||
return Response(
|
||||
{"error": "Failed to process photo"},
|
||||
status=status.HTTP_400_BAD_REQUEST
|
||||
)
|
||||
|
||||
finally:
|
||||
if temp_file:
|
||||
try:
|
||||
os.unlink(temp_file.name)
|
||||
except OSError:
|
||||
pass # Ignore deletion errors
|
||||
def get(self, request, depot_id):
|
||||
return Owncloud.get_damages(depot_id)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user