Skip to content

Commit

Permalink
Merge #468 (#480)
Browse files Browse the repository at this point in the history
* If self.app_config.url_patterns setting is 'category', categories field is set as required.

* Adapt the original code to use a setting
  • Loading branch information
yakky authored Mar 5, 2019
1 parent 8d3ebf3 commit e37d983
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 17 deletions.
5 changes: 3 additions & 2 deletions djangocms_blog/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@
from parler.forms import TranslatableModelForm
from taggit_autosuggest.widgets import TagAutoSuggest

from djangocms_blog.settings import get_setting

from .models import BlogCategory, BlogConfig, Post
from .settings import PERMALINK_TYPE_CATEGORY, get_setting


class ConfigFormBase(object):
Expand Down Expand Up @@ -124,6 +123,8 @@ def __init__(self, *args, **kwargs):
]
super(PostAdminForm, self).__init__(*args, **kwargs)
if 'categories' in self.fields:
if self.app_config and self.app_config.url_patterns == PERMALINK_TYPE_CATEGORY:
self.fields['categories'].required = True
self.fields['categories'].queryset = self.available_categories

if 'app_config' in self.fields:
Expand Down
29 changes: 21 additions & 8 deletions djangocms_blog/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,36 @@
MENU_TYPE_NONE = 'none'
DATE_FORMAT = "%a %d %b %Y %H:%M"

PERMALINK_TYPE_FULL_DATE = 'full_date'
PERMALINK_TYPE_SHORT_DATE = 'short_date'
PERMALINK_TYPE_CATEGORY = 'category'
PERMALINK_TYPE_SLUG = 'slug'


def get_setting(name):
from django.conf import settings
from django.utils.translation import ugettext_lazy as _
from meta import settings as meta_settings

PERMALINKS = (
('full_date', _('Full date')),
('short_date', _('Year / Month')),
('category', _('Category')),
('slug', _('Just slug')),
(PERMALINK_TYPE_FULL_DATE, _('Full date')),
(PERMALINK_TYPE_SHORT_DATE, _('Year / Month')),
(PERMALINK_TYPE_CATEGORY, _('Category')),
(PERMALINK_TYPE_SLUG, _('Just slug')),
)
PERMALINKS_URLS = {
'full_date': r'^(?P<year>\d{4})/(?P<month>\d{1,2})/(?P<day>\d{1,2})/(?P<slug>\w[-\w]*)/$',
'short_date': r'^(?P<year>\d{4})/(?P<month>\d{1,2})/(?P<slug>\w[-\w]*)/$',
'category': r'^(?P<category>\w[-\w]*)/(?P<slug>\w[-\w]*)/$',
'slug': r'^(?P<slug>\w[-\w]*)/$',
PERMALINK_TYPE_FULL_DATE: (
r'^(?P<year>\d{4})/(?P<month>\d{1,2})/(?P<day>\d{1,2})/(?P<slug>\w[-\w]*)/$'
),
PERMALINK_TYPE_SHORT_DATE: (
r'^(?P<year>\d{4})/(?P<month>\d{1,2})/(?P<slug>\w[-\w]*)/$'
),
PERMALINK_TYPE_CATEGORY: (
r'^(?P<category>\w[-\w]*)/(?P<slug>\w[-\w]*)/$'
),
PERMALINK_TYPE_SLUG: (
r'^(?P<slug>\w[-\w]*)/$'
),
}
MENU_TYPES = (
(MENU_TYPE_COMPLETE, _('Categories and posts')),
Expand Down
44 changes: 37 additions & 7 deletions tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@
from djangocms_blog.cms_appconfig import BlogConfig, BlogConfigForm
from djangocms_blog.forms import CategoryAdminForm, PostAdminForm
from djangocms_blog.models import BlogCategory, Post
from djangocms_blog.settings import MENU_TYPE_NONE, get_setting
from djangocms_blog.settings import (
MENU_TYPE_NONE, PERMALINK_TYPE_CATEGORY, PERMALINK_TYPE_FULL_DATE, get_setting,
)

from .base import BaseTest

Expand Down Expand Up @@ -115,6 +117,34 @@ def test_admin_thumbnails(self):
self.assertRegexpMatches(force_text(response.content), r'selected[^>]*>Blog image')
self.assertRegexpMatches(force_text(response.content), r'selected[^>]*>Blog thumbnail')

def test_admin_category_required(self):
self.get_pages()

post_admin = admin.site._registry[Post]
request = self.get_page_request('/', self.user, r'/en/blog/', edit=False)
BlogCategory.objects.create(name='category 1 - blog 2', app_config=self.app_config_2)

post = self._get_post(self._post_data[0]['en'])
post = self._get_post(self._post_data[0]['it'], post, 'it')

response = post_admin.change_view(request, str(post.pk))
self.assertEqual(
response.context_data['adminform'].form.fields['categories'].required,
self.app_config_1.url_patterns == PERMALINK_TYPE_CATEGORY
)

self.app_config_1.app_data.config.url_patterns = PERMALINK_TYPE_CATEGORY
self.app_config_1.save()

response = post_admin.change_view(request, str(post.pk))
self.assertEqual(
response.context_data['adminform'].form.fields['categories'].required,
self.app_config_1.url_patterns == PERMALINK_TYPE_CATEGORY
)

self.app_config_1.app_data.config.url_patterns = PERMALINK_TYPE_FULL_DATE
self.app_config_1.save()

def test_admin_post_views(self):
self.get_pages()

Expand Down Expand Up @@ -257,17 +287,17 @@ def test_admin_category_views(self):
request.POST = QueryDict('app_config=1')
request.method = 'POST'
response = category_admin.add_view(request)
self.assertTrue(
response.context_data['adminform'].form.fields['parent'].queryset,
BlogCategory.objects.filter(app_config=self.app_config_1)
self.assertEqual(
list(response.context_data['adminform'].form.fields['parent'].queryset),
list(BlogCategory.objects.filter(app_config=self.app_config_1))
)

request.GET = QueryDict('app_config=1')
request.method = 'GET'
response = category_admin.add_view(request)
self.assertTrue(
response.context_data['adminform'].form.fields['parent'].queryset,
BlogCategory.objects.filter(app_config=self.app_config_1)
self.assertEqual(
list(response.context_data['adminform'].form.fields['parent'].queryset),
list(BlogCategory.objects.filter(app_config=self.app_config_1))
)

# Changeview is 'normal', with a few preselected items
Expand Down

0 comments on commit e37d983

Please sign in to comment.