You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

44 lines
1.4 KiB
Python

import base64
import os
import tempfile
from datetime import datetime
from django.core.exceptions import BadRequest
from rest_framework import status
from rest_framework.response import Response
from rest_framework.views import APIView
from common.utils.owncloud_utls import Owncloud
from containers.models import ContainerPhotos
class Damages(APIView):
def post(self, request, depot_id):
data = request.get_json()
photo = data.pop("photo")
extension = data.pop("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()
own_filename = Owncloud.upload_damage_photo(temp_file.name, depot_id)
container_photo = ContainerPhotos()
container_photo.container.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)
def get(self, request, depot_id):
return Owncloud.get_damages(depot_id)