Пример #1
0
def contains_emojicons(unicode_or_utf8str_with_decoded_emojicons,
                       is_hex_str=False):
    global ALL_EMOJICONS_UTF8_PRREFIX_SET

    if unicode_or_utf8str_with_decoded_emojicons is None:
        return False

    if isinstance(unicode_or_utf8str_with_decoded_emojicons, unicode):
        utf8str_with_decoded_emojicons = unicode_or_utf8str_with_decoded_emojicons.encode(
            'utf-8')
    else:
        utf8str_with_decoded_emojicons = unicode_or_utf8str_with_decoded_emojicons

    __build_all_emojicons_utf8_prefix_set()

    if is_hex_str:
        hex_str = utf8str_with_decoded_emojicons  #E59388681020
    else:
        hex_str = byte_to_hex(utf8str_with_decoded_emojicons)  #E59388681020

    if len(utf8str_with_decoded_emojicons) <= 2:
        return False

    if len(hex_str) % 2 != 0:
        watchdog.warning(u"hex_str长度不正确,hex_str:{}".format(hex_str))

        return False

    max_index = len(hex_str)

    part_start_index = 0
    emojicon_utf8_part_index = -1
    for index in xrange(0, max_index, 2):
        if emojicon_utf8_part_index == -1:
            maybe_prefix = hex_str[index:index + 2]
        else:
            maybe_prefix = hex_str[emojicon_utf8_part_index:index + 2]

        if not ALL_EMOJICONS_UTF8_PRREFIX_SET.has_key(maybe_prefix):
            if index > 2 and emojicon_utf8_part_index >= 0:
                maybe_emojiconutf8 = hex_str[emojicon_utf8_part_index:index]
                if UTF82EMOJIICONS.has_key(maybe_emojiconutf8):
                    return True
        else:
            if emojicon_utf8_part_index == -1:
                emojicon_utf8_part_index = index

    if emojicon_utf8_part_index != -1:
        maybe_emojiconutf8 = hex_str[emojicon_utf8_part_index:]
        if UTF82EMOJIICONS.has_key(maybe_emojiconutf8):
            return True

    return False
Пример #2
0
def __create_member(username, mp_user_name):
    webapp_owner = account_models.User.select().dj_where(
        username=mp_user_name).get()
    profile = account_models.UserProfile.select().dj_where(
        user=webapp_owner).get()

    webapp_id = profile.webapp_id
    if member_models.SocialAccount.select().dj_where(
            webapp_id=webapp_id).count() == 0:
        #创建SocialAccount
        openid = '%s_%s' % (username, mp_user_name)
        token = md5(openid).hexdigest()
        social_account = member_models.SocialAccount.create(
            webapp_id=webapp_id,
            openid=openid,
            token=token,
            platform=member_models.SOCIAL_PLATFORM_WEIXIN,
            is_for_test=False)

        #创建member
        from util.string_util import byte_to_hex
        member_grade = member_models.MemberGrade.select().dj_where(
            webapp_id=webapp_id).get()
        member = member_models.Member.create(
            webapp_id=webapp_id,
            token=token,
            grade=member_grade,
            integral=0,
            is_subscribed=True,
            status=1,
            username_hexstr=byte_to_hex(username))

#创建WebappUser
    webapp_user = member_models.WebAppUser.create(webapp_id=webapp_id,
                                                  token=token,
                                                  member_id=member.id)

    #创建MemberHasSocialAccount
    member_models.MemberHasSocialAccount.create(webapp_id=webapp_id,
                                                member=member,
                                                account=social_account)
Пример #3
0
def get_member_for(username, webapp_id):
    """
	获取username对应的会员
	"""
    if isinstance(username, unicode):
        member_nickname_str = username.encode('utf-8')
    else:
        member_nickname_str = username
    username_hexstr = string_util.byte_to_hex(member_nickname_str)
    buf = []
    buf.append('=======================')
    buf.append(webapp_id)
    buf.append(username_hexstr)
    buf.append('=======================')
    print '\n'.join(buf)
    try:
        return member_models.Member.get(webapp_id=webapp_id,
                                        username_hexstr=username_hexstr)
    except:
        member = member_models.Member(id=1, grade_id=0)
        return member
Пример #4
0
def get_token_for_logined_user(user):
	"""
	根据已经登录的用户信息生成token串,可使用 get_logined_user_from_token(token)。

	@param user   登录用户信息

	@retval 据该token串解析出来对应登录后的用户信息。如果user信息非法直接返回None
	"""
	if (user is None) or (not isinstance(user, User)):
		return None
	#传递supplier_id作为关键值
	from account import models as account_models
	supplier_id = account_models.AccountHasSupplier.objects.filter(user_id=user.id).first().supplier_id
	encoded_str_with_userinfo = "{}_{}_{}".format(59, MAGIC_CODE, supplier_id)
	encoded_hex_str = byte_to_hex(encoded_str_with_userinfo)

	#进行字节码的混淆处理
	hex_byte_list = []
	for hex_byte in encoded_hex_str:
		hex_byte_list.append(hex_byte)
		hex_byte_list.append('0')
	return ''.join(hex_byte_list)
Пример #5
0
def encode_emojicons_for_html(unicode_or_utf8str_with_decoded_emojicons, is_hex_str=False):
    global ALL_EMOJICONS_UTF8_PRREFIX_SET

    if unicode_or_utf8str_with_decoded_emojicons is None:
        return ''

    if isinstance(unicode_or_utf8str_with_decoded_emojicons, unicode):
        utf8str_with_decoded_emojicons = unicode_or_utf8str_with_decoded_emojicons.encode('utf-8')
    else:
        utf8str_with_decoded_emojicons = unicode_or_utf8str_with_decoded_emojicons

    __build_all_emojicons_utf8_prefix_set()

    if is_hex_str:
        hex_str = utf8str_with_decoded_emojicons #E59388681020
    else:
        hex_str = byte_to_hex(utf8str_with_decoded_emojicons) #E59388681020

    if len(utf8str_with_decoded_emojicons) <= 2:
        if is_hex_str:
            return hex_to_byte(utf8str_with_decoded_emojicons)
        else:
            return utf8str_with_decoded_emojicons

    if len(hex_str) % 2 != 0:
        watchdog.warning(u"hex_str长度不正确,hex_str:{}".format(hex_str))

        if is_hex_str:
            return hex_to_byte(utf8str_with_decoded_emojicons)
        else:
            return utf8str_with_decoded_emojicons

    max_index = len(hex_str)
    parts = []

    part_start_index = 0
    emojicon_utf8_part_index = -1
    index = 0
    #for index in xrange(0, max_index, 2):
    #for循环改成while循环,来解决由不同表情有相同前缀码造成部分用户昵称里面的表情解析错误 by Eugene
    while index < max_index:
        if emojicon_utf8_part_index == -1:
            maybe_prefix = hex_str[index:index+2]
        else:
            maybe_prefix = hex_str[emojicon_utf8_part_index:index+2]
        if not ALL_EMOJICONS_UTF8_PRREFIX_SET.has_key(maybe_prefix):
            if index > 2 and emojicon_utf8_part_index >= 0:
                maybe_emojiconutf8 = hex_str[emojicon_utf8_part_index:index]
                if UTF82EMOJIICONS.has_key(maybe_emojiconutf8):
                    parts.append(hex_to_byte(hex_str[part_start_index:emojicon_utf8_part_index]))
                    parts.append(__complete_emojicon_html_content(maybe_emojiconutf8))

                    part_start_index = index
                    emojicon_utf8_part_index = -1
                    index -= 2
                else:
                    sub_str_len = len(maybe_emojiconutf8)
                    for sub_index in xrange(2, sub_str_len, 2):
                        if UTF82EMOJIICONS.has_key(maybe_emojiconutf8[:sub_str_len - sub_index]):
                            parts.append(hex_to_byte(hex_str[part_start_index:emojicon_utf8_part_index]))
                            parts.append(__complete_emojicon_html_content(maybe_emojiconutf8[:sub_str_len - sub_index]))

                            part_start_index = index - sub_index
                            index = part_start_index - 2
                            emojicon_utf8_part_index = -1
                            break
                    else:
                        parts.append(hex_to_byte(hex_str[part_start_index:index]))
                        part_start_index = index
                        emojicon_utf8_part_index = -1
                        index -= 2
        else:
            if emojicon_utf8_part_index == -1:
                emojicon_utf8_part_index = index

        index += 2

    if emojicon_utf8_part_index != -1:
        maybe_emojiconutf8 = hex_str[emojicon_utf8_part_index:]
        if UTF82EMOJIICONS.has_key(maybe_emojiconutf8):
            parts.append(hex_to_byte(hex_str[part_start_index:emojicon_utf8_part_index]))
            parts.append(__complete_emojicon_html_content(maybe_emojiconutf8))
        else:
            parts.append(hex_to_byte(hex_str[part_start_index:]))
    else:
        if part_start_index <= max_index:
            parts.append(hex_to_byte(hex_str[part_start_index:]))

    return ''.join(parts)
Пример #6
0
    def update(self, webapp_user_id):
        """更新会员信息

		每天更新一次,处理以下情况:
			1. 会员更改昵称
			2. 会员取消关注
		"""
        print 'TODO2: update member info'

        if settings.MODE == 'develop':
            return
        webapp_user = WebAppUser.from_id({
            'webapp_owner':
            self.context['webapp_owner'],
            'id':
            webapp_user_id
        })
        webapp_owner = self.context['webapp_owner']
        mpuser_access_token = webapp_owner.weixin_mp_user_access_token
        weixin_api = get_weixin_api(mpuser_access_token)
        userinfo = weixin_api.get_user_info(webapp_user.openid)

        if not userinfo:
            member = webapp_user.member
            if hasattr(userinfo, 'nickname'):
                nickname = userinfo.nickname
                if isinstance(nickname, unicode):
                    member_nickname_str = nickname.encode('utf-8')
                else:
                    member_nickname_str = nickname

                username_hexstr = byte_to_hex(member_nickname_str)
            else:
                username_hexstr = ''

            if hasattr(userinfo, 'headimgurl'):
                headimgurl = userinfo.headimgurl
            else:
                headimgurl = ''

            if hasattr(userinfo, 'sex'):
                sex = userinfo.sex
            else:
                sex = 0

            if hasattr(userinfo, 'subscribe'):
                if str(userinfo.subscribe) == '1':
                    subscribe = True
                else:
                    subscribe = False
            else:
                subscribe = False

            if hasattr(userinfo, 'city'):
                city = userinfo.city
            else:
                city = ''

            if hasattr(userinfo, 'province'):
                province = userinfo.province
            else:
                province = ''

            if hasattr(userinfo, 'country'):
                country = userinfo.country
            else:
                country = ''

            if subscribe is False:
                if member.is_subscribed:
                    status = 0
                else:
                    status = member.status
                member_models.Member.update(
                    is_subscribed=False,
                    status=status).dj_where(id=member.id).execute()
            else:
                member_models.Member.update(
                    is_subscribed=True,
                    update_time=datetime.now(),
                    username_hexstr=username_hexstr,
                    city=city,
                    province=province,
                    country=country,
                    sex=sex,
                    status=1).dj_where(id=member.id).execute()

            webapp_user.cleanup_cache()