Skip to content

Commit

Permalink
openlibhums#509: Adds verbose names to CMS models
Browse files Browse the repository at this point in the history
  • Loading branch information
mauromsl committed Jan 28, 2020
1 parent 9da3d72 commit eccb001
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions src/cms/models/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from django.utils.translation import ugettext_lazy as _
from model_utils.managers import InheritanceManager

from utils import setting_handler
from utils.logic import get_current_request


Expand All @@ -24,6 +25,7 @@ class CMSBlock(models.Model):
ALLOWED_COLUMNS = ((4, "1/4"), (6, "1/2"), (8, "2/3"), (12, "1"))

columns = models.PositiveIntegerField(blank=True, null=True, choices=ALLOWED_COLUMNS)
sequence = models.PositiveIntegerField()
page = models.ForeignKey("cms.CMSPage",
on_delete=models.CASCADE,
related_name="blocks",
Expand All @@ -41,6 +43,24 @@ def context(self):
""" The context required to render this block"""
return {}

@property
def next_sequence(self):
last_sequence = self.__class__.objects.filter(
article=self.article,
cms_block=self.cms_block,
).aggregate(Max("sequence"))["sequence__max"]
if last_sequence is None:
return 1
else:
return last_sequence + 1


def save(self, *args, **kwargs):
if not self.sequence:
self.sequence = self.next_sequence
super().save(*args, **kwargs)



class HTMLBlock(CMSBlock):
TEMPLATE = 'cms/blocks/html_block.html'
Expand All @@ -50,6 +70,10 @@ class HTMLBlock(CMSBlock):
def context(self):
return {"content": self.content}

class Meta:
verbose_name="HTML Block"
verbose_name_plural="HTML Blocks"


class AboutBlock(CMSBlock):
TEMPLATE = 'cms/blocks/about_block.html'
Expand All @@ -62,8 +86,15 @@ class AboutBlock(CMSBlock):
def context(self):
return {
'title': self.title,
'about_content': setting_handler.get_setting(
'general', 'journal_description', self.page.journal
).value,
}

class Meta:
verbose_name="About Block"
verbose_name_plural="About Blocks"


class NewsBlock(CMSBlock):
TEMPLATE = 'cms/blocks/news_block.html'
Expand All @@ -88,6 +119,10 @@ def context(self):

return {"news_items":news_items}

class Meta:
verbose_name="News Block"
verbose_name_plural="News Blocks"


class FeaturedJournalsBlock(CMSBlock):
TEMPLATE = 'cms/blocks/featured_journals_block.html'
Expand Down Expand Up @@ -116,6 +151,10 @@ def context(self):

return {"featured_journals": journals}

class Meta:
verbose_name="Featured Journals Block"
verbose_name_plural="Featured Journals Blocks"


class FeaturedArticlesBlock(CMSBlock):
TEMPLATE = 'cms/blocks/featured_articles_block.html'
Expand Down

0 comments on commit eccb001

Please sign in to comment.