Exemplo n.º 1
0
def firefox_family_get_ticket(request):
    periods = ('10:00', '10:30', '11:00', '11:30', '12:00', '12:30', '13:00', '13:30', '14:00', '14:30', '15:00',
               '15:30', '16:00', '16:30')
    weights = (4, 4, 4, 4, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3)
    day_mapping = {
        '1': '11/22',
        '2': '11/23',
    }
    data = {'result': 'failed'}
    if request.user.is_active:
        currentCampaign = Campaign.objects.get(slug=tenYearsCampaignSlug)
        existing = Award.objects.filter(name=u'早鳥票', activity=currentCampaign, winner=request.user)
        ticketExists = existing.exists()
        if currentCampaign.status == 'end':
            data['result'] = 'ended'
        elif not ticketExists and 'day' in request.GET:
            day = day_mapping[request.GET['day']]
            suggested_period = weighted_sample(periods, weights)[0]
            #save award
            session = '%s %s' % (day, suggested_period)
            while True:
                verification_code = generate_claim_code()
                if not Award.objects.filter(name=u'早鳥票', activity=currentCampaign, note=verification_code).exists():
                    break
            ticket = Award(name=u'早鳥票', activity=currentCampaign, note=verification_code,
                           winner=request.user, winner_extra=session)
            ticket.save()
            data['session'] = session
            data['code'] = verification_code
            data['result'] = 'success'
            data['existing'] = False
            generate_10years_ticket(data['session'], data['code'])
        elif ticketExists:
            data['result'] = 'success'
            data['session'] = existing[0].winner_extra
            data['code'] = existing[0].note
            data['existing'] = True
        else:
            data['message'] = 'invalid request'
    else:
        data['message'] = 'unauthorized'
    response = json.dumps(data)
    return HttpResponse(response, mimetype='application/x-javascript')
Exemplo n.º 2
0
def firefox_family_lottery(request):
    prices_levels = (('sorry', 'notebook', 'totebag', 'carsticker', 'nbsticker'),
                     ('mug', 'taipeipass', 'backpack', 'fxosphone'))
    data = {'result': 'failed'}
    if request.user.is_active:
        currentCampaign = Campaign.objects.get(slug=tenYearsCampaignSlug)
        existing = Award.objects.filter(name=u'幸運轉輪', activity=currentCampaign, winner=request.user)
        if currentCampaign.status == 'end':
            data['result'] = 'ended'
        elif not existing.exists() and 'level' in request.GET:
            level = int(request.GET['level'])
            price_keys = prices_levels[0] if level == 0 else prices_levels[0] + prices_levels[1]
            prices = Price.objects.filter(name__in=price_keys)
            weights = []
            for price in prices:
                if price.name == 'sorry':
                    weights += [1000, ]
                else:
                    weights += [price.quantity, ]
            winning_price = weighted_sample(prices, weights)[0]
            #save award, decrease price quantity
            if winning_price.name != 'sorry':
                winning_price.quantity -= 1
                winning_price.save()
            lottery_award = Award(name=u'幸運轉輪', activity=currentCampaign, winner=request.user, price=winning_price)
            lottery_award.save()
            data['slug'] = winning_price.name
            data['name'] = winning_price.description
            data['result'] = 'success'
            data['existing'] = False
        elif existing.exists():
            data['result'] = 'success'
            data['slug'] = existing[0].price.name
            data['name'] = existing[0].price.description
            data['existing'] = True
        else:
            data['message'] = 'invalid request'
    else:
        data['message'] = 'unauthorized'
    response = json.dumps(data)
    return HttpResponse(response, mimetype='application/x-javascript')