diff options
author | Aamir Khan <syst3m.w0rm@gmail.com> | 2012-07-01 06:14:25 -0400 |
---|---|---|
committer | Aamir Khan <syst3m.w0rm@gmail.com> | 2012-07-01 06:14:25 -0400 |
commit | 67d5552db4e48582587b0a09ce5659422d2e2d25 (patch) | |
tree | 5268b01db4fa9375ac57336718310b4a0818525a | |
parent | afd6cbf2f6911500df4c32417dc041192da41d62 (diff) | |
download | hyperkitty-67d5552db4e48582587b0a09ce5659422d2e2d25.tar.gz hyperkitty-67d5552db4e48582587b0a09ce5659422d2e2d25.tar.xz hyperkitty-67d5552db4e48582587b0a09ce5659422d2e2d25.zip |
Basic user registration up
-rw-r--r-- | templates/base.html | 2 | ||||
-rw-r--r-- | urls.py | 2 | ||||
-rw-r--r-- | views/accounts.py | 26 | ||||
-rw-r--r-- | views/forms.py | 32 |
4 files changed, 61 insertions, 1 deletions
diff --git a/templates/base.html b/templates/base.html index 7bbf6ae..b6d2251 100644 --- a/templates/base.html +++ b/templates/base.html @@ -31,7 +31,7 @@ {{month}} </li> <li> - {% if user.is_authenticated %}<a class="mm_logout" href="{% url user_logout %}">Logout</a>{% else %}<a class="mm_user" href="{% url user_login %}">Login</a>{% endif %} + {% if user.is_authenticated %}<a class="mm_logout" href="{% url user_logout %}">Logout</a>{% else %}<a class="mm_user" href="{% url user_login %}">Login</a> or <a href="{% url user_registration %}"> Sign Up </a> {% endif %} </li> </ul> @@ -14,6 +14,8 @@ urlpatterns = patterns('', url(r'^accounts/login/$', 'views.accounts.user_login', name='user_login'), url(r'^accounts/logout/$', 'views.accounts.user_logout', name='user_logout'), url(r'^accounts/profile/$', 'views.accounts.user_profile', name='user_profile'), + url(r'^accounts/register/$', 'views.accounts.user_registration', name='user_registration'), + # Index url(r'^/$', 'views.pages.index', name='index'), diff --git a/views/accounts.py b/views/accounts.py index 1eeb8c9..b79943d 100644 --- a/views/accounts.py +++ b/views/accounts.py @@ -18,6 +18,7 @@ from django.utils.translation import gettext as _ from urllib2 import HTTPError from urlparse import urlparse +from forms import RegistrationForm from gsoc.utils import log def user_logout(request): @@ -65,3 +66,28 @@ def user_profile(request, user_email = None): }) return HttpResponse(t.render(c)) + + +def user_registration(request): + if request.user.is_authenticated(): + # Already registed, redirect back to home page + return redirect('index') + + if request.POST: + form = RegistrationForm(request.POST) + if form.is_valid(): + # Save the user data. + form.save(form.cleaned_data) + user = authenticate(username=form.cleaned_data['username'], + password=form.cleaned_data['password1']) + + if user is not None: + log('debug', user) + if user.is_active: + login(request,user) + return redirect('index') + else: + form = RegistrationForm() + + return render_to_response('register.html', {'form': form}, context_instance=RequestContext(request)) + diff --git a/views/forms.py b/views/forms.py index a2aac0e..97e5550 100644 --- a/views/forms.py +++ b/views/forms.py @@ -1,4 +1,36 @@ from django import forms +from django.core import validators +from django.contrib.auth.models import User + +def isValidUsername(username): + try: + User.objects.get(username=username) + except User.DoesNotExist: + return + raise validators.ValidationError('The username "%s" is already taken.' % username) + +class RegistrationForm(forms.Form): + + username = forms.CharField(label='username', help_text=None, + widget=forms.TextInput( + attrs={'placeholder': 'username...'} + ), required = True, validators=[isValidUsername] + ) + + email = forms.EmailField(required=True) + + password1 = forms.CharField(widget=forms.PasswordInput) + + password2 = forms.CharField(widget=forms.PasswordInput) + + def save(self, new_user_data): + u = User.objects.create_user(new_user_data['username'], + new_user_data['email'], + new_user_data['password1']) + u.is_active = True + u.save() + return u + class AddTagForm(forms.Form): tag = forms.CharField(label='', help_text=None, |