Skip to content

Commit

Permalink
Merge pull request #3855 from donce/publisher_publish
Browse files Browse the repository at this point in the history
Logic of publisher_publish command moved to cms.api
  • Loading branch information
yakky committed Feb 22, 2015
2 parents 5f24b02 + 714436f commit 59e5356
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 63 deletions.
28 changes: 28 additions & 0 deletions cms/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from django.template.defaultfilters import slugify
from django.template.loader import get_template
from django.utils import six
from django.utils.translation import activate

from cms.admin.forms import save_permissions
from cms.app_base import CMSApp
Expand Down Expand Up @@ -456,6 +457,33 @@ def __init__(self, user):
return page.reload()


def publish_pages(include_unpublished=False, language=None, site=None):
"""
Create published public version of selected drafts.
"""
qs = Page.objects.drafts()
if not include_unpublished:
qs = qs.filter(title_set__published=True).distinct()
if site:
qs = qs.filter(site=site)

output_language = None
for i, page in enumerate(qs):
add = True
titles = page.title_set
if not include_unpublished:
titles = titles.filter(published=True)
for lang in titles.values_list("language", flat=True):
if language is None or lang == language:
if not output_language:
output_language = lang
if not page.publish(lang):
add = False
# we may need to activate the first (main) language for proper page title rendering
activate(output_language)
yield (page, add)


def get_page_draft(page):
"""
Returns the draft version of a page, regardless if the passed in
Expand Down
2 changes: 2 additions & 0 deletions cms/management/commands/cms.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from cms.management.commands.subcommands.check import CheckInstallation
from cms.management.commands.subcommands.list import ListCommand
from cms.management.commands.subcommands.moderator import ModeratorCommand
from cms.management.commands.subcommands.publisher_publish import PublishCommand
from cms.management.commands.subcommands.tree import FixTreeCommand
from cms.management.commands.subcommands.uninstall import UninstallCommand
from cms.management.commands.subcommands.copy_lang import CopyLangCommand
Expand Down Expand Up @@ -31,6 +32,7 @@ class Command(SubcommandsCommand):
'copy-lang': CopyLangCommand,
'delete_orphaned_plugins': DeleteOrphanedPluginsCommand,
'check': CheckInstallation,
'publisher_publish': PublishCommand,
}

@property
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@
from optparse import make_option

from django.contrib.auth import get_user_model
from django.contrib.sites.models import Site
from django.core.management.base import NoArgsCommand, CommandError
from django.utils.encoding import force_text
from django.utils.translation import activate

from cms.api import publish_pages
from cms.utils.permissions import set_current_user

class Command(NoArgsCommand):

class PublishCommand(NoArgsCommand):
option_list = NoArgsCommand.option_list + (
make_option(
'--unpublished',
Expand Down Expand Up @@ -38,55 +41,30 @@ def handle_noargs(self, **options):
if site:
try:
site = int(site)
except ValueError:
site = None
site = Site.objects.get(pk=site)
except (ValueError, Site.DoesNotExist):
raise CommandError("There is no site with given site id.")
else:
site = None

self.publish_pages(include_unpublished, language, site)

def publish_pages(self, include_unpublished, language, site):
from cms.models import Page
from cms.utils.permissions import set_current_user

# thread locals middleware needs to know, who are we - login as a first
# super user

try:
user = get_user_model().objects.filter(is_active=True, is_staff=True, is_superuser=True)[0]
except IndexError:
raise CommandError("No super user found, create one using `manage.py createsuperuser`.")

set_current_user(user) # set him as current user

qs = Page.objects.drafts()
if not include_unpublished:
qs = qs.filter(title_set__published=True).distinct()
if site:
qs = qs.filter(site_id=site)

pages_total, pages_published = qs.count(), 0

pages_published = 0
pages_total = 0
self.stdout.write(u"\nPublishing public drafts....\n")
output_language = None
for i, page in enumerate(qs):
m = " "
add = True
titles = page.title_set
if not include_unpublished:
titles = titles.filter(published=True)
for lang in titles.values_list("language", flat=True):
if language is None or lang == language:
if not output_language:
output_language = lang
if not page.publish(lang):
add = False
# we may need to activate the first (main) language for proper page title rendering
activate(output_language)
index = 0
for page, add in publish_pages(include_unpublished, language, site):
m = '*' if add else ' '
self.stdout.write(u"%d.\t%s %s [%d]\n" % (index + 1, m, force_text(page), page.id))
pages_total += 1
if add:
pages_published += 1
m = "*"
self.stdout.write(u"%d.\t%s %s [%d]\n" % (i + 1, m, force_text(page), page.id))
index += 1

self.stdout.write(u"\n")
self.stdout.write(u"=" * 40)
Expand Down
28 changes: 14 additions & 14 deletions cms/tests/publisher.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
# -*- coding: utf-8 -*-
from __future__ import with_statement
from django.contrib.sites.models import Site
from cms.utils.urlutils import admin_reverse

from djangocms_text_ckeditor.models import Text
from django.contrib.auth import get_user_model
from django.contrib.sites.models import Site
from django.core.cache import cache
from django.core.management.base import CommandError
from django.core.management import call_command
from django.core.urlresolvers import reverse

from cms.api import create_page, add_plugin, create_title
from cms.constants import PUBLISHER_STATE_PENDING, PUBLISHER_STATE_DEFAULT, PUBLISHER_STATE_DIRTY
from cms.management.commands import publisher_publish
from cms.management.commands.subcommands.publisher_publish import PublishCommand
from cms.models import CMSPlugin, Title
from cms.models.pagemodel import Page
from cms.plugin_pool import plugin_pool
Expand All @@ -21,6 +20,7 @@
from cms.test_utils.util.fuzzy_int import FuzzyInt
from cms.utils.conf import get_cms_setting
from cms.utils.i18n import force_language
from cms.utils.urlutils import admin_reverse


class PublisherCommandTests(TestCase):
Expand All @@ -30,7 +30,7 @@ class PublisherCommandTests(TestCase):

def test_command_line_should_raise_without_superuser(self):
with self.assertRaises(CommandError):
com = publisher_publish.Command()
com = PublishCommand()
com.handle_noargs()

def test_command_line_publishes_zero_pages_on_empty_db(self):
Expand All @@ -42,7 +42,7 @@ def test_command_line_publishes_zero_pages_on_empty_db(self):

with StdoutOverride() as buffer:
# Now we don't expect it to raise, but we need to redirect IO
call_command('publisher_publish')
call_command('cms', 'publisher_publish')
lines = buffer.getvalue().split('\n') #NB: readlines() doesn't work

for line in lines:
Expand All @@ -65,7 +65,7 @@ def test_command_line_ignores_draft_page(self):

with StdoutOverride() as buffer:
# Now we don't expect it to raise, but we need to redirect IO
call_command('publisher_publish')
call_command('cms', 'publisher_publish')
lines = buffer.getvalue().split('\n') #NB: readlines() doesn't work

for line in lines:
Expand All @@ -90,7 +90,7 @@ def test_command_line_publishes_draft_page(self):

with StdoutOverride() as buffer:
# Now we don't expect it to raise, but we need to redirect IO
call_command('publisher_publish', include_unpublished=True)
call_command('cms', 'publisher_publish', include_unpublished=True)
lines = buffer.getvalue().split('\n') #NB: readlines() doesn't work

for line in lines:
Expand Down Expand Up @@ -121,7 +121,7 @@ def test_command_line_publishes_selected_language(self):

with StdoutOverride() as buffer:
# Now we don't expect it to raise, but we need to redirect IO
call_command('publisher_publish', language='de')
call_command('cms', 'publisher_publish', language='de')
lines = buffer.getvalue().split('\n') #NB: readlines() doesn't work

for line in lines:
Expand Down Expand Up @@ -155,7 +155,7 @@ def test_command_line_publishes_selected_language_drafts(self):

with StdoutOverride() as buffer:
# Now we don't expect it to raise, but we need to redirect IO
call_command('publisher_publish', language='de', include_unpublished=True)
call_command('cms', 'publisher_publish', language='de', include_unpublished=True)
lines = buffer.getvalue().split('\n') #NB: readlines() doesn't work

for line in lines:
Expand Down Expand Up @@ -194,7 +194,7 @@ def test_table_name_patching(self):

with StdoutOverride():
# Now we don't expect it to raise, but we need to redirect IO
call_command('publisher_publish')
call_command('cms', 'publisher_publish')
not_drafts = len(Page.objects.filter(publisher_is_draft=False))
drafts = len(Page.objects.filter(publisher_is_draft=True))
self.assertEqual(not_drafts, 1)
Expand Down Expand Up @@ -224,7 +224,7 @@ def test_command_line_publishes_one_page(self):

with StdoutOverride() as buffer:
# Now we don't expect it to raise, but we need to redirect IO
call_command('publisher_publish')
call_command('cms', 'publisher_publish')
lines = buffer.getvalue().split('\n') #NB: readlines() doesn't work

for line in lines:
Expand Down Expand Up @@ -260,7 +260,7 @@ def test_command_line_publish_multiple_languages(self):

with StdoutOverride():
# Now we don't expect it to raise, but we need to redirect IO
call_command('publisher_publish')
call_command('cms', 'publisher_publish')

public = Page.objects.public()[0]
languages = sorted(public.title_set.values_list('language', flat=True))
Expand All @@ -282,7 +282,7 @@ def test_command_line_publish_one_site(self):

with StdoutOverride() as buffer:
# Now we don't expect it to raise, but we need to redirect IO
call_command('publisher_publish', site=siteB.id)
call_command('cms', 'publisher_publish', site=siteB.id)
lines = buffer.getvalue().split('\n') #NB: readlines() doesn't work

for line in lines:
Expand Down Expand Up @@ -313,7 +313,7 @@ def test_command_line_publish_multiple_languages_check_count(self):

with StdoutOverride() as buffer:
# Now we don't expect it to raise, but we need to redirect IO
call_command('publisher_publish')
call_command('cms', 'publisher_publish')
lines = buffer.getvalue().split('\n') #NB: readlines() doesn't work

for line in lines:
Expand Down
9 changes: 9 additions & 0 deletions docs/reference/api_references.rst
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,15 @@ Functions and constants
:type user: :class:`django.contrib.auth.models.User` instance
:param string language: The target language to publish to

.. function:: publish_pages(include_unpublished=False, language=None, site=None)

Publishes multiple pages defined by parameters.

:param bool include_unpublished: Set to ``True`` to publish all drafts, including unpublished ones; otherwise, only already published pages will be republished
:param string language: If given, only pages in this language will be published; otherwise, all languages will be published
:param site: Specify a site to publish pages for specified site only; if not specified pages from all sites are published
:type site: :class:`django.contrib.sites.models.Site` instance

.. function:: get_page_draft(page):

Returns the draft version of a page, regardless if the passed in
Expand Down
17 changes: 6 additions & 11 deletions docs/reference/cli.rst
Original file line number Diff line number Diff line change
Expand Up @@ -136,13 +136,8 @@ used moderation in the past.
if necessary ``delete_orphaned_plugins``. Running ``cms moderator`` with
orphaned plugins will fail and leave bad data in your database.


**************************************
Additional commands
**************************************

``publisher_publish``
======================
``cms publisher_publish``
=========================

If you want to publish many pages at once, this command can help you. By default,
this command publishes drafts for all public pages.
Expand All @@ -163,16 +158,16 @@ Example::
publisher_publish

#publish all drafts in all pages
publisher_publish --unpublished
cms publisher_publish --unpublished

#publish drafts for public pages in deutsch
publisher_publish --language=de
cms publisher_publish --language=de

#publish all drafts in deutsch
publisher_publish --unpublished --language=de
cms publisher_publish --unpublished --language=de

#publish all drafts in deutsch, but only for site with id=2
publisher_publish --unpublished --language=de --site=2
cms publisher_publish --unpublished --language=de --site=2

.. warning::

Expand Down

0 comments on commit 59e5356

Please sign in to comment.