3,Django回顾-权限表设计-session写入权限-权限验证-展示菜单-控制粒度到按钮级别
s13day107 内容回顾: 1. django模板中自定义方法。 2. 视图三个返回值的区别。 3. orm高级操作 F Q select_related prefetch_related only defer 原生SQL - connections - raw - extra 4. 表操作FK on_delete limit_choice_to related_name fk创造m2m 联合唯一索引 5. Form和ModelForm作用? - 动态创建HTML表单标签 - 用户提交的数据进行校验 - 显示错误信息(保留原提交的数据) 6. ModelFormSet作用? 批量的操作 - 动态创建HTML表单标签 - 用户提交的数据进行校验 - 显示错误信息(保留原提交的数据) 7. 面向对象中 init call new getitem setitem delitem setattr getattr delattr enter exit repr str 8. 简述django中session的实现原理? 中间件:django.contrib.sessions.middleware.SessionMiddleware 1. 实例化SessionMiddleware class SessionMiddleware(MiddlewareMixin): def __init__(self, get_response=None): self.get_response = get_response # engine = django.contrib.sessions.backends.db engine = import_module(settings.SESSION_ENGINE) # from django.contrib.sessions.backends.db import SessionStore # self.SessionStore = SessionStore self.SessionStore = engine.SessionStore 2. 执行process_request def process_request(self, request): # 根据sessionid获取原来我给浏览器设置的随机字符串。 session_key = request.COOKIES.get(settings.SESSION_COOKIE_NAME) # request.session是SessionStore对象 request.session = self.SessionStore(session_key) 源码执行流程: - 执行 SessionStore的 __init__方法 class SessionStore(SessionBase): def __init__(self, session_key=None): super(SessionStore, self).__init__(session_key) ... - 执行 SessionBase的 __init__方法 class SessionBase(object): TEST_COOKIE_NAME = 'testcookie' TEST_COOKIE_VALUE = 'worked' __not_given = object() def __init__(self, session_key=None): self._session_key = session_key self.accessed = False self.modified = False self.serializer = import_string(settings.SESSION_SERIALIZER) ... 3. 执行视图函数 def goods(request): # 调用 SessionStore对象的 __setitem__ # 内部:维护了一个字典SessionStore对象._session,在字典中设置了一个键值对 SessionStore对象._session = {'k1':123} request.session['k1'] = 123 # 内部:维护了一个字典SessionStore对象._session,在字典中设置了一个键值对 SessionStore对象._session = {'k1':123,'k2':456} request.session['k2'] = 456 del request.session['k2'] return render(request,'goods.html') 4. 执行中间件的process_response - 将内存中的字典序列化,并保存到数据库。 - 给用户浏览器设置cookie,将随机字符串写给浏览器。 def process_response(self, request, response): """ If request.session was modified, or if the configuration is to save the session every time, save the changes and set a session cookie or delete the session cookie if the session has been emptied. """ try: accessed = request.session.accessed modified = request.session.modified empty = request.session.is_empty() except AttributeError: pass else: # First check if we need to delete this cookie. # The session should be deleted only if the session is entirely empty if settings.SESSION_COOKIE_NAME in request.COOKIES and empty: response.delete_cookie( settings.SESSION_COOKIE_NAME, path=settings.SESSION_COOKIE_PATH, domain=settings.SESSION_COOKIE_DOMAIN, ) else: if accessed: patch_vary_headers(response, ('Cookie',)) if (modified or settings.SESSION_SAVE_EVERY_REQUEST) and not empty: if request.session.get_expire_at_browser_close(): max_age = None expires = None else: max_age = request.session.get_expiry_age() expires_time = time.time() + max_age expires = cookie_date(expires_time) # Save the session data and refresh the client cookie. # Skip session save for 500 responses, refs #3881. if response.status_code != 500: try: request.session.save() except UpdateError: raise SuspiciousOperation( "The request's session was deleted before the " "request completed. The user may have logged " "out in a concurrent request, for example." ) response.set_cookie( settings.SESSION_COOKIE_NAME, request.session.session_key, max_age=max_age, expires=expires, domain=settings.SESSION_COOKIE_DOMAIN, path=settings.SESSION_COOKIE_PATH, secure=settings.SESSION_COOKIE_SECURE or None, httponly=settings.SESSION_COOKIE_HTTPONLY or None, ) return response 今日内容: - 权限 - django rest framework 内容详细: - 权限 1. 什么是rbac? 基于角色的权限控制。 2. 你的权限系统中都有哪些表? 用户表 角色表 用户角色关系表 权限表 权限角色关系表 菜单表 3. 系统表设计 第一版:用户和权限(3张表) 第二版:用户、角色、权限(5张表) rbac,基于角色的权限控制。 第三版:菜单、用户、角色、权限(6张表) rbac,基于角色的权限控制 + 菜单。 4. 权限功能实现
%s