def edit_red_envelope_view(request, id): if int(id) > 0: try: red_envelope = RedEnvelope.objects.get(id=id) except: raise Http404('不存在该微信红包') relations = RedEnvelopeHasPrize.objects.filter(red_envelope=red_envelope) prize_ids = [r.prize_id for r in relations] prizes = Prize.objects.filter(id__in=prize_ids) id2prize = dict([(p.id, p) for p in prizes]) red_envelope.prizes = [] for r in relations: prize = id2prize[r.prize_id] prize.prize_source = r.prize_source prize.prize_type = r.prize_type red_envelope.prizes.append(prize) red_envelope.prizes.sort(lambda x,y: cmp(x.level, y.level)) #转换为json response = JsonResponse() response.id = red_envelope.id response.name = red_envelope.name response.total_award_value = red_envelope.total_award_value response.desc = red_envelope.desc response.can_repeat = red_envelope.can_repeat response.daily_play_count = red_envelope.daily_play_count response.prizes = {} for prize in red_envelope.prizes: prize_json = {} prize_json['prize_source'] = prize.prize_source prize_json['prize_type'] = prize.prize_type prize_json['prize_name'] = prize.name prize_json['prize_level'] = prize.level prize_json['prize_odds'] = prize.odds prize_json['prize_count'] = prize.count response.prizes[prize.id] = prize_json lottery_json = response.get_json() else: red_envelope = {'can_update': True} lottery_json = '' coupon_rules = get_coupon_rules(request.user) c = RequestContext(request, { 'first_nav_name': MARKET_TOOLS_NAV, 'second_navs': export.get_second_navs(request), 'second_nav_name': SECOND_NAV_NAME, 'red_envelope': red_envelope, 'lottery_json':lottery_json, 'coupon_rules':coupon_rules }) return render_to_response('red_envelope/editor/edit_red_envelope.html', c)
def edit_lottery_view(request, id): id = int(id) if id: lottery = Lottery.objects.get(id=id) lottery.can_update = False if lottery.status == LOTTERY_STATUS_NO_START: lottery.can_update = True relations = LotteryHasPrize.objects.filter(lottery=lottery) prize_ids = [r.prize_id for r in relations] prizes = Prize.objects.filter(id__in=prize_ids) id2prize = dict([(p.id, p) for p in prizes]) lottery.prizes = [] for r in relations: prize = id2prize[r.prize_id] prize.prize_source = r.prize_source prize.prize_type = r.prize_type lottery.prizes.append(prize) lottery.prizes.sort(lambda x,y: cmp(x.level, y.level)) #转换为json response = JsonResponse() response.id = lottery.id response.name = lottery.name response.detail = lottery.detail response.expend_integral = lottery.expend_integral response.can_repeat = lottery.can_repeat response.daily_play_count = lottery.daily_play_count response.type = lottery.type response.award_hour = lottery.award_hour response.not_win_desc = lottery.not_win_desc if lottery.award_type: response.award_type = lottery.award_type else: response.award_type = '' response.start_at = lottery.start_at.strftime("%Y-%m-%d") response.end_at = lottery.end_at.strftime("%Y-%m-%d") response.prizes = {} for prize in lottery.prizes: prize_json = {} prize_json['prize_source'] = prize.prize_source prize_json['prize_type'] = prize.prize_type prize_json['prize_name'] = prize.name prize_json['prize_level'] = prize.level prize_json['prize_odds'] = prize.odds prize_json['prize_count'] = prize.count response.prizes[prize.id] = prize_json lottery_json = response.get_json() else: lottery = {'can_update': True} lottery_json = '' coupon_rules = get_coupon_rules(request.user) c = RequestContext(request, { 'first_nav_name': MARKET_TOOLS_NAV, 'second_navs': export.get_second_navs(request), 'second_nav_name': SECOND_NAV_NAME, 'lottery': lottery, 'lottery_json':lottery_json, 'coupon_rules':coupon_rules }) return render_to_response('lottery/editor/edit_lottery.html', c)