• strak组件(5):为列表定制预留钩子方法


    效果图:

     新增函数

    def get_list_display(self):  获取页面上应该显示的列,预留的自定义扩展,例如:以后根据用户的不同显示不同的

    一、stark组件

    stark/service/core_func.py

    from django.urls import re_path
    from django.shortcuts import HttpResponse, render
    
    
    class StarkHandler(object):
        list_display = []
    
        def __init__(self, model_class, prev):
            self.model_class = model_class
            self.prev = prev
    
        def get_list_display(self):
            """
            获取页面上应该显示的列,预留的自定义扩展,例如:以后根据用户的不同显示不同的列
            :return:
            """
            value = []
            value.extend(self.list_display)
            return value
    
        def list_view(self, request):
            """
            列表页面
            :param request:
            :return:
            """
    
            list_display = self.get_list_display() # 会优先调用UserInfoHandler里的get_list_display()方法。
            # 1. 处理表格的表头
            header_list = []
            if list_display:
                for field in list_display:
                    verbose_name = self.model_class._meta.get_field(field).verbose_name
                    header_list.append(verbose_name)
            else:
                header_list.append(self.model_class._meta.model_name)  # 如果用户没有填写list_display,就显示表名
    
            # 2. 处理表的内容
            data_list = self.model_class.objects.all()
            body_list = []
            for obj in data_list:
                tr_list = []
                if list_display:
                    for field in list_display:
                        tr_list.append(getattr(obj, field))
                else:
                    tr_list.append(obj)  # 如果用户没有填写list_display,就显示表对象,所以表类要定义__str__方法
                body_list.append(tr_list)
    
            context = {
                'data_list': data_list,
                'header_list': header_list,
                'body_list': body_list,
            }
    
            return render(request, 'stark/data_list.html', context)
    
        def add_view(self, request):
            """
            添加页面
            :param request:
            :return:
            """
            return HttpResponse('添加页面')
    
        def edit_view(self, request, pk):
            """
            编辑页面
            :param request:
            :return:
            """
            return HttpResponse('编辑页面')
    
        def delete_view(self, request, pk):
            """
            删除页面
            :param request:
            :param pk:
            :return:
            """
            return HttpResponse('删除页面')
    
        def get_url_name(self, crud):
            app_label, model_name = self.model_class._meta.app_label, self.model_class._meta.model_name
            if self.prev:
                return '%s_%s_%s_%s' % (app_label, model_name, self.prev, crud)
            return '%s_%s_%s' % (app_label, model_name, crud)
    
        @property
        def get_list_url_name(self):
            """
            获取列表页面URL的name
            :return:
            """
            return self.get_url_name('list')
    
        @property
        def get_add_url_name(self):
            """
            获取添加页面URL的name
            :return:
            """
            return self.get_url_name('add')
    
        @property
        def get_edit_url_name(self):
            """
            获取编辑页面URL的name
            :return:
            """
            return self.get_url_name('edit')
    
        @property
        def get_delete_url_name(self):
            """
            获取删除页面URL的name
            :return:
            """
            return self.get_url_name('delete')
    
        def get_urls(self):
            patterns = [
                re_path(r'^list/$', self.list_view, name=self.get_list_url_name),
                re_path(r'^add/$', self.add_view, name=self.get_add_url_name),
                re_path(r'^edit/(d+)/$', self.edit_view, name=self.get_edit_url_name),
                re_path(r'^delete/(d+)/$', self.delete_view, name=self.get_delete_url_name),
            ]
    
            patterns.extend(self.extra_urls())
            return patterns
    
        def extra_urls(self):
            return []
    
    
    class StarkSite(object):
        def __init__(self):
            self._registry = []
            self.app_name = 'stark'
            self.namespace = 'stark'
    
        def register(self, model_class, handler_class=None, prev=None):
            """
            :param model_class: 是models中的数据库表对应的类。
            :param handler_class: 处理请求的视图函数所在的类
            :param prev: 生成URL的前缀
            :return:
            """
    
            if not handler_class:
                handler_class = StarkHandler
            self._registry.append({'model_class': model_class, 'handler': handler_class(model_class, prev), 'prev': prev})
    
        def get_urls(self):
            patterns = []
            for item in self._registry:
                model_class = item['model_class']
                handler = item['handler']
                prev = item['prev']
                app_name, model_name = model_class._meta.app_label, model_class._meta.model_name
                if prev:
                    patterns.append(
                        re_path(r'^%s/%s/%s/' % (app_name, model_name, prev,), (handler.get_urls(), None, None)))
                else:
                    patterns.append(re_path(r'^%s/%s/' % (app_name, model_name,), (handler.get_urls(), None, None)))
    
            return patterns
    
        @property
        def urls(self):
            return self.get_urls(), self.app_name, self.namespace
    
    
    site = StarkSite()

    二、业务

    web/stark.py

    from stark.service.core_func import site, StarkHandler
    
    from web import models
    
    
    class DepartmentHandler(StarkHandler):
        list_display = ['title']
    
    
    class UserInfoHandler(StarkHandler):
        list_display = ['name', 'age', 'email']
    
        def get_list_display(self):
            return ['name']
    
    
    site.register(models.Department, DepartmentHandler)  # 给部门的url增加了前缀:/stark/web/department/private/
    site.register(models.UserInfo, UserInfoHandler)

  • 相关阅读:
    栈区,堆区,全局区,文字常量区,程序代码区 详解
    2010年IT行业十大收购
    三大数据备份方式:完全备份、增量备份以及差异备
    Driver Development Part 1: Introduction to Drivers (code project)
    手工构造典型PE文件(转)
    访问IIS元数据库失败[转]
    代码注入的三种方法(转)
    对象的初始化(转)
    网络和黑客编程基本知识 (转)
    破解linux中root密码(图) 转自csdn
  • 原文地址:https://www.cnblogs.com/lshedward/p/10565673.html
Copyright © 2020-2023  润新知