You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
43 lines
1.4 KiB
43 lines
1.4 KiB
import logging
|
|
from django.http import JsonResponse
|
|
from django.db.models import Q
|
|
|
|
from blog.models import Article, Category
|
|
logger = logging.getLogger(__name__)
|
|
|
|
class BlogApi:
|
|
def __init__(self):
|
|
self.searchqueryset = Article.objects.all()
|
|
|
|
def search_articles(self, query):
|
|
if query:
|
|
# 使用 Q 对象进行多字段搜索
|
|
results = Article.objects.filter(
|
|
Q(title__icontains=query) |
|
|
Q(body__icontains=query) |
|
|
Q(category__name__icontains=query) |
|
|
Q(tags__name__icontains=query)
|
|
).filter(status='p') # 只搜索已发布的文章
|
|
return results.distinct()[:self.__max_takecount__]
|
|
return Article.objects.none()
|
|
|
|
def get_category_lists(self):
|
|
return Category.objects.all()
|
|
|
|
def get_category_articles(self, categoryname):
|
|
articles = Article.objects.filter(category__name=categoryname,status='p')
|
|
|
|
if articles:
|
|
return articles[:self.__max_takecount__]
|
|
return None
|
|
|
|
def get_recent_articles(self):
|
|
return Article.objects.filter(
|
|
status='p'
|
|
).order_by('-pub_time')[:self.__max_takecount__]
|
|
|
|
def get_popular_articles(self):
|
|
"""获取热门文章(按浏览量排序)"""
|
|
return Article.objects.filter(
|
|
status='p'
|
|
).order_by('-views')[:self.__max_takecount__] |