Builds for
1 pipeline
passed
in
14 minutes 20 seconds
유저, 그룹API 추가
Showing
21 changed files
with
433 additions
and
0 deletions
khubox-api/group/__init__.py
0 → 100644
File mode changed
khubox-api/group/admin.py
0 → 100644
khubox-api/group/apps.py
0 → 100644
khubox-api/group/migrations/0001_initial.py
0 → 100644
1 | +# Generated by Django 3.0.7 on 2020-06-09 17:57 | ||
2 | + | ||
3 | +from django.conf import settings | ||
4 | +from django.db import migrations, models | ||
5 | + | ||
6 | + | ||
7 | +class Migration(migrations.Migration): | ||
8 | + | ||
9 | + initial = True | ||
10 | + | ||
11 | + dependencies = [ | ||
12 | + migrations.swappable_dependency(settings.AUTH_USER_MODEL), | ||
13 | + ] | ||
14 | + | ||
15 | + operations = [ | ||
16 | + migrations.CreateModel( | ||
17 | + name='UserGroup', | ||
18 | + fields=[ | ||
19 | + ('group_name', models.CharField(max_length=20, primary_key=True, serialize=False, unique=True)), | ||
20 | + ('owner_email', models.EmailField(max_length=254)), | ||
21 | + ('group_users', models.ManyToManyField(to=settings.AUTH_USER_MODEL, verbose_name='Group User')), | ||
22 | + ], | ||
23 | + ), | ||
24 | + ] |
khubox-api/group/migrations/__init__.py
0 → 100644
File mode changed
khubox-api/group/models.py
0 → 100644
1 | +from django.db import models | ||
2 | +from user.models import User | ||
3 | + | ||
4 | +class UserGroup(models.Model): | ||
5 | + #group_id = models.AutoField(primary_key = True, blank = True) | ||
6 | + group_name = models.CharField(primary_key = True, unique = True, max_length=20) | ||
7 | + owner_email = models.EmailField(max_length=254) | ||
8 | + #owner = models.ForeignKey(User, verbose_name="Group Owner", on_delete=models.CASCADE) | ||
9 | + group_users = models.ManyToManyField(User, verbose_name="Group User") | ||
10 | + | ||
11 | + def user_to_group(self, user): | ||
12 | + self.group_users.add(user) | ||
13 | + #UserGroup.save(using=self._db) | ||
14 | + #return group | ||
15 | + | ||
16 | +# Create your models here. |
khubox-api/group/serializers.py
0 → 100644
1 | +from rest_framework import serializers | ||
2 | +from .models import UserGroup | ||
3 | + | ||
4 | +class CreateUserGroupSerializer(serializers.Serializer): | ||
5 | + group_name = serializers.CharField(allow_blank=False, max_length = 100) | ||
6 | + | ||
7 | +class AddUserSerializer(serializers.Serializer): | ||
8 | + group_name = serializers.CharField(allow_blank=False, max_length = 100) | ||
9 | + user_id = serializers.IntegerField() | ||
10 | + | ||
11 | +class UserGroupSerializer(serializers.ModelSerializer): | ||
12 | + class Meta: | ||
13 | + model = UserGroup | ||
14 | + fields = ("group_name","owner_email","group_users") |
khubox-api/group/tests.py
0 → 100644
khubox-api/group/urls.py
0 → 100644
1 | +from django.conf.urls import url, include | ||
2 | +from .views import ( | ||
3 | + CreateGroupAPI, | ||
4 | + GroupListAPI, | ||
5 | + AddUserAPI | ||
6 | + ) | ||
7 | + | ||
8 | +urlpatterns =[ | ||
9 | + url("^creategroup/$", CreateGroupAPI.as_view()), | ||
10 | + url("^grouplist/$", GroupListAPI.as_view()), | ||
11 | + url("^adduser/$", AddUserAPI.as_view()), | ||
12 | +] |
khubox-api/group/views.py
0 → 100644
1 | +from rest_framework import viewsets, permissions, generics, mixins, status | ||
2 | +from rest_framework.response import Response | ||
3 | +from knox.models import AuthToken | ||
4 | + | ||
5 | +from .models import UserGroup | ||
6 | +from user.models import User | ||
7 | + | ||
8 | +from .serializers import (UserGroupSerializer, CreateUserGroupSerializer, AddUserSerializer) | ||
9 | +from user.serializers import UserSerializer | ||
10 | + | ||
11 | +class CreateGroupAPI(generics.GenericAPIView): | ||
12 | + serializer_class = CreateUserGroupSerializer | ||
13 | + permission_classes = [permissions.IsAuthenticated] | ||
14 | + | ||
15 | + def post(self, request, *args, **kwargs): | ||
16 | + serializer = self.get_serializer(data=request.data) | ||
17 | + serializer.is_valid(raise_exception=True) | ||
18 | + | ||
19 | + input_name = request.data["group_name"] | ||
20 | + input_owner = UserSerializer(self.request.user).data | ||
21 | + | ||
22 | + group, created = UserGroup.objects.get_or_create(group_name = input_name, owner_email = input_owner["email"]) | ||
23 | + if created: | ||
24 | + group.save() | ||
25 | + group.user_to_group(request.user) | ||
26 | + group.save() | ||
27 | + else: | ||
28 | + body = {"message": "already exist"} | ||
29 | + return Response(body, status=status.HTTP_400_BAD_REQUEST) | ||
30 | + | ||
31 | + return Response(UserGroupSerializer(group).data) | ||
32 | + | ||
33 | + def get(self, request, *args, **kwargs): | ||
34 | + return self.retrieve(request, *args, **kwargs) | ||
35 | + | ||
36 | +class AddUserAPI(generics.GenericAPIView): | ||
37 | + serializer_class = AddUserSerializer | ||
38 | + permission_classes = [permissions.IsAuthenticated] | ||
39 | + | ||
40 | + def post(self, request, *args, **kwargs): | ||
41 | + serializer = self.get_serializer(data=request.data) | ||
42 | + serializer.is_valid(raise_exception=True) | ||
43 | + | ||
44 | + input_name = request.data["group_name"] | ||
45 | + input_user = request.data["user_id"] | ||
46 | + input_owner = UserSerializer(self.request.user).data | ||
47 | + | ||
48 | + group, created = UserGroup.objects.get_or_create(group_name = input_name) | ||
49 | + | ||
50 | + | ||
51 | + if(input_owner["email"]!=UserGroupSerializer(group).data["owner_email"]): | ||
52 | + body = {"message": "not group owner"} | ||
53 | + return Response(body, status=status.HTTP_401_UNAUTHORIZED) | ||
54 | + | ||
55 | + if created: | ||
56 | + body = {"message": "group doesn't exist"} | ||
57 | + return Response(body, status=status.HTTP_400_BAD_REQUEST) | ||
58 | + else: | ||
59 | + try : | ||
60 | + user= User.objects.get(id = input_user) | ||
61 | + except User.DoesNotExist: | ||
62 | + body = {"message": "user doesn't exist"} | ||
63 | + return Response(body, status=status.HTTP_400_BAD_REQUEST) | ||
64 | + group.user_to_group(user) | ||
65 | + group.save() | ||
66 | + | ||
67 | + return Response(UserGroupSerializer(group).data) | ||
68 | + | ||
69 | + def get(self, request, *args, **kwargs): | ||
70 | + return self.retrieve(request, *args, **kwargs) | ||
71 | + | ||
72 | + | ||
73 | +class GroupListAPI(generics.ListAPIView): | ||
74 | + queryset = UserGroup.objects.all() | ||
75 | + serializer_class = UserGroupSerializer | ||
76 | + | ||
77 | +#class GroupListAPI(generics.GenericAPIView): | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
khubox-api/user/__init__.py
0 → 100644
File mode changed
khubox-api/user/admin.py
0 → 100644
khubox-api/user/apps.py
0 → 100644
khubox-api/user/migrations/0001_initial.py
0 → 100644
1 | +# Generated by Django 3.0.7 on 2020-06-09 17:57 | ||
2 | + | ||
3 | +import datetime | ||
4 | +from django.db import migrations, models | ||
5 | +import user.models | ||
6 | + | ||
7 | + | ||
8 | +class Migration(migrations.Migration): | ||
9 | + | ||
10 | + initial = True | ||
11 | + | ||
12 | + dependencies = [ | ||
13 | + ] | ||
14 | + | ||
15 | + operations = [ | ||
16 | + migrations.CreateModel( | ||
17 | + name='User', | ||
18 | + fields=[ | ||
19 | + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), | ||
20 | + ('password', models.CharField(max_length=128, verbose_name='password')), | ||
21 | + ('last_login', models.DateTimeField(blank=True, null=True, verbose_name='last login')), | ||
22 | + ('email', models.EmailField(max_length=254, unique=True)), | ||
23 | + ('name', models.CharField(max_length=100)), | ||
24 | + ('date_of_birth', models.DateField(default=datetime.date.today)), | ||
25 | + ('storage_usage', models.FloatField(default=0)), | ||
26 | + ('profile', models.ImageField(upload_to=None)), | ||
27 | + ('is_admin', models.BooleanField(default=False)), | ||
28 | + ], | ||
29 | + options={ | ||
30 | + 'abstract': False, | ||
31 | + }, | ||
32 | + managers=[ | ||
33 | + ('objects', user.models.UserManager()), | ||
34 | + ], | ||
35 | + ), | ||
36 | + ] |
khubox-api/user/migrations/__init__.py
0 → 100644
File mode changed
khubox-api/user/models.py
0 → 100644
1 | +from django.db import models | ||
2 | +from django.contrib.auth.models import AbstractBaseUser,BaseUserManager | ||
3 | +import datetime | ||
4 | + | ||
5 | + | ||
6 | +class UserManager(BaseUserManager): | ||
7 | + use_in_migrations = True | ||
8 | + | ||
9 | + def create_user(self, email, name, date_of_birth, password=None): | ||
10 | + user = self.model( | ||
11 | + email=self.normalize_email(email), | ||
12 | + date_of_birth=date_of_birth, | ||
13 | + name=name, | ||
14 | + ) | ||
15 | + user.set_password(password) | ||
16 | + user.save(using=self._db) | ||
17 | + return user | ||
18 | + | ||
19 | + def create_staffuser(self, email, name, date_of_birth, password): | ||
20 | + user = self.create_user( | ||
21 | + email, | ||
22 | + password=password, | ||
23 | + date_of_birth=date_of_birth, | ||
24 | + name=name, | ||
25 | + ) | ||
26 | + user.staff = True | ||
27 | + user.save(using=self._db) | ||
28 | + return user | ||
29 | + | ||
30 | + def create_superuser(self, email, name, date_of_birth, password): | ||
31 | + user = self.create_user( | ||
32 | + email, | ||
33 | + password=password, | ||
34 | + date_of_birth=date_of_birth, | ||
35 | + name= "True", | ||
36 | + ) | ||
37 | + #user.is_staff = True | ||
38 | + user.is_admin = True | ||
39 | + user.save(using=self._db) | ||
40 | + return user | ||
41 | + | ||
42 | + | ||
43 | + | ||
44 | + | ||
45 | +class User(AbstractBaseUser): | ||
46 | + | ||
47 | + username = None | ||
48 | + email = models.EmailField( unique=True) | ||
49 | + name = models.CharField(max_length=100) | ||
50 | + date_of_birth = models.DateField(default=datetime.date.today) | ||
51 | + storage_usage = models.FloatField( default = 0) | ||
52 | + profile = models.ImageField(upload_to=None, height_field=None, width_field=None, max_length=None) | ||
53 | + | ||
54 | + is_admin = models.BooleanField(default=False) | ||
55 | + | ||
56 | + USERNAME_FIELD = 'email' | ||
57 | + REQUIRED_FIELDS = [ 'date_of_birth','name' ] | ||
58 | + | ||
59 | + objects = UserManager() | ||
60 | + | ||
61 | + def __str__(self): | ||
62 | + return self.email | ||
63 | + | ||
64 | + @property | ||
65 | + def is_staff(self): | ||
66 | + return self.is_admin | ||
67 | + | ||
68 | + def has_perm(self, perm, obj = None): | ||
69 | + return True | ||
70 | + | ||
71 | + def has_module_perms(self, app_label): | ||
72 | + return True | ||
73 | + |
khubox-api/user/serializers.py
0 → 100644
1 | +from rest_framework import serializers | ||
2 | +from .models import User | ||
3 | +from django.contrib.auth import authenticate | ||
4 | + | ||
5 | + | ||
6 | +# 회원가입 시리얼라이저 | ||
7 | + | ||
8 | +class CreateUserSerializer(serializers.ModelSerializer): | ||
9 | + class Meta: | ||
10 | + model = User | ||
11 | + fields = ("email", "name", "date_of_birth", "password") | ||
12 | + extra_kwargs = {"password": {"write_only": True}} | ||
13 | + | ||
14 | + def create(self, validated_data): | ||
15 | + user = User.objects.create_user( | ||
16 | + validated_data["email"], | ||
17 | + validated_data["name"], | ||
18 | + validated_data["date_of_birth"], | ||
19 | + validated_data["password"] | ||
20 | + ) | ||
21 | + return user | ||
22 | + | ||
23 | + | ||
24 | +# 접속 유지중인지 확인할 시리얼라이저 | ||
25 | + | ||
26 | +class UserSerializer(serializers.ModelSerializer): | ||
27 | + class Meta: | ||
28 | + model = User | ||
29 | + fields = ("id","email", "name","date_of_birth","storage_usage","usergroup_set") | ||
30 | + | ||
31 | + | ||
32 | +# 로그인 시리얼라이저 | ||
33 | + | ||
34 | +class LoginUserSerializer(serializers.Serializer): | ||
35 | + email = serializers.EmailField() | ||
36 | + password = serializers.CharField() | ||
37 | + | ||
38 | + def validate(self, data): | ||
39 | + user = authenticate(**data) | ||
40 | + if user and user.is_active: | ||
41 | + return user | ||
42 | + raise serializers.ValidationError("Unable to log in with provided credentials.") | ||
43 | + | ||
44 | + | ||
45 | +class UpdateUserSerializer(serializers.ModelSerializer): | ||
46 | + class Meta: | ||
47 | + model = User | ||
48 | + fields = ("email", "name", "date_of_birth","storage_usage", "password") | ||
49 | + extra_kwargs = {"password": {"write_only": True}} | ||
50 | + read_only_fields = ('email',"storage_usage") | ||
51 | + | ||
52 | + def update(self, instance, validated_data): | ||
53 | + | ||
54 | + password = validated_data.pop('password', None) | ||
55 | + | ||
56 | + for (key, value) in validated_data.items(): | ||
57 | + setattr(instance, key, value) | ||
58 | + | ||
59 | + if password is not None: | ||
60 | + instance.set_password(password) | ||
61 | + | ||
62 | + instance.save() | ||
63 | + | ||
64 | + return instance |
khubox-api/user/tests.py
0 → 100644
khubox-api/user/urls.py
0 → 100644
1 | +from django.conf.urls import url, include | ||
2 | +from .views import (RegistrationAPI, | ||
3 | + LoginAPI, | ||
4 | + UserAPI, | ||
5 | + RegistrationAPI, | ||
6 | + UserListAPI, | ||
7 | + UserUpdateAPI, | ||
8 | + ) | ||
9 | + | ||
10 | +urlpatterns =[ | ||
11 | + url("^register/$", RegistrationAPI.as_view()), | ||
12 | + url("^login/$", LoginAPI.as_view()), | ||
13 | + url("^user/$", UserAPI.as_view()), | ||
14 | + url("^update/$", UserUpdateAPI.as_view()), | ||
15 | + url("^userlist/$", UserListAPI.as_view()), | ||
16 | + url("",include("knox.urls")), | ||
17 | +] | ||
18 | + |
khubox-api/user/views.py
0 → 100644
1 | +from rest_framework import viewsets, permissions, generics, mixins, status | ||
2 | +from rest_framework.response import Response | ||
3 | +from .models import User | ||
4 | +from .serializers import ( | ||
5 | + CreateUserSerializer, | ||
6 | + UserSerializer, | ||
7 | + LoginUserSerializer, | ||
8 | + UpdateUserSerializer, | ||
9 | +) | ||
10 | +from knox.models import AuthToken | ||
11 | + | ||
12 | + | ||
13 | +class RegistrationAPI(generics.GenericAPIView): | ||
14 | + serializer_class = CreateUserSerializer | ||
15 | + | ||
16 | + def post(self, request, *args, **kwargs): | ||
17 | + if len(request.data["email"]) < 6 or len(request.data["password"]) < 4: | ||
18 | + body = {"message": "short field"} | ||
19 | + return Response(body, status=status.HTTP_400_BAD_REQUEST) | ||
20 | + serializer = self.get_serializer(data=request.data) | ||
21 | + serializer.is_valid(raise_exception=True) | ||
22 | + #return Response(serializer.name) | ||
23 | + user = serializer.save() | ||
24 | + return Response( | ||
25 | + { | ||
26 | + "user": UserSerializer( | ||
27 | + user, context=self.get_serializer_context() | ||
28 | + ).data, | ||
29 | + "token": AuthToken.objects.create(user)[1], | ||
30 | + } | ||
31 | + ) | ||
32 | + | ||
33 | + | ||
34 | +class LoginAPI(generics.GenericAPIView): | ||
35 | + serializer_class = LoginUserSerializer | ||
36 | + | ||
37 | + def post(self, request, *args, **kwargs): | ||
38 | + serializer = self.get_serializer(data=request.data) | ||
39 | + serializer.is_valid(raise_exception=True) | ||
40 | + user = serializer.validated_data | ||
41 | + return Response( | ||
42 | + { | ||
43 | + "user": UserSerializer( | ||
44 | + user, context=self.get_serializer_context() | ||
45 | + ).data, | ||
46 | + "token": AuthToken.objects.create(user)[1], | ||
47 | + } | ||
48 | + ) | ||
49 | + | ||
50 | + | ||
51 | +class UserAPI(generics.RetrieveAPIView): | ||
52 | + permission_classes = [permissions.IsAuthenticated] | ||
53 | + serializer_class = UserSerializer | ||
54 | + | ||
55 | + def get_object(self): | ||
56 | + return self.request.user | ||
57 | + | ||
58 | + | ||
59 | +class UserListAPI(generics.ListAPIView): | ||
60 | + queryset = User.objects.all() | ||
61 | + serializer_class = UserSerializer | ||
62 | + | ||
63 | + | ||
64 | +class UserUpdateAPI(generics.RetrieveUpdateAPIView): | ||
65 | + permission_classes = [permissions.IsAuthenticated] | ||
66 | + serializer_class = UpdateUserSerializer | ||
67 | + def retrieve(self, request, *args, **kwargs): | ||
68 | + serializer = self.serializer_class(request.user) | ||
69 | + return Response(serializer.data, status=status.HTTP_200_OK) | ||
70 | + def update(self, request, *args, **kwargs): | ||
71 | + serializer = self.serializer_class(request.user, data=request.data, partial=True) | ||
72 | + serializer.is_valid(raise_exception=True) | ||
73 | + serializer.save() | ||
74 | + return Response(serializer.data, status=status.HTTP_200_OK) |
-
Please register or login to post a comment