django-simplecms/cms/models.py

79 wiersze
2.1 KiB
Python

2024-12-10 22:05:36 +00:00
"""
Base page and section models.
"""
2019-03-27 15:49:14 +00:00
from django.db import models
from django.urls import reverse
from django.utils.text import slugify
2019-03-27 15:49:14 +00:00
from django.utils.translation import gettext_lazy as _
from embed_video.fields import EmbedVideoField
from . import fields, mixins
2019-08-23 15:19:40 +00:00
2024-12-10 00:03:35 +00:00
class BasePage(mixins.Numbered, models.Model):
2024-12-10 22:05:36 +00:00
"""
Abstract base model for pages.
"""
title = fields.CharField(_("page"))
slug = fields.SlugField(_("slug"), blank=True, unique=True)
number = fields.PositiveIntegerField(_("number"), blank=True)
menu = fields.BooleanField(_("visible in menu"), default=True)
2019-03-27 15:49:14 +00:00
def __str__(self):
2019-08-26 07:47:52 +00:00
if not self.pk:
return str(_("New page"))
return self.title
2019-03-27 15:49:14 +00:00
def get_absolute_url(self):
if self.slug:
return reverse("cms:page", args=[self.slug])
return reverse("cms:page")
2019-03-27 15:49:14 +00:00
class Meta:
abstract = True
verbose_name = _("page")
verbose_name_plural = _("pages")
ordering = ["number"]
2024-12-10 00:03:35 +00:00
class BaseSection(mixins.Numbered, models.Model):
2024-12-10 22:05:36 +00:00
"""
Abstract base model for sections.
"""
2019-03-27 15:49:14 +00:00
TYPES = []
title = fields.CharField(_("section"))
type = fields.CharField(_("type"))
number = fields.PositiveIntegerField(_("number"), blank=True)
content = fields.TextField(_("content"), blank=True)
image = fields.ImageField(_("image"), blank=True)
video = EmbedVideoField(
_("video"),
blank=True,
help_text=_("Paste a YouTube, Vimeo, or SoundCloud link"),
)
href = fields.CharField(_("link"), blank=True)
2024-12-10 22:08:03 +00:00
subject = fields.CharField(_("subject"), blank=True)
2019-03-27 15:49:14 +00:00
def number_with_respect_to(self):
return self.page.sections.all()
def get_absolute_url(self):
return self.page.get_absolute_url() + "#" + slugify(self.title)
2019-03-27 15:49:14 +00:00
def __str__(self):
2019-08-26 07:47:52 +00:00
if not self.pk:
return str(_("New section"))
2019-08-26 07:47:52 +00:00
elif not self.title:
return str(_("Untitled"))
2019-08-26 07:47:52 +00:00
else:
return self.title
2019-03-27 15:49:14 +00:00
class Meta:
abstract = True
verbose_name = _("section")
verbose_name_plural = _("sections")
ordering = ["number"]