event/views.py
author nishanth
Thu, 22 Apr 2010 00:36:08 +0530
changeset 41 d424b9668a74
parent 39 0fa055b8ea98
child 42 007d87112911
permissions -rw-r--r--
implemented list toppers function
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
41
d424b9668a74 implemented list toppers function
nishanth
parents: 39
diff changeset
     1
from django.db.models import Sum
6
8929b82c1dbb added home page with minimum functionality
nishanth
parents: 4
diff changeset
     2
from django.http import Http404
9
601057af86c2 implemented event create
nishanth
parents: 7
diff changeset
     3
from django.shortcuts import render_to_response, redirect
6
8929b82c1dbb added home page with minimum functionality
nishanth
parents: 4
diff changeset
     4
39
0fa055b8ea98 implemented correct_quiz function and added it at the time of closing quiz and modified question bank
nishanth
parents: 38
diff changeset
     5
from offline.settings import ADMIN_KEY
0fa055b8ea98 implemented correct_quiz function and added it at the time of closing quiz and modified question bank
nishanth
parents: 38
diff changeset
     6
6
8929b82c1dbb added home page with minimum functionality
nishanth
parents: 4
diff changeset
     7
from offline.event.models import Event
9
601057af86c2 implemented event create
nishanth
parents: 7
diff changeset
     8
from offline.event.forms import EventCreateForm
39
0fa055b8ea98 implemented correct_quiz function and added it at the time of closing quiz and modified question bank
nishanth
parents: 38
diff changeset
     9
41
d424b9668a74 implemented list toppers function
nishanth
parents: 39
diff changeset
    10
from offline.quiz.models import Quiz
39
0fa055b8ea98 implemented correct_quiz function and added it at the time of closing quiz and modified question bank
nishanth
parents: 38
diff changeset
    11
from offline.quiz.utils import correct_quiz
7
dfedb369f32e first fixed on the urls and then gave the admin_key in settings and then created main admin page.
nishanth
parents: 6
diff changeset
    12
34
8d0d82c981b3 added "return to admin page" link in open_quiz page
nishanth
parents: 26
diff changeset
    13
num2name = {"11" : "Day 1 Quiz 1",
8d0d82c981b3 added "return to admin page" link in open_quiz page
nishanth
parents: 26
diff changeset
    14
            "12" : "Day 1 Quiz 2",
8d0d82c981b3 added "return to admin page" link in open_quiz page
nishanth
parents: 26
diff changeset
    15
            "21" : "Day 2 Quiz 1",
8d0d82c981b3 added "return to admin page" link in open_quiz page
nishanth
parents: 26
diff changeset
    16
           }
8d0d82c981b3 added "return to admin page" link in open_quiz page
nishanth
parents: 26
diff changeset
    17
6
8929b82c1dbb added home page with minimum functionality
nishanth
parents: 4
diff changeset
    18
def event_home(request):
8929b82c1dbb added home page with minimum functionality
nishanth
parents: 4
diff changeset
    19
    """ The page for people to view.
8929b82c1dbb added home page with minimum functionality
nishanth
parents: 4
diff changeset
    20
    """
8929b82c1dbb added home page with minimum functionality
nishanth
parents: 4
diff changeset
    21
8929b82c1dbb added home page with minimum functionality
nishanth
parents: 4
diff changeset
    22
    try:
8929b82c1dbb added home page with minimum functionality
nishanth
parents: 4
diff changeset
    23
        event = Event.objects.all()[0]
8929b82c1dbb added home page with minimum functionality
nishanth
parents: 4
diff changeset
    24
    except IndexError:
8929b82c1dbb added home page with minimum functionality
nishanth
parents: 4
diff changeset
    25
        raise Http404
8929b82c1dbb added home page with minimum functionality
nishanth
parents: 4
diff changeset
    26
8929b82c1dbb added home page with minimum functionality
nishanth
parents: 4
diff changeset
    27
    ip = request.META['REMOTE_ADDR']
8929b82c1dbb added home page with minimum functionality
nishanth
parents: 4
diff changeset
    28
8929b82c1dbb added home page with minimum functionality
nishanth
parents: 4
diff changeset
    29
    can_submit_feedback = True if event.feedback_status != '0' and \
8929b82c1dbb added home page with minimum functionality
nishanth
parents: 4
diff changeset
    30
            not event.feedback.filter(day=event.feedback_status,user_ip=ip) else False
23
f57c30096b51 added the link on home to attempt quiz
nishanth
parents: 9
diff changeset
    31
    can_take_quiz = True if event.quiz_status != "00" else False
6
8929b82c1dbb added home page with minimum functionality
nishanth
parents: 4
diff changeset
    32
8929b82c1dbb added home page with minimum functionality
nishanth
parents: 4
diff changeset
    33
    return render_to_response('home.html', {'event':event, 'can_submit_feedback':can_submit_feedback, 'can_take_quiz':can_take_quiz})
8929b82c1dbb added home page with minimum functionality
nishanth
parents: 4
diff changeset
    34
9
601057af86c2 implemented event create
nishanth
parents: 7
diff changeset
    35
def event_admin(request, admin_key):
7
dfedb369f32e first fixed on the urls and then gave the admin_key in settings and then created main admin page.
nishanth
parents: 6
diff changeset
    36
    """ see if the key is correct and then display options.
dfedb369f32e first fixed on the urls and then gave the admin_key in settings and then created main admin page.
nishanth
parents: 6
diff changeset
    37
    """
dfedb369f32e first fixed on the urls and then gave the admin_key in settings and then created main admin page.
nishanth
parents: 6
diff changeset
    38
9
601057af86c2 implemented event create
nishanth
parents: 7
diff changeset
    39
    if not admin_key == ADMIN_KEY:
7
dfedb369f32e first fixed on the urls and then gave the admin_key in settings and then created main admin page.
nishanth
parents: 6
diff changeset
    40
        raise Http404
dfedb369f32e first fixed on the urls and then gave the admin_key in settings and then created main admin page.
nishanth
parents: 6
diff changeset
    41
dfedb369f32e first fixed on the urls and then gave the admin_key in settings and then created main admin page.
nishanth
parents: 6
diff changeset
    42
    try:
dfedb369f32e first fixed on the urls and then gave the admin_key in settings and then created main admin page.
nishanth
parents: 6
diff changeset
    43
        event = Event.objects.all()[0]
dfedb369f32e first fixed on the urls and then gave the admin_key in settings and then created main admin page.
nishanth
parents: 6
diff changeset
    44
    except IndexError:
dfedb369f32e first fixed on the urls and then gave the admin_key in settings and then created main admin page.
nishanth
parents: 6
diff changeset
    45
        return redirect('/event/create/%s'%ADMIN_KEY)
dfedb369f32e first fixed on the urls and then gave the admin_key in settings and then created main admin page.
nishanth
parents: 6
diff changeset
    46
dfedb369f32e first fixed on the urls and then gave the admin_key in settings and then created main admin page.
nishanth
parents: 6
diff changeset
    47
    return render_to_response('admin.html', {'event':event, 'admin_key':ADMIN_KEY})
dfedb369f32e first fixed on the urls and then gave the admin_key in settings and then created main admin page.
nishanth
parents: 6
diff changeset
    48
    
9
601057af86c2 implemented event create
nishanth
parents: 7
diff changeset
    49
def event_create(request, admin_key):
601057af86c2 implemented event create
nishanth
parents: 7
diff changeset
    50
601057af86c2 implemented event create
nishanth
parents: 7
diff changeset
    51
    if not admin_key == ADMIN_KEY:
601057af86c2 implemented event create
nishanth
parents: 7
diff changeset
    52
        raise Http404
601057af86c2 implemented event create
nishanth
parents: 7
diff changeset
    53
601057af86c2 implemented event create
nishanth
parents: 7
diff changeset
    54
    try:
601057af86c2 implemented event create
nishanth
parents: 7
diff changeset
    55
        event = Event.objects.all()[0]
601057af86c2 implemented event create
nishanth
parents: 7
diff changeset
    56
        return redirect("/event/admin/%s"%admin_key)
601057af86c2 implemented event create
nishanth
parents: 7
diff changeset
    57
    except IndexError:
601057af86c2 implemented event create
nishanth
parents: 7
diff changeset
    58
        new_event = Event()
7
dfedb369f32e first fixed on the urls and then gave the admin_key in settings and then created main admin page.
nishanth
parents: 6
diff changeset
    59
9
601057af86c2 implemented event create
nishanth
parents: 7
diff changeset
    60
    if request.method == "POST":
601057af86c2 implemented event create
nishanth
parents: 7
diff changeset
    61
        form = EventCreateForm(request.POST)
601057af86c2 implemented event create
nishanth
parents: 7
diff changeset
    62
        if form.is_valid():
601057af86c2 implemented event create
nishanth
parents: 7
diff changeset
    63
            new_event.title = form.cleaned_data['title']
601057af86c2 implemented event create
nishanth
parents: 7
diff changeset
    64
            new_event.start_date = form.cleaned_data['start_date']
601057af86c2 implemented event create
nishanth
parents: 7
diff changeset
    65
            new_event.stop_date = form.cleaned_data['stop_date']
7
dfedb369f32e first fixed on the urls and then gave the admin_key in settings and then created main admin page.
nishanth
parents: 6
diff changeset
    66
9
601057af86c2 implemented event create
nishanth
parents: 7
diff changeset
    67
            new_event.save()
601057af86c2 implemented event create
nishanth
parents: 7
diff changeset
    68
            return redirect('/event/admin/%s'%ADMIN_KEY)
601057af86c2 implemented event create
nishanth
parents: 7
diff changeset
    69
        else:
601057af86c2 implemented event create
nishanth
parents: 7
diff changeset
    70
            return render_to_response('create_event.html',{'form':form})
601057af86c2 implemented event create
nishanth
parents: 7
diff changeset
    71
    else:
601057af86c2 implemented event create
nishanth
parents: 7
diff changeset
    72
        form = EventCreateForm()
601057af86c2 implemented event create
nishanth
parents: 7
diff changeset
    73
        return render_to_response('create_event.html',{'form':form})
23
f57c30096b51 added the link on home to attempt quiz
nishanth
parents: 9
diff changeset
    74
f57c30096b51 added the link on home to attempt quiz
nishanth
parents: 9
diff changeset
    75
def open_quiz(request, admin_key):
25
4143a8f90b00 added open quiz functionality
nishanth
parents: 23
diff changeset
    76
    """ check for admin and then for quiz status.
4143a8f90b00 added open quiz functionality
nishanth
parents: 23
diff changeset
    77
    """
4143a8f90b00 added open quiz functionality
nishanth
parents: 23
diff changeset
    78
4143a8f90b00 added open quiz functionality
nishanth
parents: 23
diff changeset
    79
    if not admin_key == ADMIN_KEY:
4143a8f90b00 added open quiz functionality
nishanth
parents: 23
diff changeset
    80
        raise Http404
4143a8f90b00 added open quiz functionality
nishanth
parents: 23
diff changeset
    81
4143a8f90b00 added open quiz functionality
nishanth
parents: 23
diff changeset
    82
    try:
4143a8f90b00 added open quiz functionality
nishanth
parents: 23
diff changeset
    83
        event = Event.objects.all()[0]
4143a8f90b00 added open quiz functionality
nishanth
parents: 23
diff changeset
    84
    except IndexError:
4143a8f90b00 added open quiz functionality
nishanth
parents: 23
diff changeset
    85
        return redirect('/event/create/%s'%ADMIN_KEY)
23
f57c30096b51 added the link on home to attempt quiz
nishanth
parents: 9
diff changeset
    86
25
4143a8f90b00 added open quiz functionality
nishanth
parents: 23
diff changeset
    87
    if event.quiz_status != "00":
4143a8f90b00 added open quiz functionality
nishanth
parents: 23
diff changeset
    88
        return redirect('/event/create/%s'%ADMIN_KEY)
4143a8f90b00 added open quiz functionality
nishanth
parents: 23
diff changeset
    89
4143a8f90b00 added open quiz functionality
nishanth
parents: 23
diff changeset
    90
    if request.method=="POST":
4143a8f90b00 added open quiz functionality
nishanth
parents: 23
diff changeset
    91
        try:
4143a8f90b00 added open quiz functionality
nishanth
parents: 23
diff changeset
    92
            quiz_num = request.POST['quiz_num']
4143a8f90b00 added open quiz functionality
nishanth
parents: 23
diff changeset
    93
            event.quiz_status = quiz_num
4143a8f90b00 added open quiz functionality
nishanth
parents: 23
diff changeset
    94
            event.save()
34
8d0d82c981b3 added "return to admin page" link in open_quiz page
nishanth
parents: 26
diff changeset
    95
            return render_to_response("open_quiz.html",{"admin_key":ADMIN_KEY, "quiz_name":num2name[quiz_num], 'success':True})
25
4143a8f90b00 added open quiz functionality
nishanth
parents: 23
diff changeset
    96
        except MultiValueDictKeyError:
4143a8f90b00 added open quiz functionality
nishanth
parents: 23
diff changeset
    97
            raise Http404
4143a8f90b00 added open quiz functionality
nishanth
parents: 23
diff changeset
    98
    else:
4143a8f90b00 added open quiz functionality
nishanth
parents: 23
diff changeset
    99
        return render_to_response("open_quiz.html")
4143a8f90b00 added open quiz functionality
nishanth
parents: 23
diff changeset
   100
   
26
10760aa2cf52 added close quiz functionality
nishanth
parents: 25
diff changeset
   101
def close_quiz(request, admin_key):
10760aa2cf52 added close quiz functionality
nishanth
parents: 25
diff changeset
   102
    """ check for admin and then for quiz status.
10760aa2cf52 added close quiz functionality
nishanth
parents: 25
diff changeset
   103
    """
10760aa2cf52 added close quiz functionality
nishanth
parents: 25
diff changeset
   104
10760aa2cf52 added close quiz functionality
nishanth
parents: 25
diff changeset
   105
    if not admin_key == ADMIN_KEY:
10760aa2cf52 added close quiz functionality
nishanth
parents: 25
diff changeset
   106
        raise Http404
10760aa2cf52 added close quiz functionality
nishanth
parents: 25
diff changeset
   107
10760aa2cf52 added close quiz functionality
nishanth
parents: 25
diff changeset
   108
    try:
10760aa2cf52 added close quiz functionality
nishanth
parents: 25
diff changeset
   109
        event = Event.objects.all()[0]
10760aa2cf52 added close quiz functionality
nishanth
parents: 25
diff changeset
   110
    except IndexError:
10760aa2cf52 added close quiz functionality
nishanth
parents: 25
diff changeset
   111
        return redirect('/event/create/%s'%ADMIN_KEY)
10760aa2cf52 added close quiz functionality
nishanth
parents: 25
diff changeset
   112
10760aa2cf52 added close quiz functionality
nishanth
parents: 25
diff changeset
   113
    if event.quiz_status == "00":
10760aa2cf52 added close quiz functionality
nishanth
parents: 25
diff changeset
   114
        return redirect('/event/create/%s'%ADMIN_KEY)
10760aa2cf52 added close quiz functionality
nishanth
parents: 25
diff changeset
   115
    
38
307c6887cc67 added quiz name in close quiz page.
nishanth
parents: 35
diff changeset
   116
    quiz_num = event.quiz_status
307c6887cc67 added quiz name in close quiz page.
nishanth
parents: 35
diff changeset
   117
26
10760aa2cf52 added close quiz functionality
nishanth
parents: 25
diff changeset
   118
    event.quiz_status = "00"
10760aa2cf52 added close quiz functionality
nishanth
parents: 25
diff changeset
   119
    event.save()
10760aa2cf52 added close quiz functionality
nishanth
parents: 25
diff changeset
   120
39
0fa055b8ea98 implemented correct_quiz function and added it at the time of closing quiz and modified question bank
nishanth
parents: 38
diff changeset
   121
    event_quizzes = event.quiz.filter(quiz_num=quiz_num)
0fa055b8ea98 implemented correct_quiz function and added it at the time of closing quiz and modified question bank
nishanth
parents: 38
diff changeset
   122
    for quiz in event_quizzes:
0fa055b8ea98 implemented correct_quiz function and added it at the time of closing quiz and modified question bank
nishanth
parents: 38
diff changeset
   123
        correct_quiz(quiz)
0fa055b8ea98 implemented correct_quiz function and added it at the time of closing quiz and modified question bank
nishanth
parents: 38
diff changeset
   124
38
307c6887cc67 added quiz name in close quiz page.
nishanth
parents: 35
diff changeset
   125
    return render_to_response("close_quiz.html", {"admin_key":ADMIN_KEY, "quiz_name":num2name[quiz_num]})
307c6887cc67 added quiz name in close quiz page.
nishanth
parents: 35
diff changeset
   126
41
d424b9668a74 implemented list toppers function
nishanth
parents: 39
diff changeset
   127
def list_toppers(request, admin_key, quiz_num):
d424b9668a74 implemented list toppers function
nishanth
parents: 39
diff changeset
   128
    """ check for admin key
d424b9668a74 implemented list toppers function
nishanth
parents: 39
diff changeset
   129
    Then check for quiz_num in ['11', ..]
d424b9668a74 implemented list toppers function
nishanth
parents: 39
diff changeset
   130
    then give appropriate response.
d424b9668a74 implemented list toppers function
nishanth
parents: 39
diff changeset
   131
    """
d424b9668a74 implemented list toppers function
nishanth
parents: 39
diff changeset
   132
d424b9668a74 implemented list toppers function
nishanth
parents: 39
diff changeset
   133
    if not admin_key == ADMIN_KEY:
d424b9668a74 implemented list toppers function
nishanth
parents: 39
diff changeset
   134
        raise Http404
d424b9668a74 implemented list toppers function
nishanth
parents: 39
diff changeset
   135
d424b9668a74 implemented list toppers function
nishanth
parents: 39
diff changeset
   136
    try:
d424b9668a74 implemented list toppers function
nishanth
parents: 39
diff changeset
   137
        event = Event.objects.all()[0]
d424b9668a74 implemented list toppers function
nishanth
parents: 39
diff changeset
   138
    except IndexError:
d424b9668a74 implemented list toppers function
nishanth
parents: 39
diff changeset
   139
        return redirect('/event/create/%s'%ADMIN_KEY)
d424b9668a74 implemented list toppers function
nishanth
parents: 39
diff changeset
   140
d424b9668a74 implemented list toppers function
nishanth
parents: 39
diff changeset
   141
    if quiz_num not in ["11", "12", "21"]:
d424b9668a74 implemented list toppers function
nishanth
parents: 39
diff changeset
   142
        raise Http404
d424b9668a74 implemented list toppers function
nishanth
parents: 39
diff changeset
   143
d424b9668a74 implemented list toppers function
nishanth
parents: 39
diff changeset
   144
    quizzes = Quiz.objects.filter(event=event,quiz_num=quiz_num)
d424b9668a74 implemented list toppers function
nishanth
parents: 39
diff changeset
   145
    quizzes_with_scores = quizzes.annotate(score=Sum('que_answered__is_correct'))
d424b9668a74 implemented list toppers function
nishanth
parents: 39
diff changeset
   146
    sorted_quizzes = quizzes_with_scores.order_by("score").reverse()
d424b9668a74 implemented list toppers function
nishanth
parents: 39
diff changeset
   147
d424b9668a74 implemented list toppers function
nishanth
parents: 39
diff changeset
   148
    return render_to_response("list_toppers.html", {"sorted_quizzes":sorted_quizzes, "event":event, "quiz_name":num2name[quiz_num]})
d424b9668a74 implemented list toppers function
nishanth
parents: 39
diff changeset
   149