Exemplo n.º 1
0
def get_weizoom_cards(request):
	count_per_page = int(request.GET.get('count_per_page', '1'))
	cur_page = int(request.GET.get('page', '1'))
	weizoom_card_rule_id = int(request.GET.get('weizoom_card_rule_id', '-1'))
	query = request.GET.get('query', None)
	weizoom_cards = WeizoomCard.objects.filter(owner=request.user, weizoom_card_rule_id=weizoom_card_rule_id)
	#获得已经过期的微众卡id
	today = datetime.today()
	card_ids_need_expire = []
	for card in weizoom_cards:
		#记录过期并且是未使用的微众卡id
		if card.expired_time < today:
			card_ids_need_expire.append(card.id)
	
	if len(card_ids_need_expire) > 0:
		WeizoomCard.objects.filter(id__in=card_ids_need_expire).update(is_expired=True)
	
	#处理过滤
	filter_attr = request.GET.get('filter_attr', None)
	filter_value = int(request.GET.get('filter_value', -1))
	if filter_attr and (filter_value != -1):
		params = {filter_attr: filter_value}
		weizoom_cards = weizoom_cards.filter(**params)
		
	if query:
		weizoom_cards = weizoom_cards.filter(weizoom_card_id=query)
	
	pageinfo, weizoom_cards = paginator.paginate(weizoom_cards, cur_page, count_per_page, query_string=request.META['QUERY_STRING'])	
	target_user_ids = []
	for weizoom_card in weizoom_cards:
		target_user_ids.append(weizoom_card.target_user_id)
	card_has_accounts = WeizoomCardHasAccount.objects.filter(owner=request.user, account__in=target_user_ids)
	user2name = {}
	for card_has_account in card_has_accounts:
		user2name[card_has_account.account_id] = card_has_account.account_name
	
	cur_weizoom_cards = []
	for c in weizoom_cards:
		cur_weizoom_card = JsonResponse()
		cur_weizoom_card.id = c.id
		cur_weizoom_card.status = c.status
		cur_weizoom_card.weizoom_card_id = c.weizoom_card_id
		cur_weizoom_card.password = c.password
		cur_weizoom_card.money = '%.2f' % c.money
		if c.activated_at:
			cur_weizoom_card.activated_at = c.activated_at.strftime('%Y-%m-%d %H:%M:%S')
		else:
			cur_weizoom_card.activated_at = ''
		cur_weizoom_card.target_name = user2name.get(c.target_user_id, u'')
		cur_weizoom_card.is_expired = c.is_expired
		cur_weizoom_cards.append(cur_weizoom_card)
		
	response = create_response(200)
	response.data.items = cur_weizoom_cards
	response.data.sortAttr = request.GET.get('sort_attr', '-created_at')
	response.data.pageinfo = paginator.to_dict(pageinfo)
	
	return response.get_response()
Exemplo n.º 2
0
def post_all_cards(request):
    count_per_page = int(15)
    cur_page = int(request.GET.get('page', '1'))
    search_query = request.GET.get('query', '')
    orgin_card_list = request.GET.get('orginCardList', '')
    #获得已经过期的微众卡id
    today = datetime.today()
    WeizoomCard.objects.filter(expired_time__lte=today,
                               is_expired=False).update(is_expired=True)

    card_status = [
        WEIZOOM_CARD_STATUS_UNUSED, WEIZOOM_CARD_STATUS_USED,
        WEIZOOM_CARD_STATUS_EMPTY
    ]
    weizoom_cards = WeizoomCard.objects.filter(status__in=card_status,
                                               is_expired=False)
    if search_query:
        search_query_dict = json.loads(search_query)
        card_filter_id = search_query_dict['cardIds']
        if card_filter_id:
            weizoom_cards = WeizoomCard.objects.filter(
                weizoom_card_id__contains=card_filter_id)
    can_active_card = 0
    can_stop_card = 0
    can_view_card_details = 0

    pageinfo, weizoom_cards = paginator.paginate(
        weizoom_cards,
        cur_page,
        count_per_page,
        query_string=request.META['QUERY_STRING'])
    rule_card_ids = set(
        [weizoom_card.weizoom_card_rule_id for weizoom_card in weizoom_cards])
    w_card_ids = set([weizoom_card.id for weizoom_card in weizoom_cards])
    weizoom_card_rule = WeizoomCardRule.objects.filter(id__in=rule_card_ids)
    card_recharge = WeizoomCardRecharge.objects.filter(card_id__in=w_card_ids)
    id2recharge_money = {}
    for card in card_recharge:
        if card.card_id not in id2recharge_money:
            id2recharge_money[card.card_id] = card.recharge_money
        else:
            id2recharge_money[card.card_id] += card.recharge_money
    id2total_money = {
        card_rule.id: card_rule.money
        for card_rule in weizoom_card_rule
    }
    cur_weizoom_cards = []
    for c in weizoom_cards:
        cur_weizoom_card = JsonResponse()
        cur_weizoom_card.id = c.id
        cur_weizoom_card.status = c.status
        cur_weizoom_card.weizoom_card_id = c.weizoom_card_id
        cur_weizoom_card.user_id = request.user.id  #当前用户的id
        cur_weizoom_card.money = '%.2f' % c.money  # 余额

        if c.activated_at:
            cur_weizoom_card.activated_at = c.activated_at.strftime(
                '%Y-%m-%d %H:%M:%S')
        else:
            cur_weizoom_card.activated_at = ''
        recharge_money = 0 if c.id not in id2recharge_money else id2recharge_money[
            c.id]
        cur_weizoom_card.total_money = '%.2f' % (
            0 if c.weizoom_card_rule_id not in id2total_money else
            id2total_money[c.weizoom_card_rule_id])  #总额
        cur_weizoom_card.used_money = '%.2f' % (
            float(cur_weizoom_card.total_money) -
            float(cur_weizoom_card.money) + recharge_money)  #已使用金额
        cur_weizoom_card.is_expired = c.is_expired
        cur_weizoom_card.recharge_money = '%.2f' % recharge_money
        if (c.is_expired) or not c.is_expired:
            cur_weizoom_cards.append(cur_weizoom_card)
        else:
            pageinfo.object_count -= 1
    response = create_response(200)
    response.data.items = cur_weizoom_cards
    response.data.sortAttr = request.GET.get('sort_attr', '-created_at')
    response.data.pageinfo = paginator.to_dict(pageinfo)
    response.data.can_active_card = can_active_card
    response.data.can_stop_card = can_stop_card
    response.data.can_view_card_details = can_view_card_details
    response.data.orgin_card_list = orgin_card_list
    return response.get_response()
Exemplo n.º 3
0
def get_weizoom_cards(request):
    """
	卡列表
	"""
    count_per_page = int(100)
    cur_page = int(request.GET.get('page', '1'))
    weizoom_card_rule_id = int(request.GET.get('weizoom_card_rule_id', '-1'))
    weizoom_cards = WeizoomCard.objects.filter(
        weizoom_card_rule_id=weizoom_card_rule_id)
    weizoomcardpermission = WeiZoomCardPermission.objects.filter(
        user_id=request.user.id)
    can_active_card = 0
    can_stop_card = 0
    can_view_card_details = 0
    if weizoomcardpermission:
        can_active_card = weizoomcardpermission[0].can_active_card
        can_stop_card = weizoomcardpermission[0].can_stop_card
        can_view_card_details = weizoomcardpermission[0].can_view_card_details

    #获得已经过期的微众卡id
    today = datetime.today()
    card_ids_need_expire = []
    for card in weizoom_cards:
        #记录过期并且是未使用的微众卡id
        if card.expired_time < today:
            card_ids_need_expire.append(card.id)
    if len(card_ids_need_expire) > 0:
        WeizoomCard.objects.filter(id__in=card_ids_need_expire).update(
            is_expired=True)
    # weizoom_cards = WeizoomCard.objects.filter(weizoom_card_rule_id=weizoom_card_rule_id)
    filter_value = request.GET.get('filter_value', '')
    card_number = _get_cardNumber_value(filter_value)
    cardStatus = _get_status_value(filter_value)
    try:
        if card_number != -1:
            weizoom_cards = weizoom_cards.filter(
                weizoom_card_id__contains=str(card_number))
        if cardStatus != -1:
            weizoom_cards = weizoom_cards.filter(status=cardStatus)
    except:
        pass

    #卡号区间查询
    card_num_min = request.GET.get('card_num_min', '')
    card_num_max = request.GET.get('card_num_max', '')

    if card_num_min or card_num_max:
        weizoom_card_id_set = set(
            [int(c.weizoom_card_id) for c in weizoom_cards])

        if card_num_min and card_num_max:
            min_num = int(card_num_min)
            max_num = int(card_num_max)
            search_set = set(range(min_num, max_num + 1))
        elif card_num_max:
            search_set = set([int(card_num_max)])
        elif card_num_min:
            search_set = set([int(card_num_min)])
        else:
            search_set = set([])
        result_set = search_set & weizoom_card_id_set
        result_list = list(result_set)

        if len(result_list) > 0:
            filter_cards_id_list = []
            for card_num in result_list:
                filter_cards_id_list.append(u'%07d' % card_num)
            weizoom_cards = weizoom_cards.filter(
                weizoom_card_id__in=filter_cards_id_list)
        if len(result_list) == 0:
            weizoom_cards = []

    pageinfo, weizoom_cards = paginator.paginate(
        weizoom_cards,
        cur_page,
        count_per_page,
        query_string=request.META['QUERY_STRING'])

    weizoom_card_rule = WeizoomCardRule.objects.filter(id=weizoom_card_rule_id)
    rule_id2rule = {rule.id: rule for rule in weizoom_card_rule}
    card_recharge = WeizoomCardRecharge.objects.all()
    id2recharge_money = {}
    for card in card_recharge:
        if card.card_id not in id2recharge_money:
            id2recharge_money[card.card_id] = card.recharge_money
        else:
            id2recharge_money[card.card_id] += card.recharge_money
    cur_weizoom_cards = []
    for c in weizoom_cards:
        cur_weizoom_card = JsonResponse()
        cur_weizoom_card.id = c.id
        cur_weizoom_card.status = c.status
        cur_weizoom_card.weizoom_card_id = c.weizoom_card_id
        cur_weizoom_card.password = c.password
        cur_weizoom_card.active_card_user_id = c.active_card_user_id  #激活卡用户的id
        cur_weizoom_card.user_id = request.user.id  #当前用户的id
        cur_weizoom_card.money = '%.2f' % c.money  # 余额

        rule_money = 0.0 if c.weizoom_card_rule_id not in rule_id2rule else rule_id2rule[
            c.weizoom_card_rule_id].money
        money = '%.2f' % c.money  # 余额
        recharge_money = 0 if c.id not in id2recharge_money else id2recharge_money[
            c.id]
        total_money = '%.2f' % (float(rule_money) + recharge_money)  #总额
        used_money = '%.2f' % (float(total_money) - float(money))  #已使用金额

        if c.activated_at:
            cur_weizoom_card.activated_at = c.activated_at.strftime(
                '%Y-%m-%d %H:%M:%S')
        else:
            cur_weizoom_card.activated_at = ''
        valid_time_from = '' if c.weizoom_card_rule_id not in rule_id2rule else rule_id2rule[
            c.weizoom_card_rule_id].valid_time_from
        valid_time_to = '' if c.weizoom_card_rule_id not in rule_id2rule else rule_id2rule[
            c.weizoom_card_rule_id].valid_time_to
        cur_weizoom_card.total_money = total_money  #总额
        cur_weizoom_card.used_money = used_money  #已使用金额
        cur_weizoom_card.remark = c.remark
        cur_weizoom_card.activated_to = c.activated_to
        cur_weizoom_card.department = c.department
        cur_weizoom_card.valid_time_from = datetime.strftime(
            valid_time_from, '%Y-%m-%d %H:%M')
        cur_weizoom_card.valid_time_to = datetime.strftime(
            c.expired_time, '%Y-%m-%d %H:%M')
        cur_weizoom_card.is_expired = c.is_expired
        if (c.is_expired and cardStatus == -1) or not c.is_expired:
            cur_weizoom_cards.append(cur_weizoom_card)
        else:
            pageinfo.object_count -= 1

    response = create_response(200)
    response.data.items = cur_weizoom_cards
    response.data.sortAttr = request.GET.get('sort_attr', '-created_at')
    response.data.pageinfo = paginator.to_dict(pageinfo)
    response.data.can_active_card = can_active_card
    response.data.can_stop_card = can_stop_card
    response.data.can_view_card_details = can_view_card_details
    return response.get_response()