- 新增 explore 应用:Idea / SearchSpec / Repository / Note 及迁移 - Ollama 生成 GitHub 检索条件(language / stars / topic / sort),并批量解读仓库简介 - 接入 GitHub 仓库搜索 REST API,结果入库并按想法关联 - 想法级与仓库级「我的其他思考」,支持多条添加与删除 - 新增存档页:按想法 / 有想法的仓库两个视角,跨想法合并仓库 - 前端 /explore/ 四步流程、条件展示、筛选器、AI 解读目录、?idea= 恢复现场 - settings 增加 OLLAMA_* / GITHUB_* 配置
207 lines
6.6 KiB
Python
207 lines
6.6 KiB
Python
import math
|
|
import re
|
|
|
|
from django.shortcuts import render, get_object_or_404
|
|
from django.core.paginator import Paginator
|
|
from django.db.models import Count, Q
|
|
from django.utils.html import strip_tags
|
|
from .models import Category, Post, Tag , SiteInfo
|
|
from explore.models import Idea, Repository
|
|
from mindmaps.models import MindMap
|
|
|
|
def category_list(request):
|
|
# """首页:展示所有分类"""
|
|
# category_list = Category.objects.all()
|
|
|
|
# paginator = Paginator(category_list, 6) # 每页 10 个分类
|
|
# page_number = request.GET.get('page')
|
|
# categories = paginator.get_page(page_number)
|
|
### 第二版new
|
|
# ===== 新增:文章数量 & 分类数量 =====
|
|
total_posts = Post.objects.filter(status='published').count() # 已发布文章总数
|
|
total_categories = Category.objects.count() # 分类总数
|
|
mindmap_count = MindMap.objects.count() # 脑图数量
|
|
# ===== 新增:最新文章(取前5篇)=====
|
|
# latest_posts = Post.objects.filter(
|
|
# status='published'
|
|
# ).prefetch_related('categories', 'tags')[:5]
|
|
|
|
# ===== 新增:站点信息 =====
|
|
#site_info = SiteInfo.get_site_info()
|
|
|
|
# ===== 新增:每个分类各有多少篇文章(用于侧边栏/首页展示)=====
|
|
# categories_with_count = Category.objects.annotate(
|
|
# post_count=Count(
|
|
# 'posts',
|
|
# filter=models.Q(posts__status='published')
|
|
# )
|
|
# ).filter(post_count__gt=0).order_by('-post_count')
|
|
|
|
|
|
|
|
return render(request, 'base2.html', {
|
|
# 'categories': categories,
|
|
'total_posts': total_posts, # 文章总数
|
|
'total_categories': total_categories, # 分类总数
|
|
'mindmap_count' : mindmap_count
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
def category_detail(request, slug):
|
|
"""分类详情:该分类下的文章列表"""
|
|
category = get_object_or_404(Category, slug=slug)
|
|
post_list = category.posts.filter(status='published')
|
|
|
|
paginator = Paginator(post_list, 5)
|
|
page_number = request.GET.get('page')
|
|
posts = paginator.get_page(page_number)
|
|
|
|
return render(request, 'category_detail.html', {
|
|
'category': category,
|
|
'posts': posts,
|
|
})
|
|
|
|
|
|
def post_detail(request, slug):
|
|
"""文章详情:正文 + 上下篇 + 字数/阅读时长"""
|
|
post = get_object_or_404(
|
|
Post.objects.prefetch_related('categories', 'tags'),
|
|
slug=slug,
|
|
status='published',
|
|
)
|
|
|
|
# 同一时间轴上的已发布文章,按(发布时间, id)排序后取相邻两篇
|
|
published_ids = list(
|
|
Post.objects.filter(status='published')
|
|
.order_by('published_at', 'id')
|
|
.values_list('id', flat=True)
|
|
)
|
|
prev_post = next_post = None
|
|
if post.id in published_ids:
|
|
index = published_ids.index(post.id)
|
|
if index > 0:
|
|
prev_post = Post.objects.filter(id=published_ids[index - 1]).first()
|
|
if index < len(published_ids) - 1:
|
|
next_post = Post.objects.filter(id=published_ids[index + 1]).first()
|
|
|
|
# 阅读统计:先去掉 HTML 标签,再按非空白字符计数
|
|
plain_text = strip_tags(post.content or '')
|
|
char_count = len(re.sub(r'\s+', '', plain_text))
|
|
read_minutes = max(1, math.ceil(char_count / 400))
|
|
|
|
return render(request, 'post_detail.html', {
|
|
'post': post,
|
|
'prev_post': prev_post,
|
|
'next_post': next_post,
|
|
'char_count': char_count,
|
|
'read_minutes': read_minutes,
|
|
})
|
|
|
|
|
|
def tag_detail(request, slug):
|
|
"""标签详情:该标签下的文章列表"""
|
|
tag = get_object_or_404(Tag, slug=slug)
|
|
post_list = tag.posts.filter(status='published')
|
|
|
|
paginator = Paginator(post_list, 5)
|
|
page_number = request.GET.get('page')
|
|
posts = paginator.get_page(page_number)
|
|
|
|
return render(request, 'tag_detail.html', {
|
|
'tag': tag,
|
|
'posts': posts,
|
|
"container_wide": True,
|
|
})
|
|
|
|
def about_page(request):
|
|
"""关于页面"""
|
|
site_info = SiteInfo.get_site_info()
|
|
return render(request, 'about.html', {
|
|
'site_info': site_info,
|
|
})
|
|
|
|
|
|
def explore(request):
|
|
"""拾源:想法 → 关键词 → 开源仓库(当前为前端演示版,数据在 explore.js 内模拟)"""
|
|
return render(request, 'explore.html')
|
|
|
|
|
|
def explore_archive(request):
|
|
"""拾源存档:按想法回看,或从「有想法的仓库」倒查"""
|
|
view = request.GET.get("view", "ideas")
|
|
if view not in {"ideas", "repos"}:
|
|
view = "ideas"
|
|
|
|
ideas = (
|
|
Idea.objects.annotate(
|
|
spec_count=Count("specs", distinct=True),
|
|
repo_count=Count("repositories", distinct=True),
|
|
idea_note_count=Count(
|
|
"notes", filter=Q(notes__repository__isnull=True), distinct=True
|
|
),
|
|
repo_note_count=Count(
|
|
"notes", filter=Q(notes__repository__isnull=False), distinct=True
|
|
),
|
|
)
|
|
.prefetch_related("specs", "notes", "repositories__notes")
|
|
.order_by("-created_at")
|
|
)
|
|
|
|
idea_cards = []
|
|
for idea in ideas:
|
|
specs = list(idea.specs.all())
|
|
idea_cards.append({
|
|
"idea": idea,
|
|
"specs": specs,
|
|
"latest": specs[0] if specs else None,
|
|
"idea_notes": [note for note in idea.notes.all() if note.repository_id is None],
|
|
"repos": list(idea.repositories.all()),
|
|
})
|
|
|
|
repo_total = (
|
|
Repository.objects.filter(notes__isnull=False)
|
|
.values("github_id")
|
|
.distinct()
|
|
.count()
|
|
)
|
|
|
|
repo_groups = []
|
|
if view == "repos":
|
|
rows = (
|
|
Repository.objects.filter(notes__isnull=False)
|
|
.select_related("idea")
|
|
.prefetch_related("notes")
|
|
.order_by("-repo_updated_at", "-stars")
|
|
)
|
|
groups = {}
|
|
for repo in rows:
|
|
group = groups.setdefault(repo.github_id, {
|
|
"repo": repo,
|
|
"ideas": {},
|
|
"notes": [],
|
|
})
|
|
group["ideas"][repo.idea_id] = repo.idea
|
|
for note in repo.notes.all():
|
|
group["notes"].append({"note": note, "idea": repo.idea})
|
|
|
|
for group in groups.values():
|
|
group["notes"].sort(key=lambda item: item["note"].created_at, reverse=True)
|
|
group["idea_list"] = list(group["ideas"].values())
|
|
|
|
repo_groups = sorted(
|
|
groups.values(),
|
|
key=lambda group: group["notes"][0]["note"].created_at,
|
|
reverse=True,
|
|
)
|
|
|
|
return render(request, "explore_archive.html", {
|
|
"view": view,
|
|
"idea_cards": idea_cards,
|
|
"idea_total": len(idea_cards),
|
|
"repo_groups": repo_groups,
|
|
"repo_total": repo_total,
|
|
})
|