#-*- coding: utf-8 -*-
# Create your views here.
from django.shortcuts import get_object_or_404, render_to_response
from django.http import HttpResponse, HttpResponseRedirect, Http404
from django.template import RequestContext
from django.views.generic import list_detail
from member.models import Manager, Member
from member.forms import (AuthenticationForm, RegistrationForm, ManagerAuthenticationForm,
PasswordResetForm, ManagerModificationForm, MemberModificationForm)
from member.signals import user_logged_in, user_logged_out
from member.hashers import make_password
from member import mlogin, mlogout
from member import get_user, get_manager
from django.utils import importlib
from django.core.exceptions import ImproperlyConfigured
from django.conf import settings
import random
COOKIE_KEY = 'auth_user_id'
BACKEND_COOKIE_KEY='auth_backend_id'
ITEMS_PER_PAGE = 10
def member_list(request):
user = get_user(request) or get_manager(request)
return list_detail.object_list(
request,
queryset=Member.objects.order_by('-id'),
paginate_by=ITEMS_PER_PAGE,
template_name='member_list.html',
template_object_name='member',
extra_context={'request':request,'user': user}
)
def manager_list(request):
user = get_manager(request) or get_user(request)
return list_detail.object_list(
request,
queryset=Manager.objects.order_by('-id'),
paginate_by=ITEMS_PER_PAGE,
template_name='manager_list.html',
template_object_name='manager',
extra_context={'request':request,'user': user}
)
def member_detail(request, member_id):
'''
display member details
'''
member = Member.objects.get(id=member_id)
return render_to_response('member_detail.html', {'user': get_user(request),
'member': member, 'request': request,},context_instance=RequestContext(request))
def manager_detail(request, manager_id):
manager = Manager.objects.get(id=manager_id)
return render_to_response('manager_detail.html', {'user': get_manager(request),
'manager': manager, 'request': request,}, context_instance=RequestContext(request))
def manager_register(request):
if request.method == "POST":
form = RegistrationForm(request.POST)
if form.is_valid():
cd = form.cleaned_data
if cd['password1'] == cd['password2']:
salt = cd.get('salt', '')
password = make_password(cd['password1'], salt)
manager = Manager(name = cd['username'],
email = cd['email'],
salt = salt,
password = password,
mobile = cd['mobile'],
qq = cd['qq'],
status = 0,
)
manager.save()
return HttpResponseRedirect("/member/manager/login/")
else :
form = RegistrationForm()
return render_to_response("manager_register.html", {'form': form},
context_instance=RequestContext(request))
def register(request):
'''member registration'''
if request.method == "POST":
form = RegistrationForm(request.POST)
if form.is_valid():
cd = form.cleaned_data
if cd['password1'] == cd['password2']:
salt = cd.get('salt', '')
password = make_password(cd['password1'], salt)
member = Member(username = cd['username'],
email = cd['email'],
salt = salt,
password = password,
mobile = cd['mobile'],
qq = cd['qq'],
status = 0,
)
member.save()
return HttpResponseRedirect("/member/login/")
else :
form.errors.append('Password not consistent')
else :
form = RegistrationForm()
return render_to_response("register.html", {'form': form},
context_instance=RequestContext(request))
def login(request):
"""
普通会员登录采用cookies方式
"""
if request.method == "POST":
form = AuthenticationForm(data=request.POST)
if form.is_valid():
user = form.get_user()
if user:
response = HttpResponseRedirect("/member/login/success/")
response.set_cookie(COOKIE_KEY, user.id, max_age=100)
response.set_cookie(BACKEND_COOKIE_KEY, user.backend, max_age=100)
return response
else :
form = AuthenticationForm(request)
return render_to_response('login.html', {'form': form},
context_instance=RequestContext(request))
def manager_login(request):
"""
管理员登陆采用session方式
"""
if request.method == "POST":
form = ManagerAuthenticationForm(data=request.POST)
if form.is_valid():
mlogin(request, form.get_user())
if request.session.test_cookie_worked():
request.session.delete_test_cookie()
return HttpResponseRedirect("/member/managers/")
else :
form = ManagerAuthenticationForm(request)
request.session.set_test_cookie()
return render_to_response('login.html', {'form': form},
context_instance=RequestContext(request))
def logged_in(request):
return render_to_response("success.html", {'user': get_user(request)})
def logout(request):
"""
Logs out the user and displays 'You are logged out' message.
"""
#auth_logout(request)
del request.COOKIES[COOKIE_KEY]
del request.COOKIES[BACKEND_COOKIE_KEY]
return render_to_response("logout.html")
def manager_logout(request):
mlogout(request)
return render_to_response("logout.html")
def test(request):
return render_to_response('test.html', {'user': get_user(request)})
def password_reset(request):
user = get_user(request)
if request.method == "POST":
form = PasswordResetForm(user=user, data=request.POST)
if form.is_valid():
cd = form.cleaned_data
if user.check_password(cd['old_password']):
if cd['new_password1'] == cd['new_password2']:
form.save()
#return HttpResponse("password changed success!")
return HttpResponseRedirect("/member/")
else :
form = PasswordResetForm(user=user)
return render_to_response('password_reset.html', {'form': form},
context_instance=RequestContext(request))
def manager_password_reset(request):
"""
管理员修改密码,与上面不同的只有user的取得方式
"""
user = get_manager(request)
if request.method == "POST":
form = PasswordResetForm(user=user, data=request.POST)
if form.is_valid():
cd = form.cleaned_data
if user.check_password(cd['old_password']):
if cd['new_password1'] == cd['new_password2']:
form.save()
return HttpResponseRedirect("/member/managers/")
else :
form = PasswordResetForm(user=user)
return render_to_response('password_reset.html', {'form': form},
context_instance=RequestContext(request))
def modify_member(request):
user = get_user(request)
if request.method == "POST":
form = MemberModificationForm(request.POST)
if form.is_valid():
cd = form.cleaned_data
user.username = cd['username']
user.email = cd['email']
user.mobile = cd['mobile']
user.qq = cd['qq']
user.save()
return HttpResponseRedirect("/member/detail/member/%d/" % user.id)
else :
form = MemberModificationForm()
return render_to_response('modify_member.html', {'form': form},