Showing
5 changed files
with
101 additions
and
36 deletions
1 | from django.contrib import admin | 1 | from django.contrib import admin |
2 | -from .models import Video, Device, History, Lock | 2 | +from .models import Video, Device, History, Lock, Record, Door |
3 | # Register your models here. | 3 | # Register your models here. |
4 | +admin.site.register(Door) | ||
4 | admin.site.register(Video) | 5 | admin.site.register(Video) |
5 | admin.site.register(Device) | 6 | admin.site.register(Device) |
6 | admin.site.register(History) | 7 | admin.site.register(History) |
7 | -admin.site.register(Lock) | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
8 | +admin.site.register(Lock) | ||
9 | +admin.site.register(Record) | ||
... | \ No newline at end of file | ... | \ No newline at end of file | ... | ... |
... | @@ -3,19 +3,26 @@ from django.conf import settings | ... | @@ -3,19 +3,26 @@ from django.conf import settings |
3 | from django.utils import timezone | 3 | from django.utils import timezone |
4 | 4 | ||
5 | # Create your models here. | 5 | # Create your models here. |
6 | -class Device(models.Model) : | 6 | +class Door(models.Model) : |
7 | - username = models.CharField(max_length = 255) | 7 | + door_id = models.CharField(max_length = 255, primary_key = True) |
8 | - and_id = models.CharField(max_length = 255, primary_key = True) | ||
9 | 8 | ||
9 | +class Device(models.Model) : | ||
10 | + rfid_id = models.CharField(max_length = 255, primary_key = True) | ||
11 | + created = models.DateTimeField(default = timezone.now) | ||
10 | 12 | ||
11 | class Video(models.Model) : | 13 | class Video(models.Model) : |
12 | - vidname = models.CharField(max_length = 255, primary_key = True) | 14 | + vid_id = models.IntegerField(primary_key = True) |
13 | created = models.DateTimeField(default = timezone.now) | 15 | created = models.DateTimeField(default = timezone.now) |
14 | s3_link = models.CharField(max_length = 255) | 16 | s3_link = models.CharField(max_length = 255) |
15 | 17 | ||
16 | class Lock(models.Model) : | 18 | class Lock(models.Model) : |
19 | + door = models.ForeignKey(Door, on_delete=models.CASCADE) | ||
17 | state = models.BooleanField(default = True) | 20 | state = models.BooleanField(default = True) |
18 | 21 | ||
19 | class History(models.Model) : | 22 | class History(models.Model) : |
20 | - device = models.ForeignKey(Device, on_delete = models.CASCADE) | ||
21 | - ctrtime =models.DateTimeField(default = timezone.now) | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
23 | + device_name = models.CharField(max_length = 255) | ||
24 | + ctrtime = models.DateTimeField(default = timezone.now) | ||
25 | + | ||
26 | +class Record(models.Model) : | ||
27 | + door = models.ForeignKey(Door, on_delete=models.CASCADE) | ||
28 | + recording = models.BooleanField(default = True) | ||
... | \ No newline at end of file | ... | \ No newline at end of file | ... | ... |
1 | -from api.models import Device, Video, History | 1 | +from api.models import Device, Video, Lock, History, Record, Door |
2 | from rest_framework import serializers | 2 | from rest_framework import serializers |
3 | 3 | ||
4 | 4 | ||
... | @@ -15,4 +15,9 @@ class VideoSerializer(serializers.ModelSerializer) : | ... | @@ -15,4 +15,9 @@ class VideoSerializer(serializers.ModelSerializer) : |
15 | class HistorySerializer(serializers.ModelSerializer) : | 15 | class HistorySerializer(serializers.ModelSerializer) : |
16 | class Meta : | 16 | class Meta : |
17 | model = History | 17 | model = History |
18 | + fields = '__all__' | ||
19 | + | ||
20 | +class RecordSerializer(serializers.ModelSerializer) : | ||
21 | + class Meta : | ||
22 | + model = Record | ||
18 | fields = '__all__' | 23 | fields = '__all__' |
... | \ No newline at end of file | ... | \ No newline at end of file | ... | ... |
... | @@ -4,8 +4,8 @@ from django.core import serializers | ... | @@ -4,8 +4,8 @@ from django.core import serializers |
4 | from django.core.exceptions import FieldDoesNotExist, ObjectDoesNotExist | 4 | from django.core.exceptions import FieldDoesNotExist, ObjectDoesNotExist |
5 | from django.shortcuts import render | 5 | from django.shortcuts import render |
6 | 6 | ||
7 | -from api.models import Video, Device, History, Lock | 7 | +from api.models import Video, Device, History, Lock, Record, Door |
8 | -from api.serializers import VideoSerializer, DeviceSerializer, HistorySerializer | 8 | +from api.serializers import VideoSerializer, DeviceSerializer, HistorySerializer, RecordSerializer |
9 | 9 | ||
10 | from rest_framework import status | 10 | from rest_framework import status |
11 | from rest_framework.views import APIView | 11 | 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 | ... | @@ -20,18 +20,52 @@ from src.settings import S3_ACCESS_KEY_ID, S3_SECRET_ACCESS_KEY, S3_STORAGE_BUCK |
20 | from datetime import datetime, timedelta | 20 | from datetime import datetime, timedelta |
21 | # Create your views here. | 21 | # Create your views here. |
22 | 22 | ||
23 | -class Video(APIView) : | 23 | +# 비디오 목록 조회 |
24 | +class VideoList(APIView) : | ||
24 | def get(self, request, format = None) : | 25 | def get(self, request, format = None) : |
26 | + ''' | ||
27 | + request_id = request.GET.get('last_id') # requst의 last_id 받아옴 | ||
28 | + queryset = Video.objects.filter(vid_id__range = (request_id, request_id + 10)) # 쿼리셋 필터로 vid_id의 범위가 last_id ~ las_id + 10인 객체 찾기 | ||
29 | + ''' | ||
30 | + queryset = Video.objects.all() | ||
31 | + serializer = VideoSerializer(queryset, many = True) | ||
32 | + res = { | ||
33 | + 'videoList': serializer.data | ||
34 | + } # 응답코드에 포함될 데이터 | ||
35 | + return Response(res, status = status.HTTP_200_OK) | ||
36 | + | ||
37 | +# 비디오 수동 삭제 | ||
38 | + def delete(self, request, vid_id, format = None) : # request URI에 vid_id가 포함되어있음 : api/video/{vid_id} | ||
39 | + try : | ||
40 | + request_id = vid_id | ||
41 | + if request_id == 'None' : | ||
42 | + raise FieldDoesNotExist | ||
43 | + session = boto3.session.Session(aws_access_key_id = S3_ACCESS_KEY_ID, aws_secret_access_key = S3_SECRET_ACCESS_KEY, region_name = AWS_REGION) | ||
44 | + s3 = session.client('s3') | ||
45 | + | ||
46 | + target = Video.objects.get(vid_id = request_id) | ||
47 | + s3.delete_object(Bucket = S3_STORAGE_BUCKET_NAME, Key = str(target.vid_id)) | ||
48 | + target.delete() | ||
49 | + return Response(status = status.HTTP_200_OK) | ||
50 | + except FieldDoesNotExist as error : | ||
51 | + return Response({ | ||
52 | + 'error' : "FieldDoesNotExist ", | ||
53 | + 'date' : datetime.now() | ||
54 | + }, status = status.HTTP_400_BAD_REQUEST) | ||
55 | + | ||
56 | +# 비디오 확인(다운로드) | ||
57 | +class VideoDownload(APIView) : | ||
58 | + def get(self, request, vid_id, format = None) : # 요청한 URI에 vid_id가 포함되어있음 ex) api/video/1 | ||
25 | try : | 59 | try : |
26 | - request_id = request.GET.get('vidname') | 60 | + request_id = vid_id |
27 | if request_id == 'None' : | 61 | if request_id == 'None' : |
28 | raise FieldDoesNotExist | 62 | raise FieldDoesNotExist |
29 | - download_url = S3_ACCESS_URL + str(request_id) | 63 | + download_url = S3_ACCESS_URL + str(request_id) # S3 다운로드 링크 변환 |
30 | if not download_url : | 64 | if not download_url : |
31 | raise ObjectDoesNotExist | 65 | raise ObjectDoesNotExist |
32 | res = { | 66 | res = { |
33 | 's3_link' : download_url | 67 | 's3_link' : download_url |
34 | - } | 68 | + } # 응답 코드에 보낼 데이터 |
35 | return Response(res, status = status.HTTP_200_OK) | 69 | return Response(res, status = status.HTTP_200_OK) |
36 | except FieldDoesNotExist as error : | 70 | except FieldDoesNotExist as error : |
37 | return Response({ | 71 | return Response({ |
... | @@ -44,33 +78,48 @@ class Video(APIView) : | ... | @@ -44,33 +78,48 @@ class Video(APIView) : |
44 | 'date' : datetime.now() | 78 | 'date' : datetime.now() |
45 | }, status = status.HTTP_404_NOT_FOUND) | 79 | }, status = status.HTTP_404_NOT_FOUND) |
46 | 80 | ||
81 | +# 비디오 자동 삭제 | ||
82 | +class CheckDate(APIView) : | ||
47 | def delete(self, request, format = None) : | 83 | def delete(self, request, format = None) : |
48 | - try : | 84 | + checkdate = datetime.now() + timedelta(days = -7) |
49 | - request_id = request.GET.get('vidname') | 85 | + quaryset = Video.objects.filter(created__lt = checkdate) |
50 | - if request_id == 'None' : | ||
51 | - raise FieldDoesNotExist | ||
52 | session = boto3.session.Session(aws_access_key_id = S3_ACCESS_KEY_ID, aws_secret_access_key = S3_SECRET_ACCESS_KEY, region_name = AWS_REGION) | 86 | session = boto3.session.Session(aws_access_key_id = S3_ACCESS_KEY_ID, aws_secret_access_key = S3_SECRET_ACCESS_KEY, region_name = AWS_REGION) |
53 | s3 = session.client('s3') | 87 | s3 = session.client('s3') |
54 | - | 88 | + for delvid in quaryset : |
55 | - target = Video.objects.get(vidname = request_id) | 89 | + s3.delete_object(Bucket = S3_STORAGE_BUCKET_NAME, Key = str(delvid.vid_id)) |
56 | - s3.delete_object(Bucket = S3_STORAGE_BUCKET_NAME, Key = str(target.vidname)) | 90 | + quaryset.delete() |
57 | - target.delete() | ||
58 | return Response(status = status.HTTP_200_OK) | 91 | return Response(status = status.HTTP_200_OK) |
92 | + | ||
93 | +# 비디오 녹화 설정 조회/변경 | ||
94 | +class Recording(APIView) : | ||
95 | + def get(self, request, format = None) : | ||
96 | + try : | ||
97 | + ''' | ||
98 | + request_id = request.GET.get('door_id') | ||
99 | + target = Record.objects.filter(door_id = request_id) | ||
100 | + ''' | ||
101 | + target = Record.objects.all() | ||
102 | + serializer = RecordSerializer(target, many = True) | ||
103 | + res = { | ||
104 | + 'recording' : serializer.data | ||
105 | + } | ||
106 | + return Response(res, status = status.HTTP_200_OK) | ||
59 | except FieldDoesNotExist as error : | 107 | except FieldDoesNotExist as error : |
60 | return Response({ | 108 | return Response({ |
61 | 'error' : "FieldDoesNotExist ", | 109 | 'error' : "FieldDoesNotExist ", |
62 | 'date' : datetime.now() | 110 | 'date' : datetime.now() |
63 | }, status = status.HTTP_400_BAD_REQUEST) | 111 | }, status = status.HTTP_400_BAD_REQUEST) |
64 | 112 | ||
65 | -class CheckDate(APIView) : | 113 | + def put(self, request, format = None) : |
66 | - def delete(self, request, format = None) : | 114 | + try : |
67 | - checkdate = datetime.now() + timedelta(days = -7) | 115 | + request_id = request.GET.get('door_id') |
68 | - quaryset = Video.objects.filter(created__lt = checkdate) | 116 | + if not request_id : |
69 | - session = boto3.session.Session(aws_access_key_id = S3_ACCESS_KEY_ID, aws_secret_access_key = S3_SECRET_ACCESS_KEY, region_name = AWS_REGION) | 117 | + raise FieldDoesNotExist |
70 | - s3 = session.client('s3') | 118 | + target = Record.objects.filter(door_id = request_id) |
71 | - for delvid in quaryset : | 119 | + target.update(recording = request.data['recording']) |
72 | - s3.delete_object(Bucket = S3_STORAGE_BUCKET_NAME, Key = str(delvid.vidname)) | ||
73 | - quaryset.delete() | ||
74 | return Response(status = status.HTTP_200_OK) | 120 | return Response(status = status.HTTP_200_OK) |
75 | - | 121 | + except FieldDoesNotExist as error : |
76 | - | 122 | + return Response({ |
123 | + 'error' : "FieldDoesNotExist ", | ||
124 | + 'date' : datetime.now() | ||
125 | + }, status = status.HTTP_400_BAD_REQUEST) | ... | ... |
... | @@ -20,6 +20,8 @@ from api import views | ... | @@ -20,6 +20,8 @@ from api import views |
20 | 20 | ||
21 | urlpatterns = [ | 21 | urlpatterns = [ |
22 | path('admin/', admin.site.urls), | 22 | path('admin/', admin.site.urls), |
23 | - path('video/<str:vidname>/', views.Video.as_view()), | 23 | + path('api/video/', views.VideoList.as_view()), |
24 | - path('checkDate/', views.CheckDate.as_view()), | 24 | + path('api/video/<int:vid_id>/', views.VideoDownload.as_view()), |
25 | + path('auto/checkDate/', views.CheckDate.as_view()), | ||
26 | + path('api/setting/', views.Recording.as_view()), | ||
25 | ] | 27 | ] | ... | ... |
-
Please register or login to post a comment