Add scope as a parameter to all view functions for user app and adjusted the URLs to accommodate scope.
--- a/project/scipycon/user/models.py Tue Jul 20 19:27:51 2010 +0530
+++ b/project/scipycon/user/models.py Tue Jul 20 19:28:45 2010 +0530
@@ -24,15 +24,3 @@
def fullname(self):
return '%s %s' % (self.user.first_name, self.user.last_name)
-
-def add_profile(sender, instance, signal, *args, **kwargs):
- """Create user profile on create of new user"""
- if not instance.is_superuser:
- try:
- profile, new = UserProfile.objects.get_or_create(user=instance)
- if new:
- profile.save()
- except:
- pass
-
-post_save.connect(add_profile, sender=User, weak=False)
--- a/project/scipycon/user/utils.py Tue Jul 20 19:27:51 2010 +0530
+++ b/project/scipycon/user/utils.py Tue Jul 20 19:28:45 2010 +0530
@@ -1,27 +1,23 @@
-# -*- coding: utf-8 -*-
-from __future__ import absolute_import
-
-#python imports
import os
-#django
from django.conf import settings
from django.core.exceptions import ObjectDoesNotExist
-
-#django.contrib
from django.contrib.auth.models import User
-#PIL
from PIL import Image
+from project.scipycon.user.models import UserProfile
+
def scipycon_createregistrant(request, data):
- """Create user"""
- email = data.get("email")
- name = data.get("name")
- username = data.get("username")
+ """Create user
+ """
- n = name.split(" ")
+ email = data.get('email')
+ name = data.get('name')
+ username = data.get('username')
+
+ n = name.split(' ')
if len(n) > 1:
first_name = ' '.join(n[:-1])
last_name = n[-1]
@@ -38,38 +34,50 @@
return user
-def scipycon_createuser(request, data):
- """Create user"""
- email = data.get("email")
- username = data.get("username")
- password = data.get("password_1")
- password = data.get("password_1")
+def scipycon_createuser(request, data, scope):
+ """Create user
+ """
+
+ from django.contrib.auth import authenticate
+ from django.contrib.auth import login
+
+ from project.scipycon.base.models import Event
+
+ email = data.get('email')
+ username = data.get('username')
+ password = data.get('password_1')
+ password = data.get('password_1')
# Create user
user = User.objects.create_user(
username=username, email=email, password=password)
- user.first_name = data.get("first_name")
- user.last_name = data.get("last_name")
+ user.first_name = data.get('first_name')
+ user.last_name = data.get('last_name')
user.save()
# Log in user
- from django.contrib.auth import authenticate
+
user = authenticate(username=username, password=password)
- from django.contrib.auth import login
login(request, user)
- profile = user.get_profile()
+ scope_entity = Event.objects.get(scope=scope)
+
+ try:
+ profile = user.get_profile()
+ except:
+ profile, new = UserProfile.objects.get_or_create(
+ user=user, scope=scope_entity)
+
photo = request.FILES.get('photo', None)
filename= None
if photo:
filename = handle_uploaded_photo(user, request.FILES['photo'])
if filename:
profile.photo = filename
- #print photo, filename
- profile.url = data.get("url")
- profile.about = data.get("about")
+ profile.url = data.get('url')
+ profile.about = data.get('about')
profile.save()
return user
--- a/project/scipycon/user/views.py Tue Jul 20 19:27:51 2010 +0530
+++ b/project/scipycon/user/views.py Tue Jul 20 19:28:45 2010 +0530
@@ -55,8 +55,6 @@
else:
photo = '/img/user-default.png'
- qstring = ""
-
wifi_comment = None
if wifiobj:
wifi_form = False
@@ -105,7 +103,8 @@
profile.about = form.data.get("about")
profile.save()
- redirect_to = reverse("scipycon_account")
+ redirect_to = reverse('scipycon_account',
+ kwargs={'scope': scope})
return set_message_cookie(redirect_to,
msg = u"Your profile has been changed.")
@@ -119,14 +118,16 @@
})
return render_to_response(template_name, RequestContext(request, {
- "form": form
+ 'params': {'scope': scope},
+ 'form': form
}))
def login(request, scope, template_name="user/login.html"):
"""Custom view to login or register/login a user.
- Integration of register and login form
- It uses Django's standard AuthenticationForm, though.
+ Integration of register and login form
+ It uses Django's standard AuthenticationForm, though.
"""
+
user = request.user
if user.is_authenticated():
redirect_to = reverse("scipycon_account", kwargs={'scope': scope})
@@ -144,7 +145,8 @@
redirect_to = request.POST.get("next")
# Light security check -- make sure redirect_to isn't garbage.
if not redirect_to or '//' in redirect_to or ' ' in redirect_to:
- redirect_to = reverse("scipycon_account")
+ redirect_to = reverse('scipycon_account',
+ kwargs={'scope': scope})
from django.contrib.auth import login
login(request, login_form.get_user())
@@ -159,7 +161,8 @@
redirect_to = request.POST.get("next")
if not redirect_to or '//' in redirect_to or ' ' in redirect_to:
- redirect_to = reverse("scipycon_account")
+ redirect_to = reverse('scipycon_account',
+ kwargs={'scope': scope})
return set_message_cookie(
redirect_to, msg = u"You have been registered and logged in.")
@@ -169,8 +172,10 @@
if next_url is None:
next_url = request.META.get("HTTP_REFERER")
if next_url is None:
- next_url = reverse("scipycon_account")
- # Get just the path of the url. See django.contrib.auth.views.login for more
+ next_url = reverse('scipycon_account', kwargs={'scope': scope})
+
+ # Get just the path of the url.
+ # See django.contrib.auth.views.login for more
next_url = urlparse(next_url)
next_url = next_url[2]
@@ -193,23 +198,25 @@
The reason to use a custom logout method is just to provide a login and a
logoutmethod on one place.
"""
+
from django.contrib.auth import logout
logout(request)
- redirect_to = '/'
+ redirect_to = '/%s' % (scope)
return set_message_cookie(redirect_to, msg = u"You have been logged out.")
@login_required
-def password(request, scope, template_name="user/password.html"):
+def password(request, scope, template_name='user/password.html'):
"""Changes the password of current user.
"""
- if request.method == "POST":
+
+ if request.method == 'POST':
form = PasswordChangeForm(request.user, request.POST)
if form.is_valid():
form.save()
- redirect_to = reverse("scipycon_account")
+ redirect_to = reverse('scipycon_account', kwargs={'scope': scope})
return set_message_cookie(redirect_to,
- msg = u"Your password has been changed.")
+ msg = u'Your password has been changed.')
else:
form = PasswordChangeForm(request.user)
@@ -219,22 +226,26 @@
}))
@login_required
-def username(request, scope, template_name="user/username.html"):
+def username(request, scope, template_name='user/username.html'):
"""Saves the username from the data form.
"""
- if request.method == "POST":
- username_form = UsernameForm(initial={"username" : request.user.username}, data=request.POST)
+ if request.method == 'POST':
+ username_form = UsernameForm(
+ initial={'username' : request.user.username},
+ data=request.POST)
if username_form.is_valid():
request.user.username = username_form.cleaned_data.get("username")
request.user.save()
- redirect_to = reverse("scipycon_account")
+ redirect_to = reverse('scipycon_account',
+ kwargs={'scope': scope})
return set_message_cookie(redirect_to,
msg = u"Your username has been changed.")
else:
username_form = UsernameForm(initial={"username" : request.user.username})
return render_to_response(template_name, RequestContext(request, {
- "form": username_form
+ 'params': {'scope': scope},
+ 'form': username_form
}))
@@ -271,4 +282,4 @@
json_response = {'results': results}
- return HttpResponse(json.dumps(json_response))
\ No newline at end of file
+ return HttpResponse(json.dumps(json_response))