최지우

merge request

1 from django.contrib import admin 1 from django.contrib import admin
2 -from .models import Video, Device, History, Lock, Record, Door 2 +from .models import Video, Device, RemoteHistory, Lock, Record, Door
3 # Register your models here. 3 # Register your models here.
4 admin.site.register(Door) 4 admin.site.register(Door)
5 admin.site.register(Video) 5 admin.site.register(Video)
6 admin.site.register(Device) 6 admin.site.register(Device)
7 -admin.site.register(History) 7 +admin.site.register(RemoteHistory)
8 admin.site.register(Lock) 8 admin.site.register(Lock)
9 admin.site.register(Record) 9 admin.site.register(Record)
...\ No newline at end of file ...\ No newline at end of file
......
1 -# Generated by Django 3.1.2 on 2020-11-18 11:21
2 -
3 -from django.db import migrations, models
4 -import django.utils.timezone
5 -
6 -
7 -class Migration(migrations.Migration):
8 -
9 - initial = True
10 -
11 - dependencies = [
12 - ]
13 -
14 - operations = [
15 - migrations.CreateModel(
16 - name='Device',
17 - fields=[
18 - ('rfid_id', models.CharField(max_length=255, primary_key=True, serialize=False)),
19 - ('created', models.DateTimeField(default=django.utils.timezone.now)),
20 - ],
21 - ),
22 - migrations.CreateModel(
23 - name='Door',
24 - fields=[
25 - ('door_id', models.CharField(max_length=255, primary_key=True, serialize=False)),
26 - ],
27 - ),
28 - migrations.CreateModel(
29 - name='History',
30 - fields=[
31 - ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
32 - ('device_name', models.CharField(max_length=255)),
33 - ('ctrtime', models.DateTimeField(default=django.utils.timezone.now)),
34 - ],
35 - ),
36 - migrations.CreateModel(
37 - name='Lock',
38 - fields=[
39 - ('id', models.IntegerField(primary_key=True, serialize=False)),
40 - ('state', models.BooleanField(default=True)),
41 - ],
42 - ),
43 - migrations.CreateModel(
44 - name='Record',
45 - fields=[
46 - ('id', models.IntegerField(primary_key=True, serialize=False)),
47 - ('recording', models.BooleanField(default=True)),
48 - ],
49 - ),
50 - migrations.CreateModel(
51 - name='Video',
52 - fields=[
53 - ('vid_name', models.CharField(max_length=255, primary_key=True, serialize=False)),
54 - ('created', models.DateTimeField(default=django.utils.timezone.now)),
55 - ('vid_time', models.CharField(max_length=255)),
56 - ('thumb', models.CharField(max_length=255)),
57 - ],
58 - ),
59 - ]
1 from django.db import models 1 from django.db import models
2 from django.conf import settings 2 from django.conf import settings
3 from django.utils import timezone 3 from django.utils import timezone
4 +from django.contrib.auth.models import User
4 5
5 # Create your models here. 6 # Create your models here.
6 class Door(models.Model) : 7 class Door(models.Model) :
7 door_id = models.CharField(max_length = 255, primary_key = True) 8 door_id = models.CharField(max_length = 255, primary_key = True)
8 9
9 class Device(models.Model) : 10 class Device(models.Model) :
10 - rfid_id = models.CharField(max_length = 255, primary_key = True) 11 + device_id = models.AutoField(primary_key=True)
12 + rfid_id = models.CharField(max_length = 255)
11 created = models.DateTimeField(default = timezone.now) 13 created = models.DateTimeField(default = timezone.now)
12 14
13 class Video(models.Model) : 15 class Video(models.Model) :
...@@ -20,9 +22,9 @@ class Lock(models.Model) : ...@@ -20,9 +22,9 @@ class Lock(models.Model) :
20 id = models.IntegerField(primary_key = True) 22 id = models.IntegerField(primary_key = True)
21 state = models.BooleanField(default = True) 23 state = models.BooleanField(default = True)
22 24
23 -class History(models.Model) : 25 +class RemoteHistory(models.Model) :
24 device_name = models.CharField(max_length = 255) 26 device_name = models.CharField(max_length = 255)
25 - ctrtime = models.DateTimeField(default = timezone.now) 27 + created = models.DateTimeField(default = timezone.now)
26 28
27 class Record(models.Model) : 29 class Record(models.Model) :
28 id = models.IntegerField(primary_key = True) 30 id = models.IntegerField(primary_key = True)
......
1 -from api.models import Device, Video, Lock, History, Record, Door, AddDevice 1 +from api.models import Device, Video, Lock, RemoteHistory, Record, Door, AddDevice
2 from rest_framework import serializers 2 from rest_framework import serializers
3 3
4 class DoorSerializer(serializers.ModelSerializer) : 4 class DoorSerializer(serializers.ModelSerializer) :
...@@ -16,9 +16,9 @@ class VideoSerializer(serializers.ModelSerializer) : ...@@ -16,9 +16,9 @@ class VideoSerializer(serializers.ModelSerializer) :
16 model = Video 16 model = Video
17 fields = '__all__' 17 fields = '__all__'
18 18
19 -class HistorySerializer(serializers.ModelSerializer) : 19 +class RemoteHistorySerializer(serializers.ModelSerializer) :
20 class Meta : 20 class Meta :
21 - model = History 21 + model = RemoteHistory
22 fields = '__all__' 22 fields = '__all__'
23 23
24 class RecordSerializer(serializers.ModelSerializer) : 24 class RecordSerializer(serializers.ModelSerializer) :
......
...@@ -5,15 +5,18 @@ from django.http import HttpResponse ...@@ -5,15 +5,18 @@ from django.http import HttpResponse
5 from django.core import serializers 5 from django.core import serializers
6 from django.core.exceptions import FieldDoesNotExist, ObjectDoesNotExist 6 from django.core.exceptions import FieldDoesNotExist, ObjectDoesNotExist
7 from django.shortcuts import render 7 from django.shortcuts import render
8 +from django.contrib.auth.models import User
8 9
9 from api.videorecord import record 10 from api.videorecord import record
10 -from api.models import Video, Device, History, Lock, Record, Door, AddDevice 11 +from api.models import Video, Device, RemoteHistory, Lock, Record, Door, AddDevice
11 -from api.serializers import VideoSerializer, DeviceSerializer, HistorySerializer, RecordSerializer 12 +from api.serializers import VideoSerializer, DeviceSerializer, RemoteHistorySerializer, RecordSerializer
12 13
13 from rest_framework import status 14 from rest_framework import status
14 from rest_framework.views import APIView 15 from rest_framework.views import APIView
15 from rest_framework.request import Request 16 from rest_framework.request import Request
16 from rest_framework.response import Response 17 from rest_framework.response import Response
18 +from rest_framework.authtoken.models import Token
19 +
17 """ 20 """
18 from boto3.session import Session 21 from boto3.session import Session
19 from src.settings import AWS_REGION 22 from src.settings import AWS_REGION
...@@ -22,8 +25,121 @@ from src.settings import S3_ACCESS_KEY_ID, S3_SECRET_ACCESS_KEY, S3_STORAGE_BUCK ...@@ -22,8 +25,121 @@ from src.settings import S3_ACCESS_KEY_ID, S3_SECRET_ACCESS_KEY, S3_STORAGE_BUCK
22 """ 25 """
23 import time 26 import time
24 from datetime import datetime, timedelta 27 from datetime import datetime, timedelta
28 +import json
29 +import uuid
25 # Create your views here. 30 # Create your views here.
26 31
32 +#로그인 및 토큰 반환
33 +class Login(APIView) :
34 +
35 + def get(self, request, format = None) : # request query에 door_id 포함되어있음 : api/auth?door_id=12345
36 + try :
37 + request_id = request.GET.get('door_id', None)
38 + if request_id == None :
39 + raise FieldDoesNotExist
40 + queryset = Door.objects.filter(door_id = request_id) # door_id 유효성 검색
41 + if queryset.exists() :# 유효할 때
42 + userid = uuid.uuid4()
43 + pw = uuid.uuid4()
44 + user = User.objects.create_user(username=str(userid), password=str(pw))
45 + token = Token.objects.create(user=user)
46 + res = {
47 + 'is_available' : True,
48 + 'access_token' : token.key
49 + }
50 + else :
51 + res = {
52 + 'is_available' : False
53 + }
54 +
55 + return Response(res, status = status.HTTP_200_OK)
56 +
57 + except FieldDoesNotExist as error :
58 + return Response({
59 + 'error' : "FieldDoesNotExist ",
60 + 'date' : datetime.now()
61 + }, status = status.HTTP_400_BAD_REQUEST)
62 +
63 +
64 +
65 +
66 +'''
67 + def post(self, request, format = None) :
68 + queryset = Door.objects.create(door_id = 12345)
69 + return Response({
70 + 'msg' : 'doorid값 삽입 완료',
71 + })
72 +'''
73 +
74 +
75 +#기기 관련 api
76 +class Devices(APIView) :
77 + # 기기 목록 조회
78 + def get(self, request, format = None) :
79 + queryset = Device.objects.all()
80 + serializer = DeviceSerializer(queryset, many = True)
81 + res = {
82 + 'deviceList': serializer.data
83 + }
84 + return Response(res, status = status.HTTP_200_OK)
85 +
86 +
87 + # 기기 추가
88 + def post(self, request, format = None) : # request body에 rfid_id 포함되어있음 
89 + try :
90 + print('냐냐냐냐냐냐')
91 + print(request.body)
92 + data = json.loads(request.body)
93 + request_id = data.get('rfid_id', None)
94 + if request_id == None :
95 + raise FieldDoesNotExist
96 + queryset = Device.objects.create(rfid_id = request_id)
97 + return Response({
98 + 'msg' : 'success device add'
99 + })
100 +
101 + except FieldDoesNotExist as error :
102 + return Response({
103 + 'error' : "FieldDoesNotExist ",
104 + 'date' : datetime.now()
105 + }, status = status.HTTP_400_BAD_REQUEST)
106 +
107 +
108 +
109 + # 기기 삭제
110 + def delete(self, request, device_id, format = None): # request URI에 device_id(자동생성되는 기기 고유 번호 != rfid_id) 포함
111 + try :
112 + request_id = device_id
113 + if request_id == None:
114 + raise FieldDoesNotExist
115 + queryset = Device.objects.get(device_id=request_id)
116 + queryset.delete()
117 + return Response({
118 + 'msg' : 'success delete device'
119 + })
120 +
121 + except FieldDoesNotExist as error :
122 + return Response({
123 + 'error' : "FieldDoesNotExist ",
124 + 'date' : datetime.now()
125 + }, status = status.HTTP_400_BAD_REQUEST)
126 +
127 +# 원격 잠금 해제
128 +class Remote(APIView):
129 + # 원격 잠금 해제 기록 조회
130 + def get(self, request, format = None) :
131 + #models.py의 class History 사용.
132 + queryset = RemoteHistory.objects.all()
133 + serializer = RemoteHistorySerializer(queryset, many = True)
134 + res = {
135 + "remoteHistoryList": serializer.data
136 + }
137 + return Response(res, status = status.HTTP_200_OK)
138 +
139 +
140 +
141 +
142 +
27 # 비디오 목록 조회 143 # 비디오 목록 조회
28 class VideoList(APIView) : 144 class VideoList(APIView) :
29 def get(self, request, format = None) : 145 def get(self, request, format = None) :
......
...@@ -41,6 +41,7 @@ INSTALLED_APPS = [ ...@@ -41,6 +41,7 @@ INSTALLED_APPS = [
41 'api', 41 'api',
42 'rest_framework', 42 'rest_framework',
43 'corsheaders', 43 'corsheaders',
44 + 'rest_framework.authtoken',
44 ] 45 ]
45 46
46 MIDDLEWARE = [ 47 MIDDLEWARE = [
......
...@@ -24,4 +24,8 @@ urlpatterns = [ ...@@ -24,4 +24,8 @@ urlpatterns = [
24 path('api/video/<str:vid_name>', views.VideoDownload.as_view()), 24 path('api/video/<str:vid_name>', views.VideoDownload.as_view()),
25 path('auto/checkDate', views.CheckDate.as_view()), 25 path('auto/checkDate', views.CheckDate.as_view()),
26 path('api/setting', views.Recording.as_view()), 26 path('api/setting', views.Recording.as_view()),
27 + path('api/auth', views.Login.as_view()),
28 + path('api/device', views.Devices.as_view()),
29 + path('api/device/<str:device_id>', views.Devices.as_view()),
30 + path('api/remote', views.Remote.as_view()),
27 ] 31 ]
......