62 lines
2.2 KiB
Python
62 lines
2.2 KiB
Python
# -*- 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)
|