def dashboard(request): # Get list of users entries my_entries = Entry.objects.filter(user=request.user) # Get list of open compos, format times open_compos = Compo.objects.filter(active=True, adding_end__gte = datetime.now()) oclist = [] for compo in open_compos: formatted_compo = compo_times_formatter(compo) oclist.append(formatted_compo) # Check if we got data from vote code assoc form if request.method == 'POST' and request.POST['formtype'] == 'votecodeassocform': assocform = VoteCodeAssocForm(request.POST) if assocform.is_valid(): code = assocform.cleaned_data['code'] try: vc = VoteCode.objects.get(key=code) vc.associated_to = request.user vc.time = datetime.now() vc.save() except VoteCode.DoesNotExist: pass return HttpResponseRedirect('/kompomaatti/myentries/') else: assocform = VoteCodeAssocForm() # Get last VoteCodeRequest, if it exists try: vcreq = VoteCodeRequest.objects.get(user=request.user) except VoteCodeRequest.DoesNotExist: vcreq = None # Check if we got data from vote code request form if request.method == 'POST' and request.POST['formtype'] == 'requestvotecodeform': requestform = RequestVoteCodeForm(request.POST) if requestform.is_valid(): if vcreq: vcreq.text = requestform.cleaned_data['text'] vcreq.save() else: req = requestform.save(commit=False) req.user = request.user req.save() return HttpResponseRedirect('/kompomaatti/myentries/') else: if vcreq: requestform = RequestVoteCodeForm(instance=vcreq) else: requestform = RequestVoteCodeForm() # Dump the page to the user return custom_render(request, 'kompomaatti/myentries.html', { 'myentries': my_entries, 'opencompos': oclist, 'user': request.user, 'assocform': assocform, 'requestform': requestform, })
def compos(request, event_id): # Get compos, format times compo_list = [] for compo in Compo.objects.filter(active=True, event_id=int(event_id)): compo_list.append(compo_times_formatter(compo)) # Dump the template return render(request, 'kompomaatti/compos.html', { 'sel_event_id': int(event_id), 'compos': compo_list, })
def compos(request, event_id): # Get compos, format times compo_list = [] for compo in Compo.objects.filter(active=True, event_id=int(event_id)): compo_list.append(compo_times_formatter(compo)) # Dump the template return render(request, 'kompomaatti/compos.html', { 'sel_event_id': int(event_id), 'compos': compo_list, })
def compolist(request): # Get compos, format times composet = Compo.objects.filter(active=True).order_by('compo_start') compos = [] for compo in composet: compos.append(compo_times_formatter(compo)) # Get entries in compos. If compo has been flagged to show voting results, # then get those. Otherwise just show entries in alphabetical order (by entry name). entries = {} for compo in compos: if compo.show_voting_results: # Get entries entries_temp = {} for entry in Entry.objects.filter(compo=compo): entries_temp[entry.id] = { 'id': entry.id, 'creator': entry.creator, 'name': entry.name, 'score': 0.0, 'disqualified': entry.disqualified, } # Want to show disqualified entries dead last. if entry.disqualified: entries_temp[entry.id]['score'] = -1.0 # Get score for each entry. Score should be 0 for all disqualified entries, # so just discard those. Also skip votes with rank = 0. (division by zero etc.) :P all_votes = Vote.objects.select_related(depth=1).filter( compo=compo) for vote in all_votes: if not vote.entry.disqualified or vote.rank > 0: entries_temp[vote.entry.id]['score'] += (1.0 / vote.rank) # Sort entries by score, highest score first (of course). entries[compo.id] = sorted(entries_temp.values(), key=itemgetter('score'), reverse=True) else: # Just get entries in alphabetical order entries[compo.id] = Entry.objects.filter( compo=compo).order_by('name') # Return page return custom_render(request, 'kompomaatti/compolist.html', { 'compolist': compos, 'entries': entries })
def compolist(request): # Get compos, format times composet = Compo.objects.filter(active=True).order_by('compo_start') compos = [] for compo in composet: compos.append(compo_times_formatter(compo)) # Get entries in compos. If compo has been flagged to show voting results, # then get those. Otherwise just show entries in alphabetical order (by entry name). entries = {} for compo in compos: if compo.show_voting_results: # Get entries entries_temp = {} for entry in Entry.objects.filter(compo=compo): entries_temp[entry.id] = { 'id': entry.id, 'creator': entry.creator, 'name': entry.name, 'score': 0.0, 'disqualified': entry.disqualified, } # Want to show disqualified entries dead last. if entry.disqualified: entries_temp[entry.id]['score'] = -1.0 # Get score for each entry. Score should be 0 for all disqualified entries, # so just discard those. Also skip votes with rank = 0. (division by zero etc.) :P all_votes = Vote.objects.select_related(depth=1).filter(compo=compo) for vote in all_votes: if not vote.entry.disqualified or vote.rank > 0: entries_temp[vote.entry.id]['score'] += (1.0 / vote.rank) # Sort entries by score, highest score first (of course). entries[compo.id] = sorted(entries_temp.values(), key=itemgetter('score'), reverse=True) else: # Just get entries in alphabetical order entries[compo.id] = Entry.objects.filter(compo=compo).order_by('name') # Return page return custom_render(request, 'kompomaatti/compolist.html', { 'compolist': compos, 'entries': entries })
def compo(request, compo_id): # Get compo information try: c = Compo.objects.get(id=compo_id, active=True) except ObjectDoesNotExist: raise Http404 # Format times and stuff c = compo_times_formatter(c) # The following is only relevant, if the user is logged in and valid. has_voted = False voting_open = False votes = {} if request.user.is_authenticated(): # Check if user has already voted votes = Vote.objects.filter(user=request.user, compo=c).order_by('rank') if votes.count() > 0: has_voted = True # Check if voting is open now = datetime.now() if c.voting_start <= now and now < c.voting_end: voting_open = True # Check if we want to do something with forms and stuff. if request.method == 'POST': if voting_open: # Make sure the user has a valid votecode try: votecode = VoteCode.objects.get(associated_to=request.user) except: return HttpResponse("Ei äänestysoikeutta!") # Get entries in compo that are not disqualified compo_entries = Entry.objects.filter(compo=c, disqualified=False) # Get the input data, and format it so that we can handle it. # HTML mode and JS mode voting systems give out different kind # of data order = [] tmp = {} if request.POST['action'] == 'vote_html': for entry in compo_entries: check_for = "ventry_"+str(entry.id) if not request.POST.has_key(check_for): return HttpResponse("Virhe syötteen käsittelyssä!") try: tmp[entry.id] = int(request.POST[check_for]) except: return HttpResponse("Virhe syötteen käsittelyssä!") order = sorted(tmp, key=tmp.get) else: order_raw = request.POST.getlist('order[]') for id in order_raw: try: order.append(int(id)) except: return HttpResponse("Virhe syötteen käsittelyssä!") # Remove old votes by this user, on this compo if has_voted: Vote.objects.filter(user=request.user, compo=c).delete() # Check voting input for cheating :P # See if all entries have a rank. for entry in compo_entries: if entry.id not in order: return HttpResponse("Virhe syötteen käsittelyssä!") # See that we have the right amount of entries if len(order) != len(compo_entries): return HttpResponse("Virhe syötteen käsittelyssä!") # Make sure that no entry is in the list twice checked_ids = [] for entryid in order: if entryid not in checked_ids: checked_ids.append(entryid) else: return HttpResponse("Virhe syötteen käsittelyssä!") # Add new votes, if there were no errors number = 1 for entry_id in order: vote = Vote() vote.user = request.user vote.compo = c vote.entry = Entry.objects.get(id=entry_id) vote.rank = number vote.save() number += 1 # Select response mode according to input if request.POST['action'] == 'vote_html': return HttpResponseRedirect('/kompomaatti/compo/'+compo_id+'/') else: return HttpResponse("0") # 0 = Success. else: # If voting is closed, just show 404. This shouldn't really happen ... raise Http404 # Get entries. # If voting is open, and user has already voted, get the order of entries by previous voting # If voting is open, and user has NOT voted yet, get the entries in random order # Otherwise just get entries sorted by name # Make sure that no disqualified entries are included if voting is open. No need to vote for those ... if voting_open and has_voted: e = [] # First go through the entries that have been voted for and add them to list. for vote in votes: if not vote.entry.disqualified: e.append(vote.entry) # Then, make sure to also show entries that have NOT been voted previously by the user # (if entry has been added late) entries_tmp = Entry.objects.filter(compo=c,disqualified=False).order_by('?') for entry in entries_tmp: if entry not in e: e.append(entry) elif voting_open: e = Entry.objects.filter(compo=c,disqualified=False).order_by('?') else: e = Entry.objects.filter(compo=c).order_by('name') # Render the page. Ya, rly. return custom_render(request, 'kompomaatti/compo.html', { 'compo': c, 'entries': e, 'voting_open': voting_open, 'has_voted': has_voted })
def compo_details(request, event_id, compo_id): # Get compo compo = compo_times_formatter(get_object_or_404(Compo, pk=compo_id, active=True, event=event_id)) # Check if user may vote (voting open, user has code) can_vote = False if request.user.is_active and request.user.is_authenticated(): try: VoteCode.objects.get(associated_to=request.user, event=event_id) can_vote = True except VoteCode.DoesNotExist: pass # See if ticket is used as votecode try: TicketVoteCode.objects.get(associated_to=request.user, event=event_id) can_vote = True except TicketVoteCode.DoesNotExist: pass # Handle entry adding if request.method == 'POST' and compo.is_adding_open(): # Make sure user is authenticated if not request.user.is_active or not request.user.is_authenticated(): raise Http403 # Handle data entryform = EntryForm(request.POST, request.FILES, compo=compo) if entryform.is_valid(): entry = entryform.save(commit=False) entry.user = request.user entry.compo = compo entry.save() return HttpResponseRedirect(reverse('km:compo', args=(event_id, compo_id,))) else: entryform = EntryForm(compo=compo) # Get entries, and only show them if voting has started # (only show results if it has been allowed in model) all_entries = [] if compo.has_voting_started: if compo.show_voting_results: all_entries = entrysort.sort_by_score(Entry.objects.filter(compo=compo)) else: all_entries = Entry.objects.filter(compo=compo).order_by('name') # Stuff for users that have logged in my_entries = [] has_voted = False if request.user.is_active and request.user.is_authenticated(): # Get all entries added by the user my_entries = Entry.objects.filter(compo=compo, user=request.user) # Check if user has already voted if Vote.objects.filter(user=request.user, compo=compo).count() > 0: has_voted = True # Dump template return render(request, 'kompomaatti/compo_details.html', { 'sel_event_id': int(event_id), 'compo': compo, 'entryform': entryform, 'can_vote': can_vote, 'all_entries': all_entries, 'my_entries': my_entries, 'has_voted': has_voted, })
def compo_details(request, event_id, compo_id): # Get compo compo = compo_times_formatter(get_object_or_404(Compo, pk=compo_id, active=True, event=event_id)) # Check if user may vote (voting open, user has code) can_vote = False if request.user.is_active and request.user.is_authenticated: # See if ticket is used as votecode try: TicketVoteCode.objects.get(associated_to=request.user, event=event_id) can_vote = True except TicketVoteCode.DoesNotExist: pass if not can_vote: try: VoteCodeRequest.objects.get(user=request.user, event=event_id, status=1) can_vote = True except VoteCodeRequest.DoesNotExist: pass # Handle entry adding if request.method == 'POST' and compo.is_adding_open(): # Make sure user is authenticated if not request.user.is_active or not request.user.is_authenticated: raise Http403 # Handle data entryform = EntryForm(request.POST, request.FILES, compo=compo) if entryform.is_valid(): entry = entryform.save(commit=False) entry.user = request.user entry.compo = compo entry.save() return HttpResponseRedirect(reverse('km:compo', args=(event_id, compo_id,))) else: entryform = EntryForm(compo=compo) # Get entries, and only show them if voting has started # (only show results if it has been allowed in model) all_entries = [] if compo.has_voting_started: if compo.show_voting_results: all_entries = entrysort.sort_by_score(Entry.objects.filter(compo=compo)) else: all_entries = Entry.objects.filter(compo=compo).order_by('name') # Stuff for users that have logged in my_entries = [] has_voted = False if request.user.is_active and request.user.is_authenticated: # Get all entries added by the user my_entries = Entry.objects.filter(compo=compo, user=request.user) # Check if user has already voted if Vote.objects.filter(user=request.user, compo=compo).count() > 0: has_voted = True # Dump template return render(request, 'kompomaatti/compo_details.html', { 'sel_event_id': int(event_id), 'compo': compo, 'entryform': entryform, 'can_vote': can_vote, 'all_entries': all_entries, 'my_entries': my_entries, 'has_voted': has_voted, })
def compo(request, compo_id): # Get compo information try: c = Compo.objects.get(id=compo_id, active=True) except ObjectDoesNotExist: raise Http404 # Format times and stuff c = compo_times_formatter(c) # The following is only relevant, if the user is logged in and valid. has_voted = False voting_open = False votes = {} if request.user.is_authenticated(): # Check if user has already voted votes = Vote.objects.filter(user=request.user, compo=c).order_by('rank') if votes.count() > 0: has_voted = True # Check if voting is open now = datetime.now() if c.voting_start <= now and now < c.voting_end: voting_open = True # Check if we want to do something with forms and stuff. if request.method == 'POST': if voting_open: # Make sure the user has a valid votecode try: votecode = VoteCode.objects.get(associated_to=request.user) except: return HttpResponse("Ei äänestysoikeutta!") # Get entries in compo that are not disqualified compo_entries = Entry.objects.filter(compo=c, disqualified=False) # Get the input data, and format it so that we can handle it. # HTML mode and JS mode voting systems give out different kind # of data order = [] tmp = {} if request.POST['action'] == 'vote_html': for entry in compo_entries: check_for = "ventry_" + str(entry.id) if not request.POST.has_key(check_for): return HttpResponse("Virhe syötteen käsittelyssä!") try: tmp[entry.id] = int(request.POST[check_for]) except: return HttpResponse("Virhe syötteen käsittelyssä!") order = sorted(tmp, key=tmp.get) else: order_raw = request.POST.getlist('order[]') for id in order_raw: try: order.append(int(id)) except: return HttpResponse("Virhe syötteen käsittelyssä!") # Remove old votes by this user, on this compo if has_voted: Vote.objects.filter(user=request.user, compo=c).delete() # Check voting input for cheating :P # See if all entries have a rank. for entry in compo_entries: if entry.id not in order: return HttpResponse("Virhe syötteen käsittelyssä!") # See that we have the right amount of entries if len(order) != len(compo_entries): return HttpResponse("Virhe syötteen käsittelyssä!") # Make sure that no entry is in the list twice checked_ids = [] for entryid in order: if entryid not in checked_ids: checked_ids.append(entryid) else: return HttpResponse("Virhe syötteen käsittelyssä!") # Add new votes, if there were no errors number = 1 for entry_id in order: vote = Vote() vote.user = request.user vote.compo = c vote.entry = Entry.objects.get(id=entry_id) vote.rank = number vote.save() number += 1 # Select response mode according to input if request.POST['action'] == 'vote_html': return HttpResponseRedirect('/kompomaatti/compo/' + compo_id + '/') else: return HttpResponse("0") # 0 = Success. else: # If voting is closed, just show 404. This shouldn't really happen ... raise Http404 # Get entries. # If voting is open, and user has already voted, get the order of entries by previous voting # If voting is open, and user has NOT voted yet, get the entries in random order # Otherwise just get entries sorted by name # Make sure that no disqualified entries are included if voting is open. No need to vote for those ... if voting_open and has_voted: e = [] # First go through the entries that have been voted for and add them to list. for vote in votes: if not vote.entry.disqualified: e.append(vote.entry) # Then, make sure to also show entries that have NOT been voted previously by the user # (if entry has been added late) entries_tmp = Entry.objects.filter(compo=c, disqualified=False).order_by('?') for entry in entries_tmp: if entry not in e: e.append(entry) elif voting_open: e = Entry.objects.filter(compo=c, disqualified=False).order_by('?') else: e = Entry.objects.filter(compo=c).order_by('name') # Render the page. Ya, rly. return custom_render( request, 'kompomaatti/compo.html', { 'compo': c, 'entries': e, 'voting_open': voting_open, 'has_voted': has_voted })