def create_redirects_from_dataset(dataset, config): errors = [] successes = 0 total = 0 for row in dataset: total += 1 from_link = row[config["from_index"]] to_link = row[config["to_index"]] data = { "old_path": from_link, "redirect_link": to_link, "is_permanent": config["permanent"], } if config["site"]: data["site"] = config["site"].pk form = RedirectForm(data) if not form.is_valid(): error = to_readable_errors(form.errors.as_text()) errors.append([from_link, to_link, error]) continue form.save() successes += 1 return { "errors": errors, "errors_count": len(errors), "successes": successes, "total": total, }
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, 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 add(request): if request.method == 'POST': form = RedirectForm(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.")) else: form = RedirectForm() return render(request, "wagtailredirects/add.html", { 'form': form, })
def add(request): if request.method == "POST": form = RedirectForm(request.POST, request.FILES) if form.is_valid(): with transaction.atomic(): theredirect = form.save() log(instance=theredirect, action="wagtail.edit") 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.")) else: form = RedirectForm() return TemplateResponse( request, "wagtailredirects/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): raise PermissionDenied if request.method == "POST": form = RedirectForm(request.POST, request.FILES, instance=theredirect) if form.is_valid(): with transaction.atomic(): form.save() log(instance=theredirect, action="wagtail.edit") 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 TemplateResponse( request, "wagtailredirects/edit.html", { "redirect": theredirect, "form": form, "user_can_delete": permission_policy.user_has_permission(request.user, "delete"), }, )
def add(request): if request.method == 'POST': form = RedirectForm(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.")) else: form = RedirectForm() return render(request, "wagtailredirects/add.html", { 'form': form, })
def handle(self, *args, **options): src = options["src"] from_index = options.pop("from") to_index = options.pop("to") site_id = options.pop("site", None) permament = options.pop("permanent") dry_run = options.pop("dry_run", False) or options.pop( "dry-run", False) format_ = options.pop("format", None) ask = options.pop("ask") offset = options.pop("offset") limit = options.pop("limit") errors = [] successes = 0 skipped = 0 total = 0 site = None if site_id: site = Site.objects.get(id=site_id) if not os.path.exists(src): raise Exception("Missing file '{0}'".format(src)) if not os.path.getsize(src) > 0: raise Exception("File '{0}' is empty".format(src)) _, extension = os.path.splitext(src) extension = extension.lstrip(".") if not format_: format_ = extension if not get_format_cls_by_extension(format_): raise Exception("Invalid format '{0}'".format(extension)) if extension in ["xls", "xlsx"]: mode = "rb" else: mode = "r" with open(src, mode) as fh: imported_data = tablib.Dataset().load(fh.read(), format=format_) sample_data = tablib.Dataset( *imported_data[:min(len(imported_data), 4)], headers=imported_data.headers) try: self.stdout.write("Sample data:") self.stdout.write(str(sample_data)) except Exception: self.stdout.write("Warning: Cannot display sample data") self.stdout.write("--------------") if site: self.stdout.write("Using site: {0}".format(site.hostname)) self.stdout.write("Importing redirects:") if offset: imported_data = imported_data[offset:] if limit: imported_data = imported_data[:limit] for row in imported_data: total += 1 from_link = row[from_index] to_link = row[to_index] data = { "old_path": from_link, "redirect_link": to_link, "is_permanent": permament, } if site: data["site"] = site.pk form = RedirectForm(data) if not form.is_valid(): error = form.errors.as_text().replace("\n", "") self.stdout.write( "{}. Error: {} -> {} (Reason: {})".format( total, from_link, to_link, error, )) errors.append(error) continue if ask: answer = get_input( "{}. Found {} -> {} Create? Y/n: ".format( total, from_link, to_link, )) if answer != "Y": skipped += 1 continue else: self.stdout.write("{}. {} -> {}".format( total, from_link, to_link, )) if dry_run: successes += 1 continue form.save() successes += 1 self.stdout.write("\n") self.stdout.write("Found: {}".format(total)) self.stdout.write("Created: {}".format(successes)) self.stdout.write("Skipped : {}".format(skipped)) self.stdout.write("Errors: {}".format(len(errors)))