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/src/articles/models.py

170 lines
5.3 KiB
Python
Raw Normal View History

2021-12-28 22:31:53 +01:00
from __future__ import annotations
import random
import uuid
2022-09-23 22:45:29 +02:00
from collections.abc import Sequence
2021-01-05 17:59:58 +01:00
from functools import cached_property
2022-09-23 22:45:29 +02:00
from typing import Any
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
2021-03-04 19:33:43 +01:00
from django.conf import settings
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
2022-09-23 23:19:16 +02:00
from django.db.models import F, Prefetch
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,
find_first_paragraph_with_text,
format_article_content,
2021-01-03 21:51:46 +01:00
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"]
2021-12-28 22:31:53 +01:00
def __str__(self) -> str:
2021-03-03 17:30:38 +01:00
return self.name
2021-12-28 22:31:53 +01:00
def get_absolute_url(self) -> str:
2021-03-04 19:33:43 +01:00
return reverse("tag", kwargs={"slug": self.slug})
2021-12-28 22:31:53 +01:00
def get_feed_title(self) -> str:
2021-03-04 19:33:43 +01:00
return f"{self.name} - {settings.BLOG['title']}"
2021-12-28 22:31:53 +01:00
def get_feed_url(self) -> str:
2021-03-04 19:33:43 +01:00
return reverse("tag-feed", kwargs={"slug": self.slug})
2021-03-03 17:30:38 +01:00
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"),
]
CONTENT_DEFAULT = (
'!!! warning "Draft"\n'
" This article is still a draft. It may appear by error in your feed "
'if I click on the "publish" button too early 😊'
)
2020-08-14 15:53:42 +02:00
title = models.CharField(max_length=255)
content = models.TextField(default=CONTENT_DEFAULT)
2020-08-14 15:53:42 +02:00
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-06 14:50:31 +01:00
tags = models.ManyToManyField(to=Tag, related_name="articles", blank=True)
2020-08-17 09:57:24 +02:00
2020-08-14 15:53:42 +02:00
class Meta:
2022-05-25 19:18:02 +02:00
ordering = ["-published_at", "-updated_at"]
2020-08-14 15:53:42 +02:00
2021-12-28 22:31:53 +01:00
def __str__(self) -> str:
2021-03-04 18:48:48 +01:00
return self.title
2020-08-16 18:15:19 +02:00
2021-12-28 22:31:53 +01:00
def get_absolute_url(self) -> str:
2020-08-16 19:45:38 +02:00
return reverse("article-detail", kwargs={"slug": self.slug})
2020-08-16 19:23:38 +02:00
2021-12-28 22:31:53 +01:00
def get_mailto_url(self) -> str:
2021-12-27 22:41:32 +01:00
email = settings.BLOG["email"]
return f"mailto:{email}?subject={self.title}"
2021-12-28 22:31:53 +01:00
def get_abstract(self) -> str:
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
2021-12-28 22:31:53 +01:00
def get_description(self) -> str:
2020-12-28 08:04:28 +01:00
html = self.get_formatted_content
text = find_first_paragraph_with_text(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
2021-12-28 22:31:53 +01:00
def get_formatted_content(self) -> str:
return format_article_content(self.content)
2020-08-16 18:15:19 +02:00
2021-12-28 22:31:53 +01:00
def publish(self) -> Article:
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
2021-12-28 22:31:53 +01:00
def unpublish(self) -> Article:
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
2021-12-31 12:08:03 +01:00
def save(self, *args: Any, **kwargs: Any) -> None:
2020-08-16 19:45:38 +02:00
if not self.slug:
self.slug = slugify(self.title)
return super().save(*args, **kwargs)
@property
2021-12-28 22:31:53 +01:00
def draft_public_url(self) -> str:
url = self.get_absolute_url() + f"?draft_key={self.draft_key}"
return build_full_absolute_url(request=None, url=url)
2021-12-28 22:31:53 +01:00
def refresh_draft_key(self) -> None:
self.draft_key = uuid.uuid4()
self.save()
2020-12-28 08:04:28 +01:00
2021-12-28 22:31:53 +01:00
def get_read_time(self) -> int:
2022-09-23 23:19:16 +02:00
content = self.get_formatted_content
if content:
return readtime.of_markdown(content).minutes
2020-12-28 10:13:35 +01:00
return 0
@cached_property
2021-12-28 22:31:53 +01:00
def get_related_articles(self) -> Sequence[Article]:
related_articles = set()
2021-12-27 11:21:47 +01:00
published_articles = Article.objects.filter(status=Article.PUBLISHED).exclude(
pk=self.pk
)
for tag in self.tags.all().prefetch_related(
Prefetch("articles", published_articles, to_attr="published_articles")
):
2023-01-30 20:58:18 +01:00
related_articles.update(tag.published_articles)
sample_size = min([len(related_articles), 3])
return random.sample(list(related_articles), sample_size)
@cached_property
2021-12-28 22:31:53 +01:00
def keywords(self) -> str:
2022-09-23 22:45:29 +02:00
return ", ".join(tag.name for tag in self.tags.all())
2021-01-03 21:46:41 +01:00
2021-01-05 17:59:58 +01:00
@cached_property
2021-12-28 22:31:53 +01:00
def get_minified_custom_css(self) -> str:
2021-01-05 17:59:58 +01:00
return rcssmin.cssmin(self.custom_css)
2021-12-28 22:31:53 +01:00
def get_admin_url(self) -> str:
2021-01-03 21:51:46 +01:00
content_type = ContentType.objects.get_for_model(self.__class__)
return reverse(
f"admin:{content_type.app_label}_{content_type.model}_change",
2021-01-03 21:51:46 +01:00
args=(self.id,),
)
2022-09-23 23:19:16 +02:00
def increment_view_count(self) -> None:
self.views_count = F("views_count") + 1
self.save(update_fields=["views_count"])