コード例 #1
0
    def run_command_view(self, request, url_name):
        admin_command = core.get_admin_commands()[url_name]

        full_permission_codename = 'admincommand.%s' % admin_command.permission_codename()
        if not request.user.has_perm(full_permission_codename):
            return HttpResponseForbidden()
        # original needed ``change_form`` context variables
        opts = self.model._meta
        app_label = opts.app_label

        help = admin_command.get_help()

        ctx = {
            # original needed ``change_form.html`` context variables
            'module_name': force_unicode(opts.verbose_name_plural),
            'title': admin_command.name(),
            'is_popup': False,
            'root_path': None,
            'app_label': app_label,

            # ``run.html`` context
            'command_name': admin_command.name,
            'help': help,
        }

        if request.method == 'POST':
            form = admin_command.form(request.POST)
            if form.is_valid():
                coreponse = core.run_command(
                    admin_command,
                    form.cleaned_data,
                    request.user
                )
                if not admin_command.asynchronous:
                    ctx['output'] = coreponse
                    return render(request, 'admincommand/output.html', ctx)
                else:
                    msg = 'Task is set to run in the next 5 minutes or less'
                    msg += ' if by any luck, the task went with a duck'
                    msg += " and did not achieve it's duty, ask for help"
                    msg = ugettext(msg)
                    messages.info(request, msg)
                path = reverse('admin:admincommand_admincommand_changelist')
                return HttpResponseRedirect(path)
            else:
                ctx['form'] = form
        else:
            ctx['form'] = admin_command.form()
        ctx['media'] = mark_safe(ctx['form'].media.render())
        return render(request, 'admincommand/run.html', ctx)
コード例 #2
0
    def run_command_view(self, request, url_name):
        admin_command = core.get_admin_commands()[url_name]

        full_permission_codename = "admincommand.%s" % admin_command.permission_codename(
        )
        if not request.user.has_perm(full_permission_codename):
            return HttpResponseForbidden()
        # original needed ``change_form`` context variables
        opts = self.model._meta
        app_label = opts.app_label

        help = admin_command.get_help()

        ctx = {
            # original needed ``change_form.html`` context variables
            "module_name": force_text(opts.verbose_name_plural),
            "title": admin_command.name(),
            "is_popup": False,
            "root_path": None,
            "app_label": app_label,
            # ``run.html`` context
            "command_name": admin_command.name,
            "help": help,
        }

        if request.method == "POST":
            form = admin_command.form(request.POST,
                                      request.FILES,
                                      command=admin_command)
            if form.is_valid():
                coreponse = core.run_command(admin_command, form.cleaned_data,
                                             request.user)
                if not admin_command.asynchronous:
                    ctx["output"] = coreponse
                    return render(request, "admincommand/output.html", ctx)
                else:
                    msg = ugettext(
                        "Task is set to run in the next 5 minutes or less. If by any luck, the task went with a duck "
                        "and did not achieve it's duty, ask for help")
                    messages.info(request, msg)
                path = reverse("admin:admincommand_admincommand_changelist")
                return HttpResponseRedirect(path)
            else:
                ctx["form"] = form
        else:
            ctx["form"] = admin_command.form(command=admin_command)
        ctx["media"] = mark_safe(ctx["form"].media.render())
        return render(request, "admincommand/run.html", ctx)
コード例 #3
0
ファイル: admin.py プロジェクト: imt-salt/django-admincommand
    def run_command_view(self, request, url_name):
        admin_command = core.get_admin_commands()[url_name]

        full_permission_codename = 'admincommand.%s' % admin_command.permission_codename(
        )
        if not request.user.has_perm(full_permission_codename):
            return HttpResponseForbidden()
        # original needed ``change_form`` context variables
        opts = self.model._meta
        app_label = opts.app_label

        help = admin_command.get_help()

        ctx = {
            # original needed ``change_form.html`` context variables
            'module_name': force_unicode(opts.verbose_name_plural),
            'title': admin_command.name(),
            'is_popup': False,
            'root_path': None,
            'app_label': app_label,

            # ``run.html`` context
            'command_name': admin_command.name,
            'help': help,
        }

        if request.method == 'POST':
            form = admin_command.form(request.POST)
            if form.is_valid():
                coreponse = core.run_command(admin_command, form.cleaned_data,
                                             request.user)
                if not admin_command.asynchronous:
                    ctx['output'] = coreponse
                    return render(request, 'admincommand/output.html', ctx)
                else:
                    msg = 'Task is set to run in the next 5 minutes or less'
                    msg += ' if by any luck, the task went with a duck'
                    msg += " and did not achieve it's duty, ask for help"
                    msg = ugettext(msg)
                    messages.info(request, msg)
                path = reverse('admin:admincommand_admincommand_changelist')
                return HttpResponseRedirect(path)
            else:
                ctx['form'] = form
        else:
            ctx['form'] = admin_command.form()
        ctx['media'] = mark_safe(ctx['form'].media.render())
        return render(request, 'admincommand/run.html', ctx)