-
Notifications
You must be signed in to change notification settings - Fork 0
[GH-177] user app 개발 #179
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
Merged
Merged
[GH-177] user app 개발 #179
Changes from 4 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
0992e6d
feat
jueuunn7 c99efe1
feat: workflow update
jueuunn7 d30cb26
feat: workflow update
jueuunn7 2aa39c9
refactor: isort
jueuunn7 a847b6c
feat: precommit
jueuunn7 4a929fe
fix: rate throttle muniute
jueuunn7 56ff167
fix: cache timeout
jueuunn7 8857359
refactor: email value
jueuunn7 1c02d06
refactor: settings env
jueuunn7 a080c94
test: setup
jueuunn7 3181c24
test: setup
jueuunn7 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,9 @@ | ||
| urlpatterns = [] | ||
| from django.urls import include, path | ||
|
|
||
| urlpatterns = [ | ||
| path( | ||
| "/user", | ||
| include("api.users.urls"), | ||
| name="user", | ||
| ), | ||
| ] |
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| from drf_spectacular.utils import OpenApiExample, extend_schema | ||
|
|
||
| from api.users.serializers import SendVerifyCodeSerializer | ||
|
|
||
| email_send_schema = extend_schema( | ||
| tags=["users"], | ||
| summary="Send email verification code", | ||
| description="사용자 이메일로 인증 코드를 발송합니다. 분당 최대 2회의 요청만 허용됩니다.", | ||
| request=SendVerifyCodeSerializer, | ||
| responses={204: None}, | ||
| examples=[ | ||
| OpenApiExample( | ||
| name="Valid request", | ||
| value={"email": "[email protected]"}, | ||
| request_only=True, | ||
| ) | ||
| ], | ||
| ) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| from rest_framework import serializers | ||
|
|
||
|
|
||
| class SendVerifyCodeSerializer(serializers.Serializer): | ||
| email = serializers.EmailField( | ||
| help_text="이메일 주소", | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| from django.urls import path | ||
|
|
||
| from api.users.views import EmailRequestView | ||
|
|
||
| urlpatterns = [ | ||
| path( | ||
| "/email/send", | ||
| EmailRequestView.as_view(), | ||
| name="email-send", | ||
| ), | ||
| ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| from rest_framework import status | ||
| from rest_framework.permissions import AllowAny | ||
| from rest_framework.request import Request | ||
| from rest_framework.response import Response | ||
| from rest_framework.views import APIView | ||
|
|
||
| from api.users.schema import email_send_schema | ||
| from api.users.serializers import SendVerifyCodeSerializer | ||
| from apps.users.services import UserVerificationService | ||
| from core.exceptions import InvalidRequestError | ||
| from core.throttles import OneMinuteAnonRateThrottle | ||
|
|
||
|
|
||
| class EmailRequestView(APIView): | ||
| permission_classes = [AllowAny] | ||
| throttle_classes = [OneMinuteAnonRateThrottle] | ||
|
|
||
| @email_send_schema | ||
| def post(self, request: Request) -> Response: | ||
| input_serializer = SendVerifyCodeSerializer(data=request.data) | ||
| if not input_serializer.is_valid(): | ||
| raise InvalidRequestError | ||
|
|
||
| UserVerificationService().send_verification_code( | ||
| email=input_serializer.validated_data["email"], | ||
| ) | ||
|
|
||
| return Response(status=status.HTTP_204_NO_CONTENT) |
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| from django.apps import AppConfig | ||
|
|
||
|
|
||
| class UsersConfig(AppConfig): | ||
| default_auto_field = "django.db.models.BigAutoField" | ||
| name = "apps.users" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,133 @@ | ||
| # Generated by Django 5.2.8 on 2025-11-08 17:59 | ||
|
|
||
| import django.contrib.auth.models | ||
| import django.contrib.auth.validators | ||
| import django.utils.timezone | ||
| from django.db import migrations, models | ||
|
|
||
|
|
||
| class Migration(migrations.Migration): | ||
|
|
||
| initial = True | ||
|
|
||
| dependencies = [ | ||
| ("auth", "0012_alter_user_first_name_max_length"), | ||
| ] | ||
|
|
||
| operations = [ | ||
| migrations.CreateModel( | ||
| name="User", | ||
| fields=[ | ||
| ( | ||
| "id", | ||
| models.BigAutoField( | ||
| auto_created=True, | ||
| primary_key=True, | ||
| serialize=False, | ||
| verbose_name="ID", | ||
| ), | ||
| ), | ||
| ("password", models.CharField(max_length=128, verbose_name="password")), | ||
| ( | ||
| "last_login", | ||
| models.DateTimeField( | ||
| blank=True, null=True, verbose_name="last login" | ||
| ), | ||
| ), | ||
| ( | ||
| "is_superuser", | ||
| models.BooleanField( | ||
| default=False, | ||
| help_text="Designates that this user has all permissions without explicitly assigning them.", | ||
| verbose_name="superuser status", | ||
| ), | ||
| ), | ||
| ( | ||
| "username", | ||
| models.CharField( | ||
| error_messages={ | ||
| "unique": "A user with that username already exists." | ||
| }, | ||
| help_text="Required. 150 characters or fewer. Letters, digits and @/./+/-/_ only.", | ||
| max_length=150, | ||
| unique=True, | ||
| validators=[ | ||
| django.contrib.auth.validators.UnicodeUsernameValidator() | ||
| ], | ||
| verbose_name="username", | ||
| ), | ||
| ), | ||
| ( | ||
| "first_name", | ||
| models.CharField( | ||
| blank=True, max_length=150, verbose_name="first name" | ||
| ), | ||
| ), | ||
| ( | ||
| "last_name", | ||
| models.CharField( | ||
| blank=True, max_length=150, verbose_name="last name" | ||
| ), | ||
| ), | ||
| ( | ||
| "is_staff", | ||
| models.BooleanField( | ||
| default=False, | ||
| help_text="Designates whether the user can log into this admin site.", | ||
| verbose_name="staff status", | ||
| ), | ||
| ), | ||
| ( | ||
| "is_active", | ||
| models.BooleanField( | ||
| default=True, | ||
| help_text="Designates whether this user should be treated as active. Unselect this instead of deleting accounts.", | ||
| verbose_name="active", | ||
| ), | ||
| ), | ||
| ( | ||
| "date_joined", | ||
| models.DateTimeField( | ||
| default=django.utils.timezone.now, verbose_name="date joined" | ||
| ), | ||
| ), | ||
| ("created_at", models.DateTimeField(auto_now_add=True)), | ||
| ("updated_at", models.DateTimeField(auto_now=True)), | ||
| ( | ||
| "email", | ||
| models.EmailField( | ||
| max_length=254, unique=True, verbose_name="사용자 이메일 주소" | ||
| ), | ||
| ), | ||
| ("school", models.CharField(null=True, verbose_name="학교 코드")), | ||
| ( | ||
| "groups", | ||
| models.ManyToManyField( | ||
| blank=True, | ||
| help_text="The groups this user belongs to. A user will get all permissions granted to each of their groups.", | ||
| related_name="user_set", | ||
| related_query_name="user", | ||
| to="auth.group", | ||
| verbose_name="groups", | ||
| ), | ||
| ), | ||
| ( | ||
| "user_permissions", | ||
| models.ManyToManyField( | ||
| blank=True, | ||
| help_text="Specific permissions for this user.", | ||
| related_name="user_set", | ||
| related_query_name="user", | ||
| to="auth.permission", | ||
| verbose_name="user permissions", | ||
| ), | ||
| ), | ||
| ], | ||
| options={ | ||
| "db_table": "user", | ||
| }, | ||
| managers=[ | ||
| ("objects", django.contrib.auth.models.UserManager()), | ||
| ], | ||
| ), | ||
| ] |
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| from django.contrib.auth.models import AbstractUser | ||
| from django.db import models | ||
|
|
||
| from core.models import TimeStampedModel | ||
|
|
||
|
|
||
| class User(TimeStampedModel, AbstractUser): | ||
| email = models.EmailField( | ||
| "사용자 이메일 주소", | ||
| unique=True, | ||
| ) | ||
|
|
||
| school = models.CharField( | ||
| "학교 코드", | ||
| null=True, | ||
| ) | ||
|
|
||
| class Meta: | ||
| db_table = "user" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| import logging | ||
jueuunn7 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| from django.db import transaction | ||
|
|
||
| from apps.users.models import User | ||
| from apps.users.tasks import send_email_verify_code | ||
|
|
||
|
|
||
| class UserVerificationService: | ||
|
|
||
| @transaction.atomic | ||
| def send_verification_code(self, email: str) -> None: | ||
| User.objects.get_or_create( | ||
| email=email, | ||
| defaults={"is_active": False}, | ||
| ) | ||
|
|
||
| send_email_verify_code.delay(email=email) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| import random | ||
|
|
||
| from celery import shared_task | ||
| from django.conf import settings | ||
| from django.core.mail import EmailMultiAlternatives | ||
| from django.template.loader import render_to_string | ||
|
|
||
| from core.cache.cache import Cache | ||
| from core.cache.prefix import CacheKeyPrefix | ||
|
|
||
|
|
||
| @shared_task() | ||
| def send_email_verify_code(email: str) -> None: | ||
| code = random.randint(100000, 999999) | ||
|
|
||
| Cache.set( | ||
| prefix=CacheKeyPrefix.email_verification_code, | ||
| key=email, | ||
| value=code, | ||
| timeout=60 * 60, | ||
jueuunn7 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ) | ||
|
|
||
| html_content = render_to_string( | ||
| template_name="verify_code.html", | ||
| context={ | ||
| "recipient_name": email, | ||
| "verification_code": code, | ||
| }, | ||
| ) | ||
|
|
||
| email = EmailMultiAlternatives( | ||
jueuunn7 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| subject=f"Jusicool 메일 인증 코드", | ||
| body=html_content, | ||
| to=[email], | ||
| from_email=settings.EMAIL_HOST_USER, | ||
| ) | ||
| email.attach_alternative( | ||
| html_content, | ||
| "text/html", | ||
| ) | ||
| email.send() | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| """ | ||
| Django가 시작될 때 Celery app을 로드합니다. | ||
| """ | ||
|
|
||
| from .celery import app as celery_app | ||
|
|
||
| __all__ = ("celery_app",) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| from celery import Celery | ||
|
|
||
| app = Celery("jusicool") | ||
| app.config_from_object("django.conf:settings", namespace="CELERY") | ||
| app.autodiscover_tasks() | ||
| app.conf.timezone = "Asia/Seoul" |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.