project/kiwipycon/proceedings/views.py
author Madhusudan.C.S <madhusudancs@gmail.com>
Thu, 14 Jan 2010 19:19:34 +0530
changeset 87 1ec579a679e4
parent 84 d01c62c2a628
child 90 587e9c025c73
permissions -rw-r--r--
Added two models, Paper and Attachments.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
84
d01c62c2a628 Added the initial proceedings app files and enabled them in both production and development settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     1
# -*- coding: utf-8 -*-
d01c62c2a628 Added the initial proceedings app files and enabled them in both production and development settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     2
87
1ec579a679e4 Added two models, Paper and Attachments.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 84
diff changeset
     3
from django.contrib.auth import login
84
d01c62c2a628 Added the initial proceedings app files and enabled them in both production and development settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     4
from django.contrib.auth.decorators import login_required
d01c62c2a628 Added the initial proceedings app files and enabled them in both production and development settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     5
from django.contrib.auth.forms import AuthenticationForm
d01c62c2a628 Added the initial proceedings app files and enabled them in both production and development settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     6
from django.shortcuts import render_to_response
d01c62c2a628 Added the initial proceedings app files and enabled them in both production and development settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     7
from django.template import RequestContext
d01c62c2a628 Added the initial proceedings app files and enabled them in both production and development settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     8
d01c62c2a628 Added the initial proceedings app files and enabled them in both production and development settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     9
from project.kiwipycon.user.forms import RegisterForm
d01c62c2a628 Added the initial proceedings app files and enabled them in both production and development settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    10
from project.kiwipycon.proceedings.forms import ProceedingsForm
d01c62c2a628 Added the initial proceedings app files and enabled them in both production and development settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    11
d01c62c2a628 Added the initial proceedings app files and enabled them in both production and development settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    12
d01c62c2a628 Added the initial proceedings app files and enabled them in both production and development settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    13
@login_required
d01c62c2a628 Added the initial proceedings app files and enabled them in both production and development settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    14
def submit(request, template = 'proceedings/submit.html'):
d01c62c2a628 Added the initial proceedings app files and enabled them in both production and development settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    15
    """View to submit the proceedings paper.
d01c62c2a628 Added the initial proceedings app files and enabled them in both production and development settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    16
    """
d01c62c2a628 Added the initial proceedings app files and enabled them in both production and development settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    17
    user = request.user
d01c62c2a628 Added the initial proceedings app files and enabled them in both production and development settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    18
    if user.is_authenticated():
d01c62c2a628 Added the initial proceedings app files and enabled them in both production and development settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    19
        try:
d01c62c2a628 Added the initial proceedings app files and enabled them in both production and development settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    20
            profile = user.get_profile()
d01c62c2a628 Added the initial proceedings app files and enabled them in both production and development settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    21
        except:
d01c62c2a628 Added the initial proceedings app files and enabled them in both production and development settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    22
            profile, new = UserProfile.objects.get_or_create(user=user)
d01c62c2a628 Added the initial proceedings app files and enabled them in both production and development settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    23
            if new:
d01c62c2a628 Added the initial proceedings app files and enabled them in both production and development settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    24
                profile.save()
d01c62c2a628 Added the initial proceedings app files and enabled them in both production and development settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    25
    message = None
d01c62c2a628 Added the initial proceedings app files and enabled them in both production and development settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    26
d01c62c2a628 Added the initial proceedings app files and enabled them in both production and development settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    27
    if request.method == 'POST':
d01c62c2a628 Added the initial proceedings app files and enabled them in both production and development settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    28
        proceedings_form = ProceedingsForm(data=request.POST)
d01c62c2a628 Added the initial proceedings app files and enabled them in both production and development settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    29
d01c62c2a628 Added the initial proceedings app files and enabled them in both production and development settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    30
        register_form = RegisterForm(data=request.POST,
d01c62c2a628 Added the initial proceedings app files and enabled them in both production and development settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    31
                                        files=request.FILES)
d01c62c2a628 Added the initial proceedings app files and enabled them in both production and development settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    32
d01c62c2a628 Added the initial proceedings app files and enabled them in both production and development settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    33
        if request.POST.get('action', None) == 'login':
d01c62c2a628 Added the initial proceedings app files and enabled them in both production and development settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    34
            login_form = AuthenticationForm(data=request.POST)
d01c62c2a628 Added the initial proceedings app files and enabled them in both production and development settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    35
            if login_form.is_valid():
d01c62c2a628 Added the initial proceedings app files and enabled them in both production and development settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    36
d01c62c2a628 Added the initial proceedings app files and enabled them in both production and development settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    37
                login(request, login_form.get_user())
d01c62c2a628 Added the initial proceedings app files and enabled them in both production and development settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    38
d01c62c2a628 Added the initial proceedings app files and enabled them in both production and development settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    39
                redirect_to = reverse('kiwipycon_submit_proceedings')
d01c62c2a628 Added the initial proceedings app files and enabled them in both production and development settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    40
                return set_message_cookie(redirect_to,
d01c62c2a628 Added the initial proceedings app files and enabled them in both production and development settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    41
                        msg = u'You have been logged in.')
d01c62c2a628 Added the initial proceedings app files and enabled them in both production and development settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    42
d01c62c2a628 Added the initial proceedings app files and enabled them in both production and development settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    43
        if request.POST.get('action', None) == 'register':
d01c62c2a628 Added the initial proceedings app files and enabled them in both production and development settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    44
            # add the new user
d01c62c2a628 Added the initial proceedings app files and enabled them in both production and development settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    45
            if register_form.is_valid():
d01c62c2a628 Added the initial proceedings app files and enabled them in both production and development settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    46
d01c62c2a628 Added the initial proceedings app files and enabled them in both production and development settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    47
                user = kiwipycon_createuser(request, register_form.data)
d01c62c2a628 Added the initial proceedings app files and enabled them in both production and development settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    48
d01c62c2a628 Added the initial proceedings app files and enabled them in both production and development settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    49
        if proceedings_form.is_valid():
d01c62c2a628 Added the initial proceedings app files and enabled them in both production and development settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    50
            if user.is_authenticated():
d01c62c2a628 Added the initial proceedings app files and enabled them in both production and development settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    51
                title = proceedings_form.data.get('title')
d01c62c2a628 Added the initial proceedings app files and enabled them in both production and development settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    52
d01c62c2a628 Added the initial proceedings app files and enabled them in both production and development settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    53
                # Saved, ... redirect back to account
d01c62c2a628 Added the initial proceedings app files and enabled them in both production and development settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    54
                redirect_to = reverse('kiwipycon_account')
d01c62c2a628 Added the initial proceedings app files and enabled them in both production and development settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    55
                return set_message_cookie(redirect_to,
d01c62c2a628 Added the initial proceedings app files and enabled them in both production and development settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    56
                        msg = u'Thanks, your paper has been submitted.')
d01c62c2a628 Added the initial proceedings app files and enabled them in both production and development settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    57
            else:
d01c62c2a628 Added the initial proceedings app files and enabled them in both production and development settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    58
                redirect_to = reverse('kiwipycon_submit_proceedings')
d01c62c2a628 Added the initial proceedings app files and enabled them in both production and development settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    59
                return set_message_cookie(redirect_to,
d01c62c2a628 Added the initial proceedings app files and enabled them in both production and development settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    60
                        msg = u'Something is wrong here.')
d01c62c2a628 Added the initial proceedings app files and enabled them in both production and development settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    61
d01c62c2a628 Added the initial proceedings app files and enabled them in both production and development settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    62
    else:
d01c62c2a628 Added the initial proceedings app files and enabled them in both production and development settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    63
        proceedings_form = ProceedingsForm()
d01c62c2a628 Added the initial proceedings app files and enabled them in both production and development settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    64
        register_form = RegisterForm()
d01c62c2a628 Added the initial proceedings app files and enabled them in both production and development settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    65
    login_form = AuthenticationForm()
d01c62c2a628 Added the initial proceedings app files and enabled them in both production and development settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    66
d01c62c2a628 Added the initial proceedings app files and enabled them in both production and development settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    67
        
d01c62c2a628 Added the initial proceedings app files and enabled them in both production and development settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    68
    proceedings_form = ProceedingsForm()
d01c62c2a628 Added the initial proceedings app files and enabled them in both production and development settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    69
    register_form = RegisterForm()
d01c62c2a628 Added the initial proceedings app files and enabled them in both production and development settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    70
    login_form = AuthenticationForm()
d01c62c2a628 Added the initial proceedings app files and enabled them in both production and development settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    71
d01c62c2a628 Added the initial proceedings app files and enabled them in both production and development settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    72
    context = RequestContext(request, {
d01c62c2a628 Added the initial proceedings app files and enabled them in both production and development settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    73
        'proceedings_form': proceedings_form,
d01c62c2a628 Added the initial proceedings app files and enabled them in both production and development settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    74
        'register_form' : register_form,
d01c62c2a628 Added the initial proceedings app files and enabled them in both production and development settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    75
        'message' : message,
d01c62c2a628 Added the initial proceedings app files and enabled them in both production and development settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    76
        'login_form' : login_form
d01c62c2a628 Added the initial proceedings app files and enabled them in both production and development settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    77
        })
d01c62c2a628 Added the initial proceedings app files and enabled them in both production and development settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    78
d01c62c2a628 Added the initial proceedings app files and enabled them in both production and development settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    79
    return render_to_response(template, context)
d01c62c2a628 Added the initial proceedings app files and enabled them in both production and development settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    80
d01c62c2a628 Added the initial proceedings app files and enabled them in both production and development settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    81
d01c62c2a628 Added the initial proceedings app files and enabled them in both production and development settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    82
def edit(request, id, template = 'proceedings/edit.html'):
d01c62c2a628 Added the initial proceedings app files and enabled them in both production and development settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    83
    """View to edit the proceedings paper.
d01c62c2a628 Added the initial proceedings app files and enabled them in both production and development settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    84
    """
d01c62c2a628 Added the initial proceedings app files and enabled them in both production and development settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    85
d01c62c2a628 Added the initial proceedings app files and enabled them in both production and development settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    86
    context = RequestContext(request, {
d01c62c2a628 Added the initial proceedings app files and enabled them in both production and development settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    87
        'proceedings_form': proceedings_form,
d01c62c2a628 Added the initial proceedings app files and enabled them in both production and development settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    88
        'register_form' : register_form,
d01c62c2a628 Added the initial proceedings app files and enabled them in both production and development settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    89
        'message' : message,
d01c62c2a628 Added the initial proceedings app files and enabled them in both production and development settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    90
        'login_form' : login_form
d01c62c2a628 Added the initial proceedings app files and enabled them in both production and development settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    91
        })
d01c62c2a628 Added the initial proceedings app files and enabled them in both production and development settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    92
d01c62c2a628 Added the initial proceedings app files and enabled them in both production and development settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    93
    return render_to_response(template, context)