diff --git a/allianceauth/urls.py b/allianceauth/urls.py index 2cd3bd38..83977e7f 100644 --- a/allianceauth/urls.py +++ b/allianceauth/urls.py @@ -8,4 +8,7 @@ urlpatterns = patterns('', # url(r'^$', 'allianceauth.views.home', name='home'), # url(r'^blog/', include('blog.urls')), url(r'^$', 'portal.views.index', name='index'), + url(r'^loginuser/','authentication.views.login_user', name='loginuser'), + url(r'^logoutuser/','authentication.views.logout_user', name='logoutuser'), + url(r'^register/', 'registration.views.register', name='register'), ) diff --git a/authentication/forms.py b/authentication/forms.py new file mode 100644 index 00000000..726ab1fb --- /dev/null +++ b/authentication/forms.py @@ -0,0 +1,5 @@ +from django import forms + +class LoginForm(forms.Form): + username = forms.CharField(max_length=16, required = True) + password = forms.CharField(widget=forms.PasswordInput()) diff --git a/authentication/models.py b/authentication/models.py index 32985059..6f981ac7 100644 --- a/authentication/models.py +++ b/authentication/models.py @@ -24,6 +24,27 @@ class AllianceUserManager(BaseUserManager): user.save(using=self._db) return user + def create_user_withapi(self, username, email, password, api_id, api_key): + """ + Creates and saves a User with the given email, date of + birth and password. + """ + + if not username: + raise ValueError('Users must have a username') + + if not email: + raise ValueError('Users must have an email address') + + user = AllianceUser() + user.set_username(username) + user.set_email(email) + user.set_password(password) + user.set_api_id(api_id) + user.set_api_key(api_key) + user.save(using=self._db) + return user + def create_superuser(self, username, email, password): """ Creates and saves a superuser with the given email, date of @@ -43,6 +64,8 @@ class AllianceUser(AbstractBaseUser): is_admin = models.BooleanField(default=False) is_moderator = models.BooleanField(default = False) is_banned = models.BooleanField(default = False) + api_id = models.CharField(max_length = 254) + api_key = models.CharField(max_length = 254) objects = AllianceUserManager() USERNAME_FIELD = 'username' diff --git a/authentication/views.py b/authentication/views.py index 91ea44a2..776ec109 100644 --- a/authentication/views.py +++ b/authentication/views.py @@ -1,3 +1,27 @@ -from django.shortcuts import render +from django.contrib.auth import authenticate, login, logout +from django.http import HttpResponseRedirect +from django.shortcuts import render_to_response, render +from django.views.decorators.csrf import csrf_exempt +from django.template import RequestContext +from forms import LoginForm # Create your views here. +def login_user(request): + if request.method == 'POST': + form = LoginForm(request.POST) + + if form.is_valid(): + user = authenticate(username=form.cleaned_data['username'], password=form.cleaned_data['password']) + if user is not None: + if user.is_active: + login(request, user) + return HttpResponseRedirect("/") + else: + form = LoginForm() + + return render_to_response('public/login.html',{'form':form}, context_instance=RequestContext(request)) + + +def logout_user(request): + logout(request) + return HttpResponseRedirect("/") \ No newline at end of file diff --git a/registration/__init__.py b/registration/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/registration/admin.py b/registration/admin.py new file mode 100644 index 00000000..8c38f3f3 --- /dev/null +++ b/registration/admin.py @@ -0,0 +1,3 @@ +from django.contrib import admin + +# Register your models here. diff --git a/registration/forms.py b/registration/forms.py new file mode 100644 index 00000000..b758f699 --- /dev/null +++ b/registration/forms.py @@ -0,0 +1,8 @@ +from django import forms + +class RegistrationForm(forms.Form): + username = forms.CharField(max_length=16, required = True) + password = forms.CharField(widget=forms.PasswordInput()) + email = forms.CharField(max_length=254, required = True) + api_id = forms.CharField(max_length=254, required = True) + api_key = forms.CharField(max_length=254, required = True) \ No newline at end of file diff --git a/registration/models.py b/registration/models.py new file mode 100644 index 00000000..71a83623 --- /dev/null +++ b/registration/models.py @@ -0,0 +1,3 @@ +from django.db import models + +# Create your models here. diff --git a/registration/tests.py b/registration/tests.py new file mode 100644 index 00000000..7ce503c2 --- /dev/null +++ b/registration/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/registration/views.py b/registration/views.py new file mode 100644 index 00000000..005956e5 --- /dev/null +++ b/registration/views.py @@ -0,0 +1,26 @@ +from django.http import Http404,HttpResponseRedirect +from django.shortcuts import render_to_response, render +from django.template import RequestContext +from authentication.models import AllianceUserManager +from forms import RegistrationForm + +# Create your views here. +def register(request): + if request.method == 'POST': + form = RegistrationForm(request.POST) + + if form.is_valid(): + userManager = AllianceUserManager() + userManager.create_user_withapi( + form.cleaned_data['username'], + form.cleaned_data['email'], + form.cleaned_data['password'], + form.cleaned_data['api_id'], + form.cleaned_data['api_key'] + ) + + return HttpResponseRedirect("/") + else: + form = RegistrationForm() + + return render_to_response('public/register.html',{'form':form}, context_instance=RequestContext(request)) diff --git a/templates/public/base.html b/templates/public/base.html index 60d80b79..7832f9eb 100644 --- a/templates/public/base.html +++ b/templates/public/base.html @@ -32,7 +32,11 @@
@@ -40,6 +44,8 @@