forked from guohongze/adminset
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
新增accounts认证login loginout 拆分header状态栏
- Loading branch information
Showing
50 changed files
with
1,148 additions
and
1,453 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from django.contrib import admin | ||
|
||
# Register your models here. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
from __future__ import unicode_literals | ||
|
||
from django.apps import AppConfig | ||
|
||
|
||
class AccountsConfig(AppConfig): | ||
name = 'accounts' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
#! /usr/bin/env python | ||
# -*- coding: utf-8 -*- | ||
from django import forms | ||
from django.contrib import auth | ||
|
||
|
||
class LoginUserForm(forms.Form): | ||
username = forms.CharField(label=u'账 号', error_messages={'required': u'账号不能为空'}, | ||
widget=forms.TextInput(attrs={'class': 'form-control'})) | ||
password = forms.CharField(label=u'密 码', error_messages={'required': u'密码不能为空'}, | ||
widget=forms.PasswordInput(attrs={'class': 'form-control'})) | ||
|
||
def __init__(self, request=None, *args, **kwargs): | ||
self.request = request | ||
self.user_cache = None | ||
|
||
super(LoginUserForm, self).__init__(*args, **kwargs) | ||
|
||
def clean_password(self): | ||
username = self.cleaned_data.get('username') | ||
password = self.cleaned_data.get('password') | ||
|
||
if username and password: | ||
self.user_cache = auth.authenticate(username=username,password=password) | ||
if self.user_cache is None: | ||
raise forms.ValidationError(u'账号密码不匹配') | ||
elif not self.user_cache.is_active: | ||
raise forms.ValidationError(u'此账号已被禁用') | ||
return self.cleaned_data | ||
|
||
def get_user(self): | ||
return self.user_cache | ||
|
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
from __future__ import unicode_literals | ||
|
||
from django.db import models | ||
from django.contrib.auth.models import BaseUserManager, AbstractBaseUser | ||
# Create your models here. | ||
|
||
|
||
class PermissionList(models.Model): | ||
name = models.CharField(max_length=64) | ||
url = models.CharField(max_length=255) | ||
|
||
def __unicode__(self): | ||
return '%s(%s)' % (self.name, self.url) | ||
|
||
|
||
class RoleList(models.Model): | ||
name = models.CharField(max_length=64) | ||
permission = models.ManyToManyField(PermissionList, null=True, blank=True) | ||
|
||
def __unicode__(self): | ||
return self.name | ||
|
||
|
||
class UserManager(BaseUserManager): | ||
def create_user(self,email,username,password=None): | ||
if not email: | ||
raise ValueError('Users must have an email address') | ||
|
||
user = self.model( | ||
email=self.normalize_email(email), | ||
username=username, | ||
) | ||
|
||
user.set_password(password) | ||
user.save(using=self._db) | ||
return user | ||
|
||
def create_superuser(self, email, username, password): | ||
user = self.create_user(email, | ||
username=username, | ||
password=password, | ||
) | ||
|
||
user.is_active = True | ||
user.is_superuser = True | ||
user.save(using=self._db) | ||
return user | ||
|
||
|
||
class UserInfo(AbstractBaseUser): | ||
username = models.CharField(max_length=40, unique=True, db_index=True) | ||
email = models.EmailField(max_length=255) | ||
is_active = models.BooleanField(default=False) | ||
is_superuser = models.BooleanField(default=False) | ||
nickname = models.CharField(max_length=64, null=True) | ||
role = models.ForeignKey(RoleList,null=True,blank=True) | ||
|
||
def has_perm(self, perm, obj=None): | ||
if self.is_active and self.is_superuser: | ||
return True |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from django.test import TestCase | ||
|
||
# Create your tests here. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
#! /usr/bin/env python | ||
# -*- coding: utf-8 -*- | ||
from django.conf.urls import url, include | ||
from accounts import views | ||
|
||
|
||
urlpatterns = [ | ||
url(r'^$', views.index, name='accounts'), | ||
url(r'^login/$', views.login, name='loginurl'), | ||
url(r'^loginout/$', views.loginout, name='loginout'), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
from django.shortcuts import render | ||
|
||
# Create your views here. | ||
|
||
from django.shortcuts import render_to_response, redirect, HttpResponse, HttpResponseRedirect, RequestContext | ||
from django.contrib.auth.decorators import login_required | ||
from hashlib import sha1 | ||
from django.contrib import auth | ||
from forms import LoginUserForm | ||
|
||
|
||
@login_required() | ||
def index(request): | ||
return HttpResponse("ok") | ||
|
||
|
||
def login(request): | ||
if request.user.is_authenticated(): | ||
return HttpResponseRedirect('/') | ||
|
||
if request.method == 'GET' and request.GET.has_key('next'): | ||
next = request.GET['next'] | ||
else: | ||
next = '/' | ||
|
||
if request.method == "POST": | ||
form = LoginUserForm(request, data=request.POST) | ||
if form.is_valid(): | ||
auth.login(request, form.get_user()) | ||
return HttpResponseRedirect(request.POST['next']) | ||
else: | ||
form = LoginUserForm(request) | ||
|
||
kwvars = { | ||
'request': request, | ||
'form': form, | ||
'next': next, | ||
} | ||
|
||
return render_to_response('accounts/login.html', kwvars, RequestContext(request)) | ||
|
||
|
||
@login_required | ||
def loginout(request): | ||
auth.logout(request) | ||
return HttpResponseRedirect(request.META.get('HTTP_REFERER', '/')) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.