권주희

add signup, login api

# Generated by Django 3.0.7 on 2020-06-04 19:52
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
initial = True
dependencies = [
]
operations = [
migrations.CreateModel(
name='SharedItem',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('item_id', models.IntegerField()),
('valid', models.DateTimeField()),
('password', models.CharField(max_length=20)),
],
options={
'ordering': ['item_id'],
},
),
migrations.CreateModel(
name='User',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('int_id', models.IntegerField()),
('user_id', models.CharField(max_length=50)),
('name', models.CharField(max_length=50)),
('password', models.CharField(max_length=20)),
('total_size', models.IntegerField()),
('current_size', models.IntegerField()),
],
options={
'ordering': ['int_id'],
},
),
migrations.CreateModel(
name='Item',
fields=[
('item_id', models.IntegerField(primary_key=True, serialize=False)),
('is_folder', models.BooleanField(default=False)),
('name', models.CharField(max_length=50)),
('path', models.TextField()),
('user_id', models.IntegerField()),
('size', models.IntegerField()),
('is_deleted', models.BooleanField(default=False)),
('created_time', models.DateTimeField()),
('updated_time', models.DateTimeField()),
('status', models.BooleanField()),
('parent', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='api.Item')),
],
options={
'ordering': ['item_id'],
},
),
]
# Generated by Django 3.0.6 on 2020-06-11 14:54
from django.db import migrations, models
class Migration(migrations.Migration):
initial = True
dependencies = [
]
operations = [
migrations.CreateModel(
name='Item',
fields=[
('item_id', models.AutoField(primary_key=True, serialize=False)),
('is_folder', models.BooleanField(default=False)),
('name', models.CharField(max_length=50)),
('file_type', models.CharField(max_length=100, null=True)),
('path', models.TextField()),
('parent', models.IntegerField()),
('user_id', models.IntegerField()),
('size', models.IntegerField()),
('is_deleted', models.BooleanField(default=False)),
('created_time', models.DateTimeField(auto_now=True)),
('updated_time', models.DateTimeField(null=True)),
('status', models.BooleanField()),
],
options={
'ordering': ['item_id'],
},
),
migrations.CreateModel(
name='SharedItem',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('item_id', models.IntegerField()),
('expires', models.DateTimeField()),
('password', models.CharField(max_length=20)),
('created_time', models.DateTimeField(auto_now=True)),
],
options={
'ordering': ['item_id'],
},
),
migrations.CreateModel(
name='User',
fields=[
('int_id', models.AutoField(primary_key=True, serialize=False)),
('user_id', models.CharField(max_length=50)),
('name', models.CharField(max_length=50)),
('password', models.CharField(max_length=20)),
('total_size', models.IntegerField()),
('current_size', models.IntegerField()),
('created_time', models.DateTimeField(auto_now=True)),
],
options={
'ordering': ['int_id'],
},
),
]
......
# Generated by Django 3.0.7 on 2020-06-05 09:51
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('api', '0001_initial'),
]
operations = [
migrations.AlterField(
model_name='item',
name='parent',
field=models.IntegerField(),
),
]
# Generated by Django 3.0.6 on 2020-06-06 08:17
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('api', '0002_auto_20200605_1851'),
]
operations = [
migrations.RenameField(
model_name='shareditem',
old_name='valid',
new_name='expires',
),
]
# Generated by Django 3.0.6 on 2020-06-06 08:24
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('api', '0003_auto_20200606_0817'),
]
operations = [
migrations.AddField(
model_name='shareditem',
name='created_time',
field=models.DateTimeField(auto_now=True),
),
migrations.AddField(
model_name='user',
name='created_time',
field=models.DateTimeField(auto_now=True),
),
migrations.AlterField(
model_name='item',
name='created_time',
field=models.DateTimeField(auto_now=True),
),
]
# Generated by Django 3.0.6 on 2020-06-10 11:50
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('api', '0004_auto_20200606_0824'),
]
operations = [
migrations.AlterField(
model_name='item',
name='item_id',
field=models.IntegerField(auto_created=True, primary_key=True, serialize=False),
),
]
# Generated by Django 3.0.6 on 2020-06-10 12:09
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('api', '0005_auto_20200610_1150'),
]
operations = [
migrations.AlterField(
model_name='item',
name='item_id',
field=models.AutoField(primary_key=True, serialize=False),
),
]
# Generated by Django 3.0.6 on 2020-06-10 12:17
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('api', '0006_auto_20200610_1209'),
]
operations = [
migrations.AlterField(
model_name='item',
name='updated_time',
field=models.DateTimeField(null=True),
),
]
# Generated by Django 3.0.6 on 2020-06-10 13:29
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('api', '0007_auto_20200610_1217'),
]
operations = [
migrations.AddField(
model_name='item',
name='file_type',
field=models.CharField(max_length=100, null=True),
),
]
......@@ -33,7 +33,7 @@ class SharedItem(models.Model):
class User(models.Model):
int_id = models.IntegerField()
int_id = models.AutoField(primary_key = True)
user_id = models.CharField(max_length = 50)
name = models.CharField(max_length = 50)
password = models.CharField(max_length = 20)
......
import mimetypes
import json
import os
from datetime import datetime
from datetime import datetime, timedelta
import boto3
from django.contrib.auth.models import User
from django.core import serializers
from django.views.decorators.csrf import csrf_exempt
from rest_framework import viewsets
......@@ -14,10 +13,13 @@ from rest_framework.response import Response
from rest_framework.decorators import action
from rest_framework.permissions import IsAuthenticated, AllowAny
from api.models import Item, SharedItem
from api.models import Item, SharedItem, User
from api.serializers import UserSerializer,GroupSerializer,ItemSerializer
from rest_framework import status
from annoying.functions import get_object_or_None
from django.conf import settings
import jwt
from django.http import HttpResponse, JsonResponse
class UserViewSet(viewsets.ModelViewSet):
"""
......@@ -27,6 +29,70 @@ class UserViewSet(viewsets.ModelViewSet):
serializer_class = UserSerializer
permission_classes = [permissions.IsAuthenticated]
@csrf_exempt
@action(detail=False, methods=['POST'], permission_classes=[permissions.AllowAny], url_path='signup', url_name='singup')
def signup(self, request):
user_id = request.POST.get('user_id', '')
name = request.POST.get('name', '')
password = request.POST.get('password', '')
user = get_object_or_None(User, user_id=user_id)
if user == None:
user = User(user_id = user_id, name = name, password = password, total_size=100000, current_size = 0)
user.save()
return Response({
'message': 'user created',
'int_id': user.int_id,
'user_id': user.user_id,
'name': user.name,
'total_size': user.total_size,
'current_size': user.current_size,
'created_time': user.created_time
},
status=status.HTTP_200_OK,
)
else:
return Response({'message': 'user is already exist.'}, status=status.HTTP_204_NO_CONTENT)
@csrf_exempt
@action(methods=['post'], detail=False, permission_classes=[permissions.AllowAny],
url_path='login', url_name='login')
def login(self, request):
if not request.data:
return Response({'Error': "Please provide user_id/password"}, status=status.HTTP_400_BAD_REQUEST)
user_id = request.POST['user_id']
password = request.POST['password']
try:
user = User.objects.get(user_id=user_id, password=password)
except User.DoesNotExist:
return Response({'Error': "Invalid user_id/password"}, status=status.HTTP_400_BAD_REQUEST)
if user:
payload1 = {
'int_id': user.int_id,
'user_id': user.user_id,
'exp': datetime.utcnow() + timedelta(seconds=300)
}
payload2 = {
'int_id': user.int_id,
'user_id': user.user_id,
'exp': datetime.utcnow() + timedelta(days=5)
}
access = jwt.encode(payload1, settings.SECRET_KEY, algorithm='HS256').decode('utf-8')
refresh = jwt.encode(payload2, settings.SECRET_KEY, algorithm='HS256').decode('utf-8')
exp = jwt.decode(access, settings.SECRET_KEY, algorithm='HS256')['exp']
token = {'access': access,
'refresh': refresh,
'exp': exp}
return JsonResponse(
token,
status=status.HTTP_200_OK,
)
else:
return JsonResponse(
{'Error': "Invalid credentials"},
status=status.HTTP_400_BAD_REQUEST,
)
return JsonResponse(status=status.HTTP_405_METHOD_NOT_ALLOWED)
class ItemViewSet(viewsets.ViewSet):
......
......@@ -34,5 +34,6 @@ urlpatterns = [
url(r'^<int:pk>/move/$', views.ItemViewSet.move, name='move'),
url(r'^<int:pk>/copy/$', views.ItemViewSet.copy, name='copy'),
url(r'^<int:pk>/children/$', views.ItemViewSet.children, name='copy'),
url(r'^signup/$', views.UserViewSet.signup, name='signup'),
]
......