def index(request): c = {} try: end_level = Level.objects.latest('pk') except Level.DoesNotExist: raise Http404 from challenge.core.models import Config try: config = Config.objects.get(pk=1) except Config.DoesNotExist: return Http404 c['config'] = config if not config.active: return prtr ("closed.html", c, request) user = request.user try: score = Score.objects.get(user=user) except: score = Score(user=user, max_level=0) if score.max_level == end_level.pk: return HttpResponseRedirect('/done/') level = score.max_level + 1 level = Level.objects.get(pk=level) if request.method == "POST": answer = unicode(request.POST['answer']).upper() attempt = Attempt (user=user, level=level, answer=answer) if not level.multianswer: if answer == unicode(level.answer).upper(): score.max_level += 1 score.save() attempt.correct = True attempt.save () return HttpResponseRedirect('/') else: attempt.correct = False attempt.save () c['error'] = 'Feil svar! Prøv igjen :-D' else: for level_answer in unicode(level.answer).split('||'): if answer == unicode(level_answer).upper(): score.max_level += 1 score.save() attempt.correct = True attempt.save () return HttpResponseRedirect('/') c['error'] = 'Feil svar! Prøv igjen :-D' attempt.correct = False attempt.save () c['level'] = level return prtr("levels.html", c, request)
def register(request): from challenge.core.common import prtr from challenge.core.forms import UserForm from django.contrib.auth.models import User from django.contrib.auth import login, authenticate from django.http import HttpResponseRedirect c = { 'form': UserForm() } if request.method != "POST": return prtr ("core/register.html", c, request) data = request.POST.copy() c['form'] = form = UserForm(data) if not form.is_valid(): c['error'] = True return prtr ("core/register.html", c, request) if form.cleaned_data['password'] != form.cleaned_data['password2']: c['password_mismatch'] = True return prtr ("core/register.html", c, request) if User.objects.filter(username__iexact=form.cleaned_data['username']).count() > 0: c['user_exists'] = True return prtr ("core/register.html", c, request) try: user = User.objects.create_user (form.cleaned_data['username'], form.cleaned_data['email'], form.cleaned_data['password']) except: c['user_exists'] = True return prtr ("core/register.html", c, request) user.first_name = form.cleaned_data['first_name'] user.last_name = form.cleaned_data['last_name'] user.save() user = authenticate(username=user.username, password=form.cleaned_data['password']) login(request, user) return HttpResponseRedirect('/')
def attempts(request, getnum=None): if getnum == None: getnum = 25 c = {} c["getnum"] = getnum c["attempts"] = Attempt.objects.order_by("-pk")[:getnum] return prtr("attempts.html", c, request)
def done(request): c = {} try: score = Score.objects.get(user=request.user) except: return HttpResponseRedirect('/') end_level = Level.objects.latest('pk') if score.max_level == end_level.pk: score = Score.objects.get(user=request.user) c['score'] = score return prtr ("done.html", c, request) else: return HttpResponseRedirect('/')
def score(request): c = {} c["score"] = Score.objects.order_by("updated", "-max_level") return prtr("score.html", c, request)