from django.db import models
from django.shortcuts import reverse
from ckeditor.fields import RichTextField
from image_cropping import ImageRatioField
from easy_thumbnails.files import get_thumbnailer
class Groups(models.Model):
class Meta:
verbose_name_plural = 'Группы'
verbose_name = 'группу'
title = models.CharField(
max_length=255,
verbose_name='Заголовок',
db_index=True,
blank=False
)
description = models.TextField(
verbose_name='Описание',
default=None,
null=True,
blank=True,
)
body = RichTextField(
verbose_name='Основной текст',
db_index=True,
blank=True
)
created = models.DateTimeField(
verbose_name="Время создания",
auto_now_add=True
)
modified = models.DateTimeField(
verbose_name="Время изменения",
auto_now=True
)
parent = models.ForeignKey(
'self',
verbose_name='Родитель',
related_name='children',
on_delete='CASCADE',
default=None,
null=True,
blank=True,
)
@property
def count(self):
self_count = 0
children_count = 0
if hasattr(self, 'page_children'):
self_count = self.page_children.count()
if hasattr(self, 'children'):
children_count = len([children.count for children in self.children.all()])
return self_count + children_count
def get_absolute_url(self):
return reverse('group_url', kwargs={'group_id': self.id})
def get_absolute_url_page(self):
return reverse('pages_url', kwargs={'pages_id': self.page_children.pages_id})
def __str__(self):
return self.title
class Pages(models.Model):
class Meta:
verbose_name_plural = 'Страницы'
verbose_name = 'страницу'
title = models.CharField(
max_length=255,
verbose_name='Заголовок',
db_index=True,
blank=False
)
body = RichTextField(
verbose_name='Основной текст',
db_index=True,
blank=True
)
image = models.ImageField(
upload_to='library/img/',
verbose_name='Фотография',
blank=True
)
cropping = ImageRatioField(
'image',
'1210x480'
)
created = models.DateTimeField(
verbose_name="Время создания",
auto_now_add=True
)
modified = models.DateTimeField(
verbose_name="Время изменения",
auto_now=True
)
parent = models.ForeignKey(
'Groups',
verbose_name='Родитель',
related_name='page_children',
on_delete='CASCADE',
default=None,
null=True,
blank=True,
)
def get_image_1210x480(self):
return get_thumbnailer(self.image, '1210x480')
def __str__(self):
return self.title
from django.shortcuts import render, get_object_or_404
from .models import *
def index(request):
groups = Groups.objects.filter(parent__isnull=True).all()
return render(request, 'library/index.html', context={'groups': groups})
def podgroup(request, group_id):
item = get_object_or_404(Groups, id__iexact=group_id)
return render(request, 'library/block.html', context={'item': item})
def pages(request, pages_id):
page = get_object_or_404(Pages, id__iexact=pages_id)
return render(request, 'library/pages.html', context={'page':page})
from django.urls import path
from .views import *
urlpatterns = [
path('', index, name='groups_list'),
path('<str:group_id>/', podgroup, name='group_url'),
path('page/<str:pages_id>', pages, name='pages_url'),
]
{% extends 'base.html' %}
{% load cropping %}
{% load static %}
{% block title %}
{{ item.title }} - {{ block.super }}
{% endblock %}
{% block content %}
<div class="row">
<div class="col-lg-12 mt-4">
<div style="background: #3F729B; color: white; font-size: 14px;
font-weight: 400; text-transform: uppercase; text-align: center;
padding: 15px; border-top-left-radius: 5px; border-top-right-radius: 5px;">
{% if item.description %}
{{ item.description }}
{% else %}
{{ item.title }}
{% endif %}
</div>
</div>
{% if item.children.count > 0 %}
<div class="col-sm-12 flex-col">
<div class="dig-list-group__content">
{% for item in item.children.all %}
{% if item.count > 0 %}
<div class="dig-list-group__element js-link"
data-url="{{ item.get_absolute_url_page }}">
<div class="dig-list__item">
<h4 style="font-weight: 400;">
<a href="{{ item.get_absolute_url_page }}">{{ item.title }}</a>
</h4>
{% if item.description %}
<hr style="background-color: black; height: 2px; width: 80px;"/>
<span>{{ item.description }}</span>
{% endif %}
</div>
</div>
{% endif %}
{% endfor %}
</div>
</div>
{% endif %}
{% endblock %}