views.py
4.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import boto3
import botocore
import threading
from django.http import HttpResponse
from django.core import serializers
from django.core.exceptions import FieldDoesNotExist, ObjectDoesNotExist
from django.shortcuts import render
from api.videorecord import record
from api.models import Video, Device, History, Lock, Record, Door
from api.serializers import VideoSerializer, DeviceSerializer, HistorySerializer, RecordSerializer
from rest_framework import status
from rest_framework.views import APIView
from rest_framework.request import Request
from rest_framework.response import Response
"""
from boto3.session import Session
from src.settings import AWS_REGION
from src.settings import S3_ACCESS_URL
from src.settings import S3_ACCESS_KEY_ID, S3_SECRET_ACCESS_KEY, S3_STORAGE_BUCKET_NAME
"""
import time
from datetime import datetime, timedelta
# Create your views here.
# 비디오 목록 조회
class VideoList(APIView) :
def get(self, request, format = None) :
queryset = Video.objects.all()
serializer = VideoSerializer(queryset, many = True)
res = {
'videoList': serializer.data
} # 응답코드에 포함될 데이터
return Response(res, status = status.HTTP_200_OK)
# 비디오 수동 삭제
def delete(self, request, vid_name, format = None) : # request URI에 vid_name가 포함되어있음 : api/video/{vid_name}
try :
request_id = vid_name
if request_id == 'None' :
raise FieldDoesNotExist
session = boto3.session.Session(aws_access_key_id = S3_ACCESS_KEY_ID, aws_secret_access_key = S3_SECRET_ACCESS_KEY, region_name = AWS_REGION)
s3 = session.client('s3')
target = Video.objects.get(vid_name = request_id)
s3.delete_object(Bucket = S3_STORAGE_BUCKET_NAME, Key = str(target.vid_name))
target.delete()
return Response(status = status.HTTP_200_OK)
except FieldDoesNotExist as error :
return Response({
'error' : "FieldDoesNotExist ",
'date' : datetime.now()
}, status = status.HTTP_400_BAD_REQUEST)
# 비디오 확인(다운로드)
class VideoDownload(APIView) :
def get(self, request, vid_name, format = None) : # 요청한 URI에 vid_name가 포함되어있음
try :
request_id = vid_name
if request_id == 'None' :
raise FieldDoesNotExist
download_url = S3_ACCESS_URL + str(request_id) # S3 다운로드 링크 변환
if not download_url :
raise ObjectDoesNotExist
res = {
's3_link' : download_url
} # 응답 코드에 보낼 데이터
return Response(res, status = status.HTTP_200_OK)
except FieldDoesNotExist as error :
return Response({
'error' : "FieldDoesNotExist ",
'date' : datetime.now()
}, status = status.HTTP_400_BAD_REQUEST)
except ObjectDoesNotExist as error :
return Response({
'error' : "ObjectDoesNotExist",
'date' : datetime.now()
}, status = status.HTTP_404_NOT_FOUND)
# 비디오 자동 삭제
class CheckDate(APIView) :
def delete(self, request, format = None) :
checkdate = datetime.now() + timedelta(days = -7)
quaryset = Video.objects.filter(created__lt = checkdate)
session = boto3.session.Session(aws_access_key_id = S3_ACCESS_KEY_ID, aws_secret_access_key = S3_SECRET_ACCESS_KEY, region_name = AWS_REGION)
s3 = session.client('s3')
for delvid in quaryset :
s3.delete_object(Bucket = S3_STORAGE_BUCKET_NAME, Key = str(delvid.vid_name))
quaryset.delete()
return Response(status = status.HTTP_200_OK)
# 비디오 녹화 설정 조회/변경
class Recording(APIView) :
def get(self, request, format = None) :
try :
target = Record.objects.get(id = 1)
serializer = RecordSerializer(target, many = False)
res = {
'recording' : serializer.data['recording']
}
return Response(res, status = status.HTTP_200_OK)
except FieldDoesNotExist as error :
return Response({
'error' : "FieldDoesNotExist ",
'date' : datetime.now()
}, status = status.HTTP_400_BAD_REQUEST)
def put(self, request, format = None) :
try :
target = Record.objects.filter(id = 1)
target.update(recording = request.data['recording'])
if request.data['recording'] :
threading.Thread(target=record).start()
return Response(status = status.HTTP_200_OK)
except FieldDoesNotExist as error :
return Response({
'error' : "FieldDoesNotExist ",
'date' : datetime.now()
}, status = status.HTTP_400_BAD_REQUEST)