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

138 lines
4 KiB
Python
Raw Normal View History

import random
import uuid
2021-01-05 17:59:58 +01:00
from functools import cached_property
2020-08-14 16:15:40 +02:00
2021-01-05 17:59:58 +01:00
import rcssmin
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
2020-08-14 14:53:30 +02:00
2021-01-03 21:51:46 +01:00
from articles.utils import (
build_full_absolute_url,
format_article_content,
get_html_to_text_converter,
truncate_words_after_char_count,
)
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
2021-03-03 17:30:38 +01:00
class Tag(models.Model):
name = models.CharField(max_length=255, unique=True)
2021-03-04 18:24:22 +01:00
slug = models.CharField(max_length=255, unique=True)
2021-03-03 17:30:38 +01:00
class Meta:
ordering = ["name"]
def __str__(self):
return self.name
2021-01-03 21:51:46 +01:00
class Article(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)
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)
2021-03-03 17:30:38 +01:00
tags = models.ManyToManyField(to=Tag, related_name="articles")
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):
2021-03-04 18:48:48 +01:00
return 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
2021-01-03 21:51:46 +01:00
converter = get_html_to_text_converter()
2020-11-29 08:32:46 +01:00
text = converter.handle(html)
2021-01-03 21:51:46 +01:00
return truncate_words_after_char_count(text, 160)
2020-11-29 08:32:46 +01:00
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()
2021-03-03 17:30:38 +01:00
for tag in self.tags.all():
related_articles.update(tag.articles.all())
sample_size = min([len(related_articles), 3])
return random.sample(related_articles, sample_size)
@cached_property
2021-03-03 17:30:38 +01:00
def keywords(self):
return ", ".join(map(lambda tag: tag.name, self.tags.all()))
2021-01-03 21:46:41 +01:00
2021-01-05 17:59:58 +01:00
@cached_property
def get_minified_custom_css(self):
return rcssmin.cssmin(self.custom_css)
2021-01-03 21:51:46 +01:00
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,),
)