# -*- coding: utf-8 -*-

from utils.redis.connect import r
from utils.redis.rkeys import PROFILE_INFO


def set_profile_info(user):
    """ 设置用户信息 """
    r.setjson(PROFILE_INFO % user.user_id, user.data)
    return user.data


def set_profile_by_uid(user_id):
    """ 获取用户信息 """
    from account.models import UserInfo
    try:
        user = UserInfo.objects.get(user_id=user_id)
    except UserInfo.DoesNotExist:
        return {}
    return set_profile_info(user)


def get_profile_by_id(user_id):
    """ 获取用户信息 """
    return r.getjson(PROFILE_INFO % user_id) or set_profile_by_uid(user_id)
