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')
def activity_award_prizes(self, request, object_id, extra_context=None): opts = self.model._meta app_label = opts.app_label obj = self.get_object(request, unquote(object_id)) object_name = force_unicode(opts.verbose_name) participations = Participation.objects.filter(activity=obj).prefetch_related('participant', 'participant__person') participants = [] anonymous_participants = [] for participation in participations: if participation.note and not participation.note in anonymous_participants: anonymous_participants += [participation.note, ] elif not participation.participant in participants: participants += [participation.participant, ] uploads = ImageUpload.objects.filter( entity_id=obj.id, content_type=ContentType.objects.get_for_model(self.model)).prefetch_related('create_user', 'create_user__person').annotate(Count('votes')).order_by('-votes__count') uploaders = [] uploadIds = [] for upload in uploads: uploadIds += [upload.id, ] if not upload.create_user in uploaders: uploaders += [upload.create_user, ] votes = Vote.objects.filter( entity_id__in=uploadIds, content_type=ContentType.objects.get(model='imageupload')).prefetch_related('voter', 'voter__person') voters = [] for vote in votes: if not vote.voter in voters: voters += [vote.voter, ] if request.POST: award_name = request.POST['award_name'] #awarded_role = request.POST['awarded_role'] winner_amount = int(request.POST['winner_amount']) #award_type = request.POST['award_type'] repeat = request.POST['repeat'] reaward = request.POST['reaward'] obj_display = force_unicode(obj.title) if award_name == u'最高人氣獎': if reaward == 'yes': Award.objects.filter(name=u'最高人氣獎', activity=obj).delete() startIndex = 1 else: startIndex = Award.objects.filter(name=u'最高人氣獎', activity=obj).aggregate(Max('order'))['order__max'] + 1 awardedWinners = Award.objects.filter(name=u'最高人氣獎', activity=obj).values('winner_id') awardedIds = [awardedWinner['winner_id'] for awardedWinner in awardedWinners] popUploaders = [] for upload in uploads: if upload.create_user.id not in awardedIds and upload.create_user not in popUploaders: popUploaders += [upload.create_user, ] for index, uploader in enumerate(popUploaders): if index == winner_amount: break award = Award(name=u'最高人氣獎', order=index+startIndex, winner=uploader, activity=obj) award.save() self.message_user(request, u'已完成 %s 頒獎!' % award_name) elif award_name == u'投票幸運獎': if reaward == 'yes': Award.objects.filter(name=u'投票幸運獎', activity=obj).delete() startIndex = 1 else: startIndex = Award.objects.filter(name=u'投票幸運獎', activity=obj).aggregate(Max('order'))['order__max'] + 1 if repeat == 'no': awardedWinners = Award.objects.filter(activity=obj).values('winner_id') awardedIds = [awardedWinner['winner_id'] for awardedWinner in awardedWinners] filtered_voters = [voter for voter in voters if voter.id not in awardedIds] else: filtered_voters = voters from random import shuffle shuffle(filtered_voters) for index, voter in enumerate(filtered_voters): if index == winner_amount: break award = Award(name=u'投票幸運獎', order=index+startIndex, winner=voter, activity=obj) award.save() self.message_user(request, u'已完成 %s 頒獎!' % award_name) elif award_name == u'隨機抽獎': if reaward == 'yes': Award.objects.filter(name=u'隨機抽獎', activity=obj).delete() startIndex = 1 else: startIndex = Award.objects.filter(name=u'隨機抽獎', activity=obj).aggregate(Max('order'))['order__max'] + 1 if repeat == 'no': awardedWinners = Award.objects.filter(activity=obj).values('winner_extra') awardedIds = [awardedWinner['winner_extra'] for awardedWinner in awardedWinners] filtered_participants = [participant for participant in anonymous_participants if participant not in awardedIds] else: filtered_participants = anonymous_participants from random import shuffle shuffle(filtered_participants) user = User.objects.get(pk=1) for index, participant in enumerate(filtered_participants): if index == winner_amount: break award = Award(name=u'隨機抽獎', order=index+startIndex, winner=user, winner_extra=participant, activity=obj) award.save() self.message_user(request, u'已完成 %s 頒獎!' % award_name) elif award_name == u'產生認領碼': if reaward == 'yes': Award.objects.filter(name=u'產生認領碼', activity=obj).delete() startIndex = 1 else: startIndex = Award.objects.filter(name=u'產生認領碼', activity=obj).aggregate(Max('order'))['order__max'] + 1 claim_codes = set() while len(claim_codes) <= (winner_amount - startIndex): claim_codes.add(generate_claim_code()) for claim_code in claim_codes: award = Award(name=u'產生認領碼', winner=User.objects.get(pk=1), activity=obj, note=claim_code) award.save() self.message_user(request, u'已完成 %s 頒獎!' % award_name) else: self.message_user(request, u'目前不支援此頒獎組合!請選擇正確的得獎角色和頒獎方式。') popularAwards = Award.objects.filter(name=u'最高人氣獎', activity=obj).prefetch_related('winner', 'winner__person').order_by('order') luckyAwards = Award.objects.filter(name=u'投票幸運獎', activity=obj).prefetch_related('winner', 'winner__person').order_by('order') claimCodes = Award.objects.filter(name=u'產生認領碼', activity=obj).order_by('order') randomAwards = Award.objects.filter(name=u'隨機抽獎', activity=obj).order_by('order') for claimCode in claimCodes: claimCode.no_profile = True data = { 'title': '頒獎典禮', 'object_name': object_name, 'object': obj, 'opts': opts, 'app_label': app_label, 'participants': participants, 'anonymous_participants': anonymous_participants, 'uploaders': uploaders, 'voters': voters, 'awards': { u'隨機抽獎': randomAwards, u'最高人氣獎': popularAwards, u'投票幸運獎': luckyAwards, u'產生認領碼': claimCodes, } } return render(request, 'admin/activity_award_prizes.html', data)