Skip to content

Commit

Permalink
Moved certain Page fields to Title model (#6477)
Browse files Browse the repository at this point in the history
  • Loading branch information
chaosk authored and czpython committed Aug 16, 2018
1 parent cd74dc8 commit d7e2d26
Show file tree
Hide file tree
Showing 24 changed files with 509 additions and 226 deletions.
109 changes: 94 additions & 15 deletions cms/admin/forms.py
Expand Up @@ -9,6 +9,7 @@
from django.forms.utils import ErrorList
from django.forms.widgets import HiddenInput
from django.template.defaultfilters import slugify
from django.utils import timezone
from django.utils.encoding import force_text
from django.utils.functional import cached_property
from django.utils.translation import ugettext, ugettext_lazy as _
Expand Down Expand Up @@ -38,6 +39,7 @@
get_language_object,
get_site_language_from_request,
)
from cms.utils.page import get_clean_username
from cms.utils.permissions import (
get_current_user,
get_subordinate_users,
Expand Down Expand Up @@ -247,6 +249,8 @@ def create_translation(self, page):
'slug': data['slug'],
'path': data['path'],
'title': data['title'],
'template': self.get_template(),
'created_by': self._user,
}

if 'menu_title' in data:
Expand All @@ -268,11 +272,20 @@ def from_source(self, source, parent=None):
permissions=False,
extensions=False,
)
new_page.update(is_page_type=False, in_navigation=True)
new_page.update(is_page_type=False)
return new_page

def update_translations_from_source(self, new_page):
new_page.update_translations(
self._language,
in_navigation=True,
)

def get_template(self):
return Page.TEMPLATE_DEFAULT
source = self.cleaned_data.get('source')
if source:
return source.get_template(self._language)
return Title.TEMPLATE_DEFAULT

def save(self, *args, **kwargs):
source = self.cleaned_data.get('source')
Expand All @@ -287,14 +300,15 @@ def save(self, *args, **kwargs):
new_page = self.from_source(source, parent=parent)
else:
new_page = super(AddPageForm, self).save(commit=False)
new_page.template = self.get_template()
new_page.set_tree_node(self._site, target=parent, position='last-child')
new_page.save()

translation = self.create_translation(new_page)
translation.rescan_placeholders()

if source:
self.update_translations_from_source(new_page)

extension_pool.copy_extensions(
source_page=source,
target_page=new_page,
Expand Down Expand Up @@ -350,7 +364,6 @@ def get_or_create_root(self):
if not root_page:
root_page = Page(
publisher_is_draft=True,
in_navigation=False,
is_page_type=True,
)
root_page.set_tree_node(self._site)
Expand All @@ -363,6 +376,7 @@ def get_or_create_root(self):
page=root_page,
slug=PAGE_TYPES_ID,
path=PAGE_TYPES_ID,
in_navigation=False,
)
return root_page.node

Expand All @@ -387,9 +401,15 @@ def from_source(self, source, parent=None):
permissions=False,
extensions=False,
)
new_page.update(is_page_type=True, in_navigation=False)
new_page.update(is_page_type=True)
return new_page

def update_translations_from_source(self, new_page):
new_page.update_translations(
self._language,
in_navigation=False,
)

def save(self, *args, **kwargs):
new_page = super(AddPageTypeForm, self).save(*args, **kwargs)

Expand All @@ -399,7 +419,6 @@ def save(self, *args, **kwargs):
new_page.update(
draft_only=True,
is_page_type=True,
in_navigation=False,
)
return new_page

Expand Down Expand Up @@ -494,6 +513,8 @@ def save(self, commit=True):
update_count = cms_page.update_translations(
self._language,
publisher_state=PUBLISHER_STATE_DIRTY,
changed_by=get_clean_username(self._request.user),
changed_date=timezone.now(),
**translation_data
)

Expand Down Expand Up @@ -538,12 +559,23 @@ class AdvancedSettingsForm(forms.ModelForm):
overwrite_url = forms.CharField(label=_('Overwrite URL'), max_length=255, required=False,
help_text=_('Keep this field empty if standard path should be used.'))

soft_root = forms.BooleanField(label=_('Soft root'),
required=False,
help_text=_("All ancestors will not be displayed in the navigation"))

xframe_options = forms.ChoiceField(
choices=Page._meta.get_field('xframe_options').choices,
choices=Title._meta.get_field('xframe_options').choices,
label=_('X Frame Options'),
help_text=_('Whether this page can be embedded in other pages or websites'),
initial=Page._meta.get_field('xframe_options').default,
required=False
initial=Title._meta.get_field('xframe_options').default,
required=False,
)
template = forms.ChoiceField(
choices=Title._meta.get_field('template').choices,
label=_('Template'),
help_text=_('The template used to render the content.'),
initial=Title._meta.get_field('template').default,
required=False,
)

redirect = PageSmartLinkField(label=_('Redirect'), required=False,
Expand All @@ -561,20 +593,21 @@ class AdvancedSettingsForm(forms.ModelForm):
)
fieldsets = (
(None, {
'fields': ('overwrite_url', 'redirect'),
'fields': ('overwrite_url', 'redirect', 'template',
'soft_root', 'xframe_options',),
}),
(_('Language independent options'), {
'fields': ('template', 'reverse_id', 'soft_root', 'navigation_extenders',
'application_urls', 'application_namespace', 'application_configs',
'xframe_options',)
'fields': ('reverse_id', 'navigation_extenders', 'application_urls',
'application_namespace', 'application_configs',)
})
)

class Meta:
model = Page
fields = [
'template', 'reverse_id', 'overwrite_url', 'redirect', 'soft_root', 'navigation_extenders',
'template', 'reverse_id', 'overwrite_url', 'redirect', 'navigation_extenders',
'application_urls', 'application_namespace', "xframe_options",
'soft_root',
]

def __init__(self, *args, **kwargs):
Expand Down Expand Up @@ -634,6 +667,15 @@ def __init__(self, *args, **kwargs):
if 'overwrite_url' in self.fields and self.title_obj.has_url_overwrite:
self.fields['overwrite_url'].initial = self.title_obj.path

if 'xframe_options' in self.fields:
self.fields['xframe_options'].initial = self.title_obj.xframe_options

if 'template' in self.fields:
self.fields['template'].initial = self.title_obj.template

if 'soft_root' in self.fields:
self.fields['soft_root'].initial = self.title_obj.soft_root

@cached_property
def _language(self):
return get_site_language_from_request(self._request, site_id=self._site.pk)
Expand Down Expand Up @@ -758,7 +800,7 @@ def clean_xframe_options(self):

xframe_options = self.cleaned_data['xframe_options']
if xframe_options == '':
return Page._meta.get_field('xframe_options').default
return Title._meta.get_field('xframe_options').default

return xframe_options

Expand Down Expand Up @@ -807,6 +849,11 @@ def save(self, *args, **kwargs):
redirect=(data.get('redirect') or None),
publisher_state=PUBLISHER_STATE_DIRTY,
has_url_overwrite=bool(data.get('overwrite_url')),
xframe_options=data.get('xframe_options'),
template=data.get('template'),
soft_root=data.get('soft_root'),
changed_by=get_clean_username(self._request.user),
changed_date=timezone.now(),
)
is_draft_and_has_public = page.publisher_is_draft and page.publisher_public_id

Expand All @@ -817,12 +864,44 @@ def save(self, *args, **kwargs):


class PagePermissionForm(forms.ModelForm):
_site = None
_request = None

limit_visibility_in_menu = forms.TypedChoiceField(
choices=Title._meta.get_field('limit_visibility_in_menu').choices,
label=_("menu visibility"),
help_text=_("limit when this page is visible in the menu"),
initial=Title._meta.get_field('limit_visibility_in_menu').default,
required=False,
coerce=int,
empty_value=None,
)

def __init__(self, *args, **kwargs):
super(PagePermissionForm, self).__init__(*args, **kwargs)
self.title_obj = self.instance.get_title_obj(
language=self._language,
fallback=False,
force_reload=True,
)
self.fields['limit_visibility_in_menu'].initial = self.title_obj.limit_visibility_in_menu

@cached_property
def _language(self):
return get_site_language_from_request(self._request, site_id=self._site.pk)

class Meta:
model = Page
fields = ['login_required', 'limit_visibility_in_menu']

def save(self, *args, **kwargs):
if "limit_visibility_in_menu" in self.changed_data:
self.instance.update_translations(
self._language,
limit_visibility_in_menu=self.cleaned_data["limit_visibility_in_menu"],
changed_by=get_clean_username(self._request.user),
changed_date=timezone.now(),
)
page = super(PagePermissionForm, self).save(*args, **kwargs)
page.clear_cache(menu=True)
clear_permission_cache()
Expand Down
4 changes: 2 additions & 2 deletions cms/admin/pageadmin.py
Expand Up @@ -833,8 +833,8 @@ def change_template(self, request, object_id):
if to_template not in dict(get_cms_setting('TEMPLATES')):
return HttpResponseBadRequest(force_text(_("Template not valid")))

page.template = to_template
page.save()
# TODO: Make this language-aware
page.update_translations(template=to_template)
return HttpResponse(force_text(_("The template was successfully changed")))

@require_POST
Expand Down
42 changes: 33 additions & 9 deletions cms/api.py
Expand Up @@ -34,7 +34,7 @@
from cms.utils import get_current_site
from cms.utils.conf import get_cms_setting
from cms.utils.i18n import get_language_list
from cms.utils.page import get_available_slug
from cms.utils.page import get_available_slug, get_clean_username
from cms.utils.permissions import _thread_locals, current_user
from cms.utils.plugins import copy_plugins_to_placeholder
from menus.menu_pool import menu_pool
Expand Down Expand Up @@ -108,7 +108,7 @@ def create_page(title, template, language, menu_title=None, slug=None,
navigation_extenders=None, published=False, site=None,
login_required=False, limit_visibility_in_menu=constants.VISIBILITY_ALL,
position="last-child", overwrite_url=None,
xframe_options=Page.X_FRAME_OPTIONS_INHERIT):
xframe_options=constants.X_FRAME_OPTIONS_INHERIT):
"""
Create a CMS Page and it's title for the given language
Expand Down Expand Up @@ -163,7 +163,7 @@ def create_page(title, template, language, menu_title=None, slug=None,
# ugly permissions hack
if created_by and isinstance(created_by, get_user_model()):
_thread_locals.user = created_by
created_by = getattr(created_by, get_user_model().USERNAME_FIELD)
created_by = get_clean_username(created_by)
else:
_thread_locals.user = None

Expand All @@ -176,16 +176,11 @@ def create_page(title, template, language, menu_title=None, slug=None,
changed_by=created_by,
publication_date=publication_date,
publication_end_date=publication_end_date,
in_navigation=in_navigation,
soft_root=soft_root,
reverse_id=reverse_id,
navigation_extenders=navigation_extenders,
template=template,
application_urls=application_urls,
application_namespace=apphook_namespace,
login_required=login_required,
limit_visibility_in_menu=limit_visibility_in_menu,
xframe_options=xframe_options,
)
page.set_tree_node(site=site, target=target_node, position=position)
page.save()
Expand All @@ -195,10 +190,16 @@ def create_page(title, template, language, menu_title=None, slug=None,
title=title,
menu_title=menu_title,
slug=slug,
created_by=created_by,
redirect=redirect,
meta_description=meta_description,
page=page,
overwrite_url=overwrite_url,
soft_root=soft_root,
in_navigation=in_navigation,
template=template,
limit_visibility_in_menu=limit_visibility_in_menu,
xframe_options=xframe_options,
)

if published:
Expand All @@ -214,20 +215,33 @@ def create_page(title, template, language, menu_title=None, slug=None,
@transaction.atomic
def create_title(language, title, page, menu_title=None, slug=None,
redirect=None, meta_description=None, parent=None,
overwrite_url=None, page_title=None, path=None):
overwrite_url=None, page_title=None, path=None,
created_by='python-api', soft_root=False, in_navigation=False,
template=TEMPLATE_INHERITANCE_MAGIC,
limit_visibility_in_menu=constants.VISIBILITY_ALL,
xframe_options=constants.X_FRAME_OPTIONS_INHERIT):
"""
Create a title.
Parent is only used if slug=None.
See docs/extending_cms/api_reference.rst for more info
"""
# validate template
if not template == TEMPLATE_INHERITANCE_MAGIC:
assert template in [tpl[0] for tpl in get_cms_setting('TEMPLATES')]
get_template(template)

# validate page
assert isinstance(page, Page)

# validate language:
assert language in get_language_list(page.node.site_id)

# validate menu visibility
accepted_limitations = (constants.VISIBILITY_ALL, constants.VISIBILITY_USERS, constants.VISIBILITY_ANONYMOUS)
assert limit_visibility_in_menu in accepted_limitations

# set default slug:
if not slug:
base = page.get_path_for_slug(slugify(title), language)
Expand All @@ -238,6 +252,9 @@ def create_title(language, title, page, menu_title=None, slug=None,
elif path is None:
path = page.get_path_for_slug(slug, language)

if created_by and isinstance(created_by, get_user_model()):
created_by = get_clean_username(created_by)

title = Title.objects.create(
language=language,
title=title,
Expand All @@ -249,6 +266,13 @@ def create_title(language, title, page, menu_title=None, slug=None,
meta_description=meta_description,
page=page,
has_url_overwrite=bool(overwrite_url),
created_by=created_by,
changed_by=created_by,
soft_root=soft_root,
in_navigation=in_navigation,
template=template,
limit_visibility_in_menu=limit_visibility_in_menu,
xframe_options=xframe_options,
)
title.rescan_placeholders()

Expand Down

0 comments on commit d7e2d26

Please sign in to comment.