project/kiwipycon/proceedings/views.py
author Madhusudan.C.S <madhusudancs@gmail.com>
Fri, 15 Jan 2010 20:15:00 +0530
changeset 93 7c27e8d9231d
parent 90 587e9c025c73
child 103 852015a7eead
permissions -rw-r--r--
Added complete support for dynamic form to add extra authors fields to proceedings form and added initial support for autocompletion based on user names for authors.
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 -*-
93
7c27e8d9231d Added complete support for dynamic form to add extra authors fields to proceedings form and added initial support for autocompletion based on user names for authors.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 90
diff changeset
     2
import json
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
     3
87
1ec579a679e4 Added two models, Paper and Attachments.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 84
diff changeset
     4
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
     5
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
     6
from django.contrib.auth.forms import AuthenticationForm
93
7c27e8d9231d Added complete support for dynamic form to add extra authors fields to proceedings form and added initial support for autocompletion based on user names for authors.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 90
diff changeset
     7
from django.contrib.auth.models import User
7c27e8d9231d Added complete support for dynamic form to add extra authors fields to proceedings form and added initial support for autocompletion based on user names for authors.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 90
diff changeset
     8
from django.http import HttpResponse
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
     9
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
    10
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
    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
from project.kiwipycon.user.forms import RegisterForm
90
587e9c025c73 Added UserProfile import which was missing to Proceedings views.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 87
diff changeset
    13
from project.kiwipycon.user.models import UserProfile
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
    14
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
    15
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
@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
    18
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
    19
    """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
    20
    """
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
    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
    22
    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
    23
        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
    24
            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
    25
        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
    26
            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
    27
            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
    28
                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
    29
    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
    30
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
    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
    32
        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
    33
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
        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
    35
                                        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
    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
        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
    38
            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
    39
            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
    40
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
                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
    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
                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
    44
                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
    45
                        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
    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
        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
    48
            # 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
    49
            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
    50
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
                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
    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
        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
    54
            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
    55
                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
    56
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
                # 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
    58
                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
    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'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
    61
            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
    62
                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
    63
                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
    64
                        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
    65
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
    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
    67
        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
    68
        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
    69
    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
    70
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
    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
    73
    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
    74
    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
    75
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
    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
    77
        '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
    78
        '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
    79
        '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
    80
        '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
    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
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
    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
    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
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
    87
    """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
    88
    """
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
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
    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
    91
        '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
    92
        '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
    93
        '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
    94
        '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
    95
        })
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
    96
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
    97
    return render_to_response(template, context)
93
7c27e8d9231d Added complete support for dynamic form to add extra authors fields to proceedings form and added initial support for autocompletion based on user names for authors.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 90
diff changeset
    98
7c27e8d9231d Added complete support for dynamic form to add extra authors fields to proceedings form and added initial support for autocompletion based on user names for authors.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 90
diff changeset
    99
7c27e8d9231d Added complete support for dynamic form to add extra authors fields to proceedings form and added initial support for autocompletion based on user names for authors.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 90
diff changeset
   100
def getUsers(request):
7c27e8d9231d Added complete support for dynamic form to add extra authors fields to proceedings form and added initial support for autocompletion based on user names for authors.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 90
diff changeset
   101
    """View function called by autocomplete jQuery plugin to get
7c27e8d9231d Added complete support for dynamic form to add extra authors fields to proceedings form and added initial support for autocompletion based on user names for authors.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 90
diff changeset
   102
    the user names.
7c27e8d9231d Added complete support for dynamic form to add extra authors fields to proceedings form and added initial support for autocompletion based on user names for authors.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 90
diff changeset
   103
    """
7c27e8d9231d Added complete support for dynamic form to add extra authors fields to proceedings form and added initial support for autocompletion based on user names for authors.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 90
diff changeset
   104
7c27e8d9231d Added complete support for dynamic form to add extra authors fields to proceedings form and added initial support for autocompletion based on user names for authors.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 90
diff changeset
   105
    query = request.GET['query']
7c27e8d9231d Added complete support for dynamic form to add extra authors fields to proceedings form and added initial support for autocompletion based on user names for authors.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 90
diff changeset
   106
    suggestions = User.objects.filter(username__startswith=query)
7c27e8d9231d Added complete support for dynamic form to add extra authors fields to proceedings form and added initial support for autocompletion based on user names for authors.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 90
diff changeset
   107
    
7c27e8d9231d Added complete support for dynamic form to add extra authors fields to proceedings form and added initial support for autocompletion based on user names for authors.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 90
diff changeset
   108
    suggest_data = {
7c27e8d9231d Added complete support for dynamic form to add extra authors fields to proceedings form and added initial support for autocompletion based on user names for authors.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 90
diff changeset
   109
        'query': query,
7c27e8d9231d Added complete support for dynamic form to add extra authors fields to proceedings form and added initial support for autocompletion based on user names for authors.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 90
diff changeset
   110
        'suggestions':[user.username for user in suggestions],
7c27e8d9231d Added complete support for dynamic form to add extra authors fields to proceedings form and added initial support for autocompletion based on user names for authors.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 90
diff changeset
   111
        }
7c27e8d9231d Added complete support for dynamic form to add extra authors fields to proceedings form and added initial support for autocompletion based on user names for authors.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 90
diff changeset
   112
7c27e8d9231d Added complete support for dynamic form to add extra authors fields to proceedings form and added initial support for autocompletion based on user names for authors.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 90
diff changeset
   113
    return HttpResponse(json.dumps(suggest_data))