payments
This commit is contained in:
@@ -0,0 +1,3 @@
|
||||
from django.contrib import admin
|
||||
|
||||
# Register your models here.
|
||||
@@ -0,0 +1,6 @@
|
||||
from django.apps import AppConfig
|
||||
|
||||
|
||||
class DamagesApiConfig(AppConfig):
|
||||
default_auto_field = "django.db.models.BigAutoField"
|
||||
name = "damages_api"
|
||||
@@ -0,0 +1,3 @@
|
||||
from django.db import models
|
||||
|
||||
# Create your models here.
|
||||
@@ -0,0 +1,3 @@
|
||||
from django.test import TestCase
|
||||
|
||||
# Create your tests here.
|
||||
@@ -0,0 +1,7 @@
|
||||
from django.urls import path
|
||||
|
||||
from damages_api.views import Damages
|
||||
|
||||
urlpatterns = [
|
||||
path('<int:depot_id>', Damages.as_view(), name='damages_list'),
|
||||
]
|
||||
@@ -0,0 +1,43 @@
|
||||
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)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user