added user login and registration

This commit is contained in:
Raynaldo Rivera 2014-10-03 15:45:42 -07:00
parent 74ad02b677
commit 4ca6c7dbb0
14 changed files with 168 additions and 5 deletions

View File

@ -8,4 +8,7 @@ urlpatterns = patterns('',
# url(r'^$', 'allianceauth.views.home', name='home'), # url(r'^$', 'allianceauth.views.home', name='home'),
# url(r'^blog/', include('blog.urls')), # url(r'^blog/', include('blog.urls')),
url(r'^$', 'portal.views.index', name='index'), 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'),
) )

5
authentication/forms.py Normal file
View File

@ -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())

View File

@ -24,6 +24,27 @@ class AllianceUserManager(BaseUserManager):
user.save(using=self._db) user.save(using=self._db)
return user 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): def create_superuser(self, username, email, password):
""" """
Creates and saves a superuser with the given email, date of 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_admin = models.BooleanField(default=False)
is_moderator = models.BooleanField(default = False) is_moderator = models.BooleanField(default = False)
is_banned = 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() objects = AllianceUserManager()
USERNAME_FIELD = 'username' USERNAME_FIELD = 'username'

View File

@ -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. # 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("/")

0
registration/__init__.py Normal file
View File

3
registration/admin.py Normal file
View File

@ -0,0 +1,3 @@
from django.contrib import admin
# Register your models here.

8
registration/forms.py Normal file
View File

@ -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)

3
registration/models.py Normal file
View File

@ -0,0 +1,3 @@
from django.db import models
# Create your models here.

3
registration/tests.py Normal file
View File

@ -0,0 +1,3 @@
from django.test import TestCase
# Create your tests here.

26
registration/views.py Normal file
View File

@ -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))

View File

@ -32,7 +32,11 @@
</div> </div>
<div class="navbar-collapse collapse"> <div class="navbar-collapse collapse">
<ul class="nav navbar-nav navbar-right"> <ul class="nav navbar-nav navbar-right">
<li><a href="#">Logout</a></li> {% if user.is_authenticated %}
<li><a href="/logoutuser/">Logout</a></li>
{% else %}
<li><a href="/loginuser/">Login</a></li>
{% endif %}
</ul> </ul>
</div> </div>
</div> </div>
@ -40,6 +44,8 @@
<div class="container-fluid"> <div class="container-fluid">
<div class="row"> <div class="row">
{% if user.is_authenticated %}
<div class="col-sm-3 col-md-2 sidebar"> <div class="col-sm-3 col-md-2 sidebar">
<ul class="nav nav-sidebar"> <ul class="nav nav-sidebar">
<li class="active"><a href="#">Overview</a></li> <li class="active"><a href="#">Overview</a></li>
@ -49,11 +55,11 @@
<li><a href="#">Help</a></li> <li><a href="#">Help</a></li>
</ul> </ul>
</div> </div>
<div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main"> {% endif %}
{% block content %} {% block content %}
{% endblock content %} {% endblock content %}
</div> </div>
</div>
<!-- Bootstrap core JavaScript <!-- Bootstrap core JavaScript
================================================== --> ================================================== -->

View File

@ -5,5 +5,8 @@
{% block page_title %}Something something here{% endblock page_title %} {% block page_title %}Something something here{% endblock page_title %}
{% block content %} {% block content %}
<p> WELCOME THIS IS THE INDEX PAGE</p> <div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
<h1 class="page-header">Dashboard</h1>
</div>
{% endblock content %} {% endblock content %}

View File

@ -0,0 +1,22 @@
{% extends "public/base.html" %}
{% block title %}Alliance Auth{% endblock %}
{% block page_title %}Login Page{% endblock page_title %}
{% block content %}
<div class="container">
<div class="row-fluid">
<div class="center-block" style="center">
<p>
<form class="form-signin" role="form" action="/loginuser/" method="POST">
{% csrf_token %}
<h2 class="form-signin-heading">Please sign in</h2>
{{form.as_p}}
<button class="btn btn-lg btn-primary btn-block" type="submit">Sign in</button>
</form>
</p>
</div>
</div>
</div>
{% endblock content %}

View File

@ -0,0 +1,34 @@
{% extends "public/base.html" %}
{% block title %}Alliance Auth{% endblock %}
{% block page_title %}Register Page{% endblock page_title %}
{% block content %}
<div class="container">
<div class="row-fluid">
<div class="center-block" style="center">
<p>
<form action="/register/" method="POST">
{% csrf_token %}
<h2 class="form-signin-heading">Register Account</h2>
<p>
Username:<br/>
{{form.username.errors}}
{{form.username}}<br/> Email:<br/>
{{form.email.errors}}
{{form.email}}<br/> Password:<br/>
{{form.password.errors}}
{{form.password}}<br/>Api ID: <br/>
{{form.api_id.errors}}
{{form.api_id}}<br/>Api Key: <br/>
{{form.api_key.errors}}
{{form.api_key}}<br/>
</p>
<input type="submit" value="submit" />
</form>
</p>
</div>
</div>
</div>
{% endblock content %}