예제 #1
0
def listt(request, app, cls, subset=None):

    try:
        _model = apps.get_model(app, cls)
    except LookupError as e:
        return page_not_found(request, e, 'error404.html')
    title = get_metadata(_model, 'verbose_name_plural')
    subsetp = None
    list_display = None
    list_filter = None
    search_fields = None
    if subset:
        subset_func = getattr(_model.objects.get_queryset(), subset)
        can_view = subset_func._metadata['{}:can_view'.format(subset)]
        list_display = subset_func._metadata['{}:list_display'.format(subset)]
        list_filter = subset_func._metadata['{}:list_filter'.format(subset)]
        search_fields = subset_func._metadata['{}:search_fields'.format(
            subset)]
        title = '{} - {}'.format(
            title, subset_func._metadata['{}:title'.format(subset)])
    else:
        tid = request.GET.get('tid')
        subsetp = request.GET.get('tab{}'.format(tid))
        if tid and subsetp:
            subset_func = getattr(_model.objects.get_queryset(), subsetp)
            subset_title = subset_func._metadata['{}:title'.format(subsetp)]
            can_view = subset_func._metadata['{}:can_view'.format(subsetp)]
            title = '{} - {}'.format(
                title, subset_func._metadata['{}:title'.format(subsetp)])
            if not permissions.check_group_or_permission(request, can_view):
                return httprr(
                    request,
                    '/admin/login/?next={}'.format(request.get_full_path()))
        else:
            permission = '{}.list_{}'.format(app, cls)
            if not request.user.has_perm(permission):
                return httprr(
                    request,
                    '/admin/login/?next={}'.format(request.get_full_path()))

    qs = _model.objects.all(request.user)
    list_subsets = subset and [subset] or None

    paginator = Paginator(request,
                          qs,
                          title,
                          list_subsets=list_subsets,
                          is_list_view=True,
                          list_display=list_display,
                          list_filter=list_filter,
                          search_fields=search_fields)
    paginator.process_request()

    if _model in loader.class_actions:
        for group in loader.class_actions[_model]:
            for view_name in loader.class_actions[_model][group]:
                _action = loader.class_actions[_model][group][view_name]
                action_title = _action['title']
                action_message = _action['message']
                action_can_execute = _action['can_execute']
                action_input = _action['input']
                action_css = _action['css']
                action_condition = _action['condition']
                initial = _action['initial']
                choices = _action['choices']

                if subsetp:
                    if subsetp not in loader.subset_actions[
                            _model] or view_name not in loader.subset_actions[
                                _model][subsetp]:
                        continue
                else:
                    if subset not in loader.subset_actions[
                            _model] or view_name not in loader.subset_actions[
                                _model][subset]:
                        continue

                if permissions.check_group_or_permission(
                        request, action_can_execute):
                    func = hasattr(qs, view_name) and getattr(
                        qs, view_name) or None
                    if func:

                        char = '?' in request.get_full_path() and '&' or '?'
                        url = '{}{}{}'.format(request.get_full_path(), char,
                                              '{}='.format(view_name))

                        has_input = func.__code__.co_argcount > 1

                        if not has_input:
                            action_css = action_css.replace('popup', '')
                        paginator.add_subset_action(action_title, url,
                                                    action_css, None,
                                                    action_condition)

                        if view_name in request.GET:
                            ids = paginator.get_selected_ids()
                            if ids:
                                qs = paginator.get_queryset(
                                    paginate=False).filter(id__in=ids)
                            else:
                                qs = paginator.get_queryset(paginate=False)

                            func = getattr(qs, view_name)
                            redirect_url = None

                            if has_input:
                                form = factory.get_class_action_form(
                                    request, _model, _action, func)
                                paginator = ''
                                if form.is_valid():
                                    params = []
                                    for param in func.__code__.co_varnames[
                                            1:func.__code__.co_argcount]:
                                        if param in form.cleaned_data:
                                            params.append(
                                                form.cleaned_data[param])
                                    try:
                                        f_return = func(*params)
                                        redirect_url = '..'
                                    except ValidationError as e:
                                        form.add_error(None, str(e.message))
                                if not redirect_url:
                                    return render(request, 'default.html',
                                                  locals())
                            else:
                                f_return = func()
                                redirect_url = '.'

                            if redirect_url:
                                request.GET._mutable = True
                                del request.GET['ids']
                                del request.GET[view_name]
                                request.GET._mutable = False
                            return httprr(request, redirect_url,
                                          action_message)
                    else:
                        url = '/{}/{}/'.format(app, view_name)
                        if view_name in request.GET:
                            return httprr(request, url)
                        else:
                            action_css = action_css.replace('popup', '')
                            paginator.add_action(action_title, url, action_css,
                                                 None)

    paginator.add_actions()
    return render(request, 'default.html', locals())
예제 #2
0
def listt(request, app, cls, subset=None):

    try:
        _model = apps.get_model(app, cls)
    except LookupError:
        return page_not_found(request)
    subsetp = None
    if subset:
        subset_func = getattr(_model.objects.all(), subset)
        can_view = subset_func._metadata['%s:can_view' % subset]
    else:
        tid = request.GET.get('tid')
        subsetp = request.GET.get('tab%s' % tid)
        if tid and subsetp:
            subset_func = getattr(_model.objects.get_queryset(), subsetp)
            can_view = subset_func._metadata['%s:can_view' % subsetp]
            if not permissions.check_group_or_permission(request, can_view):
                return httprr(
                    request, '/admin/login/?next=%s' % request.get_full_path())
        else:
            permission = '%s.list_%s' % (app, cls)
            if not request.user.has_perm(permission):
                return httprr(
                    request, '/admin/login/?next=%s' % request.get_full_path())

    qs = _model.objects.all(request.user)
    list_subsets = subset and [subset] or None
    if subset:
        title = getattr(getattr(qs, subset), '_metadata')['%s:title' % subset]

    else:
        title = u'%s' % get_metadata(_model, 'verbose_name_plural')

    paginator = Paginator(request,
                          qs,
                          title,
                          list_subsets=list_subsets,
                          is_list_view=True)
    response = paginator.get_response()
    if response:
        return response

    if _model in loader.class_actions:
        for group in loader.class_actions[_model]:
            for view_name in loader.class_actions[_model][group]:
                _action = loader.class_actions[_model][group][view_name]
                action_title = _action['title']
                action_message = _action['message']
                action_can_execute = _action['can_execute']
                action_input = _action['input']
                action_css = _action['css']
                action_condition = _action['condition']
                initial = _action['initial']
                choices = _action['choices']

                if subsetp:
                    if subsetp not in loader.subset_actions[
                            _model] or view_name not in loader.subset_actions[
                                _model][subsetp]:
                        continue
                else:
                    if subset not in loader.subset_actions[
                            _model] or view_name not in loader.subset_actions[
                                _model][subset]:
                        continue

                if permissions.check_group_or_permission(
                        request, action_can_execute):
                    func = hasattr(qs, view_name) and getattr(
                        qs, view_name) or None
                    if func:

                        char = '?' in request.get_full_path() and '&' or '?'
                        url = '%s%s%s' % (request.get_full_path(), char,
                                          '%s=' % view_name)

                        has_input = func.func_code.co_argcount > 1

                        if not has_input:
                            action_css = action_css.replace('popup', '')
                        paginator.add_subset_action(action_title, url,
                                                    action_css, None,
                                                    action_condition)

                        if view_name in request.GET:
                            ids = paginator.get_selected_ids()
                            if ids:
                                qs = paginator.get_queryset(
                                    paginate=False).filter(id__in=ids)
                            else:
                                qs = paginator.get_queryset(paginate=False)

                            func = getattr(qs, view_name)
                            redirect_url = None

                            if has_input:
                                form = factory.get_class_action_form(
                                    request, _model, _action, func)
                                paginator = ''
                                if form.is_valid():
                                    params = []
                                    for param in func.func_code.co_varnames[
                                            1:func.func_code.co_argcount]:
                                        if param in form.cleaned_data:
                                            params.append(
                                                form.cleaned_data[param])
                                    try:
                                        f_return = func(*params)
                                        redirect_url = '..'
                                    except ValidationError, e:
                                        form.add_error(None,
                                                       unicode(e.message))
                                if not redirect_url:
                                    return render(request, 'default.html',
                                                  locals())
                            else:
                                f_return = func()
                                redirect_url = '.'

                            if redirect_url:
                                request.GET._mutable = True
                                del request.GET['ids']
                                del request.GET[view_name]
                                request.GET._mutable = False
                            return httprr(request, redirect_url,
                                          action_message)
                    else:
                        url = '/%s/%s/' % (app, view_name)
                        if view_name in request.GET:
                            return httprr(request, url)
                        else:
                            action_css = action_css.replace('popup', '')
                            paginator.add_action(action_title, url, action_css,
                                                 None)