Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

5-22 hw complete #9

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified django/seminar_project/db.sqlite3
Binary file not shown.
Binary file not shown.
Binary file modified django/seminar_project/lionapp/__pycache__/admin.cpython-38.pyc
Binary file not shown.
Binary file modified django/seminar_project/lionapp/__pycache__/apps.cpython-38.pyc
Binary file not shown.
Binary file modified django/seminar_project/lionapp/__pycache__/models.cpython-38.pyc
Binary file not shown.
Binary file not shown.
Binary file modified django/seminar_project/lionapp/__pycache__/urls.cpython-38.pyc
Binary file not shown.
Binary file modified django/seminar_project/lionapp/__pycache__/views.cpython-38.pyc
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file modified django/seminar_project/member/__pycache__/admin.cpython-38.pyc
Binary file not shown.
Binary file modified django/seminar_project/member/__pycache__/apps.cpython-38.pyc
Binary file not shown.
Binary file modified django/seminar_project/member/__pycache__/models.cpython-38.pyc
Binary file not shown.
Binary file modified django/seminar_project/member/__pycache__/urls.cpython-38.pyc
Binary file not shown.
Binary file modified django/seminar_project/member/__pycache__/views.cpython-38.pyc
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
11 changes: 11 additions & 0 deletions django/seminar_project/seminar_project/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
'member',
'util',
'lionapp',
'users', # 추가
'corsheaders',
'rest_framework',
'django.contrib.admin',
Expand Down Expand Up @@ -155,3 +156,13 @@
STATIC_URL = '/static/'


REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES': (
'rest_framework_simplejwt.authentication.JWTAuthentication',
),
}

SIMPLE_JWT = {
'USER_ID_FIELD': 'ID',
'USER_ID_CLAIM': 'user_id',
}
1 change: 1 addition & 0 deletions django/seminar_project/seminar_project/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@
path('lion/', include('lionapp.urls')),
path('member/', include('member.urls')),
path('', util_views.home, name='home'), # 기본 경로 추가
path('users/', include('users.urls'))
]
30 changes: 21 additions & 9 deletions django/seminar_project/users/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,45 +2,57 @@
from django.contrib.auth.models import BaseUserManager, AbstractBaseUser

class UserManager(BaseUserManager):
def create_user(self, email, name, password=None):

def create_user(self, email, ID, name, password=None, generation=None, gender=None):
if not email:
raise ValueError('Users must have an email address')
if not ID:
raise ValueError('Users must have an ID')

user = self.model(
email=self.normalize_email(email),
ID=ID,
name=name,
generation=generation,
gender=gender,
)

user.set_password(password)
user.save(using=self._db)
return user

def create_superuser(self, email, name, password):
def create_superuser(self, email, ID, name, password=None, generation=None, gender=None):
user = self.create_user(
email,
ID=ID,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

장고에서는 최대한 스네이크 케이스를 활용해주세요!

name=name,
password=password,
generation=generation,
gender=gender,
)

user.is_admin = True
user.save(using=self._db)
return user

class User(AbstractBaseUser):
ID = models.CharField(max_length=30, unique=True, primary_key=True)
email = models.EmailField(
verbose_name='email',
max_length=100,
unique=True,
)
name = models.CharField(max_length=30)
generation = models.IntegerField(null=True, blank=True)
gender = models.CharField(max_length=10, null=True, blank=True)
is_active = models.BooleanField(default=True)
is_admin = models.BooleanField(default=False)

objects = UserManager()

USERNAME_FIELD = 'email'
REQUIRED_FIELDS = ['name']
USERNAME_FIELD = 'ID'
REQUIRED_FIELDS = ['email', 'name']

def __str__(self):
return self.email
return self.ID

def has_perm(self, perm, obj=None):
return True
Expand All @@ -51,6 +63,6 @@ def has_module_perms(self, app_label):
@property
def is_staff(self):
return self.is_admin

class Meta:
db_table = 'user' # 테이블명을 user로 설정
db_table = 'user'
31 changes: 18 additions & 13 deletions django/seminar_project/users/serializers.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
from django.contrib.auth.models import User
from rest_framework import serializers
from django.contrib.auth import get_user_model

from .models import User

class UserSerializer(serializers.ModelSerializer):
password = serializers.CharField(write_only=True)

class Meta:
model = get_user_model()
fields = ('id', 'email', 'password', 'name')
model = User
fields = ['ID', 'email', 'name', 'password', 'generation', 'gender']
extra_kwargs = {'password': {'write_only': True}}

# 패스워드가 필요없는 다른 테이블에서 사용할 용도
class UserInfoSerializer(serializers.ModelSerializer):

class Meta:
model = get_user_model()
fields = ('id', 'email', 'name')
def create(self, validated_data):
if User.objects.filter(ID=validated_data['ID']).exists():
raise serializers.ValidationError("ID already exists.")

user = User(
email=validated_data['email'],
ID=validated_data['ID'],
name=validated_data['name'],
generation=validated_data.get('generation'),
gender=validated_data.get('gender')
)
user.set_password(validated_data['password'])
user.save()
return user
27 changes: 14 additions & 13 deletions django/seminar_project/users/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,38 +9,39 @@
from rest_framework_simplejwt.tokens import RefreshToken

from users.serializers import UserSerializer
from users.models import User

@api_view(['POST'])
@permission_classes([AllowAny])
def signup(request):
ID = request.data.get('ID')
email = request.data.get('email')
password = request.data.get('password')
name = request.data.get('name')
generation = request.data.get('generation')
gender = request.data.get('gender')

serializer = UserSerializer(data=request.data)
serializer.email = email
serializer.name = name

if serializer.is_valid(raise_exception=True):
user = serializer.save()
user.set_password(password)
user.save()

return Response(serializer.data, status=status.HTTP_201_CREATED)
serializer.is_valid(raise_exception=True)
serializer.save()

return Response(serializer.data, status=status.HTTP_201_CREATED)

@api_view(['POST'])
@permission_classes([AllowAny])
def login(request):
email = request.data.get('email')
ID = request.data.get('ID')
password = request.data.get('password')

user = authenticate(email=email, password=password)
if user is None:
try:
user = User.objects.get(ID=ID)
if not user.check_password(password):
return Response({'message': '아이디 또는 비밀번호가 일치하지 않습니다.'}, status=status.HTTP_401_UNAUTHORIZED)
except User.DoesNotExist:
return Response({'message': '아이디 또는 비밀번호가 일치하지 않습니다.'}, status=status.HTTP_401_UNAUTHORIZED)

refresh = RefreshToken.for_user(user)
update_last_login(None, user)

return Response({'refresh_token': str(refresh),
'access_token': str(refresh.access_token), }, status=status.HTTP_200_OK)
'access_token': str(refresh.access_token)}, status=status.HTTP_200_OK)
Binary file modified django/seminar_project/util/__pycache__/__init__.cpython-38.pyc
Binary file not shown.
Binary file modified django/seminar_project/util/__pycache__/admin.cpython-38.pyc
Binary file not shown.
Binary file modified django/seminar_project/util/__pycache__/apps.cpython-38.pyc
Binary file not shown.
Binary file modified django/seminar_project/util/__pycache__/models.cpython-38.pyc
Binary file not shown.
Binary file modified django/seminar_project/util/__pycache__/urls.cpython-38.pyc
Binary file not shown.
Binary file modified django/seminar_project/util/__pycache__/views.cpython-38.pyc
Binary file not shown.
Binary file not shown.