# Path: blog/models.py
# class Article(BaseModel):
#     """文章"""
#     STATUS_CHOICES = (
#         ('d', '草稿'),
#         ('p', '发表'),
#     )
#     COMMENT_STATUS = (
#         ('o', '打开'),
#         ('c', '关闭'),
#     )
#     TYPE = (
#         ('a', '文章'),
#         ('p', '页面'),
#     )
#     title = models.CharField('标题', max_length=200, unique=True)
#     body = MDTextField('正文')
#     pub_time = models.DateTimeField(
#         '发布时间', blank=False, null=False, default=now)
#     status = models.CharField(
#         '文章状态',
#         max_length=1,
#         choices=STATUS_CHOICES,
#         default='p')
#     comment_status = models.CharField(
#         '评论状态',
#         max_length=1,
#         choices=COMMENT_STATUS,
#         default='o')
#     type = models.CharField('类型', max_length=1, choices=TYPE, default='a')
#     views = models.PositiveIntegerField('浏览量', default=0)
#     author = models.ForeignKey(
#         settings.AUTH_USER_MODEL,
#         verbose_name='作者',
#         blank=False,
#         null=False,
#         on_delete=models.CASCADE)
#     article_order = models.IntegerField(
#         '排序,数字越大越靠前', blank=False, null=False, default=0)
#     show_toc = models.BooleanField("是否显示toc目录", blank=False, null=False, default=False)
#     category = models.ForeignKey(
#         'Category',
#         verbose_name='分类',
#         on_delete=models.CASCADE,
#         blank=False,
#         null=False)
#     tags = models.ManyToManyField('Tag', verbose_name='标签集合', blank=True)
# 
#     def body_to_string(self):
#         return self.body
# 
#     def __str__(self):
#         return self.title
# 
#     class Meta:
#         ordering = ['-article_order', '-pub_time']
#         verbose_name = "文章"
#         verbose_name_plural = verbose_name
#         get_latest_by = 'id'
# 
#     def get_absolute_url(self):
#         return reverse('blog:detailbyid', kwargs={
#             'article_id': self.id,
#             'year': self.created_time.year,
#             'month': self.created_time.month,
#             'day': self.created_time.day
#         })
# 
#     @cache_decorator(60 * 60 * 10)
#     def get_category_tree(self):
#         tree = self.category.get_category_tree()
#         names = list(map(lambda c: (c.name, c.get_absolute_url()), tree))
# 
#         return names
# 
#     def save(self, *args, **kwargs):
#         super().save(*args, **kwargs)
# 
#     def viewed(self):
#         self.views += 1
#         self.save(update_fields=['views'])
# 
#     def comment_list(self):
#         cache_key = 'article_comments_{id}'.format(id=self.id)
#         value = cache.get(cache_key)
#         if value:
#             logger.info('get article comments:{id}'.format(id=self.id))
#             return value
#         else:
#             comments = self.comment_set.filter(is_enable=True)
#             cache.set(cache_key, comments, 60 * 100)
#             logger.info('set article comments:{id}'.format(id=self.id))
#             return comments
# 
#     def get_admin_url(self):
#         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):
#         # 下一篇
#         return Article.objects.filter(
#             id__gt=self.id, status='p').order_by('id').first()
# 
#     @cache_decorator(expiration=60 * 100)
#     def prev_article(self):
#         # 前一篇
#         return Article.objects.filter(id__lt=self.id, status='p').first()


from django import forms
from django.contrib import admin
from django.contrib.auth import get_user_model
from django.urls import reverse
from django.utils.html import format_html
from django.utils.translation import ugettext_lazy as _
from .models import Article
            from djangoblog.utils import get_current_site

# Register your models here.


class ArticleListFilter(admin.SimpleListFilter):
    title = _("作者")
    parameter_name = 'author'

    def lookups(self, request, model_admin):
        authors = list(set(map(lambda x: x.author, Article.objects.all())))
        for author in authors:
            yield (author.id, _(author.username))

    def queryset(self, request, queryset):
        id = self.value()
        if id:
            return queryset.filter(author__id__exact=id)
        else:
            return queryset


class ArticleForm(forms.ModelForm):
    # body = forms.CharField(widget=AdminPagedownWidget())

    class Meta:

# Path: blog/models.py
# class Article(BaseModel):
#     """文章"""
#     STATUS_CHOICES = (
#         ('d', '草稿'),
#         ('p', '发表'),
#     )
#     COMMENT_STATUS = (
#         ('o', '打开'),
#         ('c', '关闭'),
#     )
#     TYPE = (
#         ('a', '文章'),
#         ('p', '页面'),
#     )
#     title = models.CharField('标题', max_length=200, unique=True)
#     body = MDTextField('正文')
#     pub_time = models.DateTimeField(
#         '发布时间', blank=False, null=False, default=now)
#     status = models.CharField(
#         '文章状态',
#         max_length=1,
#         choices=STATUS_CHOICES,
#         default='p')
#     comment_status = models.CharField(
#         '评论状态',
#         max_length=1,
#         choices=COMMENT_STATUS,
#         default='o')
#     type = models.CharField('类型', max_length=1, choices=TYPE, default='a')
#     views = models.PositiveIntegerField('浏览量', default=0)
#     author = models.ForeignKey(
#         settings.AUTH_USER_MODEL,
#         verbose_name='作者',
#         blank=False,
#         null=False,
#         on_delete=models.CASCADE)
#     article_order = models.IntegerField(
#         '排序,数字越大越靠前', blank=False, null=False, default=0)
#     show_toc = models.BooleanField("是否显示toc目录", blank=False, null=False, default=False)
#     category = models.ForeignKey(
#         'Category',
#         verbose_name='分类',
#         on_delete=models.CASCADE,
#         blank=False,
#         null=False)
#     tags = models.ManyToManyField('Tag', verbose_name='标签集合', blank=True)
# 
#     def body_to_string(self):
#         return self.body
# 
#     def __str__(self):
#         return self.title
# 
#     class Meta:
#         ordering = ['-article_order', '-pub_time']
#         verbose_name = "文章"
#         verbose_name_plural = verbose_name
#         get_latest_by = 'id'
# 
#     def get_absolute_url(self):
#         return reverse('blog:detailbyid', kwargs={
#             'article_id': self.id,
#             'year': self.created_time.year,
#             'month': self.created_time.month,
#             'day': self.created_time.day
#         })
# 
#     @cache_decorator(60 * 60 * 10)
#     def get_category_tree(self):
#         tree = self.category.get_category_tree()
#         names = list(map(lambda c: (c.name, c.get_absolute_url()), tree))
# 
#         return names
# 
#     def save(self, *args, **kwargs):
#         super().save(*args, **kwargs)
# 
#     def viewed(self):
#         self.views += 1
#         self.save(update_fields=['views'])
# 
#     def comment_list(self):
#         cache_key = 'article_comments_{id}'.format(id=self.id)
#         value = cache.get(cache_key)
#         if value:
#             logger.info('get article comments:{id}'.format(id=self.id))
#             return value
#         else:
#             comments = self.comment_set.filter(is_enable=True)
#             cache.set(cache_key, comments, 60 * 100)
#             logger.info('set article comments:{id}'.format(id=self.id))
#             return comments
# 
#     def get_admin_url(self):
#         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):
#         # 下一篇
#         return Article.objects.filter(
#             id__gt=self.id, status='p').order_by('id').first()
# 
#     @cache_decorator(expiration=60 * 100)
#     def prev_article(self):
#         # 前一篇
#         return Article.objects.filter(id__lt=self.id, status='p').first()


# Path: blog/admin.py
from django import forms
from django.contrib import admin
from django.contrib.auth import get_user_model
from django.urls import reverse
from django.utils.html import format_html
from django.utils.translation import ugettext_lazy as _
from .models import Article
            from djangoblog.utils import get_current_site


# Register your models here.


class ArticleListFilter(admin.SimpleListFilter):
    title = _("作者")
    parameter_name = 'author'

    def lookups(self, request, model_admin):
        authors = list(set(map(lambda x: x.author, Article.objects.all())))
        for author in authors:
            yield (author.id, _(author.username))

    def queryset(self, request, queryset):
        id = self.value()
        if id:
            return queryset.filter(author__id__exact=id)
        else:
            return queryset


class ArticleForm(forms.ModelForm):
    # body = forms.CharField(widget=AdminPagedownWidget())

    class Meta:

        model = Article