parent
d9f1e705ac
commit
23f9d1fbbb
20 changed files with 444 additions and 3 deletions
File diff suppressed because one or more lines are too long
@ -0,0 +1,10 @@ |
||||
<!DOCTYPE html> |
||||
<html lang="en"> |
||||
<head> |
||||
<meta charset="UTF-8"> |
||||
<title>edit</title> |
||||
</head> |
||||
<body> |
||||
<h1>用户修改页面</h1> |
||||
</body> |
||||
</html> |
@ -0,0 +1,49 @@ |
||||
{% load static %} |
||||
<!DOCTYPE html> |
||||
<html lang="en"> |
||||
<head> |
||||
<meta charset="UTF-8"> |
||||
<title>用户登录</title> |
||||
<link rel="stylesheet" href="{% static 'dist/css/adminlte.min.css' %}"> |
||||
</head> |
||||
<body class="hold-transition register-page"> |
||||
<div class="register-box"> |
||||
<div class="register-logo"><b>用户登录</b></div> |
||||
<div class="card"> |
||||
<div class="card-body register-card-body"> |
||||
<form action="" method="post"> |
||||
{% csrf_token %} |
||||
<div class="input-group mb-3"> |
||||
<input type="text" class="form-control" name="username" placeholder="用户名"> |
||||
<div class="input-group-append"> |
||||
<div class="input-group-text"> |
||||
<span class="fas fa-user"></span> |
||||
</div> |
||||
</div> |
||||
</div> |
||||
<div class="input-group mb-3"> |
||||
<input type="password" class="form-control" name="password" placeholder="密码"> |
||||
<div class="input-group-append"> |
||||
<div class="input-group-text"> |
||||
<span class="fas fa-user"></span> |
||||
</div> |
||||
</div> |
||||
</div> |
||||
<div class="row"> |
||||
<div class="col-8"> |
||||
<label for="agreement"> |
||||
{{ info }} |
||||
</label> |
||||
</div> |
||||
<div class="col-4"> |
||||
<button type="submit" class="btn btn-primary btn-block">登录</button> |
||||
</div> |
||||
</div> |
||||
</form> |
||||
|
||||
</div> |
||||
</div> |
||||
</div> |
||||
<form></form> |
||||
</body> |
||||
</html> |
@ -0,0 +1,10 @@ |
||||
<!DOCTYPE html> |
||||
<html lang="en"> |
||||
<head> |
||||
<meta charset="UTF-8"> |
||||
<title>Test</title> |
||||
</head> |
||||
<body> |
||||
<h1>Test Page</h1> |
||||
</body> |
||||
</html> |
@ -0,0 +1,33 @@ |
||||
<!DOCTYPE html> |
||||
<html lang="en"> |
||||
<head> |
||||
<meta charset="UTF-8"> |
||||
<title>UserIndex</title> |
||||
</head> |
||||
<body> |
||||
欢迎{{ request.user }}来到商场系统! |
||||
<a href="{% url 'user_manager_logout' %}">退出登录</a> |
||||
<br> |
||||
用户信息列表 |
||||
<table border="1px"> |
||||
<tr> |
||||
<td>账号</td> |
||||
<td>用户名</td> |
||||
<td>email</td> |
||||
<td>操作</td> |
||||
</tr> |
||||
{% for user in users %} |
||||
<tr> |
||||
<td>{{ user.username }}</td> |
||||
<td>{{ user.first_name }} {{ user.last_name }}</td> |
||||
<td>{{ user.email }}</td> |
||||
|
||||
<td><a href="{% url 'user_manager_edit' %}">修改</a></td> |
||||
{% if perms.myuser.delete_myuser %} |
||||
<td>删除</td> |
||||
{% endif %} |
||||
</tr> |
||||
{% endfor %} |
||||
</table> |
||||
</body> |
||||
</html> |
@ -0,0 +1,35 @@ |
||||
<!DOCTYPE html> |
||||
<html lang="en"> |
||||
<head> |
||||
<meta charset="UTF-8"> |
||||
<title>用户注册</title> |
||||
</head> |
||||
|
||||
<body> |
||||
{% load static %} |
||||
<link rel="stylesheet" href="{% static 'dist/css/adminlte.min.css' %}"> |
||||
<form action="" method="POST"> |
||||
{% csrf_token %} |
||||
<div class="input-group mb3"> |
||||
<input type="text" class="form-control" name="username" placeholder="用户名"> |
||||
</div> |
||||
<div class="input-group mb3"> |
||||
<input type="email" class="form-control" name="email" placeholder="邮箱"> |
||||
</div> |
||||
<div class="input-group mb3"> |
||||
<input type="password" class="form-control" name="password" placeholder="密码"> |
||||
</div> |
||||
<div class="input-group mb3"> |
||||
<input type="password" class="form-control" name="re-password" placeholder="重复密码"> |
||||
</div> |
||||
<div class="row"> |
||||
<div class="col-8"> |
||||
<label for="agreeTerms">{{ info }}</label> |
||||
</div> |
||||
<div class="col-4"> |
||||
<button type="submit" class="btn-primary btn-block">注册</button> |
||||
</div> |
||||
</div> |
||||
</form> |
||||
</body> |
||||
</html> |
@ -0,0 +1,11 @@ |
||||
from django.contrib import admin |
||||
from user_manager.models import MyUser |
||||
|
||||
|
||||
# Register your models here. |
||||
|
||||
class MyUserAdmin(admin.ModelAdmin): |
||||
list_display = ['username', 'email'] |
||||
|
||||
|
||||
admin.site.register(MyUser, MyUserAdmin) |
@ -0,0 +1,6 @@ |
||||
from django.apps import AppConfig |
||||
|
||||
|
||||
class UserManagerConfig(AppConfig): |
||||
default_auto_field = 'django.db.models.BigAutoField' |
||||
name = 'user_manager' |
@ -0,0 +1,40 @@ |
||||
from django.utils.deprecation import MiddlewareMixin |
||||
from django.http import HttpResponse |
||||
|
||||
|
||||
class AuthMiddleWare1(MiddlewareMixin): |
||||
def process_request(self, request): |
||||
print('process_request-1') |
||||
|
||||
def process_view(self, request, callback, callback_args, callback_kwargs): |
||||
print('process_view-1') |
||||
|
||||
def process_template_response(self, request, response): |
||||
print('process_template_response -1') |
||||
return response |
||||
|
||||
def process_exception(self, request, exception): |
||||
print('process_exception-1') |
||||
|
||||
def process_response(self, request, response): |
||||
print('prcoess_response-1') |
||||
return response |
||||
|
||||
|
||||
class AuthMiddleWare2(MiddlewareMixin): |
||||
def process_request(self, request): |
||||
print('process_request-2') |
||||
|
||||
def process_view(self, request, callback, callback_args, callback_kwargs): |
||||
print('process_view-2') |
||||
|
||||
def process_template_response(self, request, response): |
||||
print('process_template_response -2') |
||||
return response |
||||
|
||||
def process_exception(self, request, exception): |
||||
print('process_exception-2') |
||||
|
||||
def process_response(self, request, response): |
||||
print('prcoess_response-2') |
||||
return response |
@ -0,0 +1,19 @@ |
||||
from django.utils.deprecation import MiddlewareMixin |
||||
from django.shortcuts import redirect |
||||
import re |
||||
|
||||
|
||||
class PermissionMiddleware(MiddlewareMixin): |
||||
def process_request(self, request): |
||||
curr_path = request.path |
||||
print(curr_path) |
||||
# 白名单 |
||||
white_list = ['/myuser_login/', '/myuser_reg'] |
||||
for w in white_list: |
||||
if re.search(w, curr_path): |
||||
return None |
||||
|
||||
# 验证登录 |
||||
print(request.user.is_authenticated) |
||||
if not request.user.is_authenticated: |
||||
return redirect('/myuser_login/') |
@ -0,0 +1,47 @@ |
||||
# Generated by Django 4.2.3 on 2023-08-06 23:55 |
||||
|
||||
import django.contrib.auth.models |
||||
import django.contrib.auth.validators |
||||
from django.db import migrations, models |
||||
import django.utils.timezone |
||||
|
||||
|
||||
class Migration(migrations.Migration): |
||||
|
||||
initial = True |
||||
|
||||
dependencies = [ |
||||
('auth', '0012_alter_user_first_name_max_length'), |
||||
] |
||||
|
||||
operations = [ |
||||
migrations.CreateModel( |
||||
name='MyUser', |
||||
fields=[ |
||||
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), |
||||
('password', models.CharField(max_length=128, verbose_name='password')), |
||||
('last_login', models.DateTimeField(blank=True, null=True, verbose_name='last login')), |
||||
('is_superuser', models.BooleanField(default=False, help_text='Designates that this user has all permissions without explicitly assigning them.', verbose_name='superuser status')), |
||||
('username', models.CharField(error_messages={'unique': 'A user with that username already exists.'}, help_text='Required. 150 characters or fewer. Letters, digits and @/./+/-/_ only.', max_length=150, unique=True, validators=[django.contrib.auth.validators.UnicodeUsernameValidator()], verbose_name='username')), |
||||
('first_name', models.CharField(blank=True, max_length=150, verbose_name='first name')), |
||||
('last_name', models.CharField(blank=True, max_length=150, verbose_name='last name')), |
||||
('email', models.EmailField(blank=True, max_length=254, verbose_name='email address')), |
||||
('is_staff', models.BooleanField(default=False, help_text='Designates whether the user can log into this admin site.', verbose_name='staff status')), |
||||
('is_active', models.BooleanField(default=True, help_text='Designates whether this user should be treated as active. Unselect this instead of deleting accounts.', verbose_name='active')), |
||||
('date_joined', models.DateTimeField(default=django.utils.timezone.now, verbose_name='date joined')), |
||||
('photo', models.CharField(max_length=50, verbose_name='用户头像')), |
||||
('weChat', models.CharField(max_length=30, verbose_name='微信')), |
||||
('level', models.CharField(max_length=1, verbose_name='用户等级')), |
||||
('groups', models.ManyToManyField(blank=True, help_text='The groups this user belongs to. A user will get all permissions granted to each of their groups.', related_name='user_set', related_query_name='user', to='auth.group', verbose_name='groups')), |
||||
('user_permissions', models.ManyToManyField(blank=True, help_text='Specific permissions for this user.', related_name='user_set', related_query_name='user', to='auth.permission', verbose_name='user permissions')), |
||||
], |
||||
options={ |
||||
'verbose_name': 'user', |
||||
'verbose_name_plural': 'users', |
||||
'abstract': False, |
||||
}, |
||||
managers=[ |
||||
('objects', django.contrib.auth.models.UserManager()), |
||||
], |
||||
), |
||||
] |
@ -0,0 +1,17 @@ |
||||
# Generated by Django 4.2.3 on 2023-08-08 00:14 |
||||
|
||||
from django.db import migrations |
||||
|
||||
|
||||
class Migration(migrations.Migration): |
||||
|
||||
dependencies = [ |
||||
('user_manager', '0001_initial'), |
||||
] |
||||
|
||||
operations = [ |
||||
migrations.AlterModelOptions( |
||||
name='myuser', |
||||
options={'permissions': (['check_myuser', '审核用户信息'],), 'verbose_name': 'user', 'verbose_name_plural': 'users'}, |
||||
), |
||||
] |
@ -0,0 +1,17 @@ |
||||
from django.db import models |
||||
from django.contrib.auth.models import AbstractUser |
||||
|
||||
|
||||
# Create your models here. |
||||
class MyUser(AbstractUser): |
||||
photo = models.CharField('用户头像', max_length=50) |
||||
weChat = models.CharField('微信', max_length=30) |
||||
level = models.CharField('用户等级', max_length=1) |
||||
|
||||
def __str__(self): |
||||
return self.username |
||||
|
||||
class Meta(AbstractUser.Meta): |
||||
permissions = ( |
||||
['check_myuser', '审核用户信息'], |
||||
) |
@ -0,0 +1,3 @@ |
||||
from django.test import TestCase |
||||
|
||||
# Create your tests here. |
@ -0,0 +1,15 @@ |
||||
from django.contrib import admin |
||||
from django.urls import path, re_path, include |
||||
from user_manager import views |
||||
|
||||
urlpatterns = [ |
||||
path('user_reg/', views.user_reg), |
||||
path('user_login/', views.user_login), |
||||
path('myuser_reg/', views.myuser_reg), |
||||
path('myuser_login/', views.myuser_login, name='user_manager_login'), |
||||
path('myuser_index/', views.user_index), |
||||
path('myuser_logout/', views.myuser_logout, name='user_manager_logout'), |
||||
path('myuser_edit/', views.myuser_edit, name='user_manager_edit'), |
||||
path('test/', views.test_middle, name='test'), |
||||
|
||||
] |
@ -0,0 +1,106 @@ |
||||
from django.shortcuts import render, redirect |
||||
from django.http.response import HttpResponse |
||||
from django.urls import reverse |
||||
from django.contrib.auth import authenticate, login, logout |
||||
from django.contrib.auth.models import User |
||||
from django.contrib.auth.decorators import login_required, permission_required |
||||
from user_manager.models import MyUser |
||||
|
||||
|
||||
# Create your views here. |
||||
def user_reg(request): |
||||
if request.method == 'GET': |
||||
return render(request, 'user_manager/user_reg.html') |
||||
if request.method == 'POST': |
||||
user_name = request.POST.get('username') |
||||
user_pw = request.POST.get('password') |
||||
if User.objects.filter(username=user_name): |
||||
info_msg = '用户已经存在' |
||||
else: |
||||
user_data = {'username': user_name, 'password': user_pw, 'email': request.POST.get('email'), 'is_staff': 1, |
||||
'is_active': 1, 'is_superuser': 1} |
||||
User.objects.create_user(**user_data) |
||||
info_msg = '添加用户成功' |
||||
return render(request, 'user_manager/user_reg.html', {'info': info_msg}) |
||||
|
||||
|
||||
def user_login(request): |
||||
if request.method == 'GET': |
||||
return render(request, 'user_manager/login.html') |
||||
if request.method == 'POST': |
||||
user_name = request.POST.get('username') |
||||
user_pw = request.POST.get('password') |
||||
if User.objects.filter(username=user_name): |
||||
user = authenticate(username=user_name, password=user_pw) |
||||
print(user) |
||||
if user: |
||||
print(user.is_active) |
||||
if user.is_active: |
||||
login(request, user) |
||||
info = '登录成功' |
||||
else: |
||||
info = '用户未激活' |
||||
else: |
||||
info = '账号密码错误' |
||||
else: |
||||
info = '账号不存在' |
||||
return render(request, 'user_manager/login.html', {'info': info}) |
||||
|
||||
|
||||
def myuser_reg(request): |
||||
if request.method == 'GET': |
||||
return render(request, 'user_manager/user_reg.html') |
||||
if request.method == 'POST': |
||||
uname = request.POST.get('username') |
||||
pwd = request.POST.get('password') |
||||
if MyUser.objects.filter(username=uname): |
||||
info = '用户已存在' |
||||
else: |
||||
d = dict(username=uname, password=pwd, email=request.POST.get('email'), is_staff=1, is_active=1, |
||||
is_superuser=1, photo='', weChat='coder', level='1') |
||||
user = MyUser.objects.create_user(**d) |
||||
info = '注册成功' |
||||
return render(request, 'user_manager/user_reg.html', {'info': info}) |
||||
|
||||
|
||||
def myuser_login(request): |
||||
if request.method == 'GET': |
||||
return render(request, 'user_manager/login.html') |
||||
if request.method == 'POST': |
||||
uname = request.POST.get('username') |
||||
pwd = request.POST.get('password') |
||||
if MyUser.objects.filter(username=uname): |
||||
user = authenticate(username=uname, password=pwd) |
||||
if user: |
||||
if user.is_active: |
||||
login(request, user) |
||||
info = '登录成功' |
||||
else: |
||||
info = '用户未激活' |
||||
else: |
||||
info = '账号密码错误' |
||||
else: |
||||
info = '用户不存在' |
||||
return render(request, 'user_manager/login.html', {'info': info}) |
||||
|
||||
|
||||
@permission_required('user_manager.change_myuser') |
||||
@login_required |
||||
def myuser_edit(request): |
||||
return render(request, 'user_manager/edit.html') |
||||
|
||||
|
||||
def myuser_logout(request): |
||||
logout(request) |
||||
return redirect(reverse('user_manager_login')) |
||||
|
||||
|
||||
@permission_required('user_manager.view_myuser') |
||||
@login_required |
||||
def user_index(request): |
||||
users = MyUser.objects.all() |
||||
return render(request, 'user_manager/user_index.html', {'users': users}) |
||||
|
||||
|
||||
def test_middle(request): |
||||
return HttpResponse('执行了') |
Loading…
Reference in new issue