|
|
|
|
@ -18,6 +18,7 @@ logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class LinkShowType(models.TextChoices):
|
|
|
|
|
"""xjh:链接显示类型选择"""
|
|
|
|
|
I = ('i', _('index'))
|
|
|
|
|
L = ('l', _('list'))
|
|
|
|
|
P = ('p', _('post'))
|
|
|
|
|
@ -26,17 +27,20 @@ class LinkShowType(models.TextChoices):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class BaseModel(models.Model):
|
|
|
|
|
"""xjh:基础模型类,包含公共字段"""
|
|
|
|
|
id = models.AutoField(primary_key=True)
|
|
|
|
|
creation_time = models.DateTimeField(_('creation time'), default=now)
|
|
|
|
|
last_modify_time = models.DateTimeField(_('modify time'), default=now)
|
|
|
|
|
|
|
|
|
|
def save(self, *args, **kwargs):
|
|
|
|
|
#xjh:特殊处理文章浏览量更新
|
|
|
|
|
is_update_views = isinstance(
|
|
|
|
|
self,
|
|
|
|
|
Article) and 'update_fields' in kwargs and kwargs['update_fields'] == ['views']
|
|
|
|
|
if is_update_views:
|
|
|
|
|
Article.objects.filter(pk=self.pk).update(views=self.views)
|
|
|
|
|
else:
|
|
|
|
|
#xjh:自动生成slug字段
|
|
|
|
|
if 'slug' in self.__dict__:
|
|
|
|
|
slug = getattr(
|
|
|
|
|
self, 'title') if 'title' in self.__dict__ else getattr(
|
|
|
|
|
@ -45,6 +49,7 @@ class BaseModel(models.Model):
|
|
|
|
|
super().save(*args, **kwargs)
|
|
|
|
|
|
|
|
|
|
def get_full_url(self):
|
|
|
|
|
"""xjh:获取完整URL"""
|
|
|
|
|
site = get_current_site().domain
|
|
|
|
|
url = "https://{site}{path}".format(site=site,
|
|
|
|
|
path=self.get_absolute_url())
|
|
|
|
|
@ -55,11 +60,12 @@ class BaseModel(models.Model):
|
|
|
|
|
|
|
|
|
|
@abstractmethod
|
|
|
|
|
def get_absolute_url(self):
|
|
|
|
|
"""xjh:抽象方法,获取绝对URL"""
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Article(BaseModel):
|
|
|
|
|
"""文章"""
|
|
|
|
|
"""xjh:文章模型"""
|
|
|
|
|
STATUS_CHOICES = (
|
|
|
|
|
('d', _('Draft')),
|
|
|
|
|
('p', _('Published')),
|
|
|
|
|
@ -118,6 +124,7 @@ class Article(BaseModel):
|
|
|
|
|
get_latest_by = 'id'
|
|
|
|
|
|
|
|
|
|
def get_absolute_url(self):
|
|
|
|
|
"""xjh:获取文章绝对URL"""
|
|
|
|
|
return reverse('blog:detailbyid', kwargs={
|
|
|
|
|
'article_id': self.id,
|
|
|
|
|
'year': self.creation_time.year,
|
|
|
|
|
@ -127,6 +134,7 @@ class Article(BaseModel):
|
|
|
|
|
|
|
|
|
|
@cache_decorator(60 * 60 * 10)
|
|
|
|
|
def get_category_tree(self):
|
|
|
|
|
"""xjh:获取分类树"""
|
|
|
|
|
tree = self.category.get_category_tree()
|
|
|
|
|
names = list(map(lambda c: (c.name, c.get_absolute_url()), tree))
|
|
|
|
|
|
|
|
|
|
@ -136,10 +144,12 @@ class Article(BaseModel):
|
|
|
|
|
super().save(*args, **kwargs)
|
|
|
|
|
|
|
|
|
|
def viewed(self):
|
|
|
|
|
"""xjh:增加文章浏览量"""
|
|
|
|
|
self.views += 1
|
|
|
|
|
self.save(update_fields=['views'])
|
|
|
|
|
|
|
|
|
|
def comment_list(self):
|
|
|
|
|
"""xjh:获取文章评论列表"""
|
|
|
|
|
cache_key = 'article_comments_{id}'.format(id=self.id)
|
|
|
|
|
value = cache.get(cache_key)
|
|
|
|
|
if value:
|
|
|
|
|
@ -152,23 +162,24 @@ class Article(BaseModel):
|
|
|
|
|
return comments
|
|
|
|
|
|
|
|
|
|
def get_admin_url(self):
|
|
|
|
|
"""xjh:获取文章管理后台URL"""
|
|
|
|
|
info = (self._meta.app_label, self._meta.model_name)
|
|
|
|
|
return reverse('admin:%s_%s_change' % info, args=(self.pk,))
|
|
|
|
|
|
|
|
|
|
@cache_decorator(expiration=60 * 100)
|
|
|
|
|
def next_article(self):
|
|
|
|
|
# 下一篇
|
|
|
|
|
"""xjh:获取下一篇文章"""
|
|
|
|
|
return Article.objects.filter(
|
|
|
|
|
id__gt=self.id, status='p').order_by('id').first()
|
|
|
|
|
|
|
|
|
|
@cache_decorator(expiration=60 * 100)
|
|
|
|
|
def prev_article(self):
|
|
|
|
|
# 前一篇
|
|
|
|
|
"""xjh:获取上一篇文章"""
|
|
|
|
|
return Article.objects.filter(id__lt=self.id, status='p').first()
|
|
|
|
|
|
|
|
|
|
def get_first_image_url(self):
|
|
|
|
|
"""
|
|
|
|
|
Get the first image url from article.body.
|
|
|
|
|
xjh:从文章内容中提取第一张图片URL
|
|
|
|
|
:return:
|
|
|
|
|
"""
|
|
|
|
|
match = re.search(r'!\[.*?\]\((.+?)\)', self.body)
|
|
|
|
|
@ -178,7 +189,7 @@ class Article(BaseModel):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Category(BaseModel):
|
|
|
|
|
"""文章分类"""
|
|
|
|
|
"""xjh:文章分类模型"""
|
|
|
|
|
name = models.CharField(_('category name'), max_length=30, unique=True)
|
|
|
|
|
parent_category = models.ForeignKey(
|
|
|
|
|
'self',
|
|
|
|
|
@ -205,7 +216,7 @@ class Category(BaseModel):
|
|
|
|
|
@cache_decorator(60 * 60 * 10)
|
|
|
|
|
def get_category_tree(self):
|
|
|
|
|
"""
|
|
|
|
|
递归获得分类目录的父级
|
|
|
|
|
xjh:递归获得分类目录的父级
|
|
|
|
|
:return:
|
|
|
|
|
"""
|
|
|
|
|
categorys = []
|
|
|
|
|
@ -221,7 +232,7 @@ class Category(BaseModel):
|
|
|
|
|
@cache_decorator(60 * 60 * 10)
|
|
|
|
|
def get_sub_categorys(self):
|
|
|
|
|
"""
|
|
|
|
|
获得当前分类目录所有子集
|
|
|
|
|
xjh:获得当前分类目录所有子集
|
|
|
|
|
:return:
|
|
|
|
|
"""
|
|
|
|
|
categorys = []
|
|
|
|
|
@ -241,7 +252,7 @@ class Category(BaseModel):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Tag(BaseModel):
|
|
|
|
|
"""文章标签"""
|
|
|
|
|
"""xjh:文章标签模型"""
|
|
|
|
|
name = models.CharField(_('tag name'), max_length=30, unique=True)
|
|
|
|
|
slug = models.SlugField(default='no-slug', max_length=60, blank=True)
|
|
|
|
|
|
|
|
|
|
@ -253,6 +264,7 @@ class Tag(BaseModel):
|
|
|
|
|
|
|
|
|
|
@cache_decorator(60 * 60 * 10)
|
|
|
|
|
def get_article_count(self):
|
|
|
|
|
"""xjh:获取标签下的文章数量"""
|
|
|
|
|
return Article.objects.filter(tags__name=self.name).distinct().count()
|
|
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
|
@ -262,7 +274,7 @@ class Tag(BaseModel):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Links(models.Model):
|
|
|
|
|
"""友情链接"""
|
|
|
|
|
"""xjh:友情链接模型"""
|
|
|
|
|
|
|
|
|
|
name = models.CharField(_('link name'), max_length=30, unique=True)
|
|
|
|
|
link = models.URLField(_('link'))
|
|
|
|
|
@ -287,7 +299,7 @@ class Links(models.Model):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class SideBar(models.Model):
|
|
|
|
|
"""侧边栏,可以展示一些html内容"""
|
|
|
|
|
"""xjh:侧边栏模型,可以展示一些html内容"""
|
|
|
|
|
name = models.CharField(_('title'), max_length=100)
|
|
|
|
|
content = models.TextField(_('content'))
|
|
|
|
|
sequence = models.IntegerField(_('order'), unique=True)
|
|
|
|
|
@ -305,7 +317,7 @@ class SideBar(models.Model):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class BlogSettings(models.Model):
|
|
|
|
|
"""blog的配置"""
|
|
|
|
|
"""xjh:博客设置模型"""
|
|
|
|
|
site_name = models.CharField(
|
|
|
|
|
_('site name'),
|
|
|
|
|
max_length=200,
|
|
|
|
|
@ -367,10 +379,12 @@ class BlogSettings(models.Model):
|
|
|
|
|
return self.site_name
|
|
|
|
|
|
|
|
|
|
def clean(self):
|
|
|
|
|
"""xjh:验证只能有一个博客配置实例"""
|
|
|
|
|
if BlogSettings.objects.exclude(id=self.id).count():
|
|
|
|
|
raise ValidationError(_('There can only be one configuration'))
|
|
|
|
|
|
|
|
|
|
def save(self, *args, **kwargs):
|
|
|
|
|
"""xjh:保存时清除缓存"""
|
|
|
|
|
super().save(*args, **kwargs)
|
|
|
|
|
from djangoblog.utils import cache
|
|
|
|
|
cache.clear()
|
|
|
|
|
cache.clear()
|