Skip to content

Commit

Permalink
WCM-617: use better widget for list and filename to keep the cli inte…
Browse files Browse the repository at this point in the history
…rface as before
  • Loading branch information
stollero committed Jan 17, 2025
1 parent 3754401 commit a7a0c6b
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 64 deletions.
26 changes: 14 additions & 12 deletions core/src/zeit/cms/workflow/browser/form.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import gocept.form.grouped
from zope.formlib.sequencewidget import ListSequenceWidget
from zope.formlib.widget import CustomWidgetFactory
from zope.formlib.widgets import FileWidget
import zope.formlib.form
import zope.location.interfaces
import zope.site.folder
Expand All @@ -9,24 +11,24 @@
import zeit.cms.browser.menu
import zeit.cms.interfaces
import zeit.cms.workflow.interfaces
import zeit.cms.workflow.options


class ManualMultiPublishForm(zeit.cms.browser.form.FormBase, gocept.form.grouped.DisplayForm):
class ManualMultiPublishForm(zeit.cms.browser.form.AddForm):
factory = zeit.cms.workflow.options.PublicationOptions
form_fields = zope.formlib.form.FormFields(
IManualPublicationOptions,
)
form_fields['ignore_services'].custom_widget = CustomWidgetFactory(ListSequenceWidget)
form_fields['filename'].custom_widget = CustomWidgetFactory(FileWidget)

def _get_widgets(self, form_fields, ignore_request):
return zope.formlib.form.setUpInputWidgets(
form_fields, self.prefix, self.context, self.request, ignore_request=ignore_request
)
def create(self, data):
return self.factory(**data)

@zope.formlib.form.action(_('Apply'), condition=zope.formlib.form.haveInputWidgets)
def handle_edit_action(self, _, data):
to_publish = data['unique_ids']
if not to_publish:
return
zeit.cms.workflow.cli.publish_content.delay(data)
@zope.formlib.form.action(_('Add'), condition=zope.formlib.form.haveInputWidgets)
def handle_add(self, _, data):
options = self.create(data)
zeit.cms.workflow.cli.publish_content(options)


class MenuItem(zeit.cms.browser.menu.GlobalMenuItem):
Expand Down
33 changes: 11 additions & 22 deletions core/src/zeit/cms/workflow/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import zope.component

from zeit.cms.workflow.interfaces import IManualPublicationOptions, IPublish, IPublishInfo
from zeit.cms.workflow.options import PublicationOptions
import zeit.cms.celery
import zeit.cms.cli

Expand All @@ -15,12 +16,11 @@


@zeit.cms.celery.task(queue='manual')
def publish_content(options: dict):
def publish_content(options: IManualPublicationOptions):
"""This publish task is running inside a celery worker because
we do not want to unregister and reregister the hooks for the application if this
function is called inside the regular vivi process
"""
options = IManualPublicationOptions(options)
registry = zope.component.getGlobalSiteManager()

# No option, since there is no usecase for re-activating old breaking news
Expand Down Expand Up @@ -57,11 +57,10 @@ def publish_content(options: dict):
).start()

log.info('Ignoring services %s', options.ignore_services)
zeit.workflow.publish_3rdparty.PublisherData.ignore = (
options.ignore_services.split(',') + IGNORE_SERVICES
)
zeit.workflow.publish_3rdparty.PublisherData.ignore = options.ignore_services + IGNORE_SERVICES

for uid in options.unique_ids:
for line in options.filename.decode('utf-8').splitlines():
uid = line.strip()
content = zeit.cms.interfaces.ICMSContent(uid, None)
if content is None:
log.warn('Skipping %s, not found', uid)
Expand Down Expand Up @@ -91,30 +90,20 @@ def publish_content(options: dict):
@zeit.cms.cli.runner(principal=zeit.cms.cli.principal_from_args)
def publish():
parser = argparse.ArgumentParser(description='Publish content')
parser.add_argument('--filename', '-f', help='filename with uniqueId per line')

for name, field in zope.schema.getFields(IManualPublicationOptions).items():
arg_name = f'--{name.replace("_", "-")}'
if isinstance(field, zope.schema.Bool):
parser.add_argument(arg_name, action='store_true', help=field.title)
elif isinstance(field, zope.schema.TextLine):
parser.add_argument(arg_name, help=field.title, default=field.default)
elif isinstance(field, zope.schema.Text):
parser.add_argument(arg_name, help=field.title, default=field.default)
elif isinstance(field, zope.schema.List):
parser.add_argument(arg_name, help=field.title, default=field.default, nargs='+')
elif isinstance(field, zope.schema.Bytes):
parser.add_argument(arg_name, type=lambda x: open(x, 'rb').read(), help=field.title)
else:
parser.add_argument(arg_name, help=field.title)
parser.add_argument(arg_name, help=field.title, default=field.default)

options = parser.parse_args()
if not options.filename:
parser.print_help()
raise SystemExit(1)

to_publish = open(options.filename).read()

options = options.__dict__
options['unique_ids'] = to_publish

publish_content.delay(options)
publish_content(PublicationOptions(**options.__dict__))


@zeit.cms.cli.runner(principal=zeit.cms.cli.principal_from_args)
Expand Down
12 changes: 7 additions & 5 deletions core/src/zeit/cms/workflow/interfaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,10 +262,10 @@ class IManualPublicationOptions(zope.interface.Interface):
use_publish_hooks = zope.schema.Bool(
title=_('Notify webhooks after publish, like contenthub'), default=False
)
ignore_services = zope.schema.TextLine(
ignore_services = zope.schema.List(
title=_('Ignore 3rd party services'),
default='airship,speechbert,summy',
missing_value='airship,speechbert,summy',
default=['airship', 'speechbert', 'summy'],
value_type=zope.schema.TextLine(__name__='Service'),
)
wait_tms = zope.schema.Bool(
title=_('Have publisher wait for TMS before fastly purge'), default=False
Expand All @@ -274,5 +274,7 @@ class IManualPublicationOptions(zope.interface.Interface):
title=_('Skip TMS enrich, e.g. checkin already happened'), default=False
)
dlps = zope.schema.Bool(title=_('Update date_last_published_semantic timestamp'), default=False)
# newline separated list of unique ids
unique_ids = zope.schema.Text(title=_('Unique IDs'))
filename = zope.schema.Bytes(
title=_('Upload file with Unique IDs'),
required=False,
)
28 changes: 3 additions & 25 deletions core/src/zeit/cms/workflow/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,6 @@

@grok.implementer(IManualPublicationOptions)
class PublicationOptions:
_unique_ids = ''

@property
def unique_ids(self):
return self._unique_ids.split('\n')

@unique_ids.setter
def unique_ids(self, value):
self._unique_ids = value

@classmethod
def from_dict(cls, options):
obj = cls()
for key, value in options.items():
if key == 'unique_ids':
obj.unique_ids = value
else:
setattr(obj, key, value)
return obj


@grok.adapter(dict)
@grok.implementer(IManualPublicationOptions)
def options_factory(data):
return PublicationOptions.from_dict(data)
def __init__(self, **kwargs):
for name, value in kwargs.items():
setattr(self, name, value)

0 comments on commit a7a0c6b

Please sign in to comment.