# Path: blog/models.py
# class Tag(BaseModel):
#     """文章标签"""
#     name = models.CharField('标签名', max_length=30, unique=True)
#     slug = models.SlugField(default='no-slug', max_length=60, blank=True)
# 
#     def __str__(self):
#         return self.name
# 
#     def get_absolute_url(self):
#         return reverse('blog:tag_detail', kwargs={'tag_name': self.slug})
# 
#     @cache_decorator(60 * 60 * 10)
#     def get_article_count(self):
#         return Article.objects.filter(tags__name=self.name).distinct().count()
# 
#     class Meta:
#         ordering = ['name']
#         verbose_name = "标签"
#         verbose_name_plural = verbose_name
# 
# class Category(BaseModel):
#     """文章分类"""
#     name = models.CharField('分类名', max_length=30, unique=True)
#     parent_category = models.ForeignKey(
#         'self',
#         verbose_name="父级分类",
#         blank=True,
#         null=True,
#         on_delete=models.CASCADE)
#     slug = models.SlugField(default='no-slug', max_length=60, blank=True)
#     index = models.IntegerField(default=0, verbose_name="权重排序-越大越靠前")
# 
#     class Meta:
#         ordering = ['-index']
#         verbose_name = "分类"
#         verbose_name_plural = verbose_name
# 
#     def get_absolute_url(self):
#         return reverse(
#             'blog:category_detail', kwargs={
#                 'category_name': self.slug})
# 
#     def __str__(self):
#         return self.name
# 
#     @cache_decorator(60 * 60 * 10)
#     def get_category_tree(self):
#         """
#         递归获得分类目录的父级
#         :return:
#         """
#         categorys = []
# 
#         def parse(category):
#             categorys.append(category)
#             if category.parent_category:
#                 parse(category.parent_category)
# 
#         parse(self)
#         return categorys
# 
#     @cache_decorator(60 * 60 * 10)
#     def get_sub_categorys(self):
#         """
#         获得当前分类目录所有子集
#         :return:
#         """
#         categorys = []
#         all_categorys = Category.objects.all()
# 
#         def parse(category):
#             if category not in categorys:
#                 categorys.append(category)
#             childs = all_categorys.filter(parent_category=category)
#             for child in childs:
#                 if category not in categorys:
#                     categorys.append(child)
#                 parse(child)
# 
#         parse(self)
#         return categorys


from django.core.management.base import BaseCommand
from blog.models import Tag, Category



# TODO 参数化
class Command(BaseCommand):
    help = 'build search words'

    def handle(self, *args, **options):
        datas = set([t.name for t in Tag.objects.all()] +
                    [t.name for t in Category.objects.all()])

# Path: blog/models.py
# class Tag(BaseModel):
#     """文章标签"""
#     name = models.CharField('标签名', max_length=30, unique=True)
#     slug = models.SlugField(default='no-slug', max_length=60, blank=True)
# 
#     def __str__(self):
#         return self.name
# 
#     def get_absolute_url(self):
#         return reverse('blog:tag_detail', kwargs={'tag_name': self.slug})
# 
#     @cache_decorator(60 * 60 * 10)
#     def get_article_count(self):
#         return Article.objects.filter(tags__name=self.name).distinct().count()
# 
#     class Meta:
#         ordering = ['name']
#         verbose_name = "标签"
#         verbose_name_plural = verbose_name
# 
# class Category(BaseModel):
#     """文章分类"""
#     name = models.CharField('分类名', max_length=30, unique=True)
#     parent_category = models.ForeignKey(
#         'self',
#         verbose_name="父级分类",
#         blank=True,
#         null=True,
#         on_delete=models.CASCADE)
#     slug = models.SlugField(default='no-slug', max_length=60, blank=True)
#     index = models.IntegerField(default=0, verbose_name="权重排序-越大越靠前")
# 
#     class Meta:
#         ordering = ['-index']
#         verbose_name = "分类"
#         verbose_name_plural = verbose_name
# 
#     def get_absolute_url(self):
#         return reverse(
#             'blog:category_detail', kwargs={
#                 'category_name': self.slug})
# 
#     def __str__(self):
#         return self.name
# 
#     @cache_decorator(60 * 60 * 10)
#     def get_category_tree(self):
#         """
#         递归获得分类目录的父级
#         :return:
#         """
#         categorys = []
# 
#         def parse(category):
#             categorys.append(category)
#             if category.parent_category:
#                 parse(category.parent_category)
# 
#         parse(self)
#         return categorys
# 
#     @cache_decorator(60 * 60 * 10)
#     def get_sub_categorys(self):
#         """
#         获得当前分类目录所有子集
#         :return:
#         """
#         categorys = []
#         all_categorys = Category.objects.all()
# 
#         def parse(category):
#             if category not in categorys:
#                 categorys.append(category)
#             childs = all_categorys.filter(parent_category=category)
#             for child in childs:
#                 if category not in categorys:
#                     categorys.append(child)
#                 parse(child)
# 
#         parse(self)
#         return categorys


# Path: blog/management/commands/build_search_words.py
from django.core.management.base import BaseCommand
from blog.models import Tag, Category




# TODO 参数化
class Command(BaseCommand):
    help = 'build search words'

    def handle(self, *args, **options):
        datas = set([t.name for t in Tag.objects.all()] +
                    [t.name for t in Category.objects.all()])

        print('\n'.join(datas))