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