Esempio n. 1
0
def get_sessions(request):
    response = JsonResponse()
    response.code = 200
    response.data = JsonResponse()
    response.data.items = []

    mpuser = get_system_user_binded_mpuser(request.user)
    if mpuser is None:
        return response.get_response()

    #获取当前页数
    cur_page = int(request.GET.get('page', '1'))
    #获取每页个数
    count = int(request.GET.get('count', COUNT_PER_PAGE))

    is_debug = (request.GET.get('dbg', '0') == '1')
    #收藏记录
    is_collected = request.GET.get('is_collected', '0')

    start_time = request.GET.get('start_time', '').strip()
    end_time = request.GET.get('end_time', '').strip()
    search_content = request.GET.get('search_content', '').strip()
    if is_debug:
        sessions = Session.objects.select_related().filter(mpuser=mpuser)
    else:
        sessions = Session.objects.select_related().filter(mpuser=mpuser,
                                                           is_show=True)

    if start_time and end_time:
        start_time = '%s 0:0:0' % start_time
        end_time = '%s 23:59:59' % end_time
        sessions = sessions.filter(latest_contact_created_at__gte=start_time,
                                   latest_contact_created_at__lte=end_time)

    pageinfo, sessions = paginate(sessions,
                                  cur_page,
                                  count,
                                  query_string=request.META['QUERY_STRING'])

    #清空未读消息数量
    RealTimeInfo.objects.filter(mpuser=mpuser).update(unread_count=0)

    webapp_id = request.user_profile.webapp_id
    weixin_user_usernames = [s.weixin_user_id for s in sessions]
    weixin_users = WeixinUser.objects.filter(
        username__in=weixin_user_usernames)
    username2weixin_user = dict([(u.username, u) for u in weixin_users])

    # session2member = dict([(member_has_social_account.account.openid, member_has_social_account.member) for member_has_social_account \
    # 	in MemberHasSocialAccount.objects.filter()])

    for session in sessions:
        weixin_user = username2weixin_user[session.weixin_user_id]
        one_session = JsonResponse()
        one_session.id = session.id
        one_session.session_id = session.id
        one_session.sender_fake_id = weixin_user.fake_id
        one_session.sender_username = weixin_user.username
        one_session.sender_name = weixin_user.nickname_for_html
        if weixin_user.weixin_user_icon:
            one_session.weixin_user_icon = weixin_user.weixin_user_icon if len(
                weixin_user.weixin_user_icon.strip()) > 0 else DEFAULT_ICON
        else:
            one_session.weixin_user_icon = DEFAULT_ICON
        one_session.content = emotion.change_emotion_to_img(
            session.latest_contact_content)
        one_session.created_at = __format_datetime(
            session.latest_contact_created_at)
        one_session.unread_count = session.unread_count
        one_session.message_id = session.message_id
        one_session.is_collected = CollectMessage.is_collected(
            session.message_id)
        one_session.for_collected = False
        one_session.hidden_a = False
        try:
            if session.message_id != 0:
                message = Message.objects.get(id=session.message_id)
                one_session.message_type = message.message_type
                one_session.pic_url = message.pic_url
                one_session.audio_url = message.audio_url
            else:
                one_session.message_type = 'text'
                one_session.pic_url = ''
                one_session.audio_url = ''
        except:
            one_session.message_type = 'text'
            one_session.pic_url = ''
            one_session.audio_url = ''

        one_session.is_subscribed = is_subscribed(weixin_user)
        one_session.is_active = True if datetime.now(
        ) < session.latest_contact_created_at + timedelta(
            hours=EXPIRED_TIME) and datetime.now(
            ) > session.latest_contact_created_at else False
        try:
            account = SocialAccount.objects.get(webapp_id=webapp_id,
                                                openid=weixin_user.username)
            member = MemberHasSocialAccount.objects.filter(
                account=account)[0].member
            #member = session2member[session.weixin_user_id]
            if member:
                one_session.member_id = member.id
                one_session.member_remarks_name = member.remarks_name
                if member.user_icon and len(member.user_icon.strip()) > 0:
                    one_session.weixin_user_icon = member.user_icon
                if member.username_for_html:
                    one_session.sender_name = member.username_for_html
        except:
            notify_message = u"设置会话信息失败, weixin_user_openid:{}, webapp_id:{},cause:\n{}".format(
                session.weixin_user_id, webapp_id, unicode_full_stack())
            watchdog_notice(notify_message)
            continue

        response.data.items.append(one_session)
        response.data.page_info = __package_pageinfo(pageinfo)
    return response.get_response()
Esempio n. 2
0
def get_messages(request):
    response = JsonResponse()
    response.code = 200
    response.data = JsonResponse()
    response.data.items = []

    mpuser = get_system_user_binded_mpuser(request.user)
    if mpuser is None:
        return response.get_response()

    #获取当前页数
    cur_page = int(request.GET.get('page', '1'))
    #获取每页个数
    count = int(request.GET.get('count', COUNT_PER_PAGE))

    is_debug = (request.GET.get('dbg', '0') == '1')

    is_collected = request.GET.get('is_collected', '')

    search_content = request.GET.get('search_content', '')

    if is_collected:
        collected_message_ids = CollectMessage.get_message_ids(request.user)
        ordering = 'FIELD(`id`, %s)' % ','.join(
            str(id) for id in collected_message_ids)
        messages = Message.objects.filter(id__in=collected_message_ids).extra(
            select={'ordering': ordering}, order_by=('ordering', ))
    elif search_content:
        data_before_tow_days = dateutil.get_previous_date('today', 2)
        messages = Message.objects.belong_to(request.user_profile.webapp_id,
                                             mpuser, search_content)
    else:
        messages = []
    pageinfo, messages = paginate(messages,
                                  cur_page,
                                  count,
                                  query_string=request.META['QUERY_STRING'])
    webapp_id = request.user_profile.webapp_id
    for message in messages:
        weixin_user = message.session.weixin_user
        one_session = JsonResponse()
        one_session.id = message.id
        one_session.session_id = message.session.id
        one_session.sender_fake_id = weixin_user.fake_id
        one_session.sender_username = weixin_user.username
        one_session.sender_name = weixin_user.nickname_for_html
        if weixin_user.weixin_user_icon:
            one_session.weixin_user_icon = weixin_user.weixin_user_icon if len(
                weixin_user.weixin_user_icon.strip()) > 0 else DEFAULT_ICON
        else:
            one_session.weixin_user_icon = DEFAULT_ICON

        one_session.content = emotion.change_emotion_to_img(message.content)
        one_session.is_active = True if datetime.now(
        ) < message.created_at + timedelta(
            hours=EXPIRED_TIME) and datetime.now(
            ) > message.created_at else False
        if is_collected:
            one_session.is_active = False
            try:
                collect_message = CollectMessage.objects.get(
                    message_id=message.id)
                one_session.created_at = __format_datetime(
                    collect_message.created_at)
            except:
                one_session.created_at = __format_datetime(message.created_at)
        else:
            one_session.created_at = __format_datetime(message.created_at)
        one_session.message_id = message.id
        one_session.is_collected = CollectMessage.is_collected(message.id)
        one_session.message_type = message.message_type
        one_session.pic_url = message.pic_url
        one_session.audio_url = message.audio_url
        one_session.for_collected = is_collected
        one_session.hidden_a = True

        if message.message_type == IMAGE:
            one_session.content = message.pic_url

        if message.message_type == VOICE:
            one_session.content = message.audio_url

        one_session.is_subscribed = is_subscribed(weixin_user)

        try:
            account = SocialAccount.objects.get(webapp_id=webapp_id,
                                                openid=weixin_user.username)
            member = MemberHasSocialAccount.objects.filter(
                account=account)[0].member
            #member = session2member[session.weixin_user_id]
            if member:
                one_session.member_id = member.id
                one_session.member_remarks_name = member.remarks_name
                if member.user_icon and len(member.user_icon.strip()) > 0:
                    one_session.weixin_user_icon = member.user_icon
                if member.username_for_html:
                    one_session.sender_name = member.username_for_html
        except:
            notify_message = u"设置会话信息失败, weixin_user_openid:{}, cause:\n{}".format(
                session.weixin_user_id, unicode_full_stack())
            watchdog_notice(notify_message)

            continue

        response.data.items.append(one_session)
        response.data.page_info = __package_pageinfo(pageinfo)

    return response.get_response()