권주희

Merge branch 'feature/user' into 'develop'

Feature/user



See merge request !9
# 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-11 15:29
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('api', '0001_initial'),
]
operations = [
migrations.AddField(
model_name='user',
name='root_folder',
field=models.IntegerField(null=True),
),
migrations.AlterField(
model_name='item',
name='parent',
field=models.IntegerField(null=True),
),
]
......
# 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),
),
]
......@@ -8,7 +8,7 @@ class Item(models.Model):
file_type = models.CharField(max_length=100, null=True) # signed_url 생성을 위해 file type 세팅
path = models.TextField()
#parent = models.ForeignKey('Item', on_delete=models.CASCADE, null=True) #related_name
parent = models.IntegerField()
parent = models.IntegerField(null=True) # root 폴더의 경우 null임
user_id = models.IntegerField()
size = models.IntegerField()
is_deleted = models.BooleanField(default = False)
......@@ -33,10 +33,11 @@ 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)
root_folder = models.IntegerField(null=True)
total_size = models.IntegerField()
current_size = models.IntegerField()
created_time = models.DateTimeField(auto_now=True)
......
from django.contrib.auth.models import User, Group
from django.contrib.auth.models import Group
from rest_framework import serializers
from api.models import Item, SharedItem
from .models import Item, SharedItem,User
class UserSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = User
fields = ['url', 'username', 'email', 'groups']
fields = '__all__'
class GroupSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
......
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,14 @@ 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.serializers import UserSerializer,GroupSerializer,ItemSerializer
from .models import Item, SharedItem, User
from .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):
"""
......@@ -25,7 +28,94 @@ class UserViewSet(viewsets.ModelViewSet):
"""
queryset = User.objects.all().order_by('-date_joined')
serializer_class = UserSerializer
permission_classes = [permissions.IsAuthenticated]
permission_classes = [permissions.IsAuthenticatedOrReadOnly, permissions.AllowAny,
# IsOwnerOrReadOnly
]
permission_classes_by_action = {'get': [permissions.AllowAny],
'destroy': [permissions.AllowAny]}
@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()
root = Item(is_folder=True, name="root", file_type="folder", path="", user_id=user.int_id, size=0,
status=True)
root.save()
return Response({
'message': 'user created',
'int_id': user.int_id,
'user_id': user.user_id,
'name': user.name,
'root_folder':root.item_id,
'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)
def get(self, request, pk):
user = User.objects.filter(int_id=pk)
data = serializers.serialize("json", user)
json_data = json.loads(data)
res = json_data[0]['fields']
res['id']=json_data[0]['pk']
return Response({'data': res}, status=status.HTTP_200_OK)
def get_permissions(self):
try:
# return permission_classes depending on `action`
return [permission() for permission in self.permission_classes_by_action[self.action]]
except KeyError:
# action is not set return default permission_classes
return [permission() for permission in self.permission_classes]
class ItemViewSet(viewsets.ViewSet):
......
......@@ -34,5 +34,7 @@ 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'),
url(r'^login/$', views.UserViewSet.login, name='login'),
]
......