Showing
8 changed files
with
114 additions
and
74 deletions
1 | +# Generated by Django 3.0.7 on 2020-06-10 17:40 | ||
2 | + | ||
3 | +from django.db import migrations, models | ||
4 | + | ||
5 | + | ||
6 | +class Migration(migrations.Migration): | ||
7 | + | ||
8 | + dependencies = [ | ||
9 | + ('api', '0008_item_file_type'), | ||
10 | + ] | ||
11 | + | ||
12 | + operations = [ | ||
13 | + migrations.RemoveField( | ||
14 | + model_name='user', | ||
15 | + name='id', | ||
16 | + ), | ||
17 | + migrations.AlterField( | ||
18 | + model_name='user', | ||
19 | + name='int_id', | ||
20 | + field=models.AutoField(primary_key=True, serialize=False), | ||
21 | + ), | ||
22 | + ] |
1 | +# Generated by Django 3.0.7 on 2020-06-10 17:42 | ||
2 | + | ||
3 | +from django.db import migrations, models | ||
4 | + | ||
5 | + | ||
6 | +class Migration(migrations.Migration): | ||
7 | + | ||
8 | + dependencies = [ | ||
9 | + ('api', '0009_auto_20200611_0240'), | ||
10 | + ] | ||
11 | + | ||
12 | + operations = [ | ||
13 | + migrations.AlterField( | ||
14 | + model_name='user', | ||
15 | + name='current_size', | ||
16 | + field=models.BigIntegerField(blank=True, default=0), | ||
17 | + ), | ||
18 | + migrations.AlterField( | ||
19 | + model_name='user', | ||
20 | + name='total_size', | ||
21 | + field=models.BigIntegerField(blank=True, default=1099511627776), | ||
22 | + ), | ||
23 | + ] |
... | @@ -33,12 +33,13 @@ class SharedItem(models.Model): | ... | @@ -33,12 +33,13 @@ 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) |
40 | - total_size = models.IntegerField() | 40 | + total_size = models.BigIntegerField(blank=True, default=1099511627776) |
41 | - current_size = models.IntegerField() | 41 | + current_size = models.BigIntegerField(blank=True, default=0) |
42 | created_time = models.DateTimeField(auto_now=True) | 42 | created_time = models.DateTimeField(auto_now=True) |
43 | + | ||
43 | class Meta: | 44 | class Meta: |
44 | ordering = ['int_id'] | 45 | ordering = ['int_id'] | ... | ... |
1 | -from django.contrib.auth.models import User, Group | ||
2 | from rest_framework import serializers | 1 | from rest_framework import serializers |
3 | -from api.models import Item, SharedItem | 2 | +from api.models import User |
4 | 3 | ||
5 | 4 | ||
6 | -class UserSerializer(serializers.HyperlinkedModelSerializer): | 5 | +class UserSerializer(serializers.ModelSerializer): |
7 | class Meta: | 6 | class Meta: |
8 | model = User | 7 | model = User |
9 | - fields = ['url', 'username', 'email', 'groups'] | 8 | + fields = '__all__' |
9 | + | ||
10 | + | ||
11 | +class SignUpSerializer(serializers.ModelSerializer): | ||
12 | + password2 = serializers.CharField(style={"input_type": "password"}) | ||
10 | 13 | ||
11 | -class GroupSerializer(serializers.HyperlinkedModelSerializer): | ||
12 | class Meta: | 14 | class Meta: |
13 | - model = Group | 15 | + model = User |
14 | - fields = ['url', 'name'] | 16 | + fields = ['user_id', 'name', 'password', 'password2'] |
15 | 17 | ||
16 | -class ItemSerializer(serializers.ModelSerializer): | 18 | + def create(self, data): |
19 | + user_id = data['user_id'] | ||
20 | + name = data['name'] | ||
21 | + password = self.data['password'] | ||
22 | + password2 = self.data['password2'] | ||
23 | + if user_id and User.objects.filter(user_id=user_id).exclude(name=name).exists(): | ||
24 | + raise serializers.ValidationError({"user_id": "User_id must be unique."}) | ||
25 | + elif password != password2: | ||
26 | + raise serializers.ValidationError({'password': "Passwords must match."}) | ||
27 | + user = User.objects.create( | ||
28 | + user_id=data['user_id'], | ||
29 | + name=data['name'], | ||
30 | + password=data['password'], | ||
31 | + ) | ||
32 | + user.save() | ||
33 | + return user | ||
34 | + | ||
35 | + | ||
36 | +class UserInfoSerializer(serializers.ModelSerializer): | ||
17 | class Meta: | 37 | class Meta: |
18 | - model = Item | 38 | + model = User |
19 | - fields = '__all__' | 39 | + fields = ['user_id', 'name', 'total_size', 'current_size', 'created_time'] |
20 | 40 | ... | ... |
backend/api/utils.py
0 → 100644
1 | +import jwt | ||
2 | +import json | ||
3 | +from rest_framework import status | ||
4 | +from django.http import JsonResponse | ||
5 | +from django.core.exceptions import ObjectDoesNotExist | ||
6 | +from django.conf import settings | ||
7 | +from api.models import User | ||
8 | + | ||
9 | + | ||
10 | +def login_decorator(func): | ||
11 | + def wrapper(self, request, *args, **kwargs): | ||
12 | + if 'Authorization' not in request.headers: | ||
13 | + return JsonResponse({'Error': 'INVALID_LOGIN'}, status=status.HTTP_401_UNAUTHORIZED) | ||
14 | + encode_token = request.headers['Authorization'] | ||
15 | + try: | ||
16 | + payload = jwt.decode(encode_token, settings.SECRET_KEY, algorithm='HS256') | ||
17 | + user = User.objects.get(int_id=payload['int_id']) | ||
18 | + request.user = user | ||
19 | + except jwt.exceptions.DecodeError: | ||
20 | + return JsonResponse({'Error': 'INVALID_TOKEN'}, status=status.HTTP_400) | ||
21 | + except User.DoesNotExist: | ||
22 | + return JsonResponse({'Error': 'UNKNOWN_USER'}, status=status.HTTP_400) | ||
23 | + return func(self, request, *args, **kwargs) | ||
24 | + return wrapper | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
This diff is collapsed. Click to expand it.
1 | """ | 1 | """ |
2 | Django settings for khudrive project. | 2 | Django settings for khudrive project. |
3 | - | ||
4 | Generated by 'django-admin startproject' using Django 3.0.7. | 3 | Generated by 'django-admin startproject' using Django 3.0.7. |
5 | - | ||
6 | For more information on this file, see | 4 | For more information on this file, see |
7 | https://docs.djangoproject.com/en/3.0/topics/settings/ | 5 | https://docs.djangoproject.com/en/3.0/topics/settings/ |
8 | - | ||
9 | For the full list of settings and their values, see | 6 | For the full list of settings and their values, see |
10 | https://docs.djangoproject.com/en/3.0/ref/settings/ | 7 | https://docs.djangoproject.com/en/3.0/ref/settings/ |
11 | """ | 8 | """ |
12 | - | ||
13 | import os | 9 | import os |
14 | - | ||
15 | # Build paths inside the project like this: os.path.join(BASE_DIR, ...) | 10 | # Build paths inside the project like this: os.path.join(BASE_DIR, ...) |
16 | BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) | 11 | BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) |
17 | - | ||
18 | - | ||
19 | # Quick-start development settings - unsuitable for production | 12 | # Quick-start development settings - unsuitable for production |
20 | # See https://docs.djangoproject.com/en/3.0/howto/deployment/checklist/ | 13 | # See https://docs.djangoproject.com/en/3.0/howto/deployment/checklist/ |
21 | - | ||
22 | # SECURITY WARNING: keep the secret key used in production secret! | 14 | # SECURITY WARNING: keep the secret key used in production secret! |
23 | SECRET_KEY = ')i0_(*4t7k3=rcqp*_i0u((9zbk8q(2(3tk(%$woji-e-37=o*' | 15 | SECRET_KEY = ')i0_(*4t7k3=rcqp*_i0u((9zbk8q(2(3tk(%$woji-e-37=o*' |
24 | - | ||
25 | # SECURITY WARNING: don't run with debug turned on in production! | 16 | # SECURITY WARNING: don't run with debug turned on in production! |
26 | DEBUG = True | 17 | DEBUG = True |
27 | - | ||
28 | ALLOWED_HOSTS = [] | 18 | ALLOWED_HOSTS = [] |
29 | - | ||
30 | - | ||
31 | # Application definition | 19 | # Application definition |
32 | - | ||
33 | INSTALLED_APPS = [ | 20 | INSTALLED_APPS = [ |
34 | 'django.contrib.admin', | 21 | 'django.contrib.admin', |
35 | 'django.contrib.auth', | 22 | 'django.contrib.auth', |
... | @@ -40,7 +27,6 @@ INSTALLED_APPS = [ | ... | @@ -40,7 +27,6 @@ INSTALLED_APPS = [ |
40 | 'rest_framework', | 27 | 'rest_framework', |
41 | 'api.apps.ApiConfig', | 28 | 'api.apps.ApiConfig', |
42 | ] | 29 | ] |
43 | - | ||
44 | MIDDLEWARE = [ | 30 | MIDDLEWARE = [ |
45 | 'django.middleware.security.SecurityMiddleware', | 31 | 'django.middleware.security.SecurityMiddleware', |
46 | 'django.contrib.sessions.middleware.SessionMiddleware', | 32 | 'django.contrib.sessions.middleware.SessionMiddleware', |
... | @@ -50,9 +36,7 @@ MIDDLEWARE = [ | ... | @@ -50,9 +36,7 @@ MIDDLEWARE = [ |
50 | 'django.contrib.messages.middleware.MessageMiddleware', | 36 | 'django.contrib.messages.middleware.MessageMiddleware', |
51 | 'django.middleware.clickjacking.XFrameOptionsMiddleware', | 37 | 'django.middleware.clickjacking.XFrameOptionsMiddleware', |
52 | ] | 38 | ] |
53 | - | ||
54 | ROOT_URLCONF = 'khudrive.urls' | 39 | ROOT_URLCONF = 'khudrive.urls' |
55 | - | ||
56 | TEMPLATES = [ | 40 | TEMPLATES = [ |
57 | { | 41 | { |
58 | 'BACKEND': 'django.template.backends.django.DjangoTemplates', | 42 | 'BACKEND': 'django.template.backends.django.DjangoTemplates', |
... | @@ -68,10 +52,7 @@ TEMPLATES = [ | ... | @@ -68,10 +52,7 @@ TEMPLATES = [ |
68 | }, | 52 | }, |
69 | }, | 53 | }, |
70 | ] | 54 | ] |
71 | - | ||
72 | WSGI_APPLICATION = 'khudrive.wsgi.application' | 55 | WSGI_APPLICATION = 'khudrive.wsgi.application' |
73 | - | ||
74 | - | ||
75 | # Database | 56 | # Database |
76 | # https://docs.djangoproject.com/en/3.0/ref/settings/#databases | 57 | # https://docs.djangoproject.com/en/3.0/ref/settings/#databases |
77 | DATABASES = { | 58 | DATABASES = { |
... | @@ -82,17 +63,14 @@ DATABASES = { | ... | @@ -82,17 +63,14 @@ DATABASES = { |
82 | 'default': { | 63 | 'default': { |
83 | 'ENGINE': 'django.db.backends.postgresql', | 64 | 'ENGINE': 'django.db.backends.postgresql', |
84 | 'NAME': 'khuDrive', | 65 | 'NAME': 'khuDrive', |
85 | - 'USER': 'jooheekwon', | 66 | + 'USER': 'hjy', |
86 | - 'PASSWORD': '', | 67 | + 'PASSWORD': '2521', |
87 | 'HOST': 'localhost', | 68 | 'HOST': 'localhost', |
88 | - 'PORT': '', | 69 | + 'PORT': '5432', |
89 | } | 70 | } |
90 | } | 71 | } |
91 | - | ||
92 | - | ||
93 | # Password validation | 72 | # Password validation |
94 | # https://docs.djangoproject.com/en/3.0/ref/settings/#auth-password-validators | 73 | # https://docs.djangoproject.com/en/3.0/ref/settings/#auth-password-validators |
95 | - | ||
96 | AUTH_PASSWORD_VALIDATORS = [ | 74 | AUTH_PASSWORD_VALIDATORS = [ |
97 | { | 75 | { |
98 | 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator', | 76 | 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator', |
... | @@ -107,23 +85,13 @@ AUTH_PASSWORD_VALIDATORS = [ | ... | @@ -107,23 +85,13 @@ AUTH_PASSWORD_VALIDATORS = [ |
107 | 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator', | 85 | 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator', |
108 | }, | 86 | }, |
109 | ] | 87 | ] |
110 | - | ||
111 | - | ||
112 | # Internationalization | 88 | # Internationalization |
113 | # https://docs.djangoproject.com/en/3.0/topics/i18n/ | 89 | # https://docs.djangoproject.com/en/3.0/topics/i18n/ |
114 | - | ||
115 | LANGUAGE_CODE = 'en-us' | 90 | LANGUAGE_CODE = 'en-us' |
116 | - | ||
117 | TIME_ZONE = 'UTC' | 91 | TIME_ZONE = 'UTC' |
118 | - | ||
119 | USE_I18N = True | 92 | USE_I18N = True |
120 | - | ||
121 | USE_L10N = True | 93 | USE_L10N = True |
122 | - | ||
123 | USE_TZ = True | 94 | USE_TZ = True |
124 | - | ||
125 | - | ||
126 | # Static files (CSS, JavaScript, Images) | 95 | # Static files (CSS, JavaScript, Images) |
127 | # https://docs.djangoproject.com/en/3.0/howto/static-files/ | 96 | # https://docs.djangoproject.com/en/3.0/howto/static-files/ |
128 | - | ||
129 | STATIC_URL = '/static/' | 97 | STATIC_URL = '/static/' |
... | \ No newline at end of file | ... | \ No newline at end of file | ... | ... |
1 | -"""khudrive URL Configuration | ||
2 | - | ||
3 | -The `urlpatterns` list routes URLs to views. For more information please see: | ||
4 | - https://docs.djangoproject.com/en/3.0/topics/http/urls/ | ||
5 | -Examples: | ||
6 | -Function views | ||
7 | - 1. Add an import: from my_app import views | ||
8 | - 2. Add a URL to urlpatterns: path('', views.home, name='home') | ||
9 | -Class-based views | ||
10 | - 1. Add an import: from other_app.views import Home | ||
11 | - 2. Add a URL to urlpatterns: path('', Home.as_view(), name='home') | ||
12 | -Including another URLconf | ||
13 | - 1. Import the include() function: from django.urls import include, path | ||
14 | - 2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) | ||
15 | -""" | ||
16 | from django.urls import include, path | 1 | from django.urls import include, path |
17 | from rest_framework import routers | 2 | from rest_framework import routers |
18 | from django.contrib import admin | 3 | from django.contrib import admin |
19 | from api import views | 4 | from api import views |
20 | from django.conf.urls import url | 5 | from django.conf.urls import url |
21 | - | ||
22 | router = routers.DefaultRouter() | 6 | router = routers.DefaultRouter() |
23 | router.register(r'users', views.UserViewSet) | 7 | router.register(r'users', views.UserViewSet) |
24 | -router.register(r'items', views.ItemViewSet) | ||
25 | -router.register(r'items', views.SharedItemViewSet) | ||
26 | 8 | ||
27 | # Wire up our API using automatic URL routing. | 9 | # Wire up our API using automatic URL routing. |
28 | # Additionally, we include login URLs for the browsable API. | 10 | # Additionally, we include login URLs for the browsable API. |
29 | urlpatterns = [ | 11 | urlpatterns = [ |
30 | path('admin/', admin.site.urls), | 12 | path('admin/', admin.site.urls), |
31 | path('', include(router.urls)), | 13 | path('', include(router.urls)), |
32 | - url(r'^search/$', views.ItemViewSet.search, name='search'), | 14 | + url(r'^signup/$', views.UserViewSet.signup, name='signup'), |
33 | - url(r'^<int:pk>/share/$', views.SharedItemViewSet.share, name='share'), | 15 | + url(r'^login/$', views.UserViewSet.login, name='login'), |
34 | - url(r'^<int:pk>/move/$', views.ItemViewSet.move, name='move'), | 16 | + url(r'^renew/$', views.UserViewSet.renew, name='renew'), |
35 | - url(r'^<int:pk>/copy/$', views.ItemViewSet.copy, name='copy'), | 17 | + url(r'^<int:pk>/info/$', views.UserViewSet.info, name='info'), |
36 | - | 18 | + path('api-auth/', include('rest_framework.urls', namespace='rest_framework')), |
37 | ] | 19 | ] |
... | \ No newline at end of file | ... | \ No newline at end of file | ... | ... |
-
Please register or login to post a comment