2020-08-14 16:15:40 +02:00
|
|
|
import re
|
|
|
|
|
2020-08-14 15:53:42 +02:00
|
|
|
import markdown
|
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-18 09:37:13 +02:00
|
|
|
from markdown.extensions.codehilite import CodeHiliteExtension
|
2020-08-14 14:53:30 +02:00
|
|
|
|
|
|
|
|
|
|
|
class User(AbstractUser):
|
|
|
|
pass
|
2020-08-14 15:53:42 +02:00
|
|
|
|
|
|
|
|
2020-08-17 09:57:24 +02:00
|
|
|
class ArticleManager(models.Manager):
|
|
|
|
def get_queryset(self):
|
|
|
|
return super().get_queryset().filter(page__isnull=True)
|
|
|
|
|
|
|
|
|
2020-08-14 15:53:42 +02:00
|
|
|
class Article(models.Model):
|
|
|
|
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)
|
2020-08-14 22:06:38 +02:00
|
|
|
views_count = models.IntegerField(default=0)
|
2020-08-16 19:45:38 +02:00
|
|
|
slug = models.SlugField(unique=True)
|
2020-08-14 15:53:42 +02:00
|
|
|
|
2020-08-17 09:57:24 +02:00
|
|
|
objects = ArticleManager()
|
|
|
|
with_pages = models.Manager()
|
|
|
|
|
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 self.title
|
|
|
|
|
2020-08-16 19:23:38 +02: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,),
|
|
|
|
)
|
|
|
|
|
|
|
|
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-08-14 16:15:40 +02:00
|
|
|
html = self.get_formatted_content()
|
2020-08-14 15:53:42 +02:00
|
|
|
return html.split("<!--more-->")[0]
|
|
|
|
|
|
|
|
def get_formatted_content(self):
|
2020-08-18 10:30:58 +02:00
|
|
|
md = markdown.Markdown(
|
|
|
|
extensions=["extra", CodeHiliteExtension(linenums=False)]
|
|
|
|
)
|
2020-08-14 16:15:40 +02:00
|
|
|
content = self.content
|
|
|
|
content = re.sub(r"(\s)#(\w+)", r"\1\#\2", content)
|
|
|
|
return md.convert(content)
|
2020-08-16 18:15:19 +02:00
|
|
|
|
2020-08-16 18:40:02 +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
|
2020-08-16 18:40:02 +02:00
|
|
|
self.save()
|
2020-08-16 18:15:19 +02:00
|
|
|
|
2020-08-16 18:40:02 +02:00
|
|
|
def unpublish(self):
|
2020-08-16 18:15:19 +02:00
|
|
|
self.published_at = None
|
|
|
|
self.status = self.DRAFT
|
2020-08-16 18:40:02 +02:00
|
|
|
self.save()
|
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)
|
2020-08-17 09:57:24 +02:00
|
|
|
|
|
|
|
|
|
|
|
class Page(Article):
|
|
|
|
objects = models.Manager()
|
|
|
|
position = models.IntegerField(default=0)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
ordering = ["position", "-published_at"]
|
2020-08-18 08:24:40 +02:00
|
|
|
|
|
|
|
|
2020-08-18 18:49:41 +02:00
|
|
|
class Comment(models.Model):
|
|
|
|
username = models.CharField(
|
|
|
|
max_length=255, help_text="Will be displayed with your comment."
|
|
|
|
)
|
|
|
|
email = models.EmailField(
|
|
|
|
blank=True,
|
|
|
|
null=True,
|
|
|
|
help_text=(
|
|
|
|
"Not mandatory, fill only if you want me to be able to contact you. "
|
|
|
|
"Will never be displayed here nor shared with any third party."
|
|
|
|
),
|
|
|
|
)
|
|
|
|
content = models.TextField(
|
2020-08-18 19:12:47 +02:00
|
|
|
max_length=500,
|
|
|
|
help_text="Your comment, limited to 500 characters. No formatting.",
|
2020-08-18 18:49:41 +02:00
|
|
|
)
|
|
|
|
article = models.ForeignKey(
|
|
|
|
Article, on_delete=models.CASCADE, related_name="comments"
|
|
|
|
)
|
|
|
|
created_at = models.DateTimeField(auto_now_add=True)
|
|
|
|
approved = models.BooleanField(default=True)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
ordering = ["created_at"]
|