def votecoderequests_accept(request, sel_event_id, vcrid): # CHeck for permissions if not request.user.has_perm('kompomaatti.change_votecode'): raise Http403 # Get the request vcr = get_object_or_404(VoteCodeRequest, pk=vcrid) # Add votecode for user. Bang your head to the wall until you succeed, etc. try: c = VoteCode() c.event_id = int(sel_event_id) c.key = str(hashlib.md5(bytes(os.urandom(8))).hexdigest()[:8]) c.associated_to = vcr.user c.time = timezone.now() c.save() logger.info('Votecode request from "{}" accepted.'.format( vcr.user.username), extra={ 'user': request.user, 'event_id': sel_event_id }) done = True except IntegrityError: done = False # Didn't happen, user likely already has votecode if not done: logger.info( 'Votecode request from "{}" scrapped; user already has votecode.'. format(vcr.user.username), extra={ 'user': request.user, 'event_id': sel_event_id }) # Request handled, delete it vcr.delete() # Return to admin page return HttpResponseRedirect( reverse('manage-kompomaatti:votecoderequests', args=(sel_event_id, )))
def votecodes(request, sel_event_id): # Handle form if request.method == 'POST': # CHeck for permissions if not request.user.has_perm('kompomaatti.add_votecode'): raise Http403 # Handle form gentokensform = CreateTokensForm(request.POST) if gentokensform.is_valid(): amount = int(gentokensform.cleaned_data['amount']) for n in range(amount): try: c = VoteCode() c.event_id = int(sel_event_id) c.key = str( hashlib.md5(bytes(os.urandom(8))).hexdigest()[:8]) c.save() except IntegrityError: n -= 1 logger.info('Votecodes generated.', extra={ 'user': request.user, 'event_id': sel_event_id }) return HttpResponseRedirect( reverse('manage-kompomaatti:votecodes', args=(sel_event_id, ))) else: gentokensform = CreateTokensForm() # Get tokens tokens = VoteCode.objects.filter(event_id=int(sel_event_id)) # Render response return admin_render( request, "admin_kompomaatti/votecodes.html", { 'tokens': tokens, 'gentokensform': gentokensform, 'selected_event_id': int(sel_event_id), })
def admin(request): # Make sure the user is superuser. if not request.user.is_superuser: raise Http404 # Check if we got filled form if request.method == 'POST': gentokensform = CreateTokensForm(request.POST) if gentokensform.is_valid(): amount = int(gentokensform.cleaned_data['amount']) for n in range(amount): try: c = VoteCode() c.key = unicode( hashlib.md5(str(random.random())).hexdigest()[:8]) c.save() except IntegrityError: n = n - 1 # Ugly, may cause infinite loop... return HttpResponseRedirect('/kompomaatti/admin/') else: gentokensform = CreateTokensForm() # Get data compos = Compo.objects.all() entries = Entry.objects.all() tokens = VoteCode.objects.all() vcreqs = VoteCodeRequest.objects.all() # Just dump the page return custom_render( request, 'kompomaatti/admin/admin.html', { 'tokens': tokens, 'entries': entries, 'compos': compos, 'vcreqs': vcreqs, 'gentokensform': gentokensform, })
def givecode(request, vcrid): # Make sure the user is superuser. if not request.user.is_superuser: raise Http404 # Get the request try: vcr = VoteCodeRequest.objects.get(id=vcrid) except VoteCodeRequest.DoesNotExist: raise Http404 # Add votecode for user. Bang your head to the wall until you succeed, etc. # Really, do something about this later! # TODO: Do something about this shit! done = False for i in range(25): try: c = VoteCode() c.key = unicode(hashlib.md5(str(random.random())).hexdigest()[:8]) c.associated_to = vcr.user c.time = datetime.now() c.save() done = True break except IntegrityError: pass if not done: raise HttpResponse( "Virhe yritettäessä lisätä satunnaista avainta ... FIXME!") # Delete request vcr.delete() # Return to admin page return HttpResponseRedirect('/kompomaatti/admin/')