login/logout/register
This commit is contained in:
parent
b91cbbe8fb
commit
d3e68f5ba0
8 changed files with 101 additions and 1 deletions
0
app/__init__.py
Normal file
0
app/__init__.py
Normal file
3
app/models.py
Normal file
3
app/models.py
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
from django.db import models
|
||||
|
||||
# Create your models here.
|
||||
16
app/tests.py
Normal file
16
app/tests.py
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
"""
|
||||
This file demonstrates writing tests using the unittest module. These will pass
|
||||
when you run "manage.py test".
|
||||
|
||||
Replace this with more appropriate tests for your application.
|
||||
"""
|
||||
|
||||
from django.test import TestCase
|
||||
|
||||
|
||||
class SimpleTest(TestCase):
|
||||
def test_basic_addition(self):
|
||||
"""
|
||||
Tests that 1 + 1 always equals 2.
|
||||
"""
|
||||
self.assertEqual(1 + 1, 2)
|
||||
62
app/views.py
Normal file
62
app/views.py
Normal file
|
|
@ -0,0 +1,62 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
# vi:si:et:sw=4:sts=4:ts=4
|
||||
from datetime import datetime
|
||||
|
||||
from django.shortcuts import render_to_response, get_object_or_404, get_list_or_404
|
||||
from django.template import RequestContext
|
||||
from django.conf import settings
|
||||
from django.http import HttpResponse, HttpResponseRedirect
|
||||
from django.contrib import auth
|
||||
from django.contrib.auth.models import User, Group
|
||||
from django.contrib.auth import authenticate
|
||||
from django.core.mail import send_mail
|
||||
from django.core.cache import cache
|
||||
|
||||
from ox.django.shortcuts import render_to_json_response
|
||||
|
||||
|
||||
def login(request):
|
||||
message = ''
|
||||
if 'username' in request.POST and 'password' in request.POST:
|
||||
user = authenticate(username=request.POST['username'], password=request.POST['password'])
|
||||
if user and user.is_active:
|
||||
auth.login(request, user)
|
||||
return HttpResponseRedirect(settings.PREFIX+'/')
|
||||
else:
|
||||
message = 'Login failed.'
|
||||
context = RequestContext(request, {
|
||||
'settings': settings,
|
||||
'message': message
|
||||
})
|
||||
return render_to_response('login.html', context)
|
||||
|
||||
def logout(request):
|
||||
auth.logout(request)
|
||||
return HttpResponseRedirect(settings.PREFIX+'/')
|
||||
|
||||
def register(request):
|
||||
message = ''
|
||||
if 'username' in request.POST and 'password' in request.POST:
|
||||
username = request.POST['username']
|
||||
password = request.POST['password']
|
||||
email = request.POST['email']
|
||||
#fixme validate email
|
||||
if User.objects.filter(username__iexact=username).count() > 0:
|
||||
message = 'Username already exists'
|
||||
elif User.objects.filter(email__iexact=email).count() > 0:
|
||||
message = 'Email address already exits'
|
||||
elif not password:
|
||||
message = 'Password can not be empty'
|
||||
else:
|
||||
user = User(username=username, email=email)
|
||||
user.set_password(password)
|
||||
user.save()
|
||||
user = authenticate(username=username, password=password)
|
||||
auth.login(request, user)
|
||||
return HttpResponseRedirect(settings.PREFIX+'/')
|
||||
|
||||
context = RequestContext(request, {
|
||||
'settings': settings,
|
||||
'message': message
|
||||
})
|
||||
return render_to_response('register.html', context)
|
||||
Loading…
Add table
Add a link
Reference in a new issue