account_service.py 1.28 KB
# coding:utf-8
import logging
import traceback
from django.contrib import auth


class AccountService(object):
    """
    用户相关服务
    """
    def __init__(self, model, request):
        self.model = model
        self.request = request

    def get_login_next_url(self):
        """
        登录后跳转地址
        """
        try:
            source_url = self.request.META.get('HTTP_REFERER','/')
            if 'next=' in source_url:
                next_url = self.request.META.get('HTTP_REFERER').split("next=")[1]
            else:
                next_url='/'
            return next_url
        except Exception, e:
            logging.warning(traceback.format_exc())
            return '/'

    def user_auth_local(self, username, password):
        """
        本地系统验证
        """
        try:
            user = auth.authenticate(username=username, password=password)
            return user
        except Exception, e:
            logging.error(traceback.format_exc())
            return None

    def user_login(self, user):
        """
        登录
        """
        try:
            auth.login(self.request, user)
            return True, ''
        except Exception, e:
            logging.error(traceback.format_exc())
            return False, e.messge