Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Feature django-cms 4.0.x compatibility #672

Open
wants to merge 6 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions djangocms_blog/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from aldryn_apphooks_config.admin import BaseAppHookConfig, ModelAppHookConfig
from cms.admin.placeholderadmin import FrontendEditableAdminMixin, PlaceholderAdminMixin
from cms.models import CMSPlugin, ValidationError
from cms.toolbar.utils import get_object_preview_url
from django.apps import apps
from django.conf import settings
from django.contrib import admin, messages
Expand Down Expand Up @@ -449,6 +450,9 @@ def save_related(self, request, form, formsets, change):
form.instance.sites.add(*self.get_restricted_sites(request).all().values_list("pk", flat=True))
super().save_related(request, form, formsets, change)

def view_on_site(self, obj):
return get_object_preview_url(obj, obj.language_code)

class Media:
css = {"all": ("{}djangocms_blog/css/{}".format(settings.STATIC_URL, "djangocms_blog_admin.css"),)}

Expand Down
9 changes: 9 additions & 0 deletions djangocms_blog/cms_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from cms.app_base import CMSAppConfig

from .models import Post
from .rendering import render_post_content


class BlogCMSConfig(CMSAppConfig):
cms_enabled = True
cms_toolbar_enabled_models = [(Post, render_post_content)]
12 changes: 6 additions & 6 deletions djangocms_blog/cms_menus.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,12 @@ def get_nodes(self, request):
logger.exception(e)
return []
config = self._config[self.instance.application_namespace]
if not getattr(request, "toolbar", False) or not request.toolbar.edit_mode_active:
if self.instance == self.instance.get_draft_object():
return []
else:
if self.instance == self.instance.get_public_object():
return []
# if not getattr(request, "toolbar", False) or not request.toolbar.edit_mode_active:
# if self.instance == self.instance.get_draft_object():
# return []
# else:
# if self.instance == self.instance.get_public_object():
# return []
if config and config.menu_structure in (MENU_TYPE_COMPLETE, MENU_TYPE_CATEGORIES):
categories_menu = True
if config and config.menu_structure in (MENU_TYPE_COMPLETE, MENU_TYPE_POSTS):
Expand Down
49 changes: 27 additions & 22 deletions djangocms_blog/cms_wizards.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,25 +97,30 @@ class PostWizard(Wizard):
pass


for config in BlogConfig.objects.all().order_by("namespace"):
seed = slugify("{}.{}".format(config.app_title, config.namespace))
new_wizard = type(str(seed), (PostWizard,), {})
new_form = type("{}Form".format(seed), (PostWizardForm,), {"default_appconfig": config.pk})
post_wizard = new_wizard(
title=_("New {0}").format(config.object_name),
weight=200,
form=new_form,
model=Post,
description=_("Create a new {0} in {1}").format(config.object_name, config.app_title),
)
try:
wizard_pool.register(post_wizard)
except AlreadyRegisteredException: # pragma: no cover
if settings.DEBUG:
raise
else:
warnings.warn(
"Wizard {} cannot be registered. Please make sure that "
"BlogConfig.namespace {} and BlogConfig.app_title {} are"
"unique together".format(seed, config.namespace, config.app_title)
)
try:
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs moving to the cms_config to work using the new app registration system which replaces pools: https://github.com/django-cms/django-cms/blob/28d3fa54cb8795fc2276ada94e07f45fa2fe1def/cms/cms_config.py#L18

for config in BlogConfig.objects.all().order_by("namespace"):
seed = slugify("{}.{}".format(config.app_title, config.namespace))
new_wizard = type(str(seed), (PostWizard,), {})
new_form = type("{}Form".format(seed), (PostWizardForm,), {"default_appconfig": config.pk})
post_wizard = new_wizard(
title=_("New {0}").format(config.object_name),
weight=200,
form=new_form,
model=Post,
description=_("Create a new {0} in {1}").format(config.object_name, config.app_title),
)
try:
wizard_pool.register(post_wizard)
except AlreadyRegisteredException: # pragma: no cover
if settings.DEBUG:
raise
else:
warnings.warn(
"Wizard {} cannot be registered. Please make sure that "
"BlogConfig.namespace {} and BlogConfig.app_title {} are"
"unique together".format(seed, config.namespace, config.app_title)
)
except: # pragma: no cover
warnings.warn(
"Wizards not yet registered"
)
2 changes: 1 addition & 1 deletion djangocms_blog/migrations/0014_auto_20160215_1331.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def forwards(apps, schema_editor):
LatestPostsPlugin = apps.get_model("djangocms_blog", "LatestPostsPlugin")
AuthorEntriesPlugin = apps.get_model("djangocms_blog", "AuthorEntriesPlugin")
config = None
for page in Page.objects.drafts().filter(application_urls="BlogApp"):
for page in Page.objects.filter(application_urls="BlogApp"):
config, created = BlogConfig.objects.get_or_create(namespace=page.application_namespace)
if not BlogConfigTranslation.objects.exists():
for lang in get_language_list():
Expand Down
30 changes: 26 additions & 4 deletions djangocms_blog/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from aldryn_apphooks_config.fields import AppHookConfigField
from aldryn_apphooks_config.managers.parler import AppHookConfigTranslatableManager
from cms.models import CMSPlugin, PlaceholderField
from cms.models import CMSPlugin, Placeholder, PlaceholderRelationField
from django.conf import settings as dj_settings
from django.contrib.auth import get_user_model
from django.contrib.sites.shortcuts import get_current_site
Expand Down Expand Up @@ -263,9 +263,7 @@ class Post(KnockerModel, BlogMetaMixin, TranslatableModel):
post_text=HTMLField(_("text"), default="", blank=True, configuration="BLOG_POST_TEXT_CKEDITOR"),
meta={"unique_together": (("language_code", "slug"),)},
)
media = PlaceholderField("media", related_name="media")
content = PlaceholderField("post_content", related_name="post_content")
liveblog = PlaceholderField("live_blog", related_name="live_blog")
placeholders = PlaceholderRelationField()
enable_liveblog = models.BooleanField(verbose_name=_("enable liveblog on post"), default=False)

objects = GenericDateTaggedManager()
Expand Down Expand Up @@ -316,6 +314,26 @@ def __str__(self):
default = gettext("Post (no translation)")
return self.safe_translation_getter("title", any_language=True, default=default)

def _get_placeholder_from_slotname(self, slotname):
try:
return self.placeholders.get(slot=slotname)
except Placeholder.DoesNotExist:
from cms.utils.placeholder import rescan_placeholders_for_obj
rescan_placeholders_for_obj(self)
return self.placeholders.get(slot=slotname)

@cached_property
def media(self):
return self._get_placeholder_from_slotname("media")

@cached_property
def content(self):
return self._get_placeholder_from_slotname("content")

@cached_property
def liveblog(self):
return self._get_placeholder_from_slotname("liveblog")

@property
def guid(self, language=None):
if not language:
Expand Down Expand Up @@ -442,6 +460,10 @@ def full_image_options(self):
else:
return get_setting("IMAGE_FULL_SIZE")

def get_template(self):
# Used for the cms structure endpoint
return 'djangocms_blog/post_structure.html'

@property
def is_published(self):
"""
Expand Down
7 changes: 7 additions & 0 deletions djangocms_blog/rendering.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from django.template.response import TemplateResponse


def render_post_content(request, content_object):
template = 'djangocms_blog/post_detail.html'
context = {'post': content_object}
return TemplateResponse(request, template, context)
4 changes: 4 additions & 0 deletions djangocms_blog/templates/djangocms_blog/post_structure.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{% load cms_tags %}
{% placeholder 'media' %}
{% placeholder 'content' %}
{% placeholder 'liveblog' %}