#!/usr/bin/env Python
# coding=utf-8

from django.db import models
from django.contrib.auth.models import User
from django.utils.functional import cached_property
from django.template.loader import render_to_string


# 文章目录
class Category(models.Model):
    name = models.CharField(max_length=32, verbose_name='分类名称')
    desc = models.TextField(max_length=200, verbose_name='分类描述', blank=True, default='')
    add_date = models.DateTimeField(auto_now_add=True, verbose_name='添加时间')
    pub_date = models.DateTimeField(auto_now=True, verbose_name='发布时间')

    class Meta:
        verbose_name = '博客分类'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name


# 标签模型
class Tag(models.Model):
    name = models.CharField(max_length=10, verbose_name='文章标签')
    add_date = models.DateTimeField(auto_now_add=True, verbose_name='添加时间')
    pub_date = models.DateTimeField(auto_now=True, verbose_name='发布时间')

    class Meta:
        verbose_name = '文章标签'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name


# 文章模型
class Post(models.Model):
    owner = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name='文章作者', null=False)
    title = models.CharField(max_length=50, verbose_name='文章标题')
    desc = models.TextField(max_length=200, blank=True, default='', verbose_name='文章描述')
    content = models.TextField(verbose_name='文章详情')
    tags = models.ForeignKey(Tag, on_delete=models.CASCADE, verbose_name='文章标签', null=True)
    add_date = models.DateTimeField(auto_now_add=True, verbose_name='添加时间')
    pub_date = models.DateTimeField(auto_now=True, verbose_name='发布时间')
    is_hot = models.BooleanField(default=False, verbose_name='是热门话题?')
    pv = models.IntegerField(default=0, verbose_name='浏览次数')
    category = models.ForeignKey(Category, on_delete=models.CASCADE, verbose_name='文章分类')

    class Meta:
        verbose_name = '文章'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.title


# 侧边栏模型
class Sidebar(models.Model):
    STATUS = (
        (1, '隐藏'),
        (2, '显示')
    )

    DISPLAY_TYPE = (
        (1, '搜索'),
        (2, '最新文章'),
        (3, '最热文章'),
        (4, '最近评论'),
        (5, '归档'),
        (6, 'HTML')
    )

    title = models.CharField(max_length=50, verbose_name='模块名称')
    display_type = models.PositiveIntegerField(default=1, choices=DISPLAY_TYPE, verbose_name='展示类型')
    content = models.CharField(max_length=500, blank=True, default='', verbose_name='内容',
                               help_text='除HTML类型外,均可为空...')
    sort = models.PositiveIntegerField(default=1, verbose_name='排序', help_text='序列越大越靠前')
    status = models.PositiveIntegerField(default=2, choices=STATUS, verbose_name='状态')
    add_date = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')

    class Meta:
        verbose_name = '侧边栏'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.title

    @classmethod
    def get_sidebar(cla):
        return cla.objects.filter(status=2)

    @property
    def get_content(self):
        if self.display_type == 1:
            context = {}
            return render_to_string('passage/sidebar/search.html', context=context)
        elif self.display_type == 2:
            context = {}
            return render_to_string('passage/sidebar/new_post.html', context=context)
        elif self.display_type == 3:
            context = {}
            return render_to_string('passage/sidebar/hot_post.html', context=context)
        elif self.display_type == 4:
            context = {}
            return render_to_string('passage/sidebar/comment.html', context=context)
        elif self.display_type == 5:
            context = {}
            return render_to_string('passage/sidebar/archives.html', context=context)
        elif self.display_type == 6:
            return self.content
