From 6949c9cfed4747148e9a4bfe3e61b0292593223a Mon Sep 17 00:00:00 2001 From: = <=> Date: Sun, 2 Apr 2023 10:34:44 +0800 Subject: [PATCH] update:updating account model --- apps/account/admin.py | 3 + apps/account/migrations/0001_initial.py | 83 ++++++++++++++++++++++++ apps/account/models.py | 85 ++++++++++++++++++++++++- apps/account/templates/login1.html | 58 +++++++++++++++-- apps/account/urls.py | 10 +++ apps/account/views.py | 23 ++++++- apps/base.py | 13 ++++ apps/base/models.py | 12 +++- apps/base/templates/index.html | 2 +- test.py | 1 + urls.py | 6 +- 11 files changed, 282 insertions(+), 14 deletions(-) create mode 100644 apps/account/migrations/0001_initial.py create mode 100644 apps/account/urls.py create mode 100644 apps/base.py create mode 100644 test.py diff --git a/apps/account/admin.py b/apps/account/admin.py index 8c38f3f..ca04edb 100644 --- a/apps/account/admin.py +++ b/apps/account/admin.py @@ -1,3 +1,6 @@ from django.contrib import admin +from apps.account.models import Account + # Register your models here. +admin.site.register(Account) \ No newline at end of file diff --git a/apps/account/migrations/0001_initial.py b/apps/account/migrations/0001_initial.py new file mode 100644 index 0000000..bb4bee5 --- /dev/null +++ b/apps/account/migrations/0001_initial.py @@ -0,0 +1,83 @@ +# Generated by Django 3.2.18 on 2023-04-01 16:19 + +from django.db import migrations, models +import django.db.models.deletion +import uuid + + +class Migration(migrations.Migration): + + initial = True + + dependencies = [ + ] + + operations = [ + migrations.CreateModel( + name='Account', + fields=[ + ('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)), + ('create_time', models.DateTimeField(auto_now_add=True, verbose_name='创建时间')), + ('update_time', models.DateTimeField(auto_now=True, verbose_name='更新时间')), + ('name', models.CharField(max_length=50, unique=True)), + ('real_name', models.CharField(max_length=50, null=True)), + ('password', models.CharField(max_length=50)), + ('email', models.CharField(max_length=100, null=True, unique=True)), + ('gender', models.IntegerField(choices=[(0, 'Unknown'), (1, 'Female'), (2, 'Male')], default=0)), + ('avatar_url', models.CharField(max_length=255)), + ('signature', models.CharField(default=None, max_length=255, null=True)), + ('title_name', models.CharField(max_length=50, null=True)), + ('is_active', models.BooleanField(default=True)), + ], + options={ + 'abstract': False, + }, + ), + migrations.CreateModel( + name='Auth', + fields=[ + ('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)), + ('create_time', models.DateTimeField(auto_now_add=True, verbose_name='创建时间')), + ('update_time', models.DateTimeField(auto_now=True, verbose_name='更新时间')), + ('type', models.CharField(choices=[('admin', 'Admin'), ('super admin', 'Superadmin'), ('common', 'Common'), ('contest', 'Contest')], default='common', max_length=50)), + ('permission', models.CharField(max_length=50)), + ('is_active', models.BooleanField(default=True)), + ], + options={ + 'abstract': False, + }, + ), + migrations.CreateModel( + name='Role', + fields=[ + ('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)), + ('create_time', models.DateTimeField(auto_now_add=True, verbose_name='创建时间')), + ('update_time', models.DateTimeField(auto_now=True, verbose_name='更新时间')), + ('role', models.CharField(choices=[('admin', 'Admin'), ('teacher', 'Teacher'), ('tourist', 'Tourist'), ('user', 'User')], default='user', max_length=50)), + ('description', models.CharField(max_length=50, null=True)), + ('is_active', models.BooleanField(default=True)), + ('accounts', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='account.account')), + ], + options={ + 'abstract': False, + }, + ), + migrations.CreateModel( + name='RoleAuthLink', + fields=[ + ('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)), + ('create_time', models.DateTimeField(auto_now_add=True, verbose_name='创建时间')), + ('update_time', models.DateTimeField(auto_now=True, verbose_name='更新时间')), + ('auth_id', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='account.auth')), + ('role_id', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='account.role')), + ], + options={ + 'abstract': False, + }, + ), + migrations.AddField( + model_name='auth', + name='roles', + field=models.ManyToManyField(through='account.RoleAuthLink', to='account.Role'), + ), + ] diff --git a/apps/account/models.py b/apps/account/models.py index 71a8362..cf845c8 100644 --- a/apps/account/models.py +++ b/apps/account/models.py @@ -1,3 +1,86 @@ +import uuid + from django.db import models -# Create your models here. +from apps.base.models import BaseModel + + +# class BaseModel(models.Model): +# """基础模型 Usages: abstract class for Model """ +# id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False) +# create_time = models.DateTimeField(auto_now_add=True, verbose_name='创建时间') +# update_time = models.DateTimeField(auto_now=True, verbose_name='更新时间') +# +# class Meta: +# abstract = True + +class RoleType(models.TextChoices): + ADMIN = "admin" + TEACHER = "teacher" + TOURIST = "tourist" + USER = "user" + + +class AuthType(models.TextChoices): + ADMIN = "admin" + SUPERADMIN = "super admin" + COMMON = "common" + contest = "contest" + + +class Gender(models.IntegerChoices): + UNKNOW = 0, 'Unknown' + FEMALE = 1, 'Female' + MALE = 2, 'Male' + + +class Account(BaseModel): + name = models.CharField(max_length=50, unique=True) + real_name = models.CharField(max_length=50, null=True) + phone = models.CharField(max_length=11,null=True) + password = models.CharField(max_length=50) + email = models.CharField(max_length=100, unique=True, null=True) + gender = models.IntegerField(choices=Gender.choices, default=Gender.UNKNOW) + avatar_url = models.CharField(max_length=255,null=True) + signature = models.CharField(max_length=255, null=True ) + title_name = models.CharField(max_length=50, null=True) + is_active = models.BooleanField(default=True) + + # 一个账户有一个角色,one to many + # role_id = models.ForeignKey("Role", related_name="accounts", on_delete=models.CASCADE) + + def verify_password(self, password: str) -> bool: + return self.password == password + + +class Role(BaseModel): + role = models.CharField(choices=RoleType.choices, max_length=50, default=RoleType.USER) + description = models.CharField(max_length=50, null=True) + is_active = models.BooleanField(default=True) + # 一个角色有多个用户 + accounts = models.ForeignKey(Account, on_delete=models.CASCADE) + # 一个角色有多个权限,一个权限有多个角色 + # auths = models.ManyToManyField("Auth", related_name="roles") + + +class Auth(BaseModel): + type = models.CharField(choices=AuthType.choices, max_length=50, default=AuthType.COMMON) + permission = models.CharField(max_length=50) + is_active = models.BooleanField(default=True) + # 一个角色有多个权限,一个权限有多个角色 + roles = models.ManyToManyField(Role, through="RoleAuthLink") + + +# +# class AccountRoleLink(BaseModel): +# account_id = models.ForeignKey( +# Account, on_delete=models.CASCADE) +# role_id = models.ForeignKey( +# Role, on_delete=models.CASCADE) + + +class RoleAuthLink(BaseModel): + auth_id = models.ForeignKey( + Auth, on_delete=models.CASCADE) + role_id = models.ForeignKey( + Role, on_delete=models.CASCADE) diff --git a/apps/account/templates/login1.html b/apps/account/templates/login1.html index bd68e24..79b943d 100644 --- a/apps/account/templates/login1.html +++ b/apps/account/templates/login1.html @@ -7,6 +7,19 @@ +
@@ -16,10 +29,10 @@
- +
- +
@@ -29,9 +42,10 @@

注册

- - - + + + +
@@ -39,12 +53,14 @@

登录

- - + + + 忘记密码?
+
@@ -58,10 +74,38 @@
+ + diff --git a/apps/account/urls.py b/apps/account/urls.py new file mode 100644 index 0000000..887ed9b --- /dev/null +++ b/apps/account/urls.py @@ -0,0 +1,10 @@ +from django.urls import path + +from . import views + +app_name = 'account' + +urlpatterns = [ + path('login', views.login, name='login'), + path('login/submit', views.submit, name='submit'), +] diff --git a/apps/account/views.py b/apps/account/views.py index 47a5a4a..b68c741 100644 --- a/apps/account/views.py +++ b/apps/account/views.py @@ -1,5 +1,26 @@ +from django.http import HttpResponse from django.shortcuts import render +from apps.account.models import Account + + # Create your views here. def login(request): - return render(request,"login1.html") \ No newline at end of file + # + return render(request, "login1.html") + + +def submit(request): + if request.method == 'POST': + # 从前端数据中拿去表中字段 + user_name = request.POST.get('email') + user_password = request.POST.get('password') + # user_password_confirm = request.POST.get('user_password_confirm') + # 根据用户名查询数据库,并获得用户 + user_obj = Account.objects.filter(user_name=user_name).first() + if user_obj.user_name == user_name and user_obj.user_password == user_password and user_obj.user_password_confirm == user_password_confirm: + return HttpResponse("登录成功") + else: + return HttpResponse("登录失败") + else: + return HttpResponse("登录失败") diff --git a/apps/base.py b/apps/base.py new file mode 100644 index 0000000..8dda30c --- /dev/null +++ b/apps/base.py @@ -0,0 +1,13 @@ +import uuid + +from django.db import models + + +class BaseModel(models.Model): + """基础模型 Usages: abstract class for Model """ + id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False) + create_time = models.DateTimeField(auto_now_add=True, verbose_name='创建时间') + update_time = models.DateTimeField(auto_now=True, verbose_name='更新时间') + + class Meta: + abstract = True diff --git a/apps/base/models.py b/apps/base/models.py index 71a8362..8dda30c 100644 --- a/apps/base/models.py +++ b/apps/base/models.py @@ -1,3 +1,13 @@ +import uuid + from django.db import models -# Create your models here. + +class BaseModel(models.Model): + """基础模型 Usages: abstract class for Model """ + id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False) + create_time = models.DateTimeField(auto_now_add=True, verbose_name='创建时间') + update_time = models.DateTimeField(auto_now=True, verbose_name='更新时间') + + class Meta: + abstract = True diff --git a/apps/base/templates/index.html b/apps/base/templates/index.html index 0dd6c55..8d80e64 100644 --- a/apps/base/templates/index.html +++ b/apps/base/templates/index.html @@ -40,7 +40,7 @@