Skip to content

Commit

Permalink
Merge pull request #407 from sparcs-kaist/feat/top-articles
Browse files Browse the repository at this point in the history
Add minor features for top articles
  • Loading branch information
injoonH authored Aug 26, 2023
2 parents fe0caab + 464ac22 commit 344c1b1
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 9 deletions.
6 changes: 3 additions & 3 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
repos:
- repo: https://github.com/psf/black
rev: 22.10.0
rev: 23.7.0
hooks:
- id: black
args: [--safe]

- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.3.0
rev: v4.4.0
hooks:
- id: check-added-large-files
- id: check-ast
Expand All @@ -19,7 +19,7 @@ repos:
- id: trailing-whitespace

- repo: https://github.com/PyCQA/isort
rev: 5.10.1
rev: 5.12.0
hooks:
- id: isort
args: [--profile=black]
21 changes: 15 additions & 6 deletions apps/core/serializers/article.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
from apps.core.documents import ArticleDocument
from apps.core.models import Article, ArticleHiddenReason, Block, Board, Comment, Scrap
from apps.core.models.board import BoardAccessPermissionType, NameType
from apps.core.serializers.attachment import AttachmentSerializer
from apps.core.serializers.board import BoardSerializer
from apps.core.serializers.mixins.hidden import (
HiddenSerializerFieldMixin,
HiddenSerializerMixin,
)
from apps.core.serializers.attachment import AttachmentSerializer
from apps.core.serializers.topic import TopicSerializer
from apps.user.serializers.user import PublicUserSerializer
from ara.classes.serializers import MetaDataModelSerializer
Expand Down Expand Up @@ -170,14 +170,22 @@ def filter_articles(obj, request):
articles = Article.objects.filter(
scrap_set__scrapped_by=request.user
).order_by("-scrap_set__created_at")

if not articles.filter(id=obj.id).exists():
raise serializers.ValidationError(
gettext("This article is not in user's scrap list.")
)

return articles

elif from_view == "top":
top_articles = Article.objects.exclude(topped_at__isnull=True).order_by(
"-topped_at", "-pk"
)
if not top_articles.filter(id=obj.id).exists():
raise serializers.ValidationError(
gettext("This article is not in top articles.")
)
return top_articles

return Article.objects.all()

def get_side_articles(self, obj) -> dict:
Expand All @@ -195,6 +203,7 @@ def get_side_articles(self, obj) -> dict:
"user",
"scrap",
"recent",
"top",
]:
raise serializers.ValidationError(
gettext("Wrong value for parameter 'from_view'.")
Expand All @@ -205,12 +214,10 @@ def get_side_articles(self, obj) -> dict:

else:
articles = self.filter_articles(obj, request)

if request.query_params.get("search_query"):
articles = self.search_articles(
articles, request.query_params.get("search_query")
)

articles = articles.exclude(id=obj.id)

if from_view == "scrap":
Expand All @@ -237,7 +244,9 @@ def get_side_articles(self, obj) -> dict:
after = after_scrap.parent_article
else:
after = None

elif from_view == "top":
before = articles.filter(topped_at__lte=obj.topped_at).first()
after = articles.filter(topped_at__gte=obj.topped_at).last()
else:
before = articles.filter(created_at__lte=obj.created_at).first()
after = articles.filter(created_at__gte=obj.created_at).last()
Expand Down
6 changes: 6 additions & 0 deletions apps/core/views/viewsets/article.py
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,12 @@ def top(self, request):
top_articles = Article.objects.exclude(topped_at__isnull=True).order_by(
"-topped_at", "-pk"
)

search_keyword = request.query_params.get("main_search__contains")
if search_keyword:
id_set = ArticleDocument.get_main_search_id_set(search_keyword)
top_articles = top_articles.filter(id__in=id_set)

page = self.paginate_queryset(top_articles)
if page is not None:
serializer = self.get_serializer(page, many=True)
Expand Down

0 comments on commit 344c1b1

Please sign in to comment.