from django.utils.deprecation import MiddlewareMixin

from lib.http import render_json
from common import errors
from user.models import User


class AuthMiddleware(MiddlewareMixin):
    URL_WHITE_LIST = [
        '/api/user/submit/phone',
        '/api/user/submit/vcode',
    ]

    def process_request(self, request):
        # 如果当前 url 在白名单内，直接忽略
        if request.path in self.URL_WHITE_LIST:
            return

        # 检查用户是否登录
        uid = request.session.get('uid')
        if uid:
            try:
                request.user = User.objects.get(id=uid)
            except User.DoesNotExist:
                return render_json('用户不存在', errors.UserNotExist.code)
        else:
            return render_json('用户未登录', errors.LogicError.code)


class LogicErrMiddleware(MiddlewareMixin):
    def process_exception(self, request, exception):
        '''处理程序中的 LogicError'''
        if isinstance(exception, errors.LogicError):
            return render_json(exception.data, exception.code)

ea0057efcf4ef9a06cc0faeafe0fc7ddf2c5dcc2
<mask token>


class LogicErrMiddleware(MiddlewareMixin):

    def process_exception(self, request, exception):
        """处理程序中的 LogicError"""
        if isinstance(exception, errors.LogicError):
            return render_json(exception.data, exception.code)

<mask token>


class AuthMiddleware(MiddlewareMixin):
    <mask token>
    <mask token>


class LogicErrMiddleware(MiddlewareMixin):

    def process_exception(self, request, exception):
        """处理程序中的 LogicError"""
        if isinstance(exception, errors.LogicError):
            return render_json(exception.data, exception.code)

<mask token>


class AuthMiddleware(MiddlewareMixin):
    <mask token>

    def process_request(self, request):
        if request.path in self.URL_WHITE_LIST:
            return
        uid = request.session.get('uid')
        if uid:
            try:
                request.user = User.objects.get(id=uid)
            except User.DoesNotExist:
                return render_json('用户不存在', errors.UserNotExist.code)
        else:
            return render_json('用户未登录', errors.LogicError.code)


class LogicErrMiddleware(MiddlewareMixin):

    def process_exception(self, request, exception):
        """处理程序中的 LogicError"""
        if isinstance(exception, errors.LogicError):
            return render_json(exception.data, exception.code)

<mask token>


class AuthMiddleware(MiddlewareMixin):
    URL_WHITE_LIST = ['/api/user/submit/phone', '/api/user/submit/vcode']

    def process_request(self, request):
        if request.path in self.URL_WHITE_LIST:
            return
        uid = request.session.get('uid')
        if uid:
            try:
                request.user = User.objects.get(id=uid)
            except User.DoesNotExist:
                return render_json('用户不存在', errors.UserNotExist.code)
        else:
            return render_json('用户未登录', errors.LogicError.code)


class LogicErrMiddleware(MiddlewareMixin):

    def process_exception(self, request, exception):
        """处理程序中的 LogicError"""
        if isinstance(exception, errors.LogicError):
            return render_json(exception.data, exception.code)

from django.utils.deprecation import MiddlewareMixin

from lib.http import render_json
from common import errors
from user.models import User


class AuthMiddleware(MiddlewareMixin):
    URL_WHITE_LIST = [
        '/api/user/submit/phone',
        '/api/user/submit/vcode',
    ]

    def process_request(self, request):
        # 如果当前 url 在白名单内，直接忽略
        if request.path in self.URL_WHITE_LIST:
            return

        # 检查用户是否登录
        uid = request.session.get('uid')
        if uid:
            try:
                request.user = User.objects.get(id=uid)
            except User.DoesNotExist:
                return render_json('用户不存在', errors.UserNotExist.code)
        else:
            return render_json('用户未登录', errors.LogicError.code)


class LogicErrMiddleware(MiddlewareMixin):
    def process_exception(self, request, exception):
        '''处理程序中的 LogicError'''
        if isinstance(exception, errors.LogicError):
            return render_json(exception.data, exception.code)
