text: from django.db import models
  from django.conf import settings
  from django.shortcuts import reverse
  import markdown
  import re
  from mdeditor.fields import MDTextField
  # Create your models here.
  
  # 文章关键词，用来作为SEO中keywords
  class Keyword(models.Model):
      name = models.CharField('文章关键词', max_length=20)
  
      class Meta:
          verbose_name = '关键词'
          verbose_name_plural = verbose_name
          ordering = ['name']
  
      def __str__(self):
          return self.name
  
  
  # 文章标签
  class Tag(models.Model):
      name = models.CharField('文章标签', max_length=20)
      slug = models.SlugField(unique=True)
      description = models.TextField('描述', max_length=240, default=settings.SITE_DESCRIPTION,
                                     help_text='用来作为SEO中description,长度参考SEO标准')
  
      class Meta:
          verbose_name = '标签'
          verbose_name_plural = verbose_name
          ordering = ['id']
  
      def __str__(self):
          return self.name
  
      def get_absolute_url(self):
          return reverse('blog:tag', kwargs={'slug': self.slug})
  
      def get_article_list(self):
          '''返回当前标签下所有发表的文章列表'''
          return Article.objects.filter(tags=self)
  
  
  # 教程分类
  class Course(models.Model):
      IMG_LINK = '/static/blog/img/summary.png'
      name = models.CharField('教程分类', max_length=20)
      slug = models.SlugField(unique=True)
      description = models.TextField('描述', max_length=240, default=settings.SITE_DESCRIPTION,
                                     help_text='用来作为SEO中description,长度参考SEO标准')
      img_link = models.CharField('图片地址', default=IMG_LINK, max_length=255)
  
      class Meta:
          verbose_name = '教程分类'
          verbose_name_plural = verbose_name
          ordering = ['name']
  
      def __str__(self):
          return self.name
  
      def get_absolute_url(self):
          return reverse('blog:course', kwargs={'slug': self.slug})
  
      def get_article_list(self):
          return Article.objects.filter(course=self)
  
  
  
  
  # 文章分类
  class Category(models.Model):
      name = models.CharField('文章分类', max_length=20)
      slug = models.SlugField(unique=True)
      description = models.TextField('描述', max_length=240, default=settings.SITE_DESCRIPTION,
                                     help_text='用来作为SEO中description,长度参考SEO标准')
  
      class Meta:
          verbose_name = '分类'
          verbose_name_plural = verbose_name
          ordering = ['name']
  
      def __str__(self):
          return self.name
  
      def get_absolute_url(self):
          return reverse('blog:category', kwargs={'slug': self.slug})
  
      def get_article_list(self):
          return Article.objects.filter(category=self)
  
  
  # 文章
  class Article(models.Model):
      IMG_LINK = '/static/blog/img/summary.png'
      author = models.ForeignKey(settings.AUTH_USER_MODEL, verbose_name='作者', on_delete=models.PROTECT,null=True,blank=True)
      title = models.CharField(max_length=150, verbose_name='文章标题')
      summary = models.TextField('文章摘要', max_length=230, default='文章摘要等同于网页description内容，请务必填写...')
      body =MDTextField(verbose_name='文章内容')
      img_link = models.CharField('图片地址', default=IMG_LINK, max_length=255)
      create_date = models.DateTimeField(verbose_name='创建时间', auto_now_add=True)
      update_date = models.DateTimeField(verbose_name='修改时间', auto_now=True)
      views = models.IntegerField('阅览量', default=0)
      slug = models.SlugField(unique=True)
      is_top = models.BooleanField('置顶', default=False)
  
      category = models.ForeignKey(Category, verbose_name='文章分类', on_delete=models.PROTECT,null=True,blank=True)
      course = models.ForeignKey(Course, verbose_name='教程分类', on_delete=models.PROTECT,null=True,blank=True)
      tags = models.ManyToManyField(Tag, verbose_name='标签')
      keywords = models.ManyToManyField(Keyword, verbose_name='文章关键词',
                                        help_text='文章关键词，用来作为SEO中keywords，最好使用长尾词，3-4个足够')
  
      class Meta:
          verbose_name = '文章'
          verbose_name_plural = verbose_name
          ordering = ['-create_date']
  
      def __str__(self):
          return self.title[:20]
  
      def get_absolute_url(self):
          return reverse('blog:detail', kwargs={'slug': self.slug})
  
      def body_to_markdown(self):
          return markdown.markdown(self.body, extensions=[
              'markdown.extensions.extra',
              'markdown.extensions.codehilite',
          ])
  
      def update_views(self):
          self.views += 1
          self.save(update_fields=['views'])
  
      def get_pre(self):
          return Article.objects.filter(id__lt=self.id).order_by('-id').first()
  
      def get_next(self):
          return Article.objects.filter(id__gt=self.id).order_by('id').first()
  
  
  # 时间线
  class Timeline(models.Model):
      COLOR_CHOICE = (
          ('primary', '基本-蓝色'),
          ('success', '成功-绿色'),
          ('info', '信息-天蓝色'),
          ('warning', '警告-橙色'),
          ('danger', '危险-红色')
      )
      SIDE_CHOICE = (
          ('L', '左边'),
          ('R', '右边'),
      )
      STAR_NUM = (
          (1, '1颗星'),
          (2, '2颗星'),
          (3, '3颗星'),
          (4, '4颗星'),
          (5, '5颗星'),
      )
      side = models.CharField('位置', max_length=1, choices=SIDE_CHOICE, default='L')
      star_num = models.IntegerField('星星个数', choices=STAR_NUM, default=3)
      icon = models.CharField('图标', max_length=50, default='fa fa-pencil')
      icon_color = models.CharField('图标颜色', max_length=20, choices=COLOR_CHOICE, default='info')
      title = models.CharField('标题', max_length=100)
      update_date = models.DateTimeField('更新时间')
      content = models.TextField('主要内容')
  
      class Meta:
          verbose_name = '时间线'
          verbose_name_plural = verbose_name
          ordering = ['update_date']
  
      def __str__(self):
          return self.title[:20]
  
      def content_to_markdown(self):
          return markdown.markdown(self.content,
                                   extensions=['markdown.extensions.extra', ]
                                   )
  
  
  # 幻灯片
  class Carousel(models.Model):
      number = models.IntegerField('编号', help_text='编号决定图片播放的顺序，图片不要多于5张')
      title = models.CharField('标题', max_length=20, blank=True, null=True, help_text='标题可以为空')
      content = models.CharField('描述', max_length=80)
      img_url = models.CharField('图片地址', max_length=200)
      url = models.CharField('跳转链接', max_length=200, default='#', help_text='图片跳转的超链接，默认#表示不跳转')
  
      class Meta:
          verbose_name = '图片轮播'
          verbose_name_plural = verbose_name
          # 编号越小越靠前，添加的时间约晚约靠前
          ordering = ['number', '-id']
  
      def __str__(self):
          return self.content[:25]
  
  
  # 死链
  class Silian(models.Model):
      badurl = models.CharField('死链地址', max_length=200, help_text='注意：地址是以http开头的完整链接格式')
      remark = models.CharField('死链说明', max_length=50, blank=True, null=True)
      add_date = models.DateTimeField('提交日期', auto_now_add=True)
  
      class Meta:
          verbose_name = '死链'
          verbose_name_plural = verbose_name
          ordering = ['-add_date']
  
      def __str__(self):
          return self.badurl
  
  
  class FriendLink(models.Model):
      name = models.CharField('网站名称', max_length=50)
      description = models.CharField('网站描述', max_length=100, blank=True)
      link = models.URLField('友链地址', help_text='请填写http或https开头的完整形式地址')
      logo = models.URLField('网站LOGO', help_text='请填写http或https开头的完整形式地址', blank=True)
      create_date = models.DateTimeField('创建时间', auto_now_add=True)
      is_active = models.BooleanField('是否有效', default=True)
      is_show = models.BooleanField('是否首页展示', default=False)
  
      class Meta:
          verbose_name = '友情链接'
          verbose_name_plural = verbose_name
          ordering = ['create_date']
  
      def __str__(self):
          return self.name
  
      def get_home_url(self):
          '''提取友链的主页'''
          u = re.findall(r'(http|https://.*?)/.*?', self.link)
          home_url = u[0] if u else self.link
          return home_url
  
      def active_to_false(self):
          self.is_active = False
          self.save(update_fields=['is_active'])
  
      def show_to_false(self):
          self.is_show = True
          self.save(update_fields=['is_show'])
  
  class AboutBlog(models.Model):
      body = models.TextField(verbose_name='About 内容')
      create_date = models.DateTimeField(verbose_name='创建时间', auto_now_add=True)
      update_date = models.DateTimeField(verbose_name='修改时间', auto_now=True)
  
      class Meta:
          verbose_name = 'About'
          verbose_name_plural = verbose_name
  
      def __str__(self):
          return 'About'
  
      def body_to_markdown(self):
          return markdown.markdown(self.body, extensions=[
              'markdown.extensions.extra',
              'markdown.extensions.codehilite',
          ])
  
  