113 lines
3.7 KiB
Python
113 lines
3.7 KiB
Python
from django.shortcuts import render, HttpResponse
|
|
from django.http import HttpResponseNotFound
|
|
from pip._vendor.rich.markup import Tag
|
|
|
|
from home.models import *
|
|
from comment.models import *
|
|
from siteconfig.models import *
|
|
# Create your views here.
|
|
# 获取站点信息的函数
|
|
def get_site_config():
|
|
siteconf = SiteConfig.objects.all()[0]
|
|
siteconfig = {
|
|
'title': siteconf.site_name,
|
|
'site_author_name': siteconf.site_author_name,
|
|
'site_author_qq': siteconf.site_author_qq,
|
|
"site_author_email": siteconf.site_author_email,
|
|
"icp": siteconf.icp,
|
|
}
|
|
return siteconfig
|
|
# 获取标签列表的函数
|
|
def get_tag_list():
|
|
taginfos = ArticleTag.objects.all()
|
|
tags = []
|
|
for taginfo in taginfos:
|
|
tags.append({"name":taginfo.tag,"id":taginfo.id})
|
|
return tags
|
|
# 获取友情链接列表的函数
|
|
def get_friendship_links():
|
|
friendship_links = []
|
|
FriendshipLinks = FriendshipLink.objects.all()
|
|
for link in FriendshipLinks:
|
|
friendship_links.append({"link": link.friendship_site_link,"name": link.friendship_site_name})
|
|
return friendship_links
|
|
# 获取文章详情的函数
|
|
def get_archives_info(id):
|
|
i = Articles.objects.get(id=id)
|
|
previous_article = Articles.objects.filter(id__lt=id).order_by('-id').first()
|
|
previous_id = previous_article.id if previous_article else id
|
|
previous_id_title = previous_article.title if previous_article else "暂无上一篇"
|
|
next_article = Articles.objects.filter(id__gt=id).order_by('id').first()
|
|
next_id = next_article.id if next_article else id
|
|
next_id_title = next_article.title if next_article else "暂无下一篇"
|
|
return {
|
|
"id": i.id,
|
|
"title": i.title,
|
|
"abstract": i.abstract,
|
|
"created": i.created,
|
|
"stat": i.stat,
|
|
"read": i.read,
|
|
"content": i.content,
|
|
"author": i.author,
|
|
"site_config": get_site_config(),
|
|
"previous":{
|
|
"id": previous_id,
|
|
"title": previous_id_title,
|
|
},"next":{
|
|
"id": next_id,
|
|
"title": next_id_title,
|
|
},"comments": [],
|
|
"FriendshipLinks": get_friendship_links(),
|
|
"tags": get_tag_list()
|
|
}
|
|
# 获取文章列表的函数(option不为1时以tag_id进行查询)
|
|
def get_artchle(option=1,id=None):
|
|
if option == 1:
|
|
artchle = Articles.objects.all()
|
|
else:
|
|
artchle = Articles.objects.filter(tag_id=id).all()
|
|
|
|
artchles = {
|
|
"artchles": [],
|
|
"site_config": get_site_config(),
|
|
"FriendshipLinks": get_friendship_links(),
|
|
"tags": get_tag_list()
|
|
}
|
|
for i in artchle:
|
|
a = {
|
|
"id": i.id,
|
|
"title": i.title,
|
|
"abstract": i.abstract,
|
|
"created": i.created,
|
|
"stat": i.stat,
|
|
"read": i.read,
|
|
}
|
|
artchles["artchles"].append(a)
|
|
return artchles
|
|
# 首页视图函数
|
|
def index(request):
|
|
return render(request, 'index.html', context=get_artchle())
|
|
# 文章页视图函数
|
|
def archives(request, id):
|
|
try:
|
|
i = Articles.objects.get(id=id)
|
|
i.read += 1
|
|
i.save()
|
|
|
|
comments = Comment.objects.filter(archives_id=id).order_by("-comment_time").all()
|
|
a = get_archives_info(id)
|
|
for c in comments:
|
|
com = {
|
|
"id": c.id,
|
|
"comment_Content": c.comment_content,
|
|
"comment_User": c.comment_user,
|
|
"comment_Time": c.comment_time,
|
|
"qq": c.qq,
|
|
}
|
|
a["comments"].append(com)
|
|
return render(request, 'archives.html', a)
|
|
except:
|
|
return render(request, '404.html')
|
|
# 分类页视图函数
|
|
def category(request,id):
|
|
return render(request, 'index.html', context=get_artchle(option=2,id=id)) |