def edit_user(request, user_id): user = get_object_or_404(get_user_model(), id=user_id) if request.POST: form = CFGOVUserEditForm(request.POST, instance=user) if form.is_valid(): user = form.save() user.temporarylockout_set.all().delete() wagtail_messages.success(request, _("User '{0}' updated.").format(user), buttons=[ wagtail_messages.button( reverse('wagtailusers_users:edit', args=(user.id, )), _('Edit')) ]) return redirect('wagtailusers_users:index') else: if '__all__' in form.errors: wagtail_messages.error(request, form.errors['__all__']) else: form = CFGOVUserEditForm(instance=user) return render(request, 'wagtailusers/users/edit.html', { 'user': user, 'form': form, })
def add(request): Document = get_document_model() DocumentForm = get_document_form(Document) if request.method == 'POST': doc = Document(uploaded_by_user=request.user) form = DocumentForm(request.POST, request.FILES, instance=doc, user=request.user) if form.is_valid(): form.save() # Reindex the document to make sure all tags are indexed search_index.insert_or_update_object(doc) messages.success(request, _("Document '{0}' added.").format(doc.title), buttons=[ messages.button( reverse('wagtaildocs:edit', args=(doc.id, )), _('Edit')) ]) return redirect('wagtaildocs:index') else: messages.error(request, _("The document could not be saved due to errors.")) else: form = DocumentForm(user=request.user) return render(request, "wagtaildocs/documents/add.html", { 'form': form, })
def create(request): group = Group() if request.method == 'POST': form = MultitenantGroupForm(request.POST, instance=group, request=request) permission_panels = get_permission_panel_instances(request, group) if form.is_valid() and all(panel.is_valid() for panel in permission_panels): form.save() for panel in permission_panels: panel.save() messages.success(request, "Group '{0}' created.".format(group), buttons=[ messages.button( reverse('wagtailusers_groups:edit', args=(group.id, )), 'Edit') ]) return redirect('wagtailusers_groups:index') else: messages.error(request, 'The group could not be created due to errors.') else: form = MultitenantGroupForm(instance=group, request=request) permission_panels = get_permission_panel_instances(request, group) return TemplateResponse(request, 'wagtailusers/groups/create.html', { 'form': form, 'permission_panels': permission_panels, })
def post(self, request, *args, **kwargs): if request.POST: try: self.delete_instance() messages.success( request, _("{model} '{instance}' deleted.").format( model=self.model_name, instance=self.instance)) return redirect(self.get_index_url) except models.ProtectedError: messages.error( request, _( "{model} '{instance}' could not be deleted." ).format(model=self.model_name, instance=self.instance)) linked_objects = [] for rel in self.model._meta.get_all_related_objects(): if rel.on_delete == models.PROTECT: qs = getattr(self.instance, rel.get_accessor_name()) for obj in qs.all(): linked_objects.append(obj) context = { 'view': self, 'instance': self.instance, 'error_protected': True, 'linked_objects': linked_objects, } return self.render_to_response(context)
def edit(request, redirect_id): theredirect = get_object_or_404(models.Redirect, id=redirect_id) if not permission_policy.user_has_permission_for_instance( request.user, 'change', theredirect ): return permission_denied(request) if request.method == 'POST': form = RedirectForm(request.POST, request.FILES, instance=theredirect) if form.is_valid(): form.save() messages.success(request, _("Redirect '{0}' updated.").format(theredirect.title), buttons=[ messages.button(reverse('wagtailredirects:edit', args=(theredirect.id,)), _('Edit')) ]) return redirect('wagtailredirects:index') else: messages.error(request, _("The redirect could not be saved due to errors.")) else: form = RedirectForm(instance=theredirect) return render(request, "wagtailredirects/edit.html", { 'redirect': theredirect, 'form': form, 'user_can_delete': permission_policy.user_has_permission(request.user, 'delete'), })
def edit(request, document_id): doc = get_object_or_404(Document, id=document_id) if not doc.is_editable_by_user(request.user): raise PermissionDenied if request.POST: original_file = doc.file form = DocumentForm(request.POST, request.FILES, instance=doc) if form.is_valid(): if 'file' in form.changed_data: # if providing a new document file, delete the old one. # NB Doing this via original_file.delete() clears the file field, # which definitely isn't what we want... original_file.storage.delete(original_file.name) doc = form.save() # Reindex the document to make sure all tags are indexed for backend in get_search_backends(): backend.add(doc) messages.success(request, _("Document '{0}' updated").format(doc.title), buttons=[ messages.button(reverse('wagtaildocs_edit_document', args=(doc.id,)), _('Edit')) ]) return redirect('wagtaildocs_index') else: messages.error(request, _("The document could not be saved due to errors.")) else: form = DocumentForm(instance=doc) return render(request, "wagtaildocs/documents/edit.html", { 'document': doc, 'form': form })
def add(request): ImageModel = get_image_model() ImageForm = get_image_form(ImageModel) if request.method == "POST": image = ImageModel(uploaded_by_user=request.user) form = ImageForm(request.POST, request.FILES, instance=image, user=request.user) if form.is_valid(): # Set image file size image.file_size = image.file.size form.save() # Reindex the image to make sure all tags are indexed search_index.insert_or_update_object(image) messages.success( request, _("Image '{0}' added.").format(image.title), buttons=[messages.button(reverse("wagtailimages:edit", args=(image.id,)), _("Edit"))], ) return redirect("wagtailimages:index") else: messages.error(request, _("The image could not be created due to errors.")) else: form = ImageForm(user=request.user) return render(request, "wagtailimages/images/add.html", {"form": form})
def edit(request, user_id): user = get_object_or_404(User, pk=user_id) can_delete = user_can_delete_user(request.user, user) editing_self = request.user == user if request.method == 'POST': form = get_user_edit_form()(request.POST, request.FILES, instance=user, editing_self=editing_self) if form.is_valid(): user = form.save() messages.success(request, _("User '{0}' updated.").format(user), buttons=[ messages.button( reverse('wagtailusers_users:edit', args=(user.pk, )), _('Edit')) ]) return redirect('wagtailusers_users:index') else: messages.error(request, _("The user could not be saved due to errors.")) else: form = get_user_edit_form()(instance=user, editing_self=editing_self) return render(request, 'wagtailusers/users/edit.html', { 'user': user, 'form': form, 'can_delete': can_delete, })
def create(request): if request.POST: form = GroupForm(request.POST) formset = GroupPagePermissionFormSet(request.POST) if form.is_valid() and formset.is_valid(): group = form.save() formset.instance = group formset.save() messages.success(request, _("Group '{0}' created.").format(group), buttons=[ messages.button( reverse('wagtailusers_groups_edit', args=(group.id, )), _('Edit')) ]) return redirect('wagtailusers_groups_index') else: messages.error(request, _("The group could not be created due to errors.")) else: form = GroupForm() formset = GroupPagePermissionFormSet() return render(request, 'wagtailusers/groups/create.html', { 'form': form, 'formset': formset, })
def edit(request, group_id): group = get_object_or_404(Group, id=group_id) if request.POST: form = GroupForm(request.POST, instance=group) formset = GroupPagePermissionFormSet(request.POST, instance=group) if form.is_valid() and formset.is_valid(): group = form.save() formset.save() messages.success(request, _("Group '{0}' updated.").format(group), buttons=[ messages.button( reverse('wagtailusers_groups_edit', args=(group.id, )), _('Edit')) ]) return redirect('wagtailusers_groups_index') else: messages.error(request, _("The group could not be saved due to errors.")) else: form = GroupForm(instance=group) formset = GroupPagePermissionFormSet(instance=group) return render(request, 'wagtailusers/groups/edit.html', { 'group': group, 'form': form, 'formset': formset, })
def approve_moderation(request, revision_id): revision = get_object_or_404(PageRevision, id=revision_id) if not revision.page.permissions_for_user(request.user).can_publish(): raise PermissionDenied if not revision.submitted_for_moderation: messages.error( request, _("The page '{0}' is not currently awaiting moderation.").format( revision.page.title)) return redirect('wagtailadmin_home') if request.method == 'POST': revision.approve_moderation() messages.success( request, _("Page '{0}' published.").format(revision.page.title), buttons=[ messages.button(revision.page.url, _('View live')), messages.button( reverse('wagtailadmin_pages:edit', args=(revision.page.id, )), _('Edit')) ]) if not send_notification(revision.id, 'approved', request.user.pk): messages.error(request, _("Failed to send approval notifications")) return redirect('wagtailadmin_home')
def edit(request, redirect_id): theredirect = get_object_or_404(models.Redirect, id=redirect_id) form_class = REDIRECT_EDIT_HANDLER.get_form_class(models.Redirect) if request.POST: form = form_class(request.POST, request.FILES, instance=theredirect) if form.is_valid(): form.save() messages.success( request, _("Redirect '{0}' updated.").format(theredirect.title), buttons=[ messages.button( reverse('wagtailredirects:edit', args=(theredirect.id, )), _('Edit')) ]) return redirect('wagtailredirects:index') else: messages.error(request, _("The redirect could not be saved due to errors.")) edit_handler = REDIRECT_EDIT_HANDLER(instance=theredirect, form=form) else: form = form_class(instance=theredirect) edit_handler = REDIRECT_EDIT_HANDLER(instance=theredirect, form=form) return render(request, "wagtailredirects/edit.html", { 'redirect': theredirect, 'edit_handler': edit_handler, })
def edit(request, query_id): query = get_object_or_404(Query, id=query_id) if request.POST: # Get query query_form = search_forms.QueryForm(request.POST) # and the recommendations searchpicks_formset = forms.SearchPromotionsFormSet(request.POST, instance=query) if query_form.is_valid(): new_query = Query.get(query_form['query_string'].value()) # Save search picks if save_searchpicks(query, new_query, searchpicks_formset): messages.success(request, _("Editor's picks for '{0}' updated.").format(new_query), buttons=[ messages.button(reverse('wagtailsearchpromotions:edit', args=(query.id,)), _('Edit')) ]) return redirect('wagtailsearchpromotions:index') else: if len(searchpicks_formset.non_form_errors()): messages.error(request, " ".join(error for error in searchpicks_formset.non_form_errors())) # formset level error (e.g. no forms submitted) else: messages.error(request, _("Recommendations have not been saved due to errors")) # specific errors will be displayed within form fields else: query_form = search_forms.QueryForm(initial=dict(query_string=query.query_string)) searchpicks_formset = forms.SearchPromotionsFormSet(instance=query) return render(request, 'wagtailsearchpromotions/edit.html', { 'query_form': query_form, 'searchpicks_formset': searchpicks_formset, 'query': query, })
def create(request): group = Group() if request.POST: form = GroupForm(request.POST, instance=group) permission_panels = [ cls(request.POST, instance=group) for cls in get_permission_panel_classes() ] if form.is_valid() and all(panel.is_valid() for panel in permission_panels): form.save() for panel in permission_panels: panel.save() messages.success(request, _("Group '{0}' created.").format(group), buttons=[ messages.button(reverse('wagtailusers_groups:edit', args=(group.id,)), _('Edit')) ]) return redirect('wagtailusers_groups:index') else: messages.error(request, _("The group could not be created due to errors.")) else: form = GroupForm(instance=group) permission_panels = [ cls(instance=group) for cls in get_permission_panel_classes() ] return render(request, 'wagtailusers/groups/create.html', { 'form': form, 'permission_panels': permission_panels, })
def permission_denied(request): """Return a standard 'permission denied' response""" from wagtail.wagtailadmin import messages messages.error( request, _('Sorry, you do not have permission to access this area.')) return redirect('wagtailadmin_home')
def edit(request, group_id): group = get_object_or_404(Group, id=group_id) if request.method == 'POST': form = GroupForm(request.POST, instance=group) permission_panels = [ cls(request.POST, instance=group) for cls in get_permission_panel_classes() ] if form.is_valid() and all(panel.is_valid() for panel in permission_panels): form.save() for panel in permission_panels: panel.save() messages.success(request, _("Group '{0}' updated.").format(group), buttons=[ messages.button(reverse('wagtailusers_groups:edit', args=(group.id,)), _('Edit')) ]) return redirect('wagtailusers_groups:index') else: messages.error(request, _("The group could not be saved due to errors.")) else: form = GroupForm(instance=group) permission_panels = [ cls(instance=group) for cls in get_permission_panel_classes() ] return render(request, 'wagtailusers/groups/edit.html', { 'group': group, 'form': form, 'permission_panels': permission_panels, })
def edit(request, embed_video_id): EmbedVideo = get_embed_video_model() EmbedVideoForm = get_embed_video_form(EmbedVideo) embed_video = get_object_or_404(EmbedVideo, id=embed_video_id) if not embed_video.is_editable_by_user(request.user): raise PermissionDenied if request.POST: form = EmbedVideoForm(request.POST, request.FILES, instance=embed_video) if form.is_valid(): form.save() # Reindex the embed_video to make sure all tags are indexed for backend in get_search_backends(): backend.add(embed_video) messages.success(request, _("Embed video '{0}' updated.").format(embed_video.title), buttons=[ messages.button(reverse('wagtail_embed_videos_edit_embed_video', args=(embed_video.id,)), _('Edit again')) ]) return redirect('wagtail_embed_videos_index') else: messages.error(request, _("The embed video could not be saved due to errors.")) else: form = EmbedVideoForm(instance=embed_video) return render(request, "wagtail_embed_videos/embed_videos/edit.html", { 'embed_video': embed_video, 'form': form, })
def add(request): EmbedVideoModel = get_embed_video_model() EmbedVideoForm = get_embed_video_form(EmbedVideoModel) if request.method == 'POST': embed_video = EmbedVideoModel(uploaded_by_user=request.user) form = EmbedVideoForm(request.POST, request.FILES, instance=embed_video, user=request.user) if form.is_valid(): form.save() # Reindex the embed video to make sure all tags are indexed search_index.insert_or_update_object(embed_video) messages.success( request, _("Video '{0}' added.").format(embed_video.title), buttons=[ messages.button( reverse( 'wagtail_embed_videos:edit', args=(embed_video.id,) ), _('Edit') ) ] ) return redirect('wagtail_embed_videos:index') else: messages.error(request, _("The video could not be created due to errors.")) else: form = EmbedVideoForm(user=request.user) return render(request, "wagtail_embed_videos/embed_videos/add.html", { 'form': form, })
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 edit(request, group_id): group = get_object_or_404(Group, id=group_id) GroupPagePermissionFormSet = inlineformset_factory( Group, GroupPagePermission, formset=BaseGroupPagePermissionFormSet, extra=0 ) if request.POST: form = GroupForm(request.POST, instance=group) formset = GroupPagePermissionFormSet(request.POST, instance=group) if form.is_valid() and formset.is_valid(): group = form.save() formset.save() messages.success(request, _("Group '{0}' updated.").format(group), buttons = [ messages.button(reverse('wagtailusers_groups_edit', args=(group.id,)), _('Edit')) ]) return redirect('wagtailusers_groups_index') else: messages.error(request, _("The group could not be saved due to errors.")) else: form = GroupForm(instance=group) formset = GroupPagePermissionFormSet(instance=group) return render(request, 'wagtailusers/groups/edit.html', { 'group': group, 'form': form, 'formset': formset, })
def add(request): ImageModel = get_image_model() ImageForm = get_image_form(ImageModel) if request.POST: image = ImageModel(uploaded_by_user=request.user) form = ImageForm(request.POST, request.FILES, instance=image) if form.is_valid(): # Set image file size image.file_size = image.file.size form.save() # Reindex the image to make sure all tags are indexed for backend in get_search_backends(): backend.add(image) messages.success(request, _("Image '{0}' added.").format(image.title), buttons=[ messages.button( reverse('wagtailimages:edit', args=(image.id, )), _('Edit')) ]) return redirect('wagtailimages:index') else: messages.error(request, _("The image could not be created due to errors.")) else: form = ImageForm() return render(request, "wagtailimages/images/add.html", { 'form': form, })
def edit(request, image_id): Image = get_image_model() ImageForm = get_image_form(Image) image = get_object_or_404(Image, id=image_id) if not image.is_editable_by_user(request.user): raise PermissionDenied if request.POST: original_file = image.file form = ImageForm(request.POST, request.FILES, instance=image) if form.is_valid(): if 'file' in form.changed_data: # if providing a new image file, delete the old one and all renditions. # NB Doing this via original_file.delete() clears the file field, # which definitely isn't what we want... original_file.storage.delete(original_file.name) image.renditions.all().delete() # Set new image file size image.file_size = image.file.size form.save() # Reindex the image to make sure all tags are indexed for backend in get_search_backends(): backend.add(image) messages.success(request, _("Image '{0}' updated.").format(image.title), buttons=[ messages.button(reverse('wagtailimages:edit', args=(image.id,)), _('Edit again')) ]) return redirect('wagtailimages:index') else: messages.error(request, _("The image could not be saved due to errors.")) else: form = ImageForm(instance=image) # Check if we should enable the frontend url generator try: reverse('wagtailimages_serve', args=('foo', '1', 'bar')) url_generator_enabled = True except NoReverseMatch: url_generator_enabled = False if image.is_stored_locally(): # Give error if image file doesn't exist if not os.path.isfile(image.file.path): messages.error(request, _( "The source image file could not be found. Please change the source or delete the image." ).format(image.title), buttons=[ messages.button(reverse('wagtailimages:delete', args=(image.id,)), _('Delete')) ]) return render(request, "wagtailimages/images/edit.html", { 'image': image, 'form': form, 'url_generator_enabled': url_generator_enabled, 'filesize': image.get_file_size(), })
def permission_denied(request): """Return a standard 'permission denied' response""" from wagtail.wagtailadmin import messages messages.error(request, _('Sorry, you do not have permission to access this area.')) return redirect('wagtailadmin_home')
def reject_moderation(request, revision_id): revision = get_object_or_404(PageRevision, id=revision_id) if not revision.page.permissions_for_user(request.user).can_publish(): raise PermissionDenied if not revision.submitted_for_moderation: messages.error( request, _("The page '{0}' is not currently awaiting moderation.").format( revision.page.title)) return redirect('wagtailadmin_home') if request.method == 'POST': revision.reject_moderation() messages.success(request, _("Page '{0}' rejected for publication.").format( revision.page.title), buttons=[ messages.button( reverse('wagtailadmin_pages_edit', args=(revision.page.id, )), _('Edit')) ]) send_notification(revision.id, 'rejected', request.user.id) return redirect('wagtailadmin_home')
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 edit(request, redirect_id): theredirect = get_object_or_404(models.Redirect, id=redirect_id) if not permission_policy.user_has_permission_for_instance( request.user, 'change', theredirect): return permission_denied(request) if request.POST: form = RedirectForm(request.POST, request.FILES, instance=theredirect) if form.is_valid(): form.save() messages.success( request, _("Redirect '{0}' updated.").format(theredirect.title), buttons=[ messages.button( reverse('wagtailredirects:edit', args=(theredirect.id, )), _('Edit')) ]) return redirect('wagtailredirects:index') else: messages.error(request, _("The redirect could not be saved due to errors.")) else: form = RedirectForm(instance=theredirect) return render( request, "wagtailredirects/edit.html", { 'redirect': theredirect, 'form': form, 'user_can_delete': permission_policy.user_has_permission(request.user, 'delete'), })
def add(request, media_type): Media = get_media_model() MediaForm = get_media_form(Media) if request.POST: media = Media(uploaded_by_user=request.user, type=media_type) form = MediaForm(request.POST, request.FILES, instance=media, user=request.user) if form.is_valid(): form.save() # Reindex the media entry to make sure all tags are indexed for backend in get_search_backends(): backend.add(media) messages.success(request, _("Media file '{0}' added.").format(media.title), buttons=[ messages.button(reverse('wagtailmedia:edit', args=(media.id,)), _('Edit')) ]) return redirect('wagtailmedia:index') else: messages.error(request, _("The media file could not be saved due to errors.")) else: media = Media(uploaded_by_user=request.user, type=media_type) form = MediaForm(user=request.user, instance=media) return render(request, "wagtailmedia/media/add.html", { 'form': form, 'media_type': media_type, })
def create(request): GroupPagePermissionFormSet = inlineformset_factory( Group, GroupPagePermission, formset=BaseGroupPagePermissionFormSet, extra=0 ) if request.POST: form = GroupForm(request.POST) formset = GroupPagePermissionFormSet(request.POST) if form.is_valid() and formset.is_valid(): group = form.save() formset.instance = group formset.save() messages.success(request, _("Group '{0}' created.").format(group), buttons = [ messages.button(reverse('wagtailusers_groups_edit', args=(group.id,)), _('Edit')) ]) return redirect('wagtailusers_groups_index') else: messages.error(request, _("The group could not be created due to errors.")) else: form = GroupForm() formset = GroupPagePermissionFormSet() return render(request, 'wagtailusers/groups/create.html', { 'form': form, 'formset': formset, })
def post(self, request, *args, **kwargs): if not request.user.email: messages.error( request, _( "Your email address is not configured. " "Please update it before exporting.")) return redirect(request.path) drf__date_joined__gte = request.GET.get('drf__date_joined__gte') drf__date_joined__lte = request.GET.get('drf__date_joined__lte') is_active_exact = request.GET.get('is_active__exact') filter_list = { 'date_joined__range': (drf__date_joined__gte, drf__date_joined__lte) if drf__date_joined__gte and drf__date_joined__lte else None, 'is_active': is_active_exact } arguments = {} for key, value in filter_list.items(): if value: arguments[key] = value self.send_export_email_to_celery(request.user.email, arguments) messages.success(request, _( "CSV emailed to '{0}'").format(request.user.email)) return redirect(request.path)
def add(request): theredirect = models.Redirect() form_class = REDIRECT_EDIT_HANDLER.get_form_class(models.Redirect) if request.POST: form = form_class(request.POST, request.FILES) if form.is_valid(): theredirect = form.save() messages.success( request, _("Redirect '{0}' added.").format(theredirect.title), buttons=[ messages.button( reverse('wagtailredirects:edit', args=(theredirect.id, )), _('Edit')) ]) return redirect('wagtailredirects:index') else: messages.error( request, _("The redirect could not be created due to errors.")) edit_handler = REDIRECT_EDIT_HANDLER(instance=theredirect, form=form) else: form = form_class() edit_handler = REDIRECT_EDIT_HANDLER(instance=theredirect, form=form) return render(request, "wagtailredirects/add.html", { 'edit_handler': edit_handler, })
def edit(request, site_id): site = get_object_or_404(Site, id=site_id) if request.POST: form = SiteForm(request.POST, instance=site) if form.is_valid(): site = form.save() messages.success(request, _("Site '{0}' updated.").format(site.hostname), buttons=[ messages.button( reverse('wagtailsites_edit', args=(site.id, )), _('Edit')) ]) return redirect('wagtailsites_index') else: messages.error(request, _("The site could not be saved due to errors.")) else: form = SiteForm(instance=site) return render(request, 'wagtailsites/edit.html', { 'site': site, 'form': form, })
def add(request): if request.POST: # Get query query_form = search_forms.QueryForm(request.POST) if query_form.is_valid(): query = Query.get(query_form['query_string'].value()) # Save search picks searchpicks_formset = forms.SearchPromotionsFormSet(request.POST, instance=query) if save_searchpicks(query, query, searchpicks_formset): messages.success(request, _("Editor's picks for '{0}' created.").format(query), buttons=[ messages.button(reverse('wagtailsearchpromotions:edit', args=(query.id,)), _('Edit')) ]) return redirect('wagtailsearchpromotions:index') else: if len(searchpicks_formset.non_form_errors()): # formset level error (e.g. no forms submitted) messages.error(request, " ".join(error for error in searchpicks_formset.non_form_errors())) else: # specific errors will be displayed within form fields messages.error(request, _("Recommendations have not been created due to errors")) else: searchpicks_formset = forms.SearchPromotionsFormSet() else: query_form = search_forms.QueryForm() searchpicks_formset = forms.SearchPromotionsFormSet() return render(request, 'wagtailsearchpromotions/add.html', { 'query_form': query_form, 'searchpicks_formset': searchpicks_formset, })
def add(request): ImageModel = get_image_model() ImageForm = get_image_form(ImageModel) if request.POST: image = ImageModel(uploaded_by_user=request.user) form = ImageForm(request.POST, request.FILES, instance=image) if form.is_valid(): # Set image file size image.file_size = image.file.size form.save() # Reindex the image to make sure all tags are indexed for backend in get_search_backends(): backend.add(image) messages.success(request, _("Image '{0}' added.").format(image.title), buttons=[ messages.button(reverse('wagtailimages:edit', args=(image.id,)), _('Edit')) ]) return redirect('wagtailimages:index') else: messages.error(request, _("The image could not be created due to errors.")) else: form = ImageForm() return render(request, "wagtailimages/images/add.html", { 'form': form, })
def edit(request, redirect_id): theredirect = get_object_or_404(models.Redirect, id=redirect_id) if not permission_policy.user_has_permission_for_instance(request.user, "change", theredirect): return permission_denied(request) if request.POST: form = RedirectForm(request.POST, request.FILES, instance=theredirect) if form.is_valid(): form.save() messages.success( request, _("Redirect '{0}' updated.").format(theredirect.title), buttons=[messages.button(reverse("wagtailredirects:edit", args=(theredirect.id,)), _("Edit"))], ) return redirect("wagtailredirects:index") else: messages.error(request, _("The redirect could not be saved due to errors.")) else: form = RedirectForm(instance=theredirect) return render( request, "wagtailredirects/edit.html", { "redirect": theredirect, "form": form, "user_can_delete": permission_policy.user_has_permission(request.user, "delete"), }, )
def approve_moderation(request, revision_id): revision = get_object_or_404(PageRevision, id=revision_id) if not revision.page.permissions_for_user(request.user).can_publish(): raise PermissionDenied if not revision.submitted_for_moderation: messages.error( request, _("The page '{0}' is not currently awaiting moderation.").format(revision.page.get_admin_display_title()), ) return redirect("wagtailadmin_home") if request.method == "POST": revision.approve_moderation() messages.success( request, _("Page '{0}' published.").format(revision.page.get_admin_display_title()), buttons=[ messages.button(revision.page.url, _("View live")), messages.button(reverse("wagtailadmin_pages:edit", args=(revision.page.id,)), _("Edit")), ], ) if not send_notification(revision.id, "approved", request.user.pk): messages.error(request, _("Failed to send approval notifications")) return redirect("wagtailadmin_home")
def add(request): if request.POST: doc = Document(uploaded_by_user=request.user) form = DocumentForm(request.POST, request.FILES, instance=doc) if form.is_valid(): form.save() # Reindex the document to make sure all tags are indexed for backend in get_search_backends(): backend.add(doc) messages.success(request, _("Document '{0}' added.").format(doc.title), buttons=[ messages.button( reverse('wagtaildocs:edit', args=(doc.id, )), _('Edit')) ]) return redirect('wagtaildocs:index') else: messages.error(request, _("The document could not be saved due to errors.")) else: form = DocumentForm() return render(request, "wagtaildocs/documents/add.html", { 'form': form, })
def add(request): Document = get_document_model() DocumentForm = get_document_form(Document) if request.POST: doc = Document(uploaded_by_user=request.user) form = DocumentForm(request.POST, request.FILES, instance=doc, user=request.user) if form.is_valid(): form.save() # Reindex the document to make sure all tags are indexed for backend in get_search_backends(): backend.add(doc) messages.success(request, _("Document '{0}' added.").format(doc.title), buttons=[ messages.button(reverse('wagtaildocs:edit', args=(doc.id,)), _('Edit')) ]) return redirect('wagtaildocs:index') else: messages.error(request, _("The document could not be saved due to errors.")) else: form = DocumentForm(user=request.user) return render(request, "wagtaildocs/documents/add.html", { 'form': form, })
def add(request): EmbedVideoModel = get_embed_video_model() EmbedVideoForm = get_embed_video_form(EmbedVideoModel) if request.POST: embed_video = EmbedVideoModel(uploaded_by_user=request.user) form = EmbedVideoForm(request.POST, request.FILES, instance=embed_video) if form.is_valid(): form.save() # Reindex the embed video to make sure all tags are indexed for backend in get_search_backends(): backend.add(embed_video) messages.success( request, _("Video '{0}' added.").format(embed_video.title), buttons=[ messages.button( reverse('wagtail_embed_videos_edit_embed_video', args=(embed_video.id, )), _('Edit')) ]) return redirect('wagtail_embed_videos_index') else: messages.error(request, _("The video could not be created due to errors.")) else: form = EmbedVideoForm() return render(request, "wagtail_embed_videos/embed_videos/add.html", { 'form': form, })
def add(request): theredirect = models.Redirect() form_class = REDIRECT_EDIT_HANDLER.get_form_class(models.Redirect) if request.POST: form = form_class(request.POST, request.FILES) if form.is_valid(): theredirect = form.save(commit=False) theredirect.site = request.site theredirect.save() messages.success(request, _("Redirect '{0}' added.").format(theredirect.title), buttons=[ messages.button(reverse('wagtailredirects_edit_redirect', args=(theredirect.id,)), _('Edit')) ]) return redirect('wagtailredirects_index') else: messages.error(request, _("The redirect could not be created due to errors.")) edit_handler = REDIRECT_EDIT_HANDLER(instance=theredirect, form=form) else: form = form_class() edit_handler = REDIRECT_EDIT_HANDLER(instance=theredirect, form=form) return render(request, "wagtailredirects/add.html", { 'edit_handler': edit_handler, })
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 edit(request, document_id): doc = get_object_or_404(Document, id=document_id) if not doc.is_editable_by_user(request.user): raise PermissionDenied if request.POST: original_file = doc.file form = DocumentForm(request.POST, request.FILES, instance=doc) if form.is_valid(): if 'file' in form.changed_data: # if providing a new document file, delete the old one. # NB Doing this via original_file.delete() clears the file field, # which definitely isn't what we want... original_file.storage.delete(original_file.name) doc = form.save() # Reindex the document to make sure all tags are indexed for backend in get_search_backends(): backend.add(doc) messages.success(request, _("Document '{0}' updated").format(doc.title), buttons=[ messages.button( reverse('wagtaildocs:edit', args=(doc.id, )), _('Edit')) ]) return redirect('wagtaildocs:index') else: messages.error(request, _("The document could not be saved due to errors.")) else: form = DocumentForm(instance=doc) filesize = None # Get file size when there is a file associated with the Document object if doc.file: try: filesize = doc.file.size except OSError: # File doesn't exist pass if not filesize: messages.error( request, _("The file could not be found. Please change the source or delete the document" ), buttons=[ messages.button(reverse('wagtaildocs:delete', args=(doc.id, )), _('Delete')) ]) return render(request, "wagtaildocs/documents/edit.html", { 'document': doc, 'filesize': filesize, 'form': form })
def edit(request, media_id): Media = get_media_model() MediaForm = get_media_form(Media) media = get_object_or_404(Media, id=media_id) if not permission_policy.user_has_permission_for_instance(request.user, 'change', media): return permission_denied(request) if request.POST: original_file = media.file form = MediaForm(request.POST, request.FILES, instance=media, user=request.user) if form.is_valid(): if 'file' in form.changed_data: # if providing a new media file, delete the old one. # NB Doing this via original_file.delete() clears the file field, # which definitely isn't what we want... original_file.storage.delete(original_file.name) media = form.save() # Reindex the media entry to make sure all tags are indexed for backend in get_search_backends(): backend.add(media) messages.success(request, _("Media file '{0}' updated").format(media.title), buttons=[ messages.button(reverse('wagtailmedia:edit', args=(media.id,)), _('Edit')) ]) return redirect('wagtailmedia:index') else: messages.error(request, _("The media could not be saved due to errors.")) else: form = MediaForm(instance=media, user=request.user) filesize = None # Get file size when there is a file associated with the Media object if media.file: try: filesize = media.file.size except OSError: # File doesn't exist pass if not filesize: messages.error( request, _("The file could not be found. Please change the source or delete the media file"), buttons=[messages.button(reverse('wagtailmedia:delete', args=(media.id,)), _('Delete'))] ) return render(request, "wagtailmedia/media/edit.html", { 'media': media, 'filesize': filesize, 'form': form, 'user_can_delete': permission_policy.user_has_permission_for_instance( request.user, 'delete', media ), })
def edit(request, document_id): Document = get_document_model() DocumentForm = get_document_form(Document) doc = get_object_or_404(Document, id=document_id) if not permission_policy.user_has_permission_for_instance(request.user, 'change', doc): return permission_denied(request) if request.POST: original_file = doc.file form = DocumentForm(request.POST, request.FILES, instance=doc, user=request.user) if form.is_valid(): if 'file' in form.changed_data: # if providing a new document file, delete the old one. # NB Doing this via original_file.delete() clears the file field, # which definitely isn't what we want... original_file.storage.delete(original_file.name) doc = form.save() # Reindex the document to make sure all tags are indexed for backend in get_search_backends(): backend.add(doc) messages.success(request, _("Document '{0}' updated").format(doc.title), buttons=[ messages.button(reverse('wagtaildocs:edit', args=(doc.id,)), _('Edit')) ]) return redirect('wagtaildocs:index') else: messages.error(request, _("The document could not be saved due to errors.")) else: form = DocumentForm(instance=doc, user=request.user) filesize = None # Get file size when there is a file associated with the Document object if doc.file: try: filesize = doc.file.size except OSError: # File doesn't exist pass if not filesize: messages.error( request, _("The file could not be found. Please change the source or delete the document"), buttons=[messages.button(reverse('wagtaildocs:delete', args=(doc.id,)), _('Delete'))] ) return render(request, "wagtaildocs/documents/edit.html", { 'document': doc, 'filesize': filesize, 'form': form, 'user_can_delete': permission_policy.user_has_permission_for_instance( request.user, 'delete', doc ), })
def edit(request, document_id): Document = get_document_model() DocumentForm = get_document_form(Document) doc = get_object_or_404(Document, id=document_id) if not permission_policy.user_has_permission_for_instance(request.user, 'change', doc): return permission_denied(request) if request.method == 'POST': original_file = doc.file form = DocumentForm(request.POST, request.FILES, instance=doc, user=request.user) if form.is_valid(): doc = form.save() if 'file' in form.changed_data: # if providing a new document file, delete the old one. # NB Doing this via original_file.delete() clears the file field, # which definitely isn't what we want... original_file.storage.delete(original_file.name) # Reindex the document to make sure all tags are indexed search_index.insert_or_update_object(doc) messages.success(request, _("Document '{0}' updated").format(doc.title), buttons=[ messages.button(reverse('wagtaildocs:edit', args=(doc.id,)), _('Edit')) ]) return redirect('wagtaildocs:index') else: messages.error(request, _("The document could not be saved due to errors.")) else: form = DocumentForm(instance=doc, user=request.user) filesize = None # Get file size when there is a file associated with the Document object if doc.file: try: filesize = doc.file.size except OSError: # File doesn't exist pass if not filesize: messages.error( request, _("The file could not be found. Please change the source or delete the document"), buttons=[messages.button(reverse('wagtaildocs:delete', args=(doc.id,)), _('Delete'))] ) return render(request, "wagtaildocs/documents/edit.html", { 'document': doc, 'filesize': filesize, 'form': form, 'user_can_delete': permission_policy.user_has_permission_for_instance( request.user, 'delete', doc ), })
def edit(request, image_id): Image = get_image_model() ImageForm = get_image_form(Image) image = get_object_or_404(Image, id=image_id) if not image.is_editable_by_user(request.user): raise PermissionDenied if request.POST: original_file = image.file form = ImageForm(request.POST, request.FILES, instance=image) if form.is_valid(): if 'file' in form.changed_data: # if providing a new image file, delete the old one and all renditions. # NB Doing this via original_file.delete() clears the file field, # which definitely isn't what we want... original_file.storage.delete(original_file.name) image.renditions.all().delete() form.save() # Reindex the image to make sure all tags are indexed for backend in get_search_backends(): backend.add(image) messages.success(request, _("Image '{0}' updated.").format(image.title), buttons=[ messages.button(reverse('wagtailimages_edit_image', args=(image.id,)), _('Edit again')) ]) return redirect('wagtailimages_index') else: messages.error(request, _("The image could not be saved due to errors.")) else: form = ImageForm(instance=image) # Check if we should enable the frontend url generator try: reverse('wagtailimages_serve', args=('foo', '1', 'bar')) url_generator_enabled = True except NoReverseMatch: url_generator_enabled = False # Get file size try: filesize = image.file.size except OSError: # File doesn't exist filesize = None messages.error(request, _("The source image file could not be found. Please change the source or delete the image.").format(image.title), buttons=[ messages.button(reverse('wagtailimages_delete_image', args=(image.id,)), _('Delete')) ]) return render(request, "wagtailimages/images/edit.html", { 'image': image, 'form': form, 'url_generator_enabled': url_generator_enabled, 'filesize': filesize, })
def decorated_view(request, *args, **kwargs): user = request.user if user.is_anonymous(): return redirect_to_login(request) if user.has_perms(['wagtailadmin.access_admin']): return view_func(request, *args, **kwargs) messages.error(request, _('You do not have permission to access the admin')) return redirect_to_login(request)
def edit(request, content_type_app_name, content_type_model_name, id): content_type = get_content_type_from_url_params(content_type_app_name, content_type_model_name) model = content_type.model_class() permission = get_permission_name("change", model) if not request.user.has_perm(permission): return permission_denied(request) snippet_type_name = get_snippet_type_name(content_type)[0] instance = get_object_or_404(model, id=id) edit_handler_class = get_snippet_edit_handler(model) form_class = edit_handler_class.get_form_class(model) if request.POST: form = form_class(request.POST, request.FILES, instance=instance) if form.is_valid(): form.save() messages.success( request, _("{snippet_type} '{instance}' updated.").format( snippet_type=capfirst(snippet_type_name), instance=instance ), buttons=[ messages.button( reverse( "wagtailsnippets:edit", args=(content_type_app_name, content_type_model_name, instance.id) ), _("Edit"), ) ], ) return redirect("wagtailsnippets:list", content_type.app_label, content_type.model) else: messages.error(request, _("The snippet could not be saved due to errors.")) edit_handler = edit_handler_class(instance=instance, form=form) else: form = form_class(instance=instance) edit_handler = edit_handler_class(instance=instance, form=form) return render( request, "wagtailsnippets/snippets/edit.html", { "content_type": content_type, "snippet_type_name": snippet_type_name, "instance": instance, "edit_handler": edit_handler, }, )
def form_invalid(self, form): """ Processes an invalid form submittal. :param form: the form instance. :rtype: django.http.HttpResponse. """ meta = getattr(self.model, "_meta") # noinspection PyUnresolvedReferences messages.error(self.request, _(u"The {0} could not be saved due to errors.").format(meta.verbose_name.lower())) return super(BaseEditView, self).form_invalid(form)
def post(self, request, instance_id): self.instance = get_object_or_404(self.get_queryset(), id=instance_id) self.form = self.form_class(request.POST, instance=self.instance) if self.form.is_valid(): self.save_instance() messages.success(request, self.success_message.format(self.instance), buttons=[ messages.button(reverse(self.edit_url_name, args=(self.instance.id,)), _('Edit')) ]) return redirect(self.index_url_name) else: messages.error(request, self.error_message) return self.render_to_response()
def edit(request, video_id): VideoForm = get_video_form(Video) video = get_object_or_404(Video, id=video_id) if request.POST: original_file = video.file form = VideoForm(request.POST, request.FILES, instance=video) if form.is_valid(): if 'file' in form.changed_data: # if providing a new video file, delete the old one and all renditions. # NB Doing this via original_file.delete() clears the file field, # which definitely isn't what we want... original_file.storage.delete(original_file.name) # Set new video file size video.file_size = video.file.size video = form.save() video.save() # Reindex the image to make sure all tags are indexed for backend in get_search_backends(): backend.add(video) messages.success(request, _("Video '{0}' updated.").format(video.title), buttons=[ messages.button(reverse('wagtailvideos:edit', args=(video.id,)), _('Edit again')) ]) return redirect('wagtailvideos:index') else: messages.error(request, _("The video could not be saved due to errors.")) else: form = VideoForm(instance=video) if not video._meta.get_field('file').storage.exists(video.file.name): # Give error if image file doesn't exist messages.error(request, _( "The source video file could not be found. Please change the source or delete the video." ).format(video.title), buttons=[ messages.button(reverse('wagtailvideos:delete', args=(video.id,)), _('Delete')) ]) return render(request, "wagtailvideos/videos/edit.html", { 'video': video, 'form': form, 'filesize': video.get_file_size(), 'can_transcode': ffmpeg.installed(), 'transcodes': video.transcodes.all(), 'transcode_form': VideoTranscodeAdminForm(video=video), 'user_can_delete': permission_policy.user_has_permission_for_instance(request.user, 'delete', video) })
def edit(request, app_name, model_name, site_pk): model = get_model_from_url_params(app_name, model_name) if not user_can_edit_setting_type(request.user, model): raise PermissionDenied site = get_object_or_404(Site, pk=site_pk) setting_type_name = model._meta.verbose_name instance = model.for_site(site) edit_handler_class = get_setting_edit_handler(model) form_class = edit_handler_class.get_form_class(model) if request.method == 'POST': form = form_class(request.POST, request.FILES, instance=instance) if form.is_valid(): form.save() messages.success( request, _("{setting_type} updated.").format( setting_type=capfirst(setting_type_name), instance=instance)) return redirect('wagtailsettings:edit', app_name, model_name, site.pk) else: messages.error(request, _("The setting could not be saved due to errors.")) edit_handler = edit_handler_class(instance=instance, form=form) else: form = form_class(instance=instance) edit_handler = edit_handler_class(instance=instance, form=form) # Show a site switcher form if there are multiple sites site_switcher = None if Site.objects.count() > 1: site_switcher = SiteSwitchForm(site, model) return render( request, 'wagtailsettings/edit.html', { 'opts': model._meta, 'setting_type_name': setting_type_name, 'instance': instance, 'edit_handler': edit_handler, 'form': form, 'site': site, 'site_switcher': site_switcher, 'tabbed': edit_handler_class.__name__ == '_TabbedInterface', })
def edit(request, content_type_app_name, content_type_model_name, id): content_type = get_content_type_from_url_params(content_type_app_name, content_type_model_name) if not user_can_edit_snippet_type(request.user, content_type): raise PermissionDenied model = content_type.model_class() snippet_type_name = get_snippet_type_name(content_type)[0] instance = get_object_or_404(model, id=id) edit_handler_class = get_snippet_edit_handler(model) form_class = edit_handler_class.get_form_class(model) if request.POST: form = form_class(request.POST, request.FILES, instance=instance) if form.is_valid(): form.save() messages.success(request, _("{snippet_type} '{instance}' updated.").format( snippet_type=capfirst(snippet_type_name), instance=instance), buttons=[ messages.button( reverse('wagtailsnippets_edit', args=( content_type_app_name, content_type_model_name, instance.id, )), _('Edit')) ]) return redirect('wagtailsnippets_list', content_type.app_label, content_type.model) else: messages.error(request, _("The snippet could not be saved due to errors.")) edit_handler = edit_handler_class(instance=instance, form=form) else: form = form_class(instance=instance) edit_handler = edit_handler_class(instance=instance, form=form) return render( request, 'wagtailsnippets/snippets/edit.html', { 'content_type': content_type, 'snippet_type_name': snippet_type_name, 'instance': instance, 'edit_handler': edit_handler })
def create(request, content_type_app_name, content_type_model_name): content_type = get_content_type_from_url_params(content_type_app_name, content_type_model_name) model = content_type.model_class() permission = get_permission_name('add', model) if not request.user.has_perm(permission): return permission_denied(request) snippet_type_name = get_snippet_type_name(content_type)[0] instance = model() edit_handler_class = get_snippet_edit_handler(model) form_class = edit_handler_class.get_form_class(model) if request.POST: form = form_class(request.POST, request.FILES, instance=instance) if form.is_valid(): form.save() messages.success(request, _("{snippet_type} '{instance}' created.").format( snippet_type=capfirst( get_snippet_type_name(content_type)[0]), instance=instance), buttons=[ messages.button( reverse('wagtailsnippets:edit', args=(content_type_app_name, content_type_model_name, instance.id)), _('Edit')) ]) return redirect('wagtailsnippets:list', content_type.app_label, content_type.model) else: messages.error( request, _("The snippet could not be created due to errors.")) edit_handler = edit_handler_class(instance=instance, form=form) else: form = form_class(instance=instance) edit_handler = edit_handler_class(instance=instance, form=form) return render( request, 'wagtailsnippets/snippets/create.html', { 'content_type': content_type, 'snippet_type_name': snippet_type_name, 'edit_handler': edit_handler, })
def decorated_view(request, *args, **kwargs): user = request.user if user_is_anonymous(user): return redirect_to_login(request) if user.has_perms(['wagtailadmin.access_admin']): if hasattr(user, 'wagtail_userprofile'): activate_lang( user.wagtail_userprofile.get_preferred_language()) return view_func(request, *args, **kwargs) messages.error(request, _('You do not have permission to access the admin')) return redirect_to_login(request)
def decorated_view(request, *args, **kwargs): user = request.user if user_is_anonymous(user): return reject_request(request) if user.has_perms(['wagtailadmin.access_admin']): if hasattr(user, 'wagtail_userprofile'): activate_lang(user.wagtail_userprofile.get_preferred_language()) return view_func(request, *args, **kwargs) if not request.is_ajax(): messages.error(request, _('You do not have permission to access the admin')) return reject_request(request)
def reject_moderation(request, revision_id): revision = get_object_or_404(PageRevision, id=revision_id) if not revision.page.permissions_for_user(request.user).can_publish(): raise PermissionDenied if not revision.submitted_for_moderation: messages.error(request, _("The page '{0}' is not currently awaiting moderation.").format(revision.page.title)) return redirect('wagtailadmin_home') if request.method == 'POST': revision.reject_moderation() messages.success(request, _("Page '{0}' rejected for publication.").format(revision.page.title)) send_notification(revision.id, 'rejected', request.user.id) return redirect('wagtailadmin_home')