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

nplusone 적용 및 코드 리팩토링 #112

Closed
wants to merge 4 commits into from
Closed
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
11 changes: 8 additions & 3 deletions article/views/article_list_create_api_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

class ArticleListCreateAPIView(BaseView, ListCreateAPIView):
serializer_class = ArticleSerializer
queryset = Article.objects.all().prefetch_related("bookmarks", "feedbacks")
queryset = Article.objects.all().prefetch_related("user")
permission_classes = [IsAuthenticatedOrReadOnly]
filter_backends = [filters.SearchFilter, filters.OrderingFilter, TagFilter]
search_fields = ["title", "content"]
Expand All @@ -43,6 +43,9 @@ def get(self, request, *args, **kwargs):
user_id = request.GET.get("user_id")

if tab is not None:
if self.is_anonymous_user:
return Response(status=204)

if tab == "follow":
following_user_ids = Follow.objects.filter(
follower=self.current_user
Expand All @@ -59,8 +62,10 @@ def get(self, request, *args, **kwargs):
studied_article_ids = Study.objects.filter(
user=self.current_user
).values_list("article__id", flat=True)

queryset = queryset.filter(id__in=studied_article_ids).order_by('studies')

queryset = queryset.filter(id__in=studied_article_ids).order_by(
"studies"
)

if user_id is not None:
queryset = queryset.filter(user__id=user_id)
Expand Down
2 changes: 1 addition & 1 deletion article/views/article_retrieve_update_destroy_api_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

class ArticleRetrieveUpdateDestroyAPIView(BaseView, RetrieveUpdateDestroyAPIView):
serializer_class = ArticleSerializer
queryset = Article.objects.all().prefetch_related("bookmarks", "feedbacks")
queryset = Article.objects.all()
permission_classes = [IsArticleEditableOrDestroyable]

def get(self, request, *args, **kwargs):
Expand Down
2 changes: 1 addition & 1 deletion article/views/article_sub_retrieve_api_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

class ArticleSubRetrieveAPIView(BaseView, RetrieveAPIView):
serializer_class = ArticleSubSerializer
queryset = Article.objects.all().prefetch_related("bookmarks", "feedbacks")
queryset = Article.objects.all()
permission_classes = [AllowAny]

def get(self, request, *args, **kwargs):
Expand Down
35 changes: 35 additions & 0 deletions config/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import os
from pathlib import Path
from datetime import timedelta
import logging

BASE_DIR = Path(__file__).resolve().parent.parent
SITE_ID = 1
Expand Down Expand Up @@ -63,6 +64,8 @@
# django-extensions
"django_extensions",
"django_celery_beat",
# nplusone
"nplusone.ext.django",
]

DJANGO_APPS = [
Expand All @@ -85,6 +88,7 @@
"django.contrib.auth.middleware.AuthenticationMiddleware",
"django.contrib.messages.middleware.MessageMiddleware",
"django.middleware.clickjacking.XFrameOptionsMiddleware",
"nplusone.ext.django.NPlusOneMiddleware",
]

ROOT_URLCONF = "config.urls"
Expand Down Expand Up @@ -214,6 +218,37 @@
"USE_SESSION_AUTH": False,
}

NPLUSONE_LOGGER = logging.getLogger("nplusone")
NPLUSONE_LOG_LEVEL = logging.DEBUG
NPLUSONE_RAISE = False
NPLUSONE_LOG = False

LOGGING = {
"version": 1,
"handlers": {
"console": {
"class": "logging.StreamHandler",
},
},
"loggers": {
"nplusone": {
"handlers": ["console"],
"level": "DEBUG",
},
},
}

NPLUSONE_WHITELIST = [
{"model": "admin.LogEntry", "label": "unused_eager_load"},
{"model": "article.Article", "label": "todo"},
{"model": "article.Bookmark", "label": "todo"},
{"model": "article.Feedback", "label": "todo"},
{"model": "article.UploadImage", "label": "todo"},
{"model": "comment.Comment", "label": "todo"},
{"model": "study.Study", "label": "todo"},
{"model": "user.User", "label": "todo"},
]

CORS_ORIGIN_ALLOW_ALL = True

CORS_ALLOW_CREDENTIALS = True
Expand Down
3 changes: 2 additions & 1 deletion config/wsgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
import os

from django.core.wsgi import get_wsgi_application
from nplusone.ext.wsgi import NPlusOneMiddleware

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "config.settings")

application = get_wsgi_application()
application = NPlusOneMiddleware(get_wsgi_application())
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ astroid==2.9.3
backports.zoneinfo==0.2.1
billiard==3.6.4.0
black==21.12b0
blinker==1.4
celery==5.2.3
certifi==2021.10.8
cffi==1.15.0
Expand Down Expand Up @@ -52,6 +53,7 @@ lizard==1.17.9
MarkupSafe==2.0.1
mccabe==0.6.1
mypy-extensions==0.4.3
nplusone==1.0.0
oauthlib==3.1.1
packaging==21.3
pathspec==0.9.0
Expand Down
5 changes: 4 additions & 1 deletion tag/views/tag_list_api_view.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
from rest_framework.generics import ListAPIView
from rest_framework import filters
from rest_framework.generics import ListAPIView
from rest_framework.permissions import IsAuthenticatedOrReadOnly

from config.views import BaseView
from taggit.models import Tag
from tag.serializers import TagSerializer


class TagListAPIView(BaseView, ListAPIView):
serializer_class = TagSerializer
permission_classes = [IsAuthenticatedOrReadOnly]
queryset = Tag.objects.all()
filter_backends = [filters.SearchFilter]
search_fields = ["^name"]
Expand Down