diff --git a/djangocms_blog/admin.py b/djangocms_blog/admin.py index 5a4d7ea9..785d4953 100644 --- a/djangocms_blog/admin.py +++ b/djangocms_blog/admin.py @@ -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 @@ -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"),)} diff --git a/djangocms_blog/cms_config.py b/djangocms_blog/cms_config.py new file mode 100644 index 00000000..e8d10d11 --- /dev/null +++ b/djangocms_blog/cms_config.py @@ -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)] diff --git a/djangocms_blog/cms_menus.py b/djangocms_blog/cms_menus.py index 740c9128..3176b6ae 100644 --- a/djangocms_blog/cms_menus.py +++ b/djangocms_blog/cms_menus.py @@ -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): diff --git a/djangocms_blog/cms_wizards.py b/djangocms_blog/cms_wizards.py index 3e4ba5d2..1dd56220 100644 --- a/djangocms_blog/cms_wizards.py +++ b/djangocms_blog/cms_wizards.py @@ -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: + 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" + ) \ No newline at end of file diff --git a/djangocms_blog/migrations/0014_auto_20160215_1331.py b/djangocms_blog/migrations/0014_auto_20160215_1331.py index 1e4f560c..ac3aa63e 100644 --- a/djangocms_blog/migrations/0014_auto_20160215_1331.py +++ b/djangocms_blog/migrations/0014_auto_20160215_1331.py @@ -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(): diff --git a/djangocms_blog/models.py b/djangocms_blog/models.py index 150a4aa6..6810a811 100644 --- a/djangocms_blog/models.py +++ b/djangocms_blog/models.py @@ -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 @@ -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() @@ -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: @@ -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): """ diff --git a/djangocms_blog/rendering.py b/djangocms_blog/rendering.py new file mode 100644 index 00000000..a2c14aff --- /dev/null +++ b/djangocms_blog/rendering.py @@ -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) diff --git a/djangocms_blog/templates/djangocms_blog/post_structure.html b/djangocms_blog/templates/djangocms_blog/post_structure.html new file mode 100644 index 00000000..4482b950 --- /dev/null +++ b/djangocms_blog/templates/djangocms_blog/post_structure.html @@ -0,0 +1,4 @@ +{% load cms_tags %} +{% placeholder 'media' %} +{% placeholder 'content' %} +{% placeholder 'liveblog' %}