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 = datetime.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 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), })