コード例 #1
0
    def _proccess_request(self, func, _action, ignore_pdf):

        form = None
        f_return = None
        redirect_url = None

        if count_parameters_names(func) > 0:
            form = factory.get_class_action_form(self.request, self.qs.model,
                                                 _action, func)
            if form.is_valid():
                params = []
                for param in get_parameters_names(func):
                    if param in form.cleaned_data:
                        params.append(form.cleaned_data[param])
                try:
                    f_return = func(*params)
                    ignore_pdf = False
                    if f_return is None:
                        redirect_url = '..'
                except ValidationError as e:
                    form.add_error(None, str(e.message))
        else:
            f_return = func()
            ignore_pdf = False
            if f_return is None:
                redirect_url = '.'

        if redirect_url:
            self.request.GET._mutable = True
            if 'ids' in self.request.GET:
                del self.request.GET['ids']
            del self.request.GET[_action['view_name']]
            self.request.GET._mutable = False
            raise ComponentHasResponseException(
                httprr(self.request, redirect_url, _action['message']))
        else:
            action_style = _action['style']
            if f_return is None:
                f_return = dict(form=form)
                template_name = 'default.html'
            else:
                template_name = '{}.html'.format(_action['view_name'])
            return_response(f_return,
                            self.request,
                            None,
                            action_style,
                            template_name,
                            raise_response=True,
                            ignore_pdf=ignore_pdf)
コード例 #2
0
 def receive_function_args(request, *args, **kwargs):
     if can_execute and not permissions.check_group_or_permission(
             request, '{}.{}'.format(get_metadata(model, 'app_label'), func.__name__)):
         return HttpResponseRedirect('/admin/login/')
     f_return = func(request, *args, **kwargs)
     template_name = '{}.html'.format(func.__name__)
     return return_response(f_return, request, verbose_name, style, template_name)
コード例 #3
0
 def receive_function_args(request, *args, **kwargs):
     without_permission = can_view and not permissions.check_group_or_permission(request, can_view)
     without_authentication = login_required and not request.user.is_authenticated
     if without_permission or without_authentication:
             return HttpResponseRedirect('/admin/login/?next={}'.format(url))
     f_return = func(request, *args, **kwargs)
     template_name = template or '{}.html'.format(func.__name__)
     return return_response(f_return, request, verbose_name, style, template_name)
コード例 #4
0
def action(request, app, cls, action_name, pk=None):

    try:
        _model = apps.get_model(app, cls)
    except LookupError as e:
        return page_not_found(request, e, 'error404.html')

    for group in loader.instance_actions[_model]:
        if action_name in loader.instance_actions[_model][group]:
            break

    form_action = loader.instance_actions[_model][group][action_name]
    action_verbose_name = form_action['verbose_name']
    action_can_execute = form_action['can_execute']
    action_condition = form_action['condition']
    action_function = form_action['function']
    action_message = 'message' in form_action and form_action['message'] or None
    action_permission = '{}.{}'.format(_model._meta.app_label,
                                       action_function.__name__)
    action_input = form_action['input']
    action_display = form_action['display']
    action_style = form_action['style']
    action_redirect = form_action['redirect_to']

    obj = pk and _model.objects.all(
        request.user).distinct().get(pk=pk) or _model()
    obj.request = request
    obj._user = request.user
    title = action_verbose_name
    redirect_to = None

    if check_condition(request.user, action_condition,
                       obj) and (not action_can_execute
                                 or permissions.check_group_or_permission(
                                     request, action_permission)):
        f_return = None
        func = getattr(_model, action_function.__name__, action_function)
        form = factory.get_action_form(request, obj, form_action)
        if count_parameters_names(func) > 1 or action_input:
            if form.is_valid():
                if 'instance' in form.fields:
                    obj = form.cleaned_data['instance']
                func = getattr(obj, action_function.__name__, action_function)
                params = []
                for param in get_parameters_names(func,
                                                  include_annotated=True):
                    if param in form.cleaned_data:
                        params.append(form.cleaned_data[param])
                    else:
                        params.append(
                            get_role_value_for_action(func, request.user,
                                                      param))
                try:
                    f_return = func(*params)
                    if not action_redirect:
                        if count_parameters_names(func) > 0 or action_display:
                            redirect_to = '..'
                        else:
                            redirect_to = '.'
                    else:
                        redirect_to = Template(action_redirect).render(
                            Context({'self': obj}))
                except ValidationError as e:
                    form.add_error(None, str(e.message))
        else:
            try:
                if form.fields and form.is_valid() or not form.fields:
                    if 'instance' in form.fields:
                        obj = form.cleaned_data['instance']
                    func = getattr(obj, action_function.__name__,
                                   action_function)
                    params = []
                    for param in get_parameters_names(func,
                                                      include_annotated=True):
                        params.append(
                            get_role_value_for_action(func, request.user,
                                                      param))
                    f_return = func(*params)
                    if not action_redirect:
                        if count_parameters_names(func) > 0 or action_display:
                            redirect_to = '..'
                        else:
                            redirect_to = '.'
                    else:
                        redirect_to = Template(action_redirect).render(
                            Context({'self': obj}))
            except ValidationError as e:
                if form.fields:
                    form.add_error(None, str(e.message))
                return httprr(request, '.', e.message, error=True)

        if f_return:
            template_name = '{}.html'.format(action_function.__name__)
            return return_response(f_return, request, title, action_style,
                                   template_name)

        elif redirect_to:
            return httprr(request, redirect_to, action_message)

        if form.title == _('Form'):
            form.title = action_verbose_name
        if form.submit_label == _('Send'):
            form.submit_label = action_verbose_name
        return render(request, 'default.html', locals())
    else:
        return HttpResponseForbidden()