def index(request): form = SearchForm(placeholder=_("Search something")) example_form = ExampleForm() messages.success(request, _("Success message"), buttons=[ messages.button('', _('View live')), messages.button('', _('Edit')) ]) messages.warning(request, _("Warning message"), buttons=[ messages.button('', _('View live')), messages.button('', _('Edit')) ]) messages.error(request, _("Error message"), buttons=[ messages.button('', _('View live')), messages.button('', _('Edit')) ]) paginator = Paginator(list(range(100)), 10) page = paginator.page(2) return render(request, 'wagtailstyleguide/base.html', { 'search_form': form, 'example_form': example_form, 'example_page': page, })
def revisions_revert(request, page_id, revision_id): page = get_object_or_404(Page, id=page_id).specific page_perms = page.permissions_for_user(request.user) if not page_perms.can_edit(): raise PermissionDenied revision = get_object_or_404(page.revisions, id=revision_id) revision_page = revision.as_page_object() content_type = ContentType.objects.get_for_model(page) page_class = content_type.model_class() edit_handler_class = page_class.get_edit_handler() form_class = edit_handler_class.get_form_class(page_class) form = form_class(instance=revision_page) edit_handler = edit_handler_class(instance=revision_page, form=form) user_avatar = render_to_string('wagtailadmin/shared/user_avatar.html', {'user': revision.user}) messages.warning(request, mark_safe(_("You are viewing a previous revision of this page from <b>%s</b> by %s") % (revision.created_at.strftime("%d %b %Y %H:%M"), user_avatar))) return render(request, 'wagtailadmin/pages/edit.html', { 'page': page, 'revision': revision, 'is_revision': True, 'content_type': content_type, 'edit_handler': edit_handler, 'errors_debug': None, 'preview_modes': page.preview_modes, 'form': form, # Used in unit tests })
def index(request): form = SearchForm(placeholder=_("Search something")) example_form = ExampleForm() messages.success(request, _("Success message"), buttons=[ messages.button('', _('View live')), messages.button('', _('Edit')) ]) messages.warning(request, _("Warning message"), buttons=[ messages.button('', _('View live')), messages.button('', _('Edit')) ]) messages.error(request, _("Error message"), buttons=[ messages.button('', _('View live')), messages.button('', _('Edit')) ]) fake_pagination = { 'number': 1, 'previous_page_number': 1, 'next_page_number': 2, 'has_previous': True, 'has_next': True, 'paginator': { 'num_pages': 10, }, } return render(request, 'wagtailstyleguide/base.html', { 'search_form': form, 'example_form': example_form, 'fake_pagination': fake_pagination, })
def index(request): form = SearchForm(placeholder=_("Search something")) example_form = ExampleForm() messages.success( request, _("Success message"), buttons=[messages.button("", _("View live")), messages.button("", _("Edit"))] ) messages.warning( request, _("Warning message"), buttons=[messages.button("", _("View live")), messages.button("", _("Edit"))] ) messages.error( request, _("Error message"), buttons=[messages.button("", _("View live")), messages.button("", _("Edit"))] ) fake_pagination = { "number": 1, "previous_page_number": 1, "next_page_number": 2, "has_previous": True, "has_next": True, "paginator": {"num_pages": 10}, } return render( request, "wagtailstyleguide/base.html", {"search_form": form, "example_form": example_form, "fake_pagination": fake_pagination}, )
def run_scan(request): site = Site.find_for_request(request) celery_status = get_celery_worker_status() if 'ERROR' not in celery_status: broken_link_scan(site) else: messages.warning(request, _('No celery workers are running, the scan was not conducted.')) return redirect('wagtaillinkchecker')
def revisions_revert(request, page_id, revision_id): page = get_object_or_404(Page, id=page_id).specific page_perms = page.permissions_for_user(request.user) if not page_perms.can_edit(): raise PermissionDenied revision = get_object_or_404(page.revisions, id=revision_id) revision_page = revision.as_page_object() content_type = ContentType.objects.get_for_model(page) page_class = content_type.model_class() edit_handler_class = page_class.get_edit_handler() form_class = edit_handler_class.get_form_class(page_class) form = form_class(instance=revision_page) edit_handler = edit_handler_class(instance=revision_page, form=form) user_avatar = render_to_string("wagtailadmin/shared/user_avatar.html", {"user": revision.user}) messages.warning( request, mark_safe( _("You are viewing a previous revision of this page from <b>%(created_at)s</b> by %(user)s") % {"created_at": revision.created_at.strftime("%d %b %Y %H:%M"), "user": user_avatar} ), ) return render( request, "wagtailadmin/pages/edit.html", { "page": page, "revision": revision, "is_revision": True, "content_type": content_type, "edit_handler": edit_handler, "errors_debug": None, "preview_modes": page.preview_modes, "form": form, # Used in unit tests }, )
def edit(request, page_id): latest_revision = get_object_or_404(Page, id=page_id).get_latest_revision() page = get_object_or_404(Page, id=page_id).get_latest_revision_as_page() parent = page.get_parent() content_type = ContentType.objects.get_for_model(page) page_perms = page.permissions_for_user(request.user) if not page_perms.can_edit(): raise PermissionDenied edit_handler_class = get_page_edit_handler(page.__class__) form_class = edit_handler_class.get_form_class(page.__class__) errors_debug = None if request.POST: form = form_class(request.POST, request.FILES, instance=page) validate_page_form(form, parent, page) if form.is_valid() and not page.locked: page = form.save(commit=False) is_publishing = bool(request.POST.get('action-publish')) and page_perms.can_publish() is_submitting = bool(request.POST.get('action-submit')) # Save revision revision = page.save_revision( user=request.user, submitted_for_moderation=is_submitting, ) # Publish if is_publishing: revision.publish() # Notifications if is_publishing: messages.success(request, _("Page '{0}' published.").format(page.title), buttons=[ messages.button(page.url, _('View live')), messages.button(reverse('wagtailadmin_pages:edit', args=(page_id,)), _('Edit')) ]) elif is_submitting: messages.success(request, _("Page '{0}' submitted for moderation.").format(page.title), buttons=[ messages.button(reverse('wagtailadmin_pages:view_draft', args=(page_id,)), _('View draft')), messages.button(reverse('wagtailadmin_pages:edit', args=(page_id,)), _('Edit')) ]) send_notification(page.get_latest_revision().id, 'submitted', request.user.id) else: messages.success(request, _("Page '{0}' updated.").format(page.title)) for fn in hooks.get_hooks('after_edit_page'): result = fn(request, page) if hasattr(result, 'status_code'): return result if is_publishing or is_submitting: # we're done here - redirect back to the explorer return redirect('wagtailadmin_explore', page.get_parent().id) else: # Just saving - remain on edit page for further edits return redirect('wagtailadmin_pages:edit', page.id) else: if page.locked: messages.error(request, _("The page could not be saved as it is locked")) else: messages.error(request, _("The page could not be saved due to validation errors")) edit_handler = edit_handler_class(instance=page, form=form) errors_debug = ( repr(edit_handler.form.errors) + repr([(name, formset.errors) for (name, formset) in edit_handler.form.formsets.items() if formset.errors]) ) else: form = form_class(instance=page) edit_handler = edit_handler_class(instance=page, form=form) # Check for revisions still undergoing moderation and warn if latest_revision and latest_revision.submitted_for_moderation: messages.warning(request, _("This page is currently awaiting moderation")) return render(request, 'wagtailadmin/pages/edit.html', { 'page': page, 'content_type': content_type, 'edit_handler': edit_handler, 'errors_debug': errors_debug, 'preview_modes': page.preview_modes, 'form': form, # Used in unit tests })
def edit(request, page_id): latest_revision = get_object_or_404(Page, id=page_id).get_latest_revision() page = get_object_or_404(Page, id=page_id).get_latest_revision_as_page() parent = page.get_parent() content_type = ContentType.objects.get_for_model(page) page_perms = page.permissions_for_user(request.user) if not page_perms.can_edit(): raise PermissionDenied edit_handler_class = get_page_edit_handler(page.__class__) form_class = edit_handler_class.get_form_class(page.__class__) errors_debug = None if request.POST: form = form_class(request.POST, request.FILES, instance=page) validate_page_form(form, parent, page) if form.is_valid() and not page.locked: page = form.save(commit=False) is_publishing = bool(request.POST.get( 'action-publish')) and page_perms.can_publish() is_submitting = bool(request.POST.get('action-submit')) # Save revision revision = page.save_revision( user=request.user, submitted_for_moderation=is_submitting, ) # Publish if is_publishing: revision.publish() # Notifications if is_publishing: messages.success(request, _("Page '{0}' published.").format(page.title), buttons=[ messages.button(page.url, _('View live')), messages.button( reverse('wagtailadmin_pages:edit', args=(page_id, )), _('Edit')) ]) elif is_submitting: messages.success( request, _("Page '{0}' submitted for moderation.").format( page.title), buttons=[ messages.button( reverse('wagtailadmin_pages:view_draft', args=(page_id, )), _('View draft')), messages.button( reverse('wagtailadmin_pages:edit', args=(page_id, )), _('Edit')) ]) send_notification(page.get_latest_revision().id, 'submitted', request.user.id) else: messages.success(request, _("Page '{0}' updated.").format(page.title)) for fn in hooks.get_hooks('after_edit_page'): result = fn(request, page) if hasattr(result, 'status_code'): return result if is_publishing or is_submitting: # we're done here - redirect back to the explorer return redirect('wagtailadmin_explore', page.get_parent().id) else: # Just saving - remain on edit page for further edits return redirect('wagtailadmin_pages:edit', page.id) else: if page.locked: messages.error( request, _("The page could not be saved as it is locked")) else: messages.error( request, _("The page could not be saved due to validation errors")) edit_handler = edit_handler_class(instance=page, form=form) errors_debug = (repr(edit_handler.form.errors) + repr( [(name, formset.errors) for (name, formset) in edit_handler.form.formsets.items() if formset.errors])) else: form = form_class(instance=page) edit_handler = edit_handler_class(instance=page, form=form) # Check for revisions still undergoing moderation and warn if latest_revision and latest_revision.submitted_for_moderation: messages.warning(request, _("This page is currently awaiting moderation")) return render( request, 'wagtailadmin/pages/edit.html', { 'page': page, 'content_type': content_type, 'edit_handler': edit_handler, 'errors_debug': errors_debug, 'preview_modes': page.preview_modes, 'form': form, # Used in unit tests })
def edit(request, page_id): latest_revision = get_object_or_404(Page, id=page_id).get_latest_revision() page = get_object_or_404(Page, id=page_id).get_latest_revision_as_page() parent = page.get_parent() content_type = ContentType.objects.get_for_model(page) page_perms = page.permissions_for_user(request.user) if not page_perms.can_edit(): raise PermissionDenied edit_handler_class = get_page_edit_handler(page.__class__) form_class = edit_handler_class.get_form_class(page.__class__) errors_debug = None if request.POST: form = form_class(request.POST, request.FILES, instance=page) # Stick an extra validator into the form to make sure that the slug is not already in use def clean_slug(slug): # Make sure the slug isn't already in use if parent.get_children().filter(slug=slug).exclude( id=page_id).count() > 0: raise ValidationError(_("This slug is already in use")) return slug form.fields['slug'].clean = clean_slug # Stick another validator into the form to check that the scheduled publishing settings are set correctly def clean(): cleaned_data = form_class.clean(form) # Go live must be before expire go_live_at = cleaned_data.get('go_live_at') expire_at = cleaned_data.get('expire_at') if go_live_at and expire_at: if go_live_at > expire_at: msg = _( 'Go live date/time must be before expiry date/time') form._errors['go_live_at'] = form.error_class([msg]) form._errors['expire_at'] = form.error_class([msg]) del cleaned_data['go_live_at'] del cleaned_data['expire_at'] # Expire must be in the future expire_at = cleaned_data.get('expire_at') if expire_at and expire_at < timezone.now(): form._errors['expire_at'] = form.error_class( [_('Expiry date/time must be in the future')]) del cleaned_data['expire_at'] return cleaned_data form.clean = clean if form.is_valid() and not page.locked: page = form.save(commit=False) is_publishing = bool(request.POST.get( 'action-publish')) and page_perms.can_publish() is_submitting = bool(request.POST.get('action-submit')) # Save revision revision = page.save_revision( user=request.user, submitted_for_moderation=is_submitting, ) # Publish if is_publishing: revision.publish() else: # Set has_unpublished_changes flag if page.live: # To avoid overwriting the live version, we only save the page # to the revisions table Page.objects.filter(id=page.id).update( has_unpublished_changes=True) else: page.has_unpublished_changes = True page.save() # Notifications if is_publishing: messages.success(request, _("Page '{0}' published.").format(page.title), buttons=[ messages.button(page.url, _('View live')), messages.button( reverse('wagtailadmin_pages_edit', args=(page_id, )), _('Edit')) ]) elif is_submitting: messages.success( request, _("Page '{0}' submitted for moderation.").format( page.title), buttons=[ messages.button( reverse('wagtailadmin_pages_view_draft', args=(page_id, )), _('View draft')), messages.button( reverse('wagtailadmin_pages_edit', args=(page_id, )), _('Edit')) ]) tasks.send_notification.delay(page.get_latest_revision().id, 'submitted', request.user.id) else: messages.success(request, _("Page '{0}' updated.").format(page.title)) for fn in hooks.get_hooks('after_edit_page'): result = fn(request, page) if hasattr(result, 'status_code'): return result if is_publishing or is_submitting: # we're done here - redirect back to the explorer return redirect('wagtailadmin_explore', page.get_parent().id) else: # Just saving - remain on edit page for further edits return redirect('wagtailadmin_pages_edit', page.id) else: if page.locked: messages.error( request, _("The page could not be saved as it is locked")) else: messages.error( request, _("The page could not be saved due to validation errors")) edit_handler = edit_handler_class(instance=page, form=form) errors_debug = (repr(edit_handler.form.errors) + repr( [(name, formset.errors) for (name, formset) in edit_handler.form.formsets.items() if formset.errors])) else: form = form_class(instance=page) edit_handler = edit_handler_class(instance=page, form=form) # Check for revisions still undergoing moderation and warn if latest_revision and latest_revision.submitted_for_moderation: messages.warning(request, _("This page is currently awaiting moderation")) return render( request, 'wagtailadmin/pages/edit.html', { 'page': page, 'content_type': content_type, 'edit_handler': edit_handler, 'errors_debug': errors_debug, 'preview_modes': page.preview_modes, 'form': form, # Used in unit tests })
def edit(request, page_id): latest_revision = get_object_or_404(Page, id=page_id).get_latest_revision() page = get_object_or_404(Page, id=page_id).get_latest_revision_as_page() parent = page.get_parent() content_type = ContentType.objects.get_for_model(page) page_class = content_type.model_class() page_perms = page.permissions_for_user(request.user) if not page_perms.can_edit(): raise PermissionDenied for fn in hooks.get_hooks('before_edit_page'): result = fn(request, page) if hasattr(result, 'status_code'): return result edit_handler_class = page_class.get_edit_handler() form_class = edit_handler_class.get_form_class(page_class) next_url = get_valid_next_url_from_request(request) errors_debug = None if request.method == 'POST': form = form_class(request.POST, request.FILES, instance=page, parent_page=parent) if form.is_valid() and not page.locked: page = form.save(commit=False) is_publishing = bool(request.POST.get( 'action-publish')) and page_perms.can_publish() is_submitting = bool(request.POST.get('action-submit')) is_reverting = bool(request.POST.get('revision')) # If a revision ID was passed in the form, get that revision so its # date can be referenced in notification messages if is_reverting: previous_revision = get_object_or_404( page.revisions, id=request.POST.get('revision')) # Save revision revision = page.save_revision( user=request.user, submitted_for_moderation=is_submitting, ) # Publish if is_publishing: revision.publish() # Need to reload the page because the URL may have changed, and we # need the up-to-date URL for the "View Live" button. page = page.specific_class.objects.get(pk=page.pk) # Notifications if is_publishing: if page.go_live_at and page.go_live_at > timezone.now(): # Page has been scheduled for publishing in the future if is_reverting: message = _( "Revision from {0} of page '{1}' has been scheduled for publishing." ).format( previous_revision.created_at.strftime( "%d %b %Y %H:%M"), page.get_admin_display_title()) else: message = _( "Page '{0}' has been scheduled for publishing." ).format(page.get_admin_display_title()) messages.success(request, message, buttons=[ messages.button( reverse('wagtailadmin_pages:edit', args=(page.id, )), _('Edit')) ]) else: # Page is being published now if is_reverting: message = _( "Revision from {0} of page '{1}' has been published." ).format( previous_revision.created_at.strftime( "%d %b %Y %H:%M"), page.get_admin_display_title()) else: message = _("Page '{0}' has been published.").format( page.get_admin_display_title()) messages.success(request, message, buttons=[ messages.button(page.url, _('View live'), new_window=True), messages.button( reverse('wagtailadmin_pages:edit', args=(page_id, )), _('Edit')) ]) elif is_submitting: message = _( "Page '{0}' has been submitted for moderation.").format( page.get_admin_display_title()) messages.success(request, message, buttons=[ messages.button(reverse( 'wagtailadmin_pages:view_draft', args=(page_id, )), _('View draft'), new_window=True), messages.button( reverse('wagtailadmin_pages:edit', args=(page_id, )), _('Edit')) ]) if not send_notification(page.get_latest_revision().id, 'submitted', request.user.pk): messages.error( request, _("Failed to send notifications to moderators")) else: # Saving if is_reverting: message = _( "Page '{0}' has been replaced with revision from {1}." ).format( page.get_admin_display_title(), previous_revision.created_at.strftime( "%d %b %Y %H:%M")) else: message = _("Page '{0}' has been updated.").format( page.get_admin_display_title()) messages.success(request, message) for fn in hooks.get_hooks('after_edit_page'): result = fn(request, page) if hasattr(result, 'status_code'): return result if is_publishing or is_submitting: # we're done here - redirect back to the explorer if next_url: # redirect back to 'next' url if present return redirect(next_url) # redirect back to the explorer return redirect('wagtailadmin_explore', page.get_parent().id) else: # Just saving - remain on edit page for further edits target_url = reverse('wagtailadmin_pages:edit', args=[page.id]) if next_url: # Ensure the 'next' url is passed through again if present target_url += '?next=%s' % urlquote(next_url) return redirect(target_url) else: if page.locked: messages.error( request, _("The page could not be saved as it is locked")) else: messages.validation_error( request, _("The page could not be saved due to validation errors"), form) edit_handler = edit_handler_class(instance=page, form=form) errors_debug = (repr(edit_handler.form.errors) + repr( [(name, formset.errors) for (name, formset) in edit_handler.form.formsets.items() if formset.errors])) has_unsaved_changes = True else: form = form_class(instance=page) edit_handler = edit_handler_class(instance=page, form=form) has_unsaved_changes = False # Check for revisions still undergoing moderation and warn if latest_revision and latest_revision.submitted_for_moderation: buttons = [] if page.live: buttons.append( messages.button( reverse('wagtailadmin_pages:revisions_compare', args=(page.id, 'live', latest_revision.id)), _('Compare with live version'))) messages.warning(request, _("This page is currently awaiting moderation"), buttons=buttons) return render( request, 'wagtailadmin/pages/edit.html', { 'page': page, 'content_type': content_type, 'edit_handler': edit_handler, 'errors_debug': errors_debug, 'preview_modes': page.preview_modes, 'form': form, 'next': next_url, 'has_unsaved_changes': has_unsaved_changes, })
def edit(request, page_id): latest_revision = get_object_or_404(Page, id=page_id).get_latest_revision() page = get_object_or_404(Page, id=page_id).get_latest_revision_as_page() parent = page.get_parent() content_type = ContentType.objects.get_for_model(page) page_class = content_type.model_class() page_perms = page.permissions_for_user(request.user) if not page_perms.can_edit(): raise PermissionDenied edit_handler_class = page_class.get_edit_handler() form_class = edit_handler_class.get_form_class(page_class) next_url = get_valid_next_url_from_request(request) errors_debug = None if request.method == 'POST': form = form_class(request.POST, request.FILES, instance=page, parent_page=parent) if form.is_valid() and not page.locked: page = form.save(commit=False) is_publishing = bool(request.POST.get('action-publish')) and page_perms.can_publish() is_submitting = bool(request.POST.get('action-submit')) is_reverting = bool(request.POST.get('revision')) # If a revision ID was passed in the form, get that revision so its # date can be referenced in notification messages if is_reverting: previous_revision = get_object_or_404(page.revisions, id=request.POST.get('revision')) # Save revision revision = page.save_revision( user=request.user, submitted_for_moderation=is_submitting, ) # Publish if is_publishing: revision.publish() # Need to reload the page because the URL may have changed, and we # need the up-to-date URL for the "View Live" button. page = page.specific_class.objects.get(pk=page.pk) # Notifications if is_publishing: if page.go_live_at and page.go_live_at > timezone.now(): # Page has been scheduled for publishing in the future if is_reverting: message = _( "Revision from {0} of page '{1}' has been scheduled for publishing." ).format( previous_revision.created_at.strftime("%d %b %Y %H:%M"), page.title ) else: message = _( "Page '{0}' has been scheduled for publishing." ).format( page.title ) messages.success(request, message, buttons=[ messages.button( reverse('wagtailadmin_pages:edit', args=(page.id,)), _('Edit') ) ]) else: # Page is being published now if is_reverting: message = _( "Revision from {0} of page '{1}' has been published." ).format( previous_revision.created_at.strftime("%d %b %Y %H:%M"), page.title ) else: message = _( "Page '{0}' has been published." ).format( page.title ) messages.success(request, message, buttons=[ messages.button( page.url, _('View live') ), messages.button( reverse('wagtailadmin_pages:edit', args=(page_id,)), _('Edit') ) ]) elif is_submitting: message = _( "Page '{0}' has been submitted for moderation." ).format( page.title ) messages.success(request, message, buttons=[ messages.button( reverse('wagtailadmin_pages:view_draft', args=(page_id,)), _('View draft') ), messages.button( reverse('wagtailadmin_pages:edit', args=(page_id,)), _('Edit') ) ]) if not send_notification(page.get_latest_revision().id, 'submitted', request.user.pk): messages.error(request, _("Failed to send notifications to moderators")) else: # Saving if is_reverting: message = _( "Page '{0}' has been replaced with revision from {1}." ).format( page.title, previous_revision.created_at.strftime("%d %b %Y %H:%M") ) else: message = _( "Page '{0}' has been updated." ).format( page.title ) messages.success(request, message) for fn in hooks.get_hooks('after_edit_page'): result = fn(request, page) if hasattr(result, 'status_code'): return result if is_publishing or is_submitting: # we're done here - redirect back to the explorer if next_url: # redirect back to 'next' url if present return redirect(next_url) # redirect back to the explorer return redirect('wagtailadmin_explore', page.get_parent().id) else: # Just saving - remain on edit page for further edits target_url = reverse('wagtailadmin_pages:edit', args=[page.id]) if next_url: # Ensure the 'next' url is passed through again if present target_url += '?next=%s' % urlquote(next_url) return redirect(target_url) else: if page.locked: messages.error(request, _("The page could not be saved as it is locked")) else: messages.error(request, _("The page could not be saved due to validation errors")) edit_handler = edit_handler_class(instance=page, form=form) errors_debug = ( repr(edit_handler.form.errors) + repr([ (name, formset.errors) for (name, formset) in edit_handler.form.formsets.items() if formset.errors ]) ) else: form = form_class(instance=page) edit_handler = edit_handler_class(instance=page, form=form) # Check for revisions still undergoing moderation and warn if latest_revision and latest_revision.submitted_for_moderation: messages.warning(request, _("This page is currently awaiting moderation")) return render(request, 'wagtailadmin/pages/edit.html', { 'page': page, 'content_type': content_type, 'edit_handler': edit_handler, 'errors_debug': errors_debug, 'preview_modes': page.preview_modes, 'form': form, 'next': next_url, })
def edit(request, page_id): latest_revision = get_object_or_404(Page, id=page_id).get_latest_revision() page = get_object_or_404(Page, id=page_id).get_latest_revision_as_page() parent = page.get_parent() content_type = ContentType.objects.get_for_model(page) page_perms = page.permissions_for_user(request.user) if not page_perms.can_edit(): raise PermissionDenied edit_handler_class = get_page_edit_handler(page.__class__) form_class = edit_handler_class.get_form_class(page.__class__) errors_debug = None if request.POST: form = form_class(request.POST, request.FILES, instance=page) # Stick an extra validator into the form to make sure that the slug is not already in use def clean_slug(slug): # Make sure the slug isn't already in use if parent.get_children().filter(slug=slug).exclude(id=page_id).count() > 0: raise ValidationError(_("This slug is already in use")) return slug form.fields['slug'].clean = clean_slug # Validate title and seo_title are not entirely whitespace def clean_title(title): validate_not_whitespace(title) return title form.fields['title'].clean = clean_title def clean_seo_title(seo_title): if not seo_title: return '' validate_not_whitespace(seo_title) return seo_title form.fields['seo_title'].clean = clean_seo_title # Stick another validator into the form to check that the scheduled publishing settings are set correctly def clean(): cleaned_data = form_class.clean(form) # Go live must be before expire go_live_at = cleaned_data.get('go_live_at') expire_at = cleaned_data.get('expire_at') if go_live_at and expire_at: if go_live_at > expire_at: msg = _('Go live date/time must be before expiry date/time') form._errors['go_live_at'] = form.error_class([msg]) form._errors['expire_at'] = form.error_class([msg]) del cleaned_data['go_live_at'] del cleaned_data['expire_at'] # Expire must be in the future expire_at = cleaned_data.get('expire_at') if expire_at and expire_at < timezone.now(): form._errors['expire_at'] = form.error_class([_('Expiry date/time must be in the future')]) del cleaned_data['expire_at'] return cleaned_data form.clean = clean if form.is_valid() and not page.locked: page = form.save(commit=False) is_publishing = bool(request.POST.get('action-publish')) and page_perms.can_publish() is_submitting = bool(request.POST.get('action-submit')) # Save revision revision = page.save_revision( user=request.user, submitted_for_moderation=is_submitting, ) # Publish if is_publishing: revision.publish() else: # Set has_unpublished_changes flag if page.live: # To avoid overwriting the live version, we only save the page # to the revisions table Page.objects.filter(id=page.id).update(has_unpublished_changes=True) else: page.has_unpublished_changes = True page.save() # Notifications if is_publishing: messages.success(request, _("Page '{0}' published.").format(page.title), buttons=[ messages.button(page.url, _('View live')), messages.button(reverse('wagtailadmin_pages_edit', args=(page_id,)), _('Edit')) ]) elif is_submitting: messages.success(request, _("Page '{0}' submitted for moderation.").format(page.title), buttons=[ messages.button(reverse('wagtailadmin_pages_view_draft', args=(page_id,)), _('View draft')), messages.button(reverse('wagtailadmin_pages_edit', args=(page_id,)), _('Edit')) ]) send_notification(page.get_latest_revision().id, 'submitted', request.user.id) else: messages.success(request, _("Page '{0}' updated.").format(page.title)) for fn in hooks.get_hooks('after_edit_page'): result = fn(request, page) if hasattr(result, 'status_code'): return result if is_publishing or is_submitting: # we're done here - redirect back to the explorer return redirect('wagtailadmin_explore', page.get_parent().id) else: # Just saving - remain on edit page for further edits return redirect('wagtailadmin_pages_edit', page.id) else: if page.locked: messages.error(request, _("The page could not be saved as it is locked")) else: messages.error(request, _("The page could not be saved due to validation errors")) edit_handler = edit_handler_class(instance=page, form=form) errors_debug = ( repr(edit_handler.form.errors) + repr([(name, formset.errors) for (name, formset) in edit_handler.form.formsets.items() if formset.errors]) ) else: form = form_class(instance=page) edit_handler = edit_handler_class(instance=page, form=form) # Check for revisions still undergoing moderation and warn if latest_revision and latest_revision.submitted_for_moderation: messages.warning(request, _("This page is currently awaiting moderation")) return render(request, 'wagtailadmin/pages/edit.html', { 'page': page, 'content_type': content_type, 'edit_handler': edit_handler, 'errors_debug': errors_debug, 'preview_modes': page.preview_modes, 'form': form, # Used in unit tests })
def edit(request, page_id): latest_revision = get_object_or_404(Page, id=page_id).get_latest_revision() page = get_object_or_404(Page, id=page_id).get_latest_revision_as_page() parent = page.get_parent() content_type = ContentType.objects.get_for_model(page) page_perms = page.permissions_for_user(request.user) if not page_perms.can_edit(): raise PermissionDenied edit_handler_class = get_page_edit_handler(page.__class__) form_class = edit_handler_class.get_form_class(page.__class__) errors_debug = None if request.POST: form = form_class(request.POST, request.FILES, instance=page) # Stick an extra validator into the form to make sure that the slug is not already in use def clean_slug(slug): # Make sure the slug isn't already in use if parent.get_children().filter(slug=slug).exclude( id=page_id).count() > 0: raise ValidationError(_("This slug is already in use")) return slug form.fields['slug'].clean = clean_slug # Stick another validator into the form to check that the scheduled publishing settings are set correctly def clean(): cleaned_data = form_class.clean(form) # Go live must be before expire go_live_at = cleaned_data.get('go_live_at') expire_at = cleaned_data.get('expire_at') if go_live_at and expire_at: if go_live_at > expire_at: msg = _( 'Go live date/time must be before expiry date/time') form._errors['go_live_at'] = form.error_class([msg]) form._errors['expire_at'] = form.error_class([msg]) del cleaned_data['go_live_at'] del cleaned_data['expire_at'] # Expire must be in the future expire_at = cleaned_data.get('expire_at') if expire_at and expire_at < timezone.now(): form._errors['expire_at'] = form.error_class( [_('Expiry date/time must be in the future')]) del cleaned_data['expire_at'] return cleaned_data form.clean = clean if form.is_valid() and not page.locked: page = form.save(commit=False) is_publishing = bool(request.POST.get( 'action-publish')) and page_perms.can_publish() is_submitting = bool(request.POST.get('action-submit')) is_translating_from_serbian = bool( request.POST.get('action-translate-serbian')) is_translating_from_bosnian = bool( request.POST.get('action-translate-bosnian')) is_translating_from_croatian = bool( request.POST.get('action-translate-croatian')) import os google_api_key = os.environ.get('GOOGLE_KEY_ID') import HTMLParser html_parser = HTMLParser.HTMLParser() if is_translating_from_serbian: from apiclient.discovery import build # Build a service object for interacting with the API. Visit # the Google APIs Console <http://code.google.com/apis/console> # to get an API key for your own application. service = build('translate', 'v2', developerKey=google_api_key) translation = service.translations().list( source='sr', target='en', q=[ request.POST.get('source_title'), request.POST.get('source_intro'), request.POST.get('source_body') ]).execute() page.title = html_parser.unescape( translation['translations'][0]['translatedText']) page.intro = html_parser.unescape( translation['translations'][1]['translatedText']) page.body = html_parser.unescape( translation['translations'][2]['translatedText']) page.save() if is_translating_from_bosnian: from apiclient.discovery import build # Build a service object for interacting with the API. Visit # the Google APIs Console <http://code.google.com/apis/console> # to get an API key for your own application. service = build('translate', 'v2', developerKey=google_api_key) translation = service.translations().list( source='bs', target='en', q=[ request.POST.get('source_title'), request.POST.get('source_intro'), request.POST.get('source_body') ]).execute() page.title = html_parser.unescape( translation['translations'][0]['translatedText']) page.intro = html_parser.unescape( translation['translations'][1]['translatedText']) page.body = html_parser.unescape( translation['translations'][2]['translatedText']) page.save() if is_translating_from_croatian: from apiclient.discovery import build # Build a service object for interacting with the API. Visit # the Google APIs Console <http://code.google.com/apis/console> # to get an API key for your own application. service = build('translate', 'v2', developerKey=google_api_key) translation = service.translations().list( source='hr', target='en', q=[ request.POST.get('source_title'), request.POST.get('source_intro'), request.POST.get('source_body') ]).execute() page.title = html_parser.unescape( translation['translations'][0]['translatedText']) page.intro = html_parser.unescape( translation['translations'][1]['translatedText']) page.body = html_parser.unescape( translation['translations'][2]['translatedText']) page.save() # Save revision revision = page.save_revision( user=request.user, submitted_for_moderation=is_submitting, ) # Publish if is_publishing: revision.publish() else: # Set has_unpublished_changes flag if page.live: # To avoid overwriting the live version, we only save the page # to the revisions table Page.objects.filter(id=page.id).update( has_unpublished_changes=True) else: page.has_unpublished_changes = True page.save() # Notifications if is_publishing: messages.success(request, _("Page '{0}' published.").format(page.title), buttons=[ messages.button(page.url, _('View live')), messages.button( reverse('wagtailadmin_pages_edit', args=(page_id, )), _('Edit')) ]) elif is_submitting: messages.success( request, _("Page '{0}' submitted for moderation.").format( page.title), buttons=[ messages.button( reverse('wagtailadmin_pages_view_draft', args=(page_id, )), _('View draft')), messages.button( reverse('wagtailadmin_pages_edit', args=(page_id, )), _('Edit')) ]) tasks.send_notification.delay(page.get_latest_revision().id, 'submitted', request.user.id) else: messages.success(request, _("Page '{0}' updated.").format(page.title)) for fn in hooks.get_hooks('after_edit_page'): result = fn(request, page) if hasattr(result, 'status_code'): return result if is_publishing or is_submitting: # we're done here - redirect back to the explorer return redirect('wagtailadmin_explore', page.get_parent().id) else: # Just saving - remain on edit page for further edits return redirect('wagtailadmin_pages_edit', page.id) else: if page.locked: messages.error( request, _("The page could not be saved as it is locked")) else: messages.error( request, _("The page could not be saved due to validation errors")) edit_handler = edit_handler_class(instance=page, form=form) errors_debug = (repr(edit_handler.form.errors) + repr( [(name, formset.errors) for (name, formset) in edit_handler.form.formsets.items() if formset.errors])) else: form = form_class(instance=page) edit_handler = edit_handler_class(instance=page, form=form) # Check for revisions still undergoing moderation and warn if latest_revision and latest_revision.submitted_for_moderation: messages.warning(request, _("This page is currently awaiting moderation")) return render( request, 'wagtailadmin/pages/edit.html', { 'page': page, 'content_type': content_type, 'edit_handler': edit_handler, 'errors_debug': errors_debug, 'preview_modes': page.preview_modes, 'form': form, # Used in unit tests })