-
Notifications
You must be signed in to change notification settings - Fork 1
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
Database #77
Database #77
Changes from all commits
06c36cd
5f8f2d7
7036b67
ff11afa
ab2fb2e
96c7ee9
0609055
853ee3d
113fca9
cbc2916
217d835
7f30fdb
f21bbd5
282b0af
36a82fa
a8ce5d9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -143,7 +143,7 @@ | |
|
||
# 국제화 설정 | ||
LANGUAGE_CODE = "en-us" | ||
TIME_ZONE = "UTC" | ||
TIME_ZONE = "Asia/Seoul" | ||
USE_I18N = True | ||
USE_TZ = True | ||
|
||
|
@@ -201,9 +201,10 @@ | |
"repo", | ||
"read:org", | ||
], | ||
"ON_DELETE": "CASCADE", | ||
} | ||
} | ||
|
||
SOCIALACCOUNT_LOGIN_ON_GET = True | ||
LOGIN_REDIRECT_URL = "https://d3hcv7ngm54uy8.cloudfront.net/oauth-callback" | ||
ACCOUNT_LOGOUT_REDIRECT_URL = "https://d3hcv7ngm54uy8.cloudfront.net/landing" | ||
LOGIN_REDIRECT_URL = "/oauth-callback/" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 클라우드 프론드 대신에 엔드포인트 추가 |
||
ACCOUNT_LOGOUT_REDIRECT_URL = "/landing/" |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,17 +2,17 @@ | |
from django.urls import include, path | ||
|
||
urlpatterns = [ | ||
path("admin/", admin.site.urls), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 엔드포인트 앞에 api 명시해서 우선순위 구분 |
||
path("accounts/", include("dj_rest_auth.registration.urls")), | ||
path("accounts/", include("dj_rest_auth.urls")), | ||
path("accounts/", include("allauth.urls")), | ||
path("accounts/", include("users.urls")), | ||
path("baekjoons/", include("baekjoons.urls")), | ||
path("attendances/", include("attendances.urls")), | ||
path("potatoes/", include("potatoes.urls")), | ||
path("potatoes/", include("potato_types.urls")), | ||
path("stacks/", include("stacks.urls")), | ||
path("stacks/", include("user_stacks.urls")), | ||
path("todos/", include("todos.urls")), | ||
path("githubs/", include("githubs.urls")), | ||
path("api/admin/", admin.site.urls), | ||
path("api/accounts/", include("dj_rest_auth.registration.urls")), | ||
path("api/accounts/", include("dj_rest_auth.urls")), | ||
path("api/accounts/", include("allauth.urls")), | ||
path("api/accounts/", include("users.urls")), | ||
path("api/baekjoons/", include("baekjoons.urls")), | ||
path("api/attendances/", include("attendances.urls")), | ||
path("api/potatoes/", include("potatoes.urls")), | ||
path("api/potatoes/", include("potato_types.urls")), | ||
path("api/stacks/", include("stacks.urls")), | ||
path("api/stacks/", include("user_stacks.urls")), | ||
path("api/todos/", include("todos.urls")), | ||
path("api/githubs/", include("githubs.urls")), | ||
] |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -57,52 +57,24 @@ class PotatoSelectPatch(APIView): | |
|
||
def patch(self, request): | ||
try: | ||
potato = Potato.objects.filter( | ||
user=request.user, data=request.data, partial=True | ||
) # request.user 사용 | ||
potato_id = request.data.get("id") | ||
potato = Potato.objects.get(id=potato_id, user=request.user) | ||
|
||
if not potato.exists(): | ||
if not potato: | ||
return Response( | ||
{"detail": "Not found"}, status=status.HTTP_404_NOT_FOUND | ||
) | ||
|
||
if "is_selected" not in request.data: | ||
return Response( | ||
{"detail": "is_selected field is required"}, | ||
status=status.HTTP_400_BAD_REQUEST, | ||
) | ||
|
||
is_selected = request.data["is_selected"] | ||
if is_selected: | ||
# 유저가 가지고 있는 모든 감자의 is_selected 값을 False로 | ||
Potato.objects.filter(user=request.user).update(is_selected=False) | ||
|
||
# 유저가 선택한 감자의 is_selected 값을 True로 | ||
serializer = PotatoSerializer( | ||
potato.first(), data={"is_selected": is_selected}, partial=True | ||
) | ||
# 기존에 선택된 감자의 is_selected 값을 False로 변경 | ||
Potato.objects.filter(user=request.user).update(is_selected=False) | ||
# 현재 선택된 감자의 is_selected 값을 True로 변경 | ||
potato.is_selected = True # 객체 속성 직접 변경 | ||
potato.save() # 변경 사항 저장 | ||
|
||
if serializer.is_valid(): | ||
serializer.save() | ||
return Response(serializer.data) | ||
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) | ||
serializer = PotatoSerializer(potato) # 업데이트된 객체 다시 가져오기 | ||
return Response(serializer.data) | ||
|
||
except DatabaseError as e: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 예외처리..? |
||
return Response( | ||
{"error": "Database error occurred"}, | ||
status=status.HTTP_500_INTERNAL_SERVER_ERROR, | ||
) | ||
except ValidationError as e: | ||
return Response( | ||
{"error": "Serialization error occurred", "details": e.detail}, | ||
status=status.HTTP_400_BAD_REQUEST, | ||
) | ||
except ObjectDoesNotExist as e: | ||
return Response( | ||
{"error": "Object does not exist", "details": str(e)}, | ||
status=status.HTTP_404_NOT_FOUND, | ||
) | ||
except Exception as e: | ||
except Exception as e: # Exception handling remains the same | ||
return Response( | ||
{"error": "An unexpected error occurred", "details": str(e)}, | ||
status=status.HTTP_500_INTERNAL_SERVER_ERROR, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,12 +6,10 @@ | |
|
||
urlpatterns = [ | ||
path("create/", TodoCreateView.as_view()), | ||
path("<int:pk>/update/", TodoUpdateView.as_view()), | ||
path("<int:pk>/delete/", TodoDeleteView.as_view()), | ||
path("todos/<int:pk>/done/", TodoMarkDoneView.as_view(), name="todo-mark-done"), | ||
path( | ||
"todos/<int:pk>/undone/", TodoMarkUndoneView.as_view(), name="todo-mark-undone" | ||
), | ||
path("<int:id>/update/", TodoUpdateView.as_view()), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 아이디 추가 |
||
path("<int:id>/delete/", TodoDeleteView.as_view()), | ||
path("<int:id>/done/", TodoMarkDoneView.as_view(), name="todo-mark-done"), | ||
path("<int:id>/undone/", TodoMarkUndoneView.as_view(), name="todo-mark-undone"), | ||
path("today/", TodayTodoListView.as_view()), | ||
path("<str:date>/", DailyTodoListView.as_view()), # 'YYYY-MM-DD' 형식 | ||
path( | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
from datetime import datetime, timedelta | ||
|
||
from django.db.models import Count, Q | ||
from django.shortcuts import get_object_or_404 | ||
from django.utils import timezone | ||
from rest_framework import generics, permissions, status | ||
from rest_framework.permissions import IsAuthenticated | ||
|
@@ -18,7 +19,12 @@ class TodoCreateView(generics.CreateAPIView): | |
def perform_create(self, serializer): | ||
date_str = self.request.data.get("date") # 프론트엔드에서 전달된 날짜 문자열 | ||
try: | ||
date = datetime.strptime(date_str, "%Y-%m-%d").date() | ||
# datetime 객체 생성 (시간은 00:00:00으로 설정) | ||
date_obj = datetime.strptime(date_str, "%Y-%m-%d").replace( | ||
hour=0, minute=0, second=0, microsecond=0 | ||
) | ||
# timezone-aware datetime 객체로 변환 | ||
date = timezone.make_aware(date_obj) | ||
except (ValueError, TypeError): | ||
return Response( | ||
{"error": "Invalid date format or missing date."}, | ||
|
@@ -30,17 +36,25 @@ def perform_create(self, serializer): | |
|
||
# 2. 투두리스트 항목 수정 (UI에서 입력 받은 데이터 + 선택된 날짜로 수정) | ||
class TodoUpdateView(generics.UpdateAPIView): | ||
queryset = Todo.objects.all() | ||
serializer_class = TodoSerializer | ||
permission_classes = [IsAuthenticated] | ||
|
||
def get_object(self): | ||
todo_id = self.kwargs.get("id") | ||
return get_object_or_404(Todo, id=todo_id, user=self.request.user) | ||
|
||
def get_queryset(self): | ||
return super().get_queryset().filter(user=self.request.user) | ||
|
||
def perform_update(self, serializer): | ||
date_str = self.request.data.get("date") | ||
try: | ||
date = datetime.strptime(date_str, "%Y-%m-%d").date() | ||
# datetime 객체 생성 (시간은 00:00:00으로 설정) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 시간설정 |
||
date_obj = datetime.strptime(date_str, "%Y-%m-%d").replace( | ||
hour=0, minute=0, second=0, microsecond=0 | ||
) | ||
# timezone-aware datetime 객체로 변환 | ||
date = timezone.make_aware(date_obj) | ||
except (ValueError, TypeError): | ||
return Response( | ||
{"error": "Invalid date format or missing date."}, | ||
|
@@ -52,18 +66,25 @@ def perform_update(self, serializer): | |
|
||
# 3. 투두리스트 항목 삭제 | ||
class TodoDeleteView(generics.DestroyAPIView): | ||
queryset = Todo.objects.all() | ||
permission_classes = [IsAuthenticated] | ||
|
||
def get_object(self): | ||
todo_id = self.kwargs.get("id") | ||
return get_object_or_404(Todo, id=todo_id, user=self.request.user) | ||
|
||
def get_queryset(self): | ||
return super().get_queryset().filter(user=self.request.user) | ||
|
||
|
||
# 4. 투두리스트 is_done True<->False | ||
class TodoMarkDoneView(generics.UpdateAPIView): | ||
queryset = Todo.objects.all() | ||
permission_classes = [IsAuthenticated] | ||
serializer_class = TodoSerializer | ||
http_method_names = ["patch"] # PATCH 메서드만 허용 | ||
|
||
def get_object(self): | ||
todo_id = self.kwargs.get("id") | ||
return get_object_or_404(Todo, id=todo_id, user=self.request.user) | ||
|
||
def get_queryset(self): | ||
return super().get_queryset().filter(user=self.request.user) | ||
|
@@ -74,9 +95,13 @@ def perform_update(self, serializer): | |
|
||
|
||
class TodoMarkUndoneView(generics.UpdateAPIView): | ||
queryset = Todo.objects.all() | ||
permission_classes = [IsAuthenticated] | ||
serializer_class = TodoSerializer | ||
http_method_names = ["patch"] # PATCH 메서드만 허용 | ||
|
||
def get_object(self): | ||
todo_id = self.kwargs.get("id") | ||
return get_object_or_404(Todo, id=todo_id, user=self.request.user) | ||
|
||
def get_queryset(self): | ||
return super().get_queryset().filter(user=self.request.user) | ||
|
@@ -92,7 +117,7 @@ class TodayTodoListView(generics.ListAPIView): | |
permission_classes = [IsAuthenticated] | ||
|
||
def get_queryset(self): | ||
today = timezone.now().date() | ||
today = timezone.localtime(timezone.now()).date() | ||
return Todo.objects.filter(user=self.request.user, date__date=today) | ||
|
||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,12 +21,24 @@ def get(self, request): # user_id 매개변수 제거 | |
class UserStackCreate(APIView): | ||
permission_classes = [IsAuthenticated] | ||
|
||
def post(self, request): # user_id 매개변수 제거 | ||
serializer = UserStackSerializer(data=request.data) | ||
if serializer.is_valid(): | ||
serializer.save(user=request.user) # request.user 사용 | ||
return Response(serializer.data, status=status.HTTP_201_CREATED) | ||
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) | ||
def post(self, request): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. post내용에 스택 아이디 추가 |
||
stack_id = request.data.get("stack_id") # stack_id 받아오기 | ||
|
||
if not stack_id: | ||
return Response( | ||
{"error": "stack_id is required"}, status=status.HTTP_400_BAD_REQUEST | ||
) | ||
|
||
try: | ||
user_stack = UserStack.objects.create(user=request.user, stack_id=stack_id) | ||
return Response( | ||
{"message": "UserStack created", "id": user_stack.id}, | ||
status=status.HTTP_201_CREATED, | ||
) | ||
except Exception as e: | ||
return Response( | ||
{"error": str(e)}, status=status.HTTP_500_INTERNAL_SERVER_ERROR | ||
) | ||
|
||
|
||
# # 유저 스택 업데이트 | ||
|
@@ -58,9 +70,9 @@ def post(self, request): # user_id 매개변수 제거 | |
class UserStackDelete(APIView): | ||
permission_classes = [IsAuthenticated] | ||
|
||
def delete(self, request, stack_id): | ||
def delete(self, request, user_stack_id): | ||
try: | ||
stack = UserStack.objects.get(user=request.user, id=stack_id) | ||
stack = UserStack.objects.get(user=request.user, id=user_stack_id) | ||
except UserStack.DoesNotExist: | ||
return Response( | ||
{"error": "Stack not found for this user"}, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
배포용으로 바꾸신거죠?