권주희

add signup, login api

1 -# Generated by Django 3.0.7 on 2020-06-04 19:52 1 +# Generated by Django 3.0.6 on 2020-06-11 14:54
2 2
3 from django.db import migrations, models 3 from django.db import migrations, models
4 -import django.db.models.deletion
5 4
6 5
7 class Migration(migrations.Migration): 6 class Migration(migrations.Migration):
...@@ -13,12 +12,33 @@ class Migration(migrations.Migration): ...@@ -13,12 +12,33 @@ class Migration(migrations.Migration):
13 12
14 operations = [ 13 operations = [
15 migrations.CreateModel( 14 migrations.CreateModel(
15 + name='Item',
16 + fields=[
17 + ('item_id', models.AutoField(primary_key=True, serialize=False)),
18 + ('is_folder', models.BooleanField(default=False)),
19 + ('name', models.CharField(max_length=50)),
20 + ('file_type', models.CharField(max_length=100, null=True)),
21 + ('path', models.TextField()),
22 + ('parent', models.IntegerField()),
23 + ('user_id', models.IntegerField()),
24 + ('size', models.IntegerField()),
25 + ('is_deleted', models.BooleanField(default=False)),
26 + ('created_time', models.DateTimeField(auto_now=True)),
27 + ('updated_time', models.DateTimeField(null=True)),
28 + ('status', models.BooleanField()),
29 + ],
30 + options={
31 + 'ordering': ['item_id'],
32 + },
33 + ),
34 + migrations.CreateModel(
16 name='SharedItem', 35 name='SharedItem',
17 fields=[ 36 fields=[
18 ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), 37 ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
19 ('item_id', models.IntegerField()), 38 ('item_id', models.IntegerField()),
20 - ('valid', models.DateTimeField()), 39 + ('expires', models.DateTimeField()),
21 ('password', models.CharField(max_length=20)), 40 ('password', models.CharField(max_length=20)),
41 + ('created_time', models.DateTimeField(auto_now=True)),
22 ], 42 ],
23 options={ 43 options={
24 'ordering': ['item_id'], 44 'ordering': ['item_id'],
...@@ -27,35 +47,16 @@ class Migration(migrations.Migration): ...@@ -27,35 +47,16 @@ class Migration(migrations.Migration):
27 migrations.CreateModel( 47 migrations.CreateModel(
28 name='User', 48 name='User',
29 fields=[ 49 fields=[
30 - ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), 50 + ('int_id', models.AutoField(primary_key=True, serialize=False)),
31 - ('int_id', models.IntegerField()),
32 ('user_id', models.CharField(max_length=50)), 51 ('user_id', models.CharField(max_length=50)),
33 ('name', models.CharField(max_length=50)), 52 ('name', models.CharField(max_length=50)),
34 ('password', models.CharField(max_length=20)), 53 ('password', models.CharField(max_length=20)),
35 ('total_size', models.IntegerField()), 54 ('total_size', models.IntegerField()),
36 ('current_size', models.IntegerField()), 55 ('current_size', models.IntegerField()),
56 + ('created_time', models.DateTimeField(auto_now=True)),
37 ], 57 ],
38 options={ 58 options={
39 'ordering': ['int_id'], 59 'ordering': ['int_id'],
40 }, 60 },
41 ), 61 ),
42 - migrations.CreateModel(
43 - name='Item',
44 - fields=[
45 - ('item_id', models.IntegerField(primary_key=True, serialize=False)),
46 - ('is_folder', models.BooleanField(default=False)),
47 - ('name', models.CharField(max_length=50)),
48 - ('path', models.TextField()),
49 - ('user_id', models.IntegerField()),
50 - ('size', models.IntegerField()),
51 - ('is_deleted', models.BooleanField(default=False)),
52 - ('created_time', models.DateTimeField()),
53 - ('updated_time', models.DateTimeField()),
54 - ('status', models.BooleanField()),
55 - ('parent', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='api.Item')),
56 - ],
57 - options={
58 - 'ordering': ['item_id'],
59 - },
60 - ),
61 ] 62 ]
......
1 -# Generated by Django 3.0.7 on 2020-06-05 09:51
2 -
3 -from django.db import migrations, models
4 -
5 -
6 -class Migration(migrations.Migration):
7 -
8 - dependencies = [
9 - ('api', '0001_initial'),
10 - ]
11 -
12 - operations = [
13 - migrations.AlterField(
14 - model_name='item',
15 - name='parent',
16 - field=models.IntegerField(),
17 - ),
18 - ]
1 -# Generated by Django 3.0.6 on 2020-06-06 08:17
2 -
3 -from django.db import migrations
4 -
5 -
6 -class Migration(migrations.Migration):
7 -
8 - dependencies = [
9 - ('api', '0002_auto_20200605_1851'),
10 - ]
11 -
12 - operations = [
13 - migrations.RenameField(
14 - model_name='shareditem',
15 - old_name='valid',
16 - new_name='expires',
17 - ),
18 - ]
1 -# Generated by Django 3.0.6 on 2020-06-06 08:24
2 -
3 -from django.db import migrations, models
4 -
5 -
6 -class Migration(migrations.Migration):
7 -
8 - dependencies = [
9 - ('api', '0003_auto_20200606_0817'),
10 - ]
11 -
12 - operations = [
13 - migrations.AddField(
14 - model_name='shareditem',
15 - name='created_time',
16 - field=models.DateTimeField(auto_now=True),
17 - ),
18 - migrations.AddField(
19 - model_name='user',
20 - name='created_time',
21 - field=models.DateTimeField(auto_now=True),
22 - ),
23 - migrations.AlterField(
24 - model_name='item',
25 - name='created_time',
26 - field=models.DateTimeField(auto_now=True),
27 - ),
28 - ]
1 -# Generated by Django 3.0.6 on 2020-06-10 11:50
2 -
3 -from django.db import migrations, models
4 -
5 -
6 -class Migration(migrations.Migration):
7 -
8 - dependencies = [
9 - ('api', '0004_auto_20200606_0824'),
10 - ]
11 -
12 - operations = [
13 - migrations.AlterField(
14 - model_name='item',
15 - name='item_id',
16 - field=models.IntegerField(auto_created=True, primary_key=True, serialize=False),
17 - ),
18 - ]
1 -# Generated by Django 3.0.6 on 2020-06-10 12:09
2 -
3 -from django.db import migrations, models
4 -
5 -
6 -class Migration(migrations.Migration):
7 -
8 - dependencies = [
9 - ('api', '0005_auto_20200610_1150'),
10 - ]
11 -
12 - operations = [
13 - migrations.AlterField(
14 - model_name='item',
15 - name='item_id',
16 - field=models.AutoField(primary_key=True, serialize=False),
17 - ),
18 - ]
1 -# Generated by Django 3.0.6 on 2020-06-10 12:17
2 -
3 -from django.db import migrations, models
4 -
5 -
6 -class Migration(migrations.Migration):
7 -
8 - dependencies = [
9 - ('api', '0006_auto_20200610_1209'),
10 - ]
11 -
12 - operations = [
13 - migrations.AlterField(
14 - model_name='item',
15 - name='updated_time',
16 - field=models.DateTimeField(null=True),
17 - ),
18 - ]
1 -# Generated by Django 3.0.6 on 2020-06-10 13:29
2 -
3 -from django.db import migrations, models
4 -
5 -
6 -class Migration(migrations.Migration):
7 -
8 - dependencies = [
9 - ('api', '0007_auto_20200610_1217'),
10 - ]
11 -
12 - operations = [
13 - migrations.AddField(
14 - model_name='item',
15 - name='file_type',
16 - field=models.CharField(max_length=100, null=True),
17 - ),
18 - ]
...@@ -33,7 +33,7 @@ class SharedItem(models.Model): ...@@ -33,7 +33,7 @@ class SharedItem(models.Model):
33 33
34 34
35 class User(models.Model): 35 class User(models.Model):
36 - int_id = models.IntegerField() 36 + int_id = models.AutoField(primary_key = True)
37 user_id = models.CharField(max_length = 50) 37 user_id = models.CharField(max_length = 50)
38 name = models.CharField(max_length = 50) 38 name = models.CharField(max_length = 50)
39 password = models.CharField(max_length = 20) 39 password = models.CharField(max_length = 20)
......
1 import mimetypes 1 import mimetypes
2 import json 2 import json
3 import os 3 import os
4 -from datetime import datetime 4 +from datetime import datetime, timedelta
5 5
6 import boto3 6 import boto3
7 7
8 -from django.contrib.auth.models import User
9 from django.core import serializers 8 from django.core import serializers
10 from django.views.decorators.csrf import csrf_exempt 9 from django.views.decorators.csrf import csrf_exempt
11 from rest_framework import viewsets 10 from rest_framework import viewsets
...@@ -14,10 +13,13 @@ from rest_framework.response import Response ...@@ -14,10 +13,13 @@ from rest_framework.response import Response
14 from rest_framework.decorators import action 13 from rest_framework.decorators import action
15 from rest_framework.permissions import IsAuthenticated, AllowAny 14 from rest_framework.permissions import IsAuthenticated, AllowAny
16 15
17 -from api.models import Item, SharedItem 16 +from api.models import Item, SharedItem, User
18 from api.serializers import UserSerializer,GroupSerializer,ItemSerializer 17 from api.serializers import UserSerializer,GroupSerializer,ItemSerializer
19 from rest_framework import status 18 from rest_framework import status
20 from annoying.functions import get_object_or_None 19 from annoying.functions import get_object_or_None
20 +from django.conf import settings
21 +import jwt
22 +from django.http import HttpResponse, JsonResponse
21 23
22 class UserViewSet(viewsets.ModelViewSet): 24 class UserViewSet(viewsets.ModelViewSet):
23 """ 25 """
...@@ -27,6 +29,70 @@ class UserViewSet(viewsets.ModelViewSet): ...@@ -27,6 +29,70 @@ class UserViewSet(viewsets.ModelViewSet):
27 serializer_class = UserSerializer 29 serializer_class = UserSerializer
28 permission_classes = [permissions.IsAuthenticated] 30 permission_classes = [permissions.IsAuthenticated]
29 31
32 + @csrf_exempt
33 + @action(detail=False, methods=['POST'], permission_classes=[permissions.AllowAny], url_path='signup', url_name='singup')
34 + def signup(self, request):
35 + user_id = request.POST.get('user_id', '')
36 + name = request.POST.get('name', '')
37 + password = request.POST.get('password', '')
38 + user = get_object_or_None(User, user_id=user_id)
39 + if user == None:
40 + user = User(user_id = user_id, name = name, password = password, total_size=100000, current_size = 0)
41 + user.save()
42 + return Response({
43 + 'message': 'user created',
44 + 'int_id': user.int_id,
45 + 'user_id': user.user_id,
46 + 'name': user.name,
47 + 'total_size': user.total_size,
48 + 'current_size': user.current_size,
49 + 'created_time': user.created_time
50 + },
51 + status=status.HTTP_200_OK,
52 + )
53 + else:
54 + return Response({'message': 'user is already exist.'}, status=status.HTTP_204_NO_CONTENT)
55 +
56 + @csrf_exempt
57 + @action(methods=['post'], detail=False, permission_classes=[permissions.AllowAny],
58 + url_path='login', url_name='login')
59 + def login(self, request):
60 + if not request.data:
61 + return Response({'Error': "Please provide user_id/password"}, status=status.HTTP_400_BAD_REQUEST)
62 + user_id = request.POST['user_id']
63 + password = request.POST['password']
64 + try:
65 + user = User.objects.get(user_id=user_id, password=password)
66 + except User.DoesNotExist:
67 + return Response({'Error': "Invalid user_id/password"}, status=status.HTTP_400_BAD_REQUEST)
68 + if user:
69 + payload1 = {
70 + 'int_id': user.int_id,
71 + 'user_id': user.user_id,
72 + 'exp': datetime.utcnow() + timedelta(seconds=300)
73 + }
74 + payload2 = {
75 + 'int_id': user.int_id,
76 + 'user_id': user.user_id,
77 + 'exp': datetime.utcnow() + timedelta(days=5)
78 + }
79 + access = jwt.encode(payload1, settings.SECRET_KEY, algorithm='HS256').decode('utf-8')
80 + refresh = jwt.encode(payload2, settings.SECRET_KEY, algorithm='HS256').decode('utf-8')
81 + exp = jwt.decode(access, settings.SECRET_KEY, algorithm='HS256')['exp']
82 + token = {'access': access,
83 + 'refresh': refresh,
84 + 'exp': exp}
85 + return JsonResponse(
86 + token,
87 + status=status.HTTP_200_OK,
88 + )
89 + else:
90 + return JsonResponse(
91 + {'Error': "Invalid credentials"},
92 + status=status.HTTP_400_BAD_REQUEST,
93 + )
94 + return JsonResponse(status=status.HTTP_405_METHOD_NOT_ALLOWED)
95 +
30 96
31 class ItemViewSet(viewsets.ViewSet): 97 class ItemViewSet(viewsets.ViewSet):
32 98
......
...@@ -34,5 +34,6 @@ urlpatterns = [ ...@@ -34,5 +34,6 @@ urlpatterns = [
34 url(r'^<int:pk>/move/$', views.ItemViewSet.move, name='move'), 34 url(r'^<int:pk>/move/$', views.ItemViewSet.move, name='move'),
35 url(r'^<int:pk>/copy/$', views.ItemViewSet.copy, name='copy'), 35 url(r'^<int:pk>/copy/$', views.ItemViewSet.copy, name='copy'),
36 url(r'^<int:pk>/children/$', views.ItemViewSet.children, name='copy'), 36 url(r'^<int:pk>/children/$', views.ItemViewSet.children, name='copy'),
37 + url(r'^signup/$', views.UserViewSet.signup, name='signup'),
37 38
38 ] 39 ]
......