combo/combo/apps/kb/models.py

47 lines
1.8 KiB
Python

# combo - content management system
# Copyright (C) 2020 Entr'ouvert
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import datetime
from django.db import models
from django.utils import timezone
from django.utils.translation import ugettext_lazy as _
from combo.data.models import CellBase, Page
from combo.data.library import register_cell_class
@register_cell_class
class LatestPageUpdatesCell(CellBase):
root_page = models.ForeignKey(
Page, on_delete=models.SET_NULL, null=True, blank=True,
verbose_name=_('Root Page'), related_name='kb_latest_page_updates_cell_root_page')
limit = models.PositiveSmallIntegerField(
_('Maximum number of entries'), null=True, blank=True)
template_name = 'combo/latest-page-updates-cell.html'
class Meta:
verbose_name = _('Latest Page Updates')
def get_cell_extra_context(self, context):
extra_context = super(LatestPageUpdatesCell, self).get_cell_extra_context(context)
if self.root_page:
pages = self.root_page.get_descendants_and_me()
else:
pages = Page.objects.all()
extra_context['pages'] = pages.order_by('-last_update_timestamp')[:self.limit]
return extra_context