project/scipycon/user/views.py
author Amit Sethi
Thu, 18 Nov 2010 12:20:15 +0530
changeset 239 1973bf1a0a5d
parent 237 4cff1f43e4e1
child 320 9414017a5589
permissions -rw-r--r--
Changes to the template get-user-dump
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
94
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    37
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    38
@login_required
119
05a3e98b1fe0 Adding scope as a parameter to all the views.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 98
diff changeset
    39
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
    40
    """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
    41
    """
119
05a3e98b1fe0 Adding scope as a parameter to all the views.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 98
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
    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
    44
    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
    45
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    46
    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
    47
94
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    48
    try:
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    49
        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
    50
    except ObjectDoesNotExist:
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    51
        registration = None
144
27bde1b86666 Added registration and event to accounts page form.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 138
diff changeset
    52
94
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    53
    try:
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    54
        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
    55
    except ObjectDoesNotExist:
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    56
        wifiobj = None
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    57
144
27bde1b86666 Added registration and event to accounts page form.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 138
diff changeset
    58
    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
    59
94
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    60
    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
    61
        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
    62
    else:
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    63
        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
    64
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    65
    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
    66
        'params': {'scope': scope},
144
27bde1b86666 Added registration and event to accounts page form.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 138
diff changeset
    67
        'user' : user,
27bde1b86666 Added registration and event to accounts page form.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 138
diff changeset
    68
        'profile' : profile,
27bde1b86666 Added registration and event to accounts page form.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 138
diff changeset
    69
        'photo' : photo,
27bde1b86666 Added registration and event to accounts page form.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 138
diff changeset
    70
        'talks' : talks,
27bde1b86666 Added registration and event to accounts page form.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 138
diff changeset
    71
        'registration' : registration,
27bde1b86666 Added registration and event to accounts page form.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 138
diff changeset
    72
        '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
    73
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    74
@login_required
119
05a3e98b1fe0 Adding scope as a parameter to all the views.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 98
diff changeset
    75
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
    76
    """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
    77
    """
138
250e2731039d Reformatted the file and imports.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 128
diff changeset
    78
94
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    79
    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
    80
    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
    81
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    82
    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
    83
        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
    84
                               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
    85
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    86
        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
    87
            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
    88
            filename= None
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    89
            if photo:
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    90
                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
    91
            if filename:
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    92
                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
    93
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    94
            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
    95
            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
    96
            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
    97
            user.save()
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    98
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    99
            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
   100
            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
   101
            profile.save()
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   102
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
   103
            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
   104
                                  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
   105
            return set_message_cookie(redirect_to,
138
250e2731039d Reformatted the file and imports.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 128
diff changeset
   106
                    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
   107
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   108
    else:
138
250e2731039d Reformatted the file and imports.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 128
diff changeset
   109
        form = EditProfileForm(
250e2731039d Reformatted the file and imports.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 128
diff changeset
   110
            initial={
250e2731039d Reformatted the file and imports.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 128
diff changeset
   111
                'email' : user.email,
250e2731039d Reformatted the file and imports.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 128
diff changeset
   112
                'email2' : user.email, # hidden field
250e2731039d Reformatted the file and imports.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 128
diff changeset
   113
                'first_name' : user.first_name,
250e2731039d Reformatted the file and imports.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 128
diff changeset
   114
                'last_name' : user.last_name,
250e2731039d Reformatted the file and imports.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 128
diff changeset
   115
                'url' : profile.url,
250e2731039d Reformatted the file and imports.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 128
diff changeset
   116
                'about' : profile.about,
250e2731039d Reformatted the file and imports.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 128
diff changeset
   117
            })
94
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   118
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   119
    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
   120
        '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
   121
        '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
   122
    }))
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   123
119
05a3e98b1fe0 Adding scope as a parameter to all the views.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 98
diff changeset
   124
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
   125
    """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
   126
       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
   127
       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
   128
    """
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
   129
94
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   130
    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
   131
    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
   132
        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
   133
        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
   134
                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
   135
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   136
    # 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
   137
    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
   138
    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
   139
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   140
    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
   141
        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
   142
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   143
        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
   144
            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
   145
            # 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
   146
            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
   147
                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
   148
                                      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
   149
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   150
            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
   151
            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
   152
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   153
            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
   154
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   155
    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
   156
        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
   157
        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
   158
119
05a3e98b1fe0 Adding scope as a parameter to all the views.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 98
diff changeset
   159
            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
   160
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   161
            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
   162
            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
   163
                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
   164
                                      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
   165
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   166
            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
   167
                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
   168
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   169
    # 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
   170
    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
   171
    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
   172
        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
   173
    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
   174
        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
   175
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
   176
    # 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
   177
    # 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
   178
    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
   179
    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
   180
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   181
    try:
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   182
        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
   183
    except KeyError:
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   184
        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
   185
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   186
    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
   187
        'params': {'scope': scope},
05a3e98b1fe0 Adding scope as a parameter to all the views.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 98
diff changeset
   188
        'login_form' : login_form,
05a3e98b1fe0 Adding scope as a parameter to all the views.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 98
diff changeset
   189
        '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
   190
        'register_form' : register_form,
05a3e98b1fe0 Adding scope as a parameter to all the views.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 98
diff changeset
   191
        '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
   192
    }))
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   193
119
05a3e98b1fe0 Adding scope as a parameter to all the views.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 98
diff changeset
   194
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
   195
    """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
   196
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   197
    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
   198
    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
   199
    """
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
   200
94
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   201
    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
   202
    logout(request)
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   203
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
   204
    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
   205
    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
   206
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   207
@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
   208
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
   209
    """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
   210
    """
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
   211
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
   212
    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
   213
        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
   214
        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
   215
            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
   216
            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
   217
            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
   218
                    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
   219
    else:
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   220
        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
   221
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   222
    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
   223
        'params': {'scope': scope},
05a3e98b1fe0 Adding scope as a parameter to all the views.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 98
diff changeset
   224
        '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
   225
    }))
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   226
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   227
@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
   228
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
   229
    """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
   230
    """
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
   231
    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
   232
        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
   233
            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
   234
            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
   235
        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
   236
            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
   237
            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
   238
            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
   239
                                  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
   240
            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
   241
                    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
   242
    else:        
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   243
        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
   244
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   245
    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
   246
        '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
   247
        '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
   248
    }))
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   249
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   250
119
05a3e98b1fe0 Adding scope as a parameter to all the views.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 98
diff changeset
   251
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
   252
    """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
   253
    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
   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
    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
   257
    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
   258
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   259
    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
   260
        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
   261
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   262
    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
   263
    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
   264
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   265
    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
   266
        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
   267
        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
   268
        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
   269
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   270
    results = [{'id': '',
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   271
                '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
   272
                '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
   273
              }]
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   274
    
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   275
    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
   276
        results.append(
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   277
            {'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
   278
             '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
   279
             '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
   280
            })
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   281
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   282
    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
   283
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
   284
    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
   285
4cff1f43e4e1 Changes to make it possible for someone to view daily dumps.
Amit Sethi
parents: 144
diff changeset
   286
4cff1f43e4e1 Changes to make it possible for someone to view daily dumps.
Amit Sethi
parents: 144
diff changeset
   287
@login_required
4cff1f43e4e1 Changes to make it possible for someone to view daily dumps.
Amit Sethi
parents: 144
diff changeset
   288
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
   289
    """ 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
   290
    """
4cff1f43e4e1 Changes to make it possible for someone to view daily dumps.
Amit Sethi
parents: 144
diff changeset
   291
    print request.user.is_staff
4cff1f43e4e1 Changes to make it possible for someone to view daily dumps.
Amit Sethi
parents: 144
diff changeset
   292
    if request.user.is_staff:
4cff1f43e4e1 Changes to make it possible for someone to view daily dumps.
Amit Sethi
parents: 144
diff changeset
   293
        qs=Registration.objects.all()
4cff1f43e4e1 Changes to make it possible for someone to view daily dumps.
Amit Sethi
parents: 144
diff changeset
   294
        rows=[]    
4cff1f43e4e1 Changes to make it possible for someone to view daily dumps.
Amit Sethi
parents: 144
diff changeset
   295
        for obj in qs:
4cff1f43e4e1 Changes to make it possible for someone to view daily dumps.
Amit Sethi
parents: 144
diff changeset
   296
            row = {}
4cff1f43e4e1 Changes to make it possible for someone to view daily dumps.
Amit Sethi
parents: 144
diff changeset
   297
            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
   298
            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
   299
            try:
4cff1f43e4e1 Changes to make it possible for someone to view daily dumps.
Amit Sethi
parents: 144
diff changeset
   300
                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
   301
                row['sex'] = accomodation_require.sex
4cff1f43e4e1 Changes to make it possible for someone to view daily dumps.
Amit Sethi
parents: 144
diff changeset
   302
            except:
239
1973bf1a0a5d Changes to the template get-user-dump
Amit Sethi
parents: 237
diff changeset
   303
                row['sex'] = '-'
237
4cff1f43e4e1 Changes to make it possible for someone to view daily dumps.
Amit Sethi
parents: 144
diff changeset
   304
            row['city'] = obj.city
4cff1f43e4e1 Changes to make it possible for someone to view daily dumps.
Amit Sethi
parents: 144
diff changeset
   305
            row['organization'] = obj.organisation
4cff1f43e4e1 Changes to make it possible for someone to view daily dumps.
Amit Sethi
parents: 144
diff changeset
   306
            row['occupation'] = obj.occupation
4cff1f43e4e1 Changes to make it possible for someone to view daily dumps.
Amit Sethi
parents: 144
diff changeset
   307
            row['conference'] = obj.conference 
4cff1f43e4e1 Changes to make it possible for someone to view daily dumps.
Amit Sethi
parents: 144
diff changeset
   308
            row['sprint'] = obj.sprint
4cff1f43e4e1 Changes to make it possible for someone to view daily dumps.
Amit Sethi
parents: 144
diff changeset
   309
            row['tutorial'] = obj.tutorial
4cff1f43e4e1 Changes to make it possible for someone to view daily dumps.
Amit Sethi
parents: 144
diff changeset
   310
            try:
4cff1f43e4e1 Changes to make it possible for someone to view daily dumps.
Amit Sethi
parents: 144
diff changeset
   311
                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
   312
                row['wifi'] = wifi_require.wifi
4cff1f43e4e1 Changes to make it possible for someone to view daily dumps.
Amit Sethi
parents: 144
diff changeset
   313
            except:
4cff1f43e4e1 Changes to make it possible for someone to view daily dumps.
Amit Sethi
parents: 144
diff changeset
   314
                row['wifi']='Wifi Unspecified'
4cff1f43e4e1 Changes to make it possible for someone to view daily dumps.
Amit Sethi
parents: 144
diff changeset
   315
            rows.append(row)
4cff1f43e4e1 Changes to make it possible for someone to view daily dumps.
Amit Sethi
parents: 144
diff changeset
   316
        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
   317
                    'rows':  rows}))
4cff1f43e4e1 Changes to make it possible for someone to view daily dumps.
Amit Sethi
parents: 144
diff changeset
   318
                    
4cff1f43e4e1 Changes to make it possible for someone to view daily dumps.
Amit Sethi
parents: 144
diff changeset
   319
4cff1f43e4e1 Changes to make it possible for someone to view daily dumps.
Amit Sethi
parents: 144
diff changeset
   320
    else:
4cff1f43e4e1 Changes to make it possible for someone to view daily dumps.
Amit Sethi
parents: 144
diff changeset
   321
            raise Http404