This repository has been archived on 2023-05-31. You can view files and clone it, but cannot push or open issues or pull requests.
python-blog/articles/models.py

168 lines
5.1 KiB
Python
Raw Normal View History

import random
2020-08-14 16:15:40 +02:00
import re
import uuid
2021-01-03 21:46:41 +01:00
from functools import cached_property, reduce
2020-08-14 16:15:40 +02:00
2020-11-29 08:32:46 +01:00
import html2text
2020-08-14 15:53:42 +02:00
import markdown
2020-12-28 08:04:28 +01:00
import readtime
2020-08-14 14:53:30 +02:00
from django.contrib.auth.models import AbstractUser
2020-08-16 19:23:38 +02:00
from django.contrib.contenttypes.models import ContentType
2020-08-14 14:53:30 +02:00
from django.db import models
2020-08-16 19:45:38 +02:00
from django.template.defaultfilters import slugify
2020-08-16 19:23:38 +02:00
from django.urls import reverse
2020-08-16 18:15:19 +02:00
from django.utils import timezone
from markdown.extensions.codehilite import CodeHiliteExtension
2020-08-14 14:53:30 +02:00
2020-08-25 23:06:12 +02:00
from articles.markdown import LazyLoadingImageExtension
from articles.utils import build_full_absolute_url
2020-08-25 23:06:12 +02:00
2020-08-14 14:53:30 +02:00
class User(AbstractUser):
pass
2020-08-14 15:53:42 +02:00
2020-08-18 19:51:54 +02:00
class AdminUrlMixin:
def get_admin_url(self):
content_type = ContentType.objects.get_for_model(self.__class__)
return reverse(
"admin:%s_%s_change" % (content_type.app_label, content_type.model),
args=(self.id,),
)
2020-12-29 13:16:54 +01:00
def format_article_content(content):
md = markdown.Markdown(
extensions=[
"extra",
2020-12-31 08:46:23 +01:00
"admonition",
2020-12-29 13:16:54 +01:00
CodeHiliteExtension(linenums=False, guess_lang=False),
LazyLoadingImageExtension(),
]
)
content = re.sub(r"(\s)#(\w+)", r"\1\#\2", content)
return md.convert(content)
2020-08-18 19:51:54 +02:00
class Article(AdminUrlMixin, models.Model):
2020-08-14 15:53:42 +02:00
DRAFT = "draft"
PUBLISHED = "published"
STATUS_CHOICES = [
(DRAFT, "Draft"),
(PUBLISHED, "Published"),
]
title = models.CharField(max_length=255)
content = models.TextField()
status = models.CharField(max_length=15, choices=STATUS_CHOICES, default=DRAFT)
published_at = models.DateTimeField(null=True, blank=True)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
2020-08-17 17:49:19 +02:00
author = models.ForeignKey(User, on_delete=models.PROTECT, default=1)
views_count = models.IntegerField(default=0)
2020-08-21 12:33:03 +02:00
slug = models.SlugField(unique=True, max_length=255)
2020-11-28 20:09:37 +01:00
keywords = models.CharField(max_length=255, blank=True)
has_code = models.BooleanField(default=False, blank=True)
is_home = models.BooleanField(default=False, blank=True)
2020-12-25 11:53:49 +01:00
custom_css = models.TextField(blank=True)
draft_key = models.UUIDField(default=uuid.uuid4)
2020-08-17 09:57:24 +02:00
2020-08-14 15:53:42 +02:00
class Meta:
ordering = ["-published_at"]
2020-08-16 18:15:19 +02:00
def __str__(self):
return f"{self.title}"
2020-08-16 18:15:19 +02:00
2020-08-16 19:23:38 +02:00
def get_absolute_url(self):
2020-08-16 19:45:38 +02:00
return reverse("article-detail", kwargs={"slug": self.slug})
2020-08-16 19:23:38 +02:00
2020-08-14 15:53:42 +02:00
def get_abstract(self):
2020-12-28 08:04:28 +01:00
html = self.get_formatted_content
2020-08-14 15:53:42 +02:00
return html.split("<!--more-->")[0]
2020-11-29 08:32:46 +01:00
@cached_property
def get_description(self):
2020-12-28 08:04:28 +01:00
html = self.get_formatted_content
2020-11-29 08:32:46 +01:00
converter = html2text.HTML2Text()
converter.ignore_images = True
converter.ignore_links = True
converter.ignore_tables = True
converter.ignore_emphasis = True
text = converter.handle(html)
total_length = 0
text_result = []
for word in text.split():
if len(word) + 1 + total_length > 160:
break
text_result.append(word)
total_length += len(word) + 1
return " ".join(text_result) + "..."
2020-12-28 08:04:28 +01:00
@cached_property
2020-08-14 15:53:42 +02:00
def get_formatted_content(self):
2020-12-29 13:16:54 +01:00
return format_article_content(self.content)
2020-08-16 18:15:19 +02:00
def publish(self):
2020-08-16 18:15:19 +02:00
if not self.published_at:
self.published_at = timezone.now()
self.status = self.PUBLISHED
self.save()
2020-12-03 22:04:45 +01:00
return self
2020-08-16 18:15:19 +02:00
def unpublish(self):
2020-08-16 18:15:19 +02:00
self.published_at = None
self.status = self.DRAFT
self.save()
2020-12-03 22:04:45 +01:00
return self
2020-08-16 19:45:38 +02:00
2020-08-17 09:57:24 +02:00
def save(self, *args, **kwargs):
2020-08-16 19:45:38 +02:00
if not self.slug:
self.slug = slugify(self.title)
return super().save(*args, **kwargs)
@property
def draft_public_url(self):
url = self.get_absolute_url() + f"?draft_key={self.draft_key}"
return build_full_absolute_url(request=None, url=url)
def refresh_draft_key(self):
self.draft_key = uuid.uuid4()
self.save()
2020-12-28 08:04:28 +01:00
def get_read_time(self):
2020-12-28 10:13:35 +01:00
content = self.get_formatted_content
if content:
return readtime.of_html(content).minutes
return 0
@cached_property
def get_related_articles(self):
related_articles = set()
for keyword in self.get_formatted_keywords:
potential_articles = Article.objects.filter(
keywords__icontains=keyword,
status=Article.PUBLISHED,
).exclude(pk=self.pk)
for article in potential_articles:
if keyword in article.get_formatted_keywords:
related_articles.add(article)
sample_size = min([len(related_articles), 3])
return random.sample(related_articles, sample_size)
@cached_property
def get_formatted_keywords(self):
2021-01-03 21:13:21 +01:00
return list(
filter(None, map(lambda k: k.strip().lower(), self.keywords.split(",")))
)
2021-01-03 21:46:41 +01:00
@cached_property
def get_minified_custom_css(self):
def reducer(res, next_char):
if len(res) == 0:
return next_char
if res[-1] == next_char == " ":
return res
return res + next_char
css = self.custom_css.replace("\n", " ")
return reduce(reducer, css, "")