예제 #1
0
def display_table_list(request, app_name, table_name):
    errors = []
    if app_name in site.enabled_admins:
        ##print(enabled_admins[url])
        if table_name in site.enabled_admins[app_name]:
            admin_class = site.enabled_admins[app_name][table_name]

            if request.method == "POST":  # action 来了

                print(request.POST)

                editable_data = request.POST.get("editable_data")
                if editable_data:  #for list editable
                    editable_data = json.loads(editable_data)
                    #print("editable",editable_data)
                    res_state, error = batch_update(request, editable_data,
                                                    admin_class)
                    if res_state == False:
                        errors.append(error)

                else:  #for action
                    selected_ids = request.POST.get("selected_ids")
                    action = request.POST.get("admin_action")
                    if selected_ids:
                        selected_objs = admin_class.model.objects.filter(
                            id__in=selected_ids.split(','))
                    else:
                        raise KeyError("No object selected.")
                    if hasattr(admin_class, action):
                        action_func = getattr(admin_class, action)
                        request._admin_action = action
                        return action_func(admin_class, request, selected_objs)

            if request.method == "POST2":  #drepcated
                #print('post-->', request.POST)

                delete_tag = request.POST.get("_delete_confirm")
                if delete_tag == "yes":
                    del_ids = request.POST.getlist("deleted_objs")

                    admin_class.model.objects.filter(id__in=del_ids).delete()

                else:

                    admin_action = request.POST.get('admin_action')
                    selected_raw_ids = request.POST.get('selected_ids', '')
                    selected_ids = selected_raw_ids.split(',')
                    selected_objs = admin_class.model.objects.filter(
                        id__in=selected_ids)

                    if hasattr(admin_class, admin_action):
                        admin_action_func = getattr(admin_class, admin_action)
                        return admin_action_func(admin_class, request,
                                                 selected_objs)
                    else:
                        raise NotImplementedError(
                            "admin_action %s cannot find" % admin_action)

            querysets = tables.table_filter(request, admin_class,
                                            admin_class.model)
            searched_querysets = tables.search_by(request, querysets,
                                                  admin_class)
            order_res = tables.get_orderby(request, searched_querysets,
                                           admin_class)

            paginator = Paginator(order_res[0], admin_class.list_per_page)

            page = request.GET.get('page')
            try:
                table_obj_list = paginator.page(page)
            except PageNotAnInteger:
                table_obj_list = paginator.page(1)
            except EmptyPage:
                table_obj_list = paginator.page(paginator.num_pages)

            table_obj = tables.TableHandler(request, admin_class.model,
                                            admin_class, table_obj_list,
                                            order_res)

            return render(
                request, 'kingadmin/model_obj_list.html', {
                    'table_obj': table_obj,
                    'app_name': app_name,
                    'active_url': '/kingadmin/',
                    'paginator': paginator,
                    'errors': errors
                })

    else:
        raise Http404("url %s/%s not found" % (app_name, table_name))
예제 #2
0
def display_table_list(request, app_name, table_name, embed=False):
    """

    :param request:
    :param app_name:
    :param table_name:
    :param embed:
    :return:
    """
    errors = []
    if app_name in site.enabled_admins:
        if table_name in site.enabled_admins[app_name]:
            admin_class = site.enabled_admins[app_name][table_name]
            if request.method == "POST":
                editable_data = request.POST.get("editable_data")
                if editable_data:
                    editable_data = json.loads(editable_data)
                    res_state, errors = batch_update(request, editable_data,
                                                     admin_class)

                else:
                    selected_ids = request.POST.get("selected_ids")
                    action = request.POST.get("admin_action")
                    if selected_ids:
                        selected_objs = admin_class.model.objects.filter(
                            id__in=selected_ids.split(","))
                    else:
                        raise KeyError("No object selected")
                    if hasattr(admin_class, action):
                        action_func = getattr(admin_class, action)
                        request._admin_action = action
                        return action_func(request, selected_ids)

            querysets = tables.table_filter(request, admin_class,
                                            admin_class.model)
            searched_querysets = tables.search_by(request, querysets,
                                                  admin_class)
            order_res = tables.get_orderby(request, searched_querysets,
                                           admin_class)

            paginator = Paginator(order_res[0], admin_class.list_per_page)
            page = request.GET.get("page")
            try:
                table_obj_list = paginator.page(page)
            except PageNotAnInteger:
                table_obj_list = paginator.page(1)
            except EmptyPage:
                table_obj_list = paginator.page(paginator.num_pages)
            table_obj = tables.TableHandler(request, admin_class.model,
                                            admin_class, table_obj_list,
                                            order_res)
            return_data = {
                "table_obj": table_obj,
                "app_name": app_name,
                "paginator": paginator,
                "errors": errors,
                "enabled_admins": site.enabled_admins,
            }
            if embed:
                return return_data
            else:
                return render(request, "kingadmin/model_obj_list.html",
                              return_data)
    else:
        raise Http404("url %s/%s not found" % (app_name, table_name))
예제 #3
0
def display_table_list(request, app_name, table_name, embed=False):
    """
    
    :param request: 
    :param app_name: 
    :param table_name: 
    :param embed: 若此函数是被另一个view调用的,则embed=True,通常用在把kingadmin套件嵌入在其他项目时
    :return: 
    """

    errors = []
    if app_name in site.enabled_admins:
        ##print(enabled_admins[url])
        if table_name in site.enabled_admins[app_name]:
            admin_class = site.enabled_admins[app_name][table_name]

            if request.method == "POST":  # action 来了

                print(request.POST)

                editable_data = request.POST.get("editable_data")
                if editable_data:  #for list editable
                    editable_data = json.loads(editable_data)
                    #print("editable",editable_data)
                    res_state, errors = batch_update(request, editable_data,
                                                     admin_class)
                    #if res_state == False:
                    #    #errors.append(error)

                else:  #for action
                    selected_ids = request.POST.get("selected_ids")
                    action = request.POST.get("admin_action")
                    if selected_ids:
                        selected_objs = admin_class.model.objects.filter(
                            id__in=selected_ids.split(','))
                    else:
                        raise KeyError("No object selected.")
                    if hasattr(admin_class, action):
                        action_func = getattr(admin_class, action)
                        request._admin_action = action
                        return action_func(admin_class, request, selected_objs)

            querysets = tables.table_filter(request, admin_class,
                                            admin_class.model)
            searched_querysets = tables.search_by(request, querysets,
                                                  admin_class)
            order_res = tables.get_orderby(request, searched_querysets,
                                           admin_class)

            paginator = Paginator(order_res[0], admin_class.list_per_page)

            page = request.GET.get('page')
            try:
                table_obj_list = paginator.page(page)
            except PageNotAnInteger:
                table_obj_list = paginator.page(1)
            except EmptyPage:
                table_obj_list = paginator.page(paginator.num_pages)

            table_obj = tables.TableHandler(request, admin_class.model,
                                            admin_class, table_obj_list,
                                            order_res)

            return_data = {
                'table_obj': table_obj,
                'app_name': app_name,
                'paginator': paginator,
                'errors': errors,
                'enabled_admins': site.enabled_admins
            }
            if embed:
                return return_data
            else:
                return render(request, 'kingadmin/model_obj_list.html',
                              return_data)

    else:
        raise Http404("url %s/%s not found" % (app_name, table_name))