# Path: apps/ext/models.py
# class CacheMixin(object):
#     def __get_prefix_key(self):
#         return f"{self.__class__.__name__}:{self.pk}"
# 
#     def set_value_to_cache(self, key, value, timeout=3600):
#         _cache_key = f"{self.__get_prefix_key()}:{key}"
#         cache.set(_cache_key, value, timeout=timeout)
# 
#     def get_value_from_cache(self, key):
#         _cache_key = f"{self.__get_prefix_key()}:{key}"
#         return cache.get(_cache_key)
# 
# Path: apps/ext/render/md.py

# 
# Path: apps/images/models/db.py
# class Image(ImageAbstractModel):
#     description = models.CharField(max_length=255, blank=True)
#     is_cover = models.BooleanField(default=False)
# 
#     def __str__(self):
#         return self.description
# 
#     class Meta(ImageAbstractModel.Meta):
#         db_table = "generic_image"
# 
#     @cached_property
#     def thumb(self):
#         return self.resize_image()
# 
#     def resize_image(self, size=128):
#         _img_url = f"/upload/{size}/{self.file.name}"
#         if not debug:
#             _img_url = f"https://image.jiaxin.im{_img_url}"
#         return _img_url
# 
# Path: apps/blog/models/manager.py
# class PostManager(models.Manager):
#     def published(self):
#         return self.filter(status=self.model.publish)


import logging
import re
from typing import List
from django.contrib.contenttypes.fields import GenericRelation
from django.db import models
from django.utils.functional import cached_property
from django.utils.html import strip_tags
from django.utils.translation import ugettext_lazy as _
from taggit.managers import TaggableManager
from apps.ext.models import CacheMixin
from apps.ext.render.md import md
from apps.images.models import Image
from .manager import PostManager
    def html_content(self):
        value = self.get_value_from_cache("html:content")
        if value is None:
            value, toc = self.render_markdown()
            self.set_value_to_cache("html:content", value, timeout=86400)
        return value

    @property
    def toc(self):
        html, toc = self.render_markdown()
        return toc

    def process_content(self):
        _content = f"{self.title}{strip_tags(self.html_content)}"
        _content = re.sub("(\\d|\\W)+", " ", _content)
        _content = re.sub(r"\s+", " ", _content)
        _content = _content.lower()
        return _content

    def tag_list(self) -> List[str]:
        return [o.name for o in self.tags.all()]

    def tag_string(self):
        return ",".join(self.tag_list())

    def get_absolute_url(self) -> str:
        return f"/posts/{self.slug}/"

    def render_markdown(self):
        html = md.convert(self.content)

# Path: apps/ext/models.py
# class CacheMixin(object):
#     def __get_prefix_key(self):
#         return f"{self.__class__.__name__}:{self.pk}"
# 
#     def set_value_to_cache(self, key, value, timeout=3600):
#         _cache_key = f"{self.__get_prefix_key()}:{key}"
#         cache.set(_cache_key, value, timeout=timeout)
# 
#     def get_value_from_cache(self, key):
#         _cache_key = f"{self.__get_prefix_key()}:{key}"
#         return cache.get(_cache_key)
# 
# Path: apps/ext/render/md.py

# 
# Path: apps/images/models/db.py
# class Image(ImageAbstractModel):
#     description = models.CharField(max_length=255, blank=True)
#     is_cover = models.BooleanField(default=False)
# 
#     def __str__(self):
#         return self.description
# 
#     class Meta(ImageAbstractModel.Meta):
#         db_table = "generic_image"
# 
#     @cached_property
#     def thumb(self):
#         return self.resize_image()
# 
#     def resize_image(self, size=128):
#         _img_url = f"/upload/{size}/{self.file.name}"
#         if not debug:
#             _img_url = f"https://image.jiaxin.im{_img_url}"
#         return _img_url
# 
# Path: apps/blog/models/manager.py
# class PostManager(models.Manager):
#     def published(self):
#         return self.filter(status=self.model.publish)


# Path: apps/blog/models/db.py
import logging
import re
from typing import List
from django.contrib.contenttypes.fields import GenericRelation
from django.db import models
from django.utils.functional import cached_property
from django.utils.html import strip_tags
from django.utils.translation import ugettext_lazy as _
from taggit.managers import TaggableManager
from apps.ext.models import CacheMixin
from apps.ext.render.md import md
from apps.images.models import Image
from .manager import PostManager

    def html_content(self):
        value = self.get_value_from_cache("html:content")
        if value is None:
            value, toc = self.render_markdown()
            self.set_value_to_cache("html:content", value, timeout=86400)
        return value

    @property
    def toc(self):
        html, toc = self.render_markdown()
        return toc

    def process_content(self):
        _content = f"{self.title}{strip_tags(self.html_content)}"
        _content = re.sub("(\\d|\\W)+", " ", _content)
        _content = re.sub(r"\s+", " ", _content)
        _content = _content.lower()
        return _content

    def tag_list(self) -> List[str]:
        return [o.name for o in self.tags.all()]

    def tag_string(self):
        return ",".join(self.tag_list())

    def get_absolute_url(self) -> str:
        return f"/posts/{self.slug}/"

    def render_markdown(self):
        html = md.convert(self.content)

        return html, md.toc