최지우

add settings api

from django.contrib import admin
from .models import Video, Device, History, Lock
from .models import Video, Device, History, Lock, Record, Door
# Register your models here.
admin.site.register(Door)
admin.site.register(Video)
admin.site.register(Device)
admin.site.register(History)
admin.site.register(Lock)
admin.site.register(Record)
\ No newline at end of file
......
......@@ -3,19 +3,26 @@ from django.conf import settings
from django.utils import timezone
# Create your models here.
class Device(models.Model) :
username = models.CharField(max_length = 255)
and_id = models.CharField(max_length = 255, primary_key = True)
class Door(models.Model) :
door_id = models.CharField(max_length = 255, primary_key = True)
class Device(models.Model) :
rfid_id = models.CharField(max_length = 255, primary_key = True)
created = models.DateTimeField(default = timezone.now)
class Video(models.Model) :
vidname = models.CharField(max_length = 255, primary_key = True)
vid_id = models.IntegerField(primary_key = True)
created = models.DateTimeField(default = timezone.now)
s3_link = models.CharField(max_length = 255)
class Lock(models.Model) :
door = models.ForeignKey(Door, on_delete=models.CASCADE)
state = models.BooleanField(default = True)
class History(models.Model) :
device = models.ForeignKey(Device, on_delete = models.CASCADE)
ctrtime =models.DateTimeField(default = timezone.now)
\ No newline at end of file
device_name = models.CharField(max_length = 255)
ctrtime = models.DateTimeField(default = timezone.now)
class Record(models.Model) :
door = models.ForeignKey(Door, on_delete=models.CASCADE)
recording = models.BooleanField(default = True)
\ No newline at end of file
......
from api.models import Device, Video, History
from api.models import Device, Video, Lock, History, Record, Door
from rest_framework import serializers
......@@ -16,3 +16,8 @@ class HistorySerializer(serializers.ModelSerializer) :
class Meta :
model = History
fields = '__all__'
class RecordSerializer(serializers.ModelSerializer) :
class Meta :
model = Record
fields = '__all__'
\ No newline at end of file
......
......@@ -4,8 +4,8 @@ from django.core import serializers
from django.core.exceptions import FieldDoesNotExist, ObjectDoesNotExist
from django.shortcuts import render
from api.models import Video, Device, History, Lock
from api.serializers import VideoSerializer, DeviceSerializer, HistorySerializer
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
......@@ -20,18 +20,52 @@ from src.settings import S3_ACCESS_KEY_ID, S3_SECRET_ACCESS_KEY, S3_STORAGE_BUCK
from datetime import datetime, timedelta
# Create your views here.
class Video(APIView) :
# 비디오 목록 조회
class VideoList(APIView) :
def get(self, request, format = None) :
'''
request_id = request.GET.get('last_id') # requst의 last_id 받아옴
queryset = Video.objects.filter(vid_id__range = (request_id, request_id + 10)) # 쿼리셋 필터로 vid_id의 범위가 last_id ~ las_id + 10인 객체 찾기
'''
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_id, format = None) : # request URI에 vid_id가 포함되어있음 : api/video/{vid_id}
try :
request_id = vid_id
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_id = request_id)
s3.delete_object(Bucket = S3_STORAGE_BUCKET_NAME, Key = str(target.vid_id))
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_id, format = None) : # 요청한 URI에 vid_id가 포함되어있음 ex) api/video/1
try :
request_id = request.GET.get('vidname')
request_id = vid_id
if request_id == 'None' :
raise FieldDoesNotExist
download_url = S3_ACCESS_URL + str(request_id)
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({
......@@ -44,24 +78,7 @@ class Video(APIView) :
'date' : datetime.now()
}, status = status.HTTP_404_NOT_FOUND)
def delete(self, request, format = None) :
try :
request_id = request.GET.get('vidname')
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(vidname = request_id)
s3.delete_object(Bucket = S3_STORAGE_BUCKET_NAME, Key = str(target.vidname))
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 CheckDate(APIView) :
def delete(self, request, format = None) :
checkdate = datetime.now() + timedelta(days = -7)
......@@ -69,8 +86,40 @@ class CheckDate(APIView) :
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.vidname))
s3.delete_object(Bucket = S3_STORAGE_BUCKET_NAME, Key = str(delvid.vid_id))
quaryset.delete()
return Response(status = status.HTTP_200_OK)
# 비디오 녹화 설정 조회/변경
class Recording(APIView) :
def get(self, request, format = None) :
try :
'''
request_id = request.GET.get('door_id')
target = Record.objects.filter(door_id = request_id)
'''
target = Record.objects.all()
serializer = RecordSerializer(target, many = True)
res = {
'recording' : serializer.data
}
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 :
request_id = request.GET.get('door_id')
if not request_id :
raise FieldDoesNotExist
target = Record.objects.filter(door_id = request_id)
target.update(recording = request.data['recording'])
return Response(status = status.HTTP_200_OK)
except FieldDoesNotExist as error :
return Response({
'error' : "FieldDoesNotExist ",
'date' : datetime.now()
}, status = status.HTTP_400_BAD_REQUEST)
......
......@@ -20,6 +20,8 @@ from api import views
urlpatterns = [
path('admin/', admin.site.urls),
path('video/<str:vidname>/', views.Video.as_view()),
path('checkDate/', views.CheckDate.as_view()),
path('api/video/', views.VideoList.as_view()),
path('api/video/<int:vid_id>/', views.VideoDownload.as_view()),
path('auto/checkDate/', views.CheckDate.as_view()),
path('api/setting/', views.Recording.as_view()),
]
......