최지우

merge request

from django.contrib import admin
from .models import Video, Device, History, Lock, Record, Door
from .models import Video, Device, RemoteHistory, 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(RemoteHistory)
admin.site.register(Lock)
admin.site.register(Record)
\ No newline at end of file
......
# Generated by Django 3.1.2 on 2020-11-18 11:21
from django.db import migrations, models
import django.utils.timezone
class Migration(migrations.Migration):
initial = True
dependencies = [
]
operations = [
migrations.CreateModel(
name='Device',
fields=[
('rfid_id', models.CharField(max_length=255, primary_key=True, serialize=False)),
('created', models.DateTimeField(default=django.utils.timezone.now)),
],
),
migrations.CreateModel(
name='Door',
fields=[
('door_id', models.CharField(max_length=255, primary_key=True, serialize=False)),
],
),
migrations.CreateModel(
name='History',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('device_name', models.CharField(max_length=255)),
('ctrtime', models.DateTimeField(default=django.utils.timezone.now)),
],
),
migrations.CreateModel(
name='Lock',
fields=[
('id', models.IntegerField(primary_key=True, serialize=False)),
('state', models.BooleanField(default=True)),
],
),
migrations.CreateModel(
name='Record',
fields=[
('id', models.IntegerField(primary_key=True, serialize=False)),
('recording', models.BooleanField(default=True)),
],
),
migrations.CreateModel(
name='Video',
fields=[
('vid_name', models.CharField(max_length=255, primary_key=True, serialize=False)),
('created', models.DateTimeField(default=django.utils.timezone.now)),
('vid_time', models.CharField(max_length=255)),
('thumb', models.CharField(max_length=255)),
],
),
]
from django.db import models
from django.conf import settings
from django.utils import timezone
from django.contrib.auth.models import User
# Create your models here.
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)
device_id = models.AutoField(primary_key=True)
rfid_id = models.CharField(max_length = 255)
created = models.DateTimeField(default = timezone.now)
class Video(models.Model) :
......@@ -20,9 +22,9 @@ class Lock(models.Model) :
id = models.IntegerField(primary_key = True)
state = models.BooleanField(default = True)
class History(models.Model) :
class RemoteHistory(models.Model) :
device_name = models.CharField(max_length = 255)
ctrtime = models.DateTimeField(default = timezone.now)
created = models.DateTimeField(default = timezone.now)
class Record(models.Model) :
id = models.IntegerField(primary_key = True)
......
from api.models import Device, Video, Lock, History, Record, Door, AddDevice
from api.models import Device, Video, Lock, RemoteHistory, Record, Door, AddDevice
from rest_framework import serializers
class DoorSerializer(serializers.ModelSerializer) :
......@@ -16,9 +16,9 @@ class VideoSerializer(serializers.ModelSerializer) :
model = Video
fields = '__all__'
class HistorySerializer(serializers.ModelSerializer) :
class RemoteHistorySerializer(serializers.ModelSerializer) :
class Meta :
model = History
model = RemoteHistory
fields = '__all__'
class RecordSerializer(serializers.ModelSerializer) :
......
......@@ -5,15 +5,18 @@ from django.http import HttpResponse
from django.core import serializers
from django.core.exceptions import FieldDoesNotExist, ObjectDoesNotExist
from django.shortcuts import render
from django.contrib.auth.models import User
from api.videorecord import record
from api.models import Video, Device, History, Lock, Record, Door, AddDevice
from api.serializers import VideoSerializer, DeviceSerializer, HistorySerializer, RecordSerializer
from api.models import Video, Device, RemoteHistory, Lock, Record, Door, AddDevice
from api.serializers import VideoSerializer, DeviceSerializer, RemoteHistorySerializer, 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 rest_framework.authtoken.models import Token
"""
from boto3.session import Session
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
"""
import time
from datetime import datetime, timedelta
import json
import uuid
# Create your views here.
#로그인 및 토큰 반환
class Login(APIView) :
def get(self, request, format = None) : # request query에 door_id 포함되어있음 : api/auth?door_id=12345
try :
request_id = request.GET.get('door_id', None)
if request_id == None :
raise FieldDoesNotExist
queryset = Door.objects.filter(door_id = request_id) # door_id 유효성 검색
if queryset.exists() :# 유효할 때
userid = uuid.uuid4()
pw = uuid.uuid4()
user = User.objects.create_user(username=str(userid), password=str(pw))
token = Token.objects.create(user=user)
res = {
'is_available' : True,
'access_token' : token.key
}
else :
res = {
'is_available' : False
}
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 post(self, request, format = None) :
queryset = Door.objects.create(door_id = 12345)
return Response({
'msg' : 'doorid값 삽입 완료',
})
'''
#기기 관련 api
class Devices(APIView) :
# 기기 목록 조회
def get(self, request, format = None) :
queryset = Device.objects.all()
serializer = DeviceSerializer(queryset, many = True)
res = {
'deviceList': serializer.data
}
return Response(res, status = status.HTTP_200_OK)
# 기기 추가
def post(self, request, format = None) : # request body에 rfid_id 포함되어있음 
try :
print('냐냐냐냐냐냐')
print(request.body)
data = json.loads(request.body)
request_id = data.get('rfid_id', None)
if request_id == None :
raise FieldDoesNotExist
queryset = Device.objects.create(rfid_id = request_id)
return Response({
'msg' : 'success device add'
})
except FieldDoesNotExist as error :
return Response({
'error' : "FieldDoesNotExist ",
'date' : datetime.now()
}, status = status.HTTP_400_BAD_REQUEST)
# 기기 삭제
def delete(self, request, device_id, format = None): # request URI에 device_id(자동생성되는 기기 고유 번호 != rfid_id) 포함
try :
request_id = device_id
if request_id == None:
raise FieldDoesNotExist
queryset = Device.objects.get(device_id=request_id)
queryset.delete()
return Response({
'msg' : 'success delete device'
})
except FieldDoesNotExist as error :
return Response({
'error' : "FieldDoesNotExist ",
'date' : datetime.now()
}, status = status.HTTP_400_BAD_REQUEST)
# 원격 잠금 해제
class Remote(APIView):
# 원격 잠금 해제 기록 조회
def get(self, request, format = None) :
#models.py의 class History 사용.
queryset = RemoteHistory.objects.all()
serializer = RemoteHistorySerializer(queryset, many = True)
res = {
"remoteHistoryList": serializer.data
}
return Response(res, status = status.HTTP_200_OK)
# 비디오 목록 조회
class VideoList(APIView) :
def get(self, request, format = None) :
......
......@@ -41,6 +41,7 @@ INSTALLED_APPS = [
'api',
'rest_framework',
'corsheaders',
'rest_framework.authtoken',
]
MIDDLEWARE = [
......
......@@ -24,4 +24,8 @@ urlpatterns = [
path('api/video/<str:vid_name>', views.VideoDownload.as_view()),
path('auto/checkDate', views.CheckDate.as_view()),
path('api/setting', views.Recording.as_view()),
path('api/auth', views.Login.as_view()),
path('api/device', views.Devices.as_view()),
path('api/device/<str:device_id>', views.Devices.as_view()),
path('api/remote', views.Remote.as_view()),
]
......