Add very stupid check to avoid circular relationships for BlogCategory

This commit is contained in:
Iacopo Spalletti 2016-08-03 09:00:46 +02:00
parent 5cc81adb98
commit e56d5aa137
No known key found for this signature in database
GPG key ID: BDCBC2EB289F60C6
4 changed files with 51 additions and 2 deletions

View file

@ -19,7 +19,7 @@ from django.utils.translation import get_language_from_request, ugettext_lazy as
from parler.admin import TranslatableAdmin
from .cms_appconfig import BlogConfig
from .forms import PostAdminForm
from .forms import CategoryAdminForm, PostAdminForm
from .models import BlogCategory, Post
from .settings import get_setting
@ -31,6 +31,10 @@ except ImportError:
class BlogCategoryAdmin(EnhancedModelAdminMixin, ModelAppHookConfig, TranslatableAdmin):
form = CategoryAdminForm
list_display = [
'name', 'parent', 'all_languages_column',
]
def get_prepopulated_fields(self, request, obj=None):
app_config_default = self._app_config_select(request, obj)

View file

@ -9,6 +9,20 @@ from taggit_autosuggest.widgets import TagAutoSuggest
from .models import BlogCategory, BlogConfig, Post
class CategoryAdminForm(TranslatableModelForm):
def __init__(self, *args, **kwargs):
super(CategoryAdminForm, self).__init__(*args, **kwargs)
if self.instance.pk:
self.fields['parent'].queryset = self.fields['parent'].queryset.exclude(
pk__in=[self.instance.pk] + [child.pk for child in self.instance.descendants()]
)
class Meta:
model = BlogCategory
fields = '__all__'
class LatestEntriesForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super(LatestEntriesForm, self).__init__(*args, **kwargs)

View file

@ -0,0 +1,21 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.9.9 on 2016-08-03 06:58
from __future__ import unicode_literals
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('djangocms_blog', '0024_auto_20160706_1524'),
]
operations = [
migrations.AlterField(
model_name='blogcategory',
name='parent',
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='children', to='djangocms_blog.BlogCategory', verbose_name='parent'),
),
]

View file

@ -60,7 +60,9 @@ class BlogCategory(TranslatableModel):
"""
Blog category
"""
parent = models.ForeignKey('self', verbose_name=_('parent'), null=True, blank=True)
parent = models.ForeignKey(
'self', verbose_name=_('parent'), null=True, blank=True, related_name='children'
)
date_created = models.DateTimeField(_('created at'), auto_now_add=True)
date_modified = models.DateTimeField(_('modified at'), auto_now=True)
app_config = AppHookConfigField(
@ -79,6 +81,14 @@ class BlogCategory(TranslatableModel):
verbose_name = _('blog category')
verbose_name_plural = _('blog categories')
def descendants(self):
children = []
if self.children.exists():
children.extend(self.children.all())
for child in self.children.all():
children.extend(child.descendants())
return children
@cached_property
def linked_posts(self):
return self.blog_posts.namespace(self.app_config.namespace)