project/scipycon/user/views.py
author Amit Sethi
Wed, 01 Dec 2010 12:20:13 +0530
changeset 325 1255f442e4f8
parent 324 1ad0be6866d6
child 333 142f7f94de19
permissions -rw-r--r--
Some changes to font size on badge according to recommendation
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
119
05a3e98b1fe0 Adding scope as a parameter to all the views.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 98
diff changeset
     1
from urlparse import urlparse
94
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     2
98
d6108402f324 Changed json import to accommodate for Python 2.5.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 96
diff changeset
     3
import simplejson as json
94
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     4
import os
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     5
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     6
from django.conf import settings
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     7
from django.contrib.auth.decorators import login_required
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     8
from django.contrib.auth.forms import AuthenticationForm
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     9
from django.contrib.auth.forms import PasswordChangeForm
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    10
from django.contrib.auth.models import User
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    11
from django.core.exceptions import ObjectDoesNotExist
119
05a3e98b1fe0 Adding scope as a parameter to all the views.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 98
diff changeset
    12
from django.core.urlresolvers import reverse
05a3e98b1fe0 Adding scope as a parameter to all the views.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 98
diff changeset
    13
from django.db.models import Q
05a3e98b1fe0 Adding scope as a parameter to all the views.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 98
diff changeset
    14
from django.http import HttpResponse
05a3e98b1fe0 Adding scope as a parameter to all the views.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 98
diff changeset
    15
from django.shortcuts import render_to_response
05a3e98b1fe0 Adding scope as a parameter to all the views.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 98
diff changeset
    16
from django.template import RequestContext
94
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    17
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    18
from PIL import Image
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    19
144
27bde1b86666 Added registration and event to accounts page form.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 138
diff changeset
    20
from project.scipycon.base.models import Event
96
178b89a3ca4f Removed unwanted files and made more changes to make SciPyCon a clean app.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 94
diff changeset
    21
from project.scipycon.registration.models import Registration
178b89a3ca4f Removed unwanted files and made more changes to make SciPyCon a clean app.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 94
diff changeset
    22
from project.scipycon.registration.models import Wifi
178b89a3ca4f Removed unwanted files and made more changes to make SciPyCon a clean app.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 94
diff changeset
    23
from project.scipycon.registration.forms import WifiForm
119
05a3e98b1fe0 Adding scope as a parameter to all the views.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 98
diff changeset
    24
from project.scipycon.talk.models import Talk
05a3e98b1fe0 Adding scope as a parameter to all the views.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 98
diff changeset
    25
from project.scipycon.user.forms import EditProfileForm
138
250e2731039d Reformatted the file and imports.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 128
diff changeset
    26
from project.scipycon.user.forms import RegisterForm
250e2731039d Reformatted the file and imports.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 128
diff changeset
    27
from project.scipycon.user.forms import UsernameForm
119
05a3e98b1fe0 Adding scope as a parameter to all the views.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 98
diff changeset
    28
from project.scipycon.user.utils import handle_uploaded_photo
05a3e98b1fe0 Adding scope as a parameter to all the views.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 98
diff changeset
    29
from project.scipycon.user.utils import scipycon_createuser
138
250e2731039d Reformatted the file and imports.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 128
diff changeset
    30
from project.scipycon.utils import set_message_cookie
119
05a3e98b1fe0 Adding scope as a parameter to all the views.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 98
diff changeset
    31
237
4cff1f43e4e1 Changes to make it possible for someone to view daily dumps.
Amit Sethi
parents: 144
diff changeset
    32
#User_dump Http404 Error
4cff1f43e4e1 Changes to make it possible for someone to view daily dumps.
Amit Sethi
parents: 144
diff changeset
    33
from django.http import Http404
239
1973bf1a0a5d Changes to the template get-user-dump
Amit Sethi
parents: 237
diff changeset
    34
#for user_dump creation
1973bf1a0a5d Changes to the template get-user-dump
Amit Sethi
parents: 237
diff changeset
    35
from project.scipycon.registration.models import Accommodation
1973bf1a0a5d Changes to the template get-user-dump
Amit Sethi
parents: 237
diff changeset
    36
320
9414017a5589 Added view for automatic badge generation
Amit Sethi
parents: 239
diff changeset
    37
#Pdf badge generation
9414017a5589 Added view for automatic badge generation
Amit Sethi
parents: 239
diff changeset
    38
from reportlab.pdfgen import canvas
9414017a5589 Added view for automatic badge generation
Amit Sethi
parents: 239
diff changeset
    39
from reportlab.lib.units import cm
9414017a5589 Added view for automatic badge generation
Amit Sethi
parents: 239
diff changeset
    40
from reportlab.platypus import  Image as reportlabImage
324
1ad0be6866d6 Added Laptop registration no to badge
Amit Sethi
parents: 320
diff changeset
    41
from django.core.exceptions import ObjectDoesNotExist
320
9414017a5589 Added view for automatic badge generation
Amit Sethi
parents: 239
diff changeset
    42
94
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    43
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    44
@login_required
119
05a3e98b1fe0 Adding scope as a parameter to all the views.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 98
diff changeset
    45
def account(request, scope, template_name="user/account.html"):
94
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    46
    """Displays the main screen of the current user's account.
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    47
    """
119
05a3e98b1fe0 Adding scope as a parameter to all the views.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 98
diff changeset
    48
94
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    49
    user = request.user
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    50
    profile = user.get_profile()
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    51
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    52
    talks = Talk.objects.filter(speaker=user)
144
27bde1b86666 Added registration and event to accounts page form.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 138
diff changeset
    53
94
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    54
    try:
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    55
        registration = Registration.objects.get(registrant=user)
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    56
    except ObjectDoesNotExist:
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    57
        registration = None
144
27bde1b86666 Added registration and event to accounts page form.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 138
diff changeset
    58
94
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    59
    try:
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    60
        wifiobj = Wifi.objects.get(user=user)
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    61
    except ObjectDoesNotExist:
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    62
        wifiobj = None
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    63
144
27bde1b86666 Added registration and event to accounts page form.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 138
diff changeset
    64
    event = Event.objects.get(scope=scope)
27bde1b86666 Added registration and event to accounts page form.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 138
diff changeset
    65
94
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    66
    if profile.photo:
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    67
        photo = os.path.join(settings.USER_MEDIA_URL, profile.photo)
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    68
    else:
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    69
        photo = '/img/user-default.png'
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    70
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    71
    return render_to_response(template_name, RequestContext(request, {
119
05a3e98b1fe0 Adding scope as a parameter to all the views.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 98
diff changeset
    72
        'params': {'scope': scope},
144
27bde1b86666 Added registration and event to accounts page form.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 138
diff changeset
    73
        'user' : user,
27bde1b86666 Added registration and event to accounts page form.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 138
diff changeset
    74
        'profile' : profile,
27bde1b86666 Added registration and event to accounts page form.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 138
diff changeset
    75
        'photo' : photo,
27bde1b86666 Added registration and event to accounts page form.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 138
diff changeset
    76
        'talks' : talks,
27bde1b86666 Added registration and event to accounts page form.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 138
diff changeset
    77
        'registration' : registration,
27bde1b86666 Added registration and event to accounts page form.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 138
diff changeset
    78
        'event': event}))
94
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    79
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    80
@login_required
119
05a3e98b1fe0 Adding scope as a parameter to all the views.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 98
diff changeset
    81
def edit_profile(request, scope, template_name="user/editprofile.html"):
94
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    82
    """Allows user to edit profile
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    83
    """
138
250e2731039d Reformatted the file and imports.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 128
diff changeset
    84
94
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    85
    user = request.user
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    86
    profile = user.get_profile()
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    87
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    88
    if request.method == "POST":
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    89
        form = EditProfileForm(data=request.POST,
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    90
                               files=request.FILES)
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    91
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    92
        if form.is_valid():
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    93
            photo = request.FILES.get('photo', None)
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    94
            filename= None
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    95
            if photo:
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    96
                filename = handle_uploaded_photo(user, request.FILES['photo'])
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    97
            if filename:
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    98
                profile.photo = filename
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    99
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   100
            user.email = form.data.get("email")
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   101
            user.first_name = form.data.get("first_name")
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   102
            user.last_name = form.data.get("last_name")
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   103
            user.save()
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   104
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   105
            profile.url = form.data.get("url")
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   106
            profile.about = form.data.get("about")
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   107
            profile.save()
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   108
128
d804be977989 Add scope as a parameter to all view functions for user app and adjusted the URLs to accommodate scope.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 119
diff changeset
   109
            redirect_to = reverse('scipycon_account',
d804be977989 Add scope as a parameter to all view functions for user app and adjusted the URLs to accommodate scope.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 119
diff changeset
   110
                                  kwargs={'scope': scope})
94
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   111
            return set_message_cookie(redirect_to,
138
250e2731039d Reformatted the file and imports.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 128
diff changeset
   112
                    msg = u'Your profile has been changed.')
94
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   113
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   114
    else:
138
250e2731039d Reformatted the file and imports.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 128
diff changeset
   115
        form = EditProfileForm(
250e2731039d Reformatted the file and imports.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 128
diff changeset
   116
            initial={
250e2731039d Reformatted the file and imports.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 128
diff changeset
   117
                'email' : user.email,
250e2731039d Reformatted the file and imports.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 128
diff changeset
   118
                'email2' : user.email, # hidden field
250e2731039d Reformatted the file and imports.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 128
diff changeset
   119
                'first_name' : user.first_name,
250e2731039d Reformatted the file and imports.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 128
diff changeset
   120
                'last_name' : user.last_name,
250e2731039d Reformatted the file and imports.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 128
diff changeset
   121
                'url' : profile.url,
250e2731039d Reformatted the file and imports.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 128
diff changeset
   122
                'about' : profile.about,
250e2731039d Reformatted the file and imports.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 128
diff changeset
   123
            })
94
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   124
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   125
    return render_to_response(template_name, RequestContext(request, {
128
d804be977989 Add scope as a parameter to all view functions for user app and adjusted the URLs to accommodate scope.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 119
diff changeset
   126
        'params': {'scope': scope},
d804be977989 Add scope as a parameter to all view functions for user app and adjusted the URLs to accommodate scope.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 119
diff changeset
   127
        'form': form
94
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   128
    }))
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   129
119
05a3e98b1fe0 Adding scope as a parameter to all the views.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 98
diff changeset
   130
def login(request, scope, template_name="user/login.html"):
94
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   131
    """Custom view to login or register/login a user.
128
d804be977989 Add scope as a parameter to all view functions for user app and adjusted the URLs to accommodate scope.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 119
diff changeset
   132
       Integration of register and login form
d804be977989 Add scope as a parameter to all view functions for user app and adjusted the URLs to accommodate scope.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 119
diff changeset
   133
       It uses Django's standard AuthenticationForm, though.
94
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   134
    """
128
d804be977989 Add scope as a parameter to all view functions for user app and adjusted the URLs to accommodate scope.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 119
diff changeset
   135
94
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   136
    user = request.user
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   137
    if user.is_authenticated():
119
05a3e98b1fe0 Adding scope as a parameter to all the views.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 98
diff changeset
   138
        redirect_to = reverse("scipycon_account", kwargs={'scope': scope})
94
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   139
        return set_message_cookie(redirect_to,
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   140
                msg = u"Redirected to account from login form.")
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   141
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   142
    # Using Djangos default AuthenticationForm
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   143
    login_form = AuthenticationForm()
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   144
    register_form = RegisterForm()
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   145
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   146
    if request.POST.get("action") == "login":
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   147
        login_form = AuthenticationForm(data=request.POST)
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   148
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   149
        if login_form.is_valid():
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   150
            redirect_to = request.POST.get("next")
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   151
            # Light security check -- make sure redirect_to isn't garbage.
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   152
            if not redirect_to or '//' in redirect_to or ' ' in redirect_to:
128
d804be977989 Add scope as a parameter to all view functions for user app and adjusted the URLs to accommodate scope.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 119
diff changeset
   153
                redirect_to = reverse('scipycon_account',
d804be977989 Add scope as a parameter to all view functions for user app and adjusted the URLs to accommodate scope.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 119
diff changeset
   154
                                      kwargs={'scope': scope})
94
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   155
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   156
            from django.contrib.auth import login
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   157
            login(request, login_form.get_user())
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   158
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   159
            return set_message_cookie(redirect_to, msg = u"You have been logged in.")
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   160
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   161
    elif request.POST.get("action") == "register":
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   162
        register_form = RegisterForm(data=request.POST)
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   163
        if register_form.is_valid():
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   164
119
05a3e98b1fe0 Adding scope as a parameter to all the views.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 98
diff changeset
   165
            user = scipycon_createuser(request, register_form.data, scope)
94
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   166
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   167
            redirect_to = request.POST.get("next")
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   168
            if not redirect_to or '//' in redirect_to or ' ' in redirect_to:
128
d804be977989 Add scope as a parameter to all view functions for user app and adjusted the URLs to accommodate scope.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 119
diff changeset
   169
                redirect_to = reverse('scipycon_account',
d804be977989 Add scope as a parameter to all view functions for user app and adjusted the URLs to accommodate scope.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 119
diff changeset
   170
                                      kwargs={'scope': scope})
94
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   171
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   172
            return set_message_cookie(
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   173
                redirect_to, msg = u"You have been registered and logged in.")
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   174
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   175
    # Get next_url
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   176
    next_url = request.REQUEST.get("next")
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   177
    if next_url is None:
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   178
        next_url = request.META.get("HTTP_REFERER")
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   179
    if next_url is None:
128
d804be977989 Add scope as a parameter to all view functions for user app and adjusted the URLs to accommodate scope.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 119
diff changeset
   180
        next_url = reverse('scipycon_account', kwargs={'scope': scope})
d804be977989 Add scope as a parameter to all view functions for user app and adjusted the URLs to accommodate scope.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 119
diff changeset
   181
d804be977989 Add scope as a parameter to all view functions for user app and adjusted the URLs to accommodate scope.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 119
diff changeset
   182
    # Get just the path of the url.
d804be977989 Add scope as a parameter to all view functions for user app and adjusted the URLs to accommodate scope.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 119
diff changeset
   183
    # See django.contrib.auth.views.login for more
94
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   184
    next_url = urlparse(next_url)
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   185
    next_url = next_url[2]
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   186
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   187
    try:
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   188
        login_form_errors = login_form.errors["__all__"]
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   189
    except KeyError:
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   190
        login_form_errors = None
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   191
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   192
    return render_to_response(template_name, RequestContext(request, {
119
05a3e98b1fe0 Adding scope as a parameter to all the views.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 98
diff changeset
   193
        'params': {'scope': scope},
05a3e98b1fe0 Adding scope as a parameter to all the views.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 98
diff changeset
   194
        'login_form' : login_form,
05a3e98b1fe0 Adding scope as a parameter to all the views.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 98
diff changeset
   195
        'login_form_errors' : login_form_errors,
05a3e98b1fe0 Adding scope as a parameter to all the views.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 98
diff changeset
   196
        'register_form' : register_form,
05a3e98b1fe0 Adding scope as a parameter to all the views.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 98
diff changeset
   197
        'next_url' : next_url,
94
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   198
    }))
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   199
119
05a3e98b1fe0 Adding scope as a parameter to all the views.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 98
diff changeset
   200
def logout(request, scope):
94
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   201
    """Custom method to logout a user.
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   202
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   203
    The reason to use a custom logout method is just to provide a login and a
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   204
    logoutmethod on one place.
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   205
    """
128
d804be977989 Add scope as a parameter to all view functions for user app and adjusted the URLs to accommodate scope.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 119
diff changeset
   206
94
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   207
    from django.contrib.auth import logout
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   208
    logout(request)
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   209
128
d804be977989 Add scope as a parameter to all view functions for user app and adjusted the URLs to accommodate scope.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 119
diff changeset
   210
    redirect_to = '/%s' % (scope)
94
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   211
    return set_message_cookie(redirect_to, msg = u"You have been logged out.")
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   212
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   213
@login_required
128
d804be977989 Add scope as a parameter to all view functions for user app and adjusted the URLs to accommodate scope.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 119
diff changeset
   214
def password(request, scope, template_name='user/password.html'):
94
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   215
    """Changes the password of current user.
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   216
    """
128
d804be977989 Add scope as a parameter to all view functions for user app and adjusted the URLs to accommodate scope.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 119
diff changeset
   217
d804be977989 Add scope as a parameter to all view functions for user app and adjusted the URLs to accommodate scope.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 119
diff changeset
   218
    if request.method == 'POST':
94
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   219
        form = PasswordChangeForm(request.user, request.POST)
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   220
        if form.is_valid():
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   221
            form.save()
128
d804be977989 Add scope as a parameter to all view functions for user app and adjusted the URLs to accommodate scope.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 119
diff changeset
   222
            redirect_to = reverse('scipycon_account', kwargs={'scope': scope})
94
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   223
            return set_message_cookie(redirect_to,
128
d804be977989 Add scope as a parameter to all view functions for user app and adjusted the URLs to accommodate scope.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 119
diff changeset
   224
                    msg = u'Your password has been changed.')
94
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   225
    else:
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   226
        form = PasswordChangeForm(request.user)
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   227
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   228
    return render_to_response(template_name, RequestContext(request, {
119
05a3e98b1fe0 Adding scope as a parameter to all the views.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 98
diff changeset
   229
        'params': {'scope': scope},
05a3e98b1fe0 Adding scope as a parameter to all the views.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 98
diff changeset
   230
        'form' : form
94
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   231
    }))
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   232
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   233
@login_required
128
d804be977989 Add scope as a parameter to all view functions for user app and adjusted the URLs to accommodate scope.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 119
diff changeset
   234
def username(request, scope, template_name='user/username.html'):
94
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   235
    """Saves the username from the data form.
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   236
    """
128
d804be977989 Add scope as a parameter to all view functions for user app and adjusted the URLs to accommodate scope.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 119
diff changeset
   237
    if request.method == 'POST':
d804be977989 Add scope as a parameter to all view functions for user app and adjusted the URLs to accommodate scope.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 119
diff changeset
   238
        username_form = UsernameForm(
d804be977989 Add scope as a parameter to all view functions for user app and adjusted the URLs to accommodate scope.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 119
diff changeset
   239
            initial={'username' : request.user.username},
d804be977989 Add scope as a parameter to all view functions for user app and adjusted the URLs to accommodate scope.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 119
diff changeset
   240
            data=request.POST)
94
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   241
        if username_form.is_valid():
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   242
            request.user.username = username_form.cleaned_data.get("username")
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   243
            request.user.save()
128
d804be977989 Add scope as a parameter to all view functions for user app and adjusted the URLs to accommodate scope.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 119
diff changeset
   244
            redirect_to = reverse('scipycon_account',
d804be977989 Add scope as a parameter to all view functions for user app and adjusted the URLs to accommodate scope.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 119
diff changeset
   245
                                  kwargs={'scope': scope})
94
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   246
            return set_message_cookie(redirect_to,
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   247
                    msg = u"Your username has been changed.")
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   248
    else:        
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   249
        username_form = UsernameForm(initial={"username" : request.user.username})
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   250
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   251
    return render_to_response(template_name, RequestContext(request, {
128
d804be977989 Add scope as a parameter to all view functions for user app and adjusted the URLs to accommodate scope.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 119
diff changeset
   252
        'params': {'scope': scope},
d804be977989 Add scope as a parameter to all view functions for user app and adjusted the URLs to accommodate scope.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 119
diff changeset
   253
        'form': username_form
94
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   254
    }))
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   255
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   256
119
05a3e98b1fe0 Adding scope as a parameter to all the views.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 98
diff changeset
   257
def get_usernames(request, scope):
94
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   258
    """Returns in json the list of ten possible usernames
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   259
    starting with the last pattern in the comma separated string
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   260
    """
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   261
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   262
    get_params = request.GET
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   263
    authors_str = get_params.get('input')
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   264
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   265
    if not authors_str:
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   266
        return HttpResponse(json.dumps(''))
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   267
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   268
    authors = authors_str.split(',')
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   269
    search_author = authors[-1].strip()
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   270
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   271
    users = User.objects.filter(
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   272
        Q(username__istartswith=search_author) | Q(
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   273
        first_name__istartswith=search_author) | Q(
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   274
        last_name__istartswith=search_author))
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   275
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   276
    results = [{'id': '',
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   277
                'info': 'plugin_header',
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   278
                'value': 'User Names'
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   279
              }]
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   280
    
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   281
    for user in users:
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   282
        results.append(
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   283
            {'id': 'author_name',
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   284
             'info': str(user.get_full_name()),
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   285
             'value': str(user.username)
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   286
            })
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   287
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   288
    json_response = {'results': results}
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   289
128
d804be977989 Add scope as a parameter to all view functions for user app and adjusted the URLs to accommodate scope.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 119
diff changeset
   290
    return HttpResponse(json.dumps(json_response))
237
4cff1f43e4e1 Changes to make it possible for someone to view daily dumps.
Amit Sethi
parents: 144
diff changeset
   291
4cff1f43e4e1 Changes to make it possible for someone to view daily dumps.
Amit Sethi
parents: 144
diff changeset
   292
4cff1f43e4e1 Changes to make it possible for someone to view daily dumps.
Amit Sethi
parents: 144
diff changeset
   293
@login_required
4cff1f43e4e1 Changes to make it possible for someone to view daily dumps.
Amit Sethi
parents: 144
diff changeset
   294
def get_user_dump(request, scope,template_name='user/dump.html'):
4cff1f43e4e1 Changes to make it possible for someone to view daily dumps.
Amit Sethi
parents: 144
diff changeset
   295
    """ Gets a general dump of user related info
4cff1f43e4e1 Changes to make it possible for someone to view daily dumps.
Amit Sethi
parents: 144
diff changeset
   296
    """
4cff1f43e4e1 Changes to make it possible for someone to view daily dumps.
Amit Sethi
parents: 144
diff changeset
   297
    print request.user.is_staff
4cff1f43e4e1 Changes to make it possible for someone to view daily dumps.
Amit Sethi
parents: 144
diff changeset
   298
    if request.user.is_staff:
4cff1f43e4e1 Changes to make it possible for someone to view daily dumps.
Amit Sethi
parents: 144
diff changeset
   299
        qs=Registration.objects.all()
4cff1f43e4e1 Changes to make it possible for someone to view daily dumps.
Amit Sethi
parents: 144
diff changeset
   300
        rows=[]    
4cff1f43e4e1 Changes to make it possible for someone to view daily dumps.
Amit Sethi
parents: 144
diff changeset
   301
        for obj in qs:
4cff1f43e4e1 Changes to make it possible for someone to view daily dumps.
Amit Sethi
parents: 144
diff changeset
   302
            row = {}
4cff1f43e4e1 Changes to make it possible for someone to view daily dumps.
Amit Sethi
parents: 144
diff changeset
   303
            row['first_name'] = obj.registrant.first_name
4cff1f43e4e1 Changes to make it possible for someone to view daily dumps.
Amit Sethi
parents: 144
diff changeset
   304
            row['last_name'] = obj.registrant.last_name
4cff1f43e4e1 Changes to make it possible for someone to view daily dumps.
Amit Sethi
parents: 144
diff changeset
   305
            try:
4cff1f43e4e1 Changes to make it possible for someone to view daily dumps.
Amit Sethi
parents: 144
diff changeset
   306
                accomodation_require = Accommodation.objects.filter(user__username=obj.registrant.username)[0]
4cff1f43e4e1 Changes to make it possible for someone to view daily dumps.
Amit Sethi
parents: 144
diff changeset
   307
                row['sex'] = accomodation_require.sex
4cff1f43e4e1 Changes to make it possible for someone to view daily dumps.
Amit Sethi
parents: 144
diff changeset
   308
            except:
239
1973bf1a0a5d Changes to the template get-user-dump
Amit Sethi
parents: 237
diff changeset
   309
                row['sex'] = '-'
237
4cff1f43e4e1 Changes to make it possible for someone to view daily dumps.
Amit Sethi
parents: 144
diff changeset
   310
            row['city'] = obj.city
4cff1f43e4e1 Changes to make it possible for someone to view daily dumps.
Amit Sethi
parents: 144
diff changeset
   311
            row['organization'] = obj.organisation
4cff1f43e4e1 Changes to make it possible for someone to view daily dumps.
Amit Sethi
parents: 144
diff changeset
   312
            row['occupation'] = obj.occupation
4cff1f43e4e1 Changes to make it possible for someone to view daily dumps.
Amit Sethi
parents: 144
diff changeset
   313
            row['conference'] = obj.conference 
4cff1f43e4e1 Changes to make it possible for someone to view daily dumps.
Amit Sethi
parents: 144
diff changeset
   314
            row['sprint'] = obj.sprint
4cff1f43e4e1 Changes to make it possible for someone to view daily dumps.
Amit Sethi
parents: 144
diff changeset
   315
            row['tutorial'] = obj.tutorial
4cff1f43e4e1 Changes to make it possible for someone to view daily dumps.
Amit Sethi
parents: 144
diff changeset
   316
            try:
4cff1f43e4e1 Changes to make it possible for someone to view daily dumps.
Amit Sethi
parents: 144
diff changeset
   317
                wifi_require = Wifi.objects.filter(user__username=obj.registrant.username)[0]
4cff1f43e4e1 Changes to make it possible for someone to view daily dumps.
Amit Sethi
parents: 144
diff changeset
   318
                row['wifi'] = wifi_require.wifi
4cff1f43e4e1 Changes to make it possible for someone to view daily dumps.
Amit Sethi
parents: 144
diff changeset
   319
            except:
4cff1f43e4e1 Changes to make it possible for someone to view daily dumps.
Amit Sethi
parents: 144
diff changeset
   320
                row['wifi']='Wifi Unspecified'
4cff1f43e4e1 Changes to make it possible for someone to view daily dumps.
Amit Sethi
parents: 144
diff changeset
   321
            rows.append(row)
4cff1f43e4e1 Changes to make it possible for someone to view daily dumps.
Amit Sethi
parents: 144
diff changeset
   322
        return render_to_response(template_name, RequestContext(request, {
4cff1f43e4e1 Changes to make it possible for someone to view daily dumps.
Amit Sethi
parents: 144
diff changeset
   323
                    'rows':  rows}))
4cff1f43e4e1 Changes to make it possible for someone to view daily dumps.
Amit Sethi
parents: 144
diff changeset
   324
                    
4cff1f43e4e1 Changes to make it possible for someone to view daily dumps.
Amit Sethi
parents: 144
diff changeset
   325
4cff1f43e4e1 Changes to make it possible for someone to view daily dumps.
Amit Sethi
parents: 144
diff changeset
   326
    else:
4cff1f43e4e1 Changes to make it possible for someone to view daily dumps.
Amit Sethi
parents: 144
diff changeset
   327
            raise Http404
320
9414017a5589 Added view for automatic badge generation
Amit Sethi
parents: 239
diff changeset
   328
9414017a5589 Added view for automatic badge generation
Amit Sethi
parents: 239
diff changeset
   329
9414017a5589 Added view for automatic badge generation
Amit Sethi
parents: 239
diff changeset
   330
@login_required
9414017a5589 Added view for automatic badge generation
Amit Sethi
parents: 239
diff changeset
   331
def badge(request,scope):
9414017a5589 Added view for automatic badge generation
Amit Sethi
parents: 239
diff changeset
   332
    # Create the HttpResponse object with the appropriate PDF headers.
9414017a5589 Added view for automatic badge generation
Amit Sethi
parents: 239
diff changeset
   333
    response = HttpResponse(mimetype='application/pdf')
9414017a5589 Added view for automatic badge generation
Amit Sethi
parents: 239
diff changeset
   334
    response['Content-Disposition'] = 'attachment; filename=scipybadge.pdf'
9414017a5589 Added view for automatic badge generation
Amit Sethi
parents: 239
diff changeset
   335
9414017a5589 Added view for automatic badge generation
Amit Sethi
parents: 239
diff changeset
   336
    # Create the PDF object, using the response object as its "file."
9414017a5589 Added view for automatic badge generation
Amit Sethi
parents: 239
diff changeset
   337
    c = canvas.Canvas(response)
9414017a5589 Added view for automatic badge generation
Amit Sethi
parents: 239
diff changeset
   338
9414017a5589 Added view for automatic badge generation
Amit Sethi
parents: 239
diff changeset
   339
    ref=5*cm
9414017a5589 Added view for automatic badge generation
Amit Sethi
parents: 239
diff changeset
   340
    # Draw things on the PDF. Here's where the PDF generation happens.
9414017a5589 Added view for automatic badge generation
Amit Sethi
parents: 239
diff changeset
   341
    # See the ReportLab documentation for the full list of functionality.
324
1ad0be6866d6 Added Laptop registration no to badge
Amit Sethi
parents: 320
diff changeset
   342
    c.rect(ref,ref,9*cm,6*cm)
320
9414017a5589 Added view for automatic badge generation
Amit Sethi
parents: 239
diff changeset
   343
9414017a5589 Added view for automatic badge generation
Amit Sethi
parents: 239
diff changeset
   344
    im = reportlabImage("project/static/img/scipyshiny_small.png", width=1.75*cm, height=1.75*cm)
324
1ad0be6866d6 Added Laptop registration no to badge
Amit Sethi
parents: 320
diff changeset
   345
    im.drawOn(c,(ref+0.8*cm),(ref+4.3*cm))
320
9414017a5589 Added view for automatic badge generation
Amit Sethi
parents: 239
diff changeset
   346
    c.setFont('Helvetica', 6)
324
1ad0be6866d6 Added Laptop registration no to badge
Amit Sethi
parents: 320
diff changeset
   347
    c.drawString((ref+1.0*cm),(ref+4.2*cm),'scipy.in 2010') 
1ad0be6866d6 Added Laptop registration no to badge
Amit Sethi
parents: 320
diff changeset
   348
    c.drawString((ref+1.1*cm),(ref+4.0*cm),'Hyderabad') 
320
9414017a5589 Added view for automatic badge generation
Amit Sethi
parents: 239
diff changeset
   349
324
1ad0be6866d6 Added Laptop registration no to badge
Amit Sethi
parents: 320
diff changeset
   350
    c.setFont('Helvetica', 14)
320
9414017a5589 Added view for automatic badge generation
Amit Sethi
parents: 239
diff changeset
   351
    print request.user.id
9414017a5589 Added view for automatic badge generation
Amit Sethi
parents: 239
diff changeset
   352
    reg_obj=Registration.objects.get(registrant=request.user.id)
324
1ad0be6866d6 Added Laptop registration no to badge
Amit Sethi
parents: 320
diff changeset
   353
    c.drawString((ref+3.4*cm),(ref+4.9*cm),str(reg_obj.slug)) 
1ad0be6866d6 Added Laptop registration no to badge
Amit Sethi
parents: 320
diff changeset
   354
    
325
1255f442e4f8 Some changes to font size on badge according to recommendation
Amit Sethi
parents: 324
diff changeset
   355
    c.setFont('Helvetica-Bold', 15)
324
1ad0be6866d6 Added Laptop registration no to badge
Amit Sethi
parents: 320
diff changeset
   356
    c.drawString((ref+0.6*cm),(ref+3.4*cm),str(request.user.get_full_name()))
325
1255f442e4f8 Some changes to font size on badge according to recommendation
Amit Sethi
parents: 324
diff changeset
   357
    c.setFont('Helvetica', 11)
324
1ad0be6866d6 Added Laptop registration no to badge
Amit Sethi
parents: 320
diff changeset
   358
    c.drawString((ref+2.8*cm),(ref+2.7*cm),reg_obj.organisation)
325
1255f442e4f8 Some changes to font size on badge according to recommendation
Amit Sethi
parents: 324
diff changeset
   359
    c.setFont('Helvetica', 11)
320
9414017a5589 Added view for automatic badge generation
Amit Sethi
parents: 239
diff changeset
   360
    try:
324
1ad0be6866d6 Added Laptop registration no to badge
Amit Sethi
parents: 320
diff changeset
   361
        c.drawString((ref+2.8*cm),(ref+2.2*cm),reg_obj.occupation.split(':')[1])
320
9414017a5589 Added view for automatic badge generation
Amit Sethi
parents: 239
diff changeset
   362
    except IndexError:
9414017a5589 Added view for automatic badge generation
Amit Sethi
parents: 239
diff changeset
   363
        c.drawString((ref+2.8*cm),(ref+2.3*cm),reg_obj.occupation)
9414017a5589 Added view for automatic badge generation
Amit Sethi
parents: 239
diff changeset
   364
        
9414017a5589 Added view for automatic badge generation
Amit Sethi
parents: 239
diff changeset
   365
    c.setFont('Helvetica', 10)
324
1ad0be6866d6 Added Laptop registration no to badge
Amit Sethi
parents: 320
diff changeset
   366
    c.drawString((ref+2.8*cm),(ref+1.7*cm),reg_obj.city)
320
9414017a5589 Added view for automatic badge generation
Amit Sethi
parents: 239
diff changeset
   367
    c.setFont('Helvetica', 10)
324
1ad0be6866d6 Added Laptop registration no to badge
Amit Sethi
parents: 320
diff changeset
   368
    c.drawString((ref+2.8*cm),(ref+1*cm),'Participant')
1ad0be6866d6 Added Laptop registration no to badge
Amit Sethi
parents: 320
diff changeset
   369
    
1ad0be6866d6 Added Laptop registration no to badge
Amit Sethi
parents: 320
diff changeset
   370
   
1ad0be6866d6 Added Laptop registration no to badge
Amit Sethi
parents: 320
diff changeset
   371
    try:
1ad0be6866d6 Added Laptop registration no to badge
Amit Sethi
parents: 320
diff changeset
   372
        wifi_obj=Wifi.objects.get(user=request.user.id)
1ad0be6866d6 Added Laptop registration no to badge
Amit Sethi
parents: 320
diff changeset
   373
        c.setFont('Helvetica', 10)
1ad0be6866d6 Added Laptop registration no to badge
Amit Sethi
parents: 320
diff changeset
   374
        c.drawString((ref+5.6*cm),(ref+0.5*cm),wifi_obj.registration_id)
1ad0be6866d6 Added Laptop registration no to badge
Amit Sethi
parents: 320
diff changeset
   375
    except :
1ad0be6866d6 Added Laptop registration no to badge
Amit Sethi
parents: 320
diff changeset
   376
        pass
320
9414017a5589 Added view for automatic badge generation
Amit Sethi
parents: 239
diff changeset
   377
9414017a5589 Added view for automatic badge generation
Amit Sethi
parents: 239
diff changeset
   378
    
9414017a5589 Added view for automatic badge generation
Amit Sethi
parents: 239
diff changeset
   379
    # Close the PDF object cleanly, and we're done.
9414017a5589 Added view for automatic badge generation
Amit Sethi
parents: 239
diff changeset
   380
    c.showPage()
9414017a5589 Added view for automatic badge generation
Amit Sethi
parents: 239
diff changeset
   381
    c.save()
9414017a5589 Added view for automatic badge generation
Amit Sethi
parents: 239
diff changeset
   382
    return response