初步框架搭建
This commit is contained in:
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,3 @@
|
||||
from django.contrib import admin
|
||||
|
||||
# Register your models here.
|
||||
@@ -0,0 +1,6 @@
|
||||
from django.apps import AppConfig
|
||||
|
||||
|
||||
class ServerAuthConfig(AppConfig):
|
||||
default_auto_field = "django.db.models.BigAutoField"
|
||||
name = "server_auth"
|
||||
@@ -0,0 +1,21 @@
|
||||
from django.utils.deprecation import MiddlewareMixin
|
||||
from django.shortcuts import HttpResponse, redirect
|
||||
|
||||
|
||||
class AuthMiddleware(MiddlewareMixin):
|
||||
|
||||
def process_request(self, request):
|
||||
# 0.排除那些不需要登录就能访问的页面
|
||||
# request.path_info 获取当前用户请求的URL /login/
|
||||
if request.path_info in ["/account/login/", "/account/code/"]:
|
||||
return
|
||||
|
||||
# 1.读取当前访问的用户的session信息,如果能读到,说明已登陆过,就可以继续向后走。
|
||||
# info_dict = request.session.get("info")
|
||||
username = request.COOKIES.get('username')
|
||||
# print(info_dict)
|
||||
if username:
|
||||
return
|
||||
|
||||
# 2.没有登录过,重新回到登录页面
|
||||
return redirect('/account/login/')
|
||||
@@ -0,0 +1,140 @@
|
||||
# Generated by Django 4.2 on 2024-08-31 16:43
|
||||
|
||||
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="BlogUser",
|
||||
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"
|
||||
),
|
||||
),
|
||||
(
|
||||
"user_identity",
|
||||
models.SmallIntegerField(
|
||||
choices=[(1, "普通用户"), (2, "编辑者"), (3, "管理员")],
|
||||
default="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": "博客用户表",
|
||||
"db_table": "Blog_User",
|
||||
"ordering": ["-id"],
|
||||
},
|
||||
managers=[
|
||||
("objects", django.contrib.auth.models.UserManager()),
|
||||
],
|
||||
),
|
||||
]
|
||||
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,27 @@
|
||||
from django.db import models
|
||||
from django.contrib.auth.models import AbstractUser
|
||||
import json
|
||||
# Create your models here.
|
||||
|
||||
class BlogUser(AbstractUser):
|
||||
# 继承了AbstractUser拥有的所有字段
|
||||
# 自定义我们需要的字段
|
||||
# wx_openid = models.CharField(verbose_name='微信openid', max_length=64, null=True, blank=True)
|
||||
# wx_nick_name = models.CharField(verbose_name='微信名称', max_length=64, null=True, blank=True)
|
||||
# sex_type = (
|
||||
# (1, "普通用户"),
|
||||
# (2, "编辑者"),
|
||||
# (3, "管理员")
|
||||
# )
|
||||
# type_t_or_s = models.SmallIntegerField(verbose_name="身份", choices=sex_type, default="2")
|
||||
user_type = (
|
||||
(1, "普通用户"),
|
||||
(2, "编辑者"),
|
||||
(3, "管理员")
|
||||
)
|
||||
user_identity = models.SmallIntegerField(verbose_name="身份",choices=user_type, default="1")
|
||||
|
||||
class Meta:
|
||||
db_table = 'Blog_User'
|
||||
ordering = ['-id']
|
||||
verbose_name = "博客用户表"
|
||||
@@ -0,0 +1,3 @@
|
||||
from django.test import TestCase
|
||||
|
||||
# Create your tests here.
|
||||
@@ -0,0 +1,10 @@
|
||||
from django.urls import path, include,re_path
|
||||
from server_auth.views import LoginView,LogoutView,RegisterView
|
||||
|
||||
urlpatterns = [
|
||||
# re_path(r'^index/$',views.IndexView.as_view()),
|
||||
# re_path(r'^login/$', views.login),
|
||||
path('register', RegisterView.as_view()), # 用户注册
|
||||
path('login', LoginView.as_view()), # 用户登录
|
||||
path('logout', LogoutView.as_view()), # 用户登出
|
||||
]
|
||||
@@ -0,0 +1,72 @@
|
||||
from django import http
|
||||
from django.contrib.auth import authenticate, login, logout
|
||||
from rest_framework.views import APIView
|
||||
from django.http.response import JsonResponse
|
||||
from xiaoanblog.utils.constant_util import *
|
||||
from xiaoanblog.utils.mix_utils import build_response
|
||||
from django.shortcuts import render
|
||||
|
||||
from server_auth.models import BlogUser
|
||||
import json
|
||||
# Create your views here.
|
||||
class RegisterView(APIView):
|
||||
"""
|
||||
用户注册
|
||||
"""
|
||||
|
||||
def post(self, request):
|
||||
data_dict = request.POST
|
||||
user_name = data_dict.get('username')
|
||||
input_password = data_dict.get('password')
|
||||
input_email = data_dict.get('email')
|
||||
print(data_dict)
|
||||
# 根据用户名查询 MindUser
|
||||
db_user = BlogUser.objects.filter(username=user_name).first()
|
||||
if db_user is not None:
|
||||
return build_response(code=-1, msg=f"用户名:{user_name} 已经存在,请更换!")
|
||||
|
||||
BlogUser.objects.create_user(username=user_name, password=input_password, email=input_email)
|
||||
return JsonResponse({"code": 0, "msg": "注册成功,跳转登录界面中"})
|
||||
|
||||
class LoginView(APIView):
|
||||
"""
|
||||
登录管理
|
||||
"""
|
||||
|
||||
def post(self, request):
|
||||
username = request.POST.get('username')
|
||||
password = request.POST.get('password')
|
||||
print(username)
|
||||
print(password)
|
||||
# django内置认证来校验登录用户
|
||||
|
||||
user = authenticate(username=username, password=password)
|
||||
|
||||
if user is not None:
|
||||
if user.is_active:
|
||||
# 记住登录状态
|
||||
login(request, user)
|
||||
return build_response(msg='登录成功!')
|
||||
# elif user.is_active:
|
||||
# # 记住登录状态
|
||||
# # 记住登录状态
|
||||
# login(request, user)
|
||||
# return build_response(msg='管理员登录成功')
|
||||
else:
|
||||
return build_response(code=CODE_FAILED, msg='登录失败!')
|
||||
else:
|
||||
return build_response(code=CODE_FAILED, msg='登录失败!')
|
||||
|
||||
|
||||
class LogoutView(APIView):
|
||||
"""
|
||||
注销
|
||||
"""
|
||||
|
||||
def get(self, request):
|
||||
logout(request)
|
||||
return build_response(msg='注销成功')
|
||||
|
||||
def post(self, request):
|
||||
logout(request)
|
||||
return build_response(msg='注销成功')
|
||||
Reference in New Issue
Block a user