Skip to content

Commit

Permalink
Merge pull request #867 from pagezhou/mobile_bugfix_user_error
Browse files Browse the repository at this point in the history
bugfix:修复移动端访问设置request.user属性错误导致500的问题 #863
  • Loading branch information
homholueng authored Aug 6, 2019
2 parents c7b4ef5 + f3078d1 commit caf3c2c
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions weixin/core/middlewares.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,21 @@
specific language governing permissions and limitations under the License.
"""

import logging

from django.utils.deprecation import MiddlewareMixin
from django.utils.functional import SimpleLazyObject
from django.contrib.auth.models import AnonymousUser

from blueapps.account.models import UserProperty
from blueapps.account import get_user_model

from . import settings
from .accounts import WeixinAccount
from .models import BkWeixinUser

logger = logging.getLogger('root')


def get_user(request):
user = None
Expand All @@ -36,11 +41,13 @@ def get_user(request):
def get_bk_user(request):
bkuser = None
if request.weixin_user and not isinstance(request.weixin_user, AnonymousUser):
user_model = get_user_model()
try:
user_property = UserProperty.objects.get(key='wx_userid', value=request.weixin_user.userid)
bkuser = user_property.user
except UserProperty.DoesNotExist:
bkuser = None
logger.warning('user[wx_userid=%s] not in UserProperty' % request.weixin_user.userid)
else:
bkuser = user_model.objects.get(username=user_property.user.username)
return bkuser or AnonymousUser()


Expand Down

0 comments on commit caf3c2c

Please sign in to comment.