init: initial Django LMS backend
This commit is contained in:
@@ -0,0 +1,78 @@
|
||||
from django.contrib import admin
|
||||
from django.contrib.auth.admin import UserAdmin
|
||||
from unfold.admin import ModelAdmin
|
||||
|
||||
from .models import (
|
||||
CustomUser,
|
||||
StudentProfile,
|
||||
InstructorProfile,
|
||||
AdminProfile,
|
||||
)
|
||||
|
||||
@admin.register(CustomUser)
|
||||
class CustomUserAdmin(UserAdmin, ModelAdmin):
|
||||
model = CustomUser
|
||||
|
||||
list_display = (
|
||||
"email",
|
||||
"username",
|
||||
"is_active",
|
||||
"is_staff",
|
||||
)
|
||||
|
||||
list_filter = (
|
||||
"is_active",
|
||||
"is_staff",
|
||||
"is_superuser",
|
||||
)
|
||||
|
||||
search_fields = ("email","username")
|
||||
ordering = ("email",)
|
||||
|
||||
fieldsets = (
|
||||
(None, {"fields": ("email", "username", "password")}),
|
||||
("Permissions", {"fields": ("is_active", "is_staff", "is_superuser", "groups")}),
|
||||
("Important Dates", {"fields": ("last_login", "date_joined")}),
|
||||
)
|
||||
|
||||
|
||||
add_fieldsets = (
|
||||
(None, {
|
||||
"classes": ("wide",),
|
||||
"fields": (
|
||||
"email",
|
||||
"username",
|
||||
"password1",
|
||||
"password2",
|
||||
"is_staff",
|
||||
"is_superuser",
|
||||
),
|
||||
}),
|
||||
)
|
||||
|
||||
@admin.register(StudentProfile)
|
||||
class StudentProfileAdmin(ModelAdmin):
|
||||
list_display = ("user", "is_active")
|
||||
search_fields = ("user__email", "user__username")
|
||||
list_filter = ("is_active",)
|
||||
|
||||
@admin.register(InstructorProfile)
|
||||
class InstructorProfileAdmin(ModelAdmin):
|
||||
list_display = ("user", "expertise", "is_verified", "is_active")
|
||||
search_fields = ("user__email", "user__username", "expertise")
|
||||
list_filter = ("is_active", "is_verified")
|
||||
|
||||
actions = ["verify_instructor", "unverify_instructor"]
|
||||
|
||||
@admin.action(description="ยืนยันผู้สอน")
|
||||
def verify_instructor(self, request, queryset):
|
||||
queryset.update(is_verified=True)
|
||||
|
||||
@admin.action(description="ยกเลิกการยืนยันผู้สอน")
|
||||
def unverify_instructor(self, request, queryset):
|
||||
queryset.update(is_verified=False)
|
||||
|
||||
@admin.register(AdminProfile)
|
||||
class AdminProfileAdmin(ModelAdmin):
|
||||
list_display = ("user", "is_active", "is_super")
|
||||
list_filter = ("is_active", "is_super")
|
||||
@@ -0,0 +1,5 @@
|
||||
from django.apps import AppConfig
|
||||
|
||||
class AccountsConfig(AppConfig):
|
||||
name = 'apps.accounts'
|
||||
verbose_name = "ตั้งค่าบัญชีผู้ใช้งาน"
|
||||
@@ -0,0 +1,87 @@
|
||||
# Generated by Django 6.0.2 on 2026-04-22 11:34
|
||||
|
||||
import django.contrib.auth.models
|
||||
import django.contrib.auth.validators
|
||||
import django.db.models.deletion
|
||||
import django.utils.timezone
|
||||
from django.conf import settings
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
initial = True
|
||||
|
||||
dependencies = [
|
||||
('auth', '0012_alter_user_first_name_max_length'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.CreateModel(
|
||||
name='CustomUser',
|
||||
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')),
|
||||
('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')),
|
||||
('email', models.EmailField(max_length=254, unique=True)),
|
||||
('phone', models.CharField(blank=True, max_length=20)),
|
||||
('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': 'บัญชีผู้ใช้งาน',
|
||||
'verbose_name_plural': 'บัญชีผู้ใช้งาน',
|
||||
},
|
||||
managers=[
|
||||
('objects', django.contrib.auth.models.UserManager()),
|
||||
],
|
||||
),
|
||||
migrations.CreateModel(
|
||||
name='AdminProfile',
|
||||
fields=[
|
||||
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||
('is_active', models.BooleanField(default=True)),
|
||||
('is_super', models.BooleanField(default=False)),
|
||||
('user', models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
|
||||
],
|
||||
options={
|
||||
'verbose_name': 'ผู้ดูแลระบบ',
|
||||
'verbose_name_plural': 'ผู้ดูแลระบบ',
|
||||
},
|
||||
),
|
||||
migrations.CreateModel(
|
||||
name='InstructorProfile',
|
||||
fields=[
|
||||
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||
('is_active', models.BooleanField(default=True)),
|
||||
('bio', models.TextField(blank=True)),
|
||||
('expertise', models.CharField(blank=True, max_length=200)),
|
||||
('is_verified', models.BooleanField(default=False)),
|
||||
('user', models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
|
||||
],
|
||||
options={
|
||||
'verbose_name': 'ผู้สอน',
|
||||
'verbose_name_plural': 'ผู้สอน',
|
||||
},
|
||||
),
|
||||
migrations.CreateModel(
|
||||
name='StudentProfile',
|
||||
fields=[
|
||||
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||
('is_active', models.BooleanField(default=True)),
|
||||
('interests', models.TextField(blank=True)),
|
||||
('user', models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
|
||||
],
|
||||
options={
|
||||
'verbose_name': 'ผู้เรียน',
|
||||
'verbose_name_plural': 'ผู้เรียน',
|
||||
},
|
||||
),
|
||||
]
|
||||
@@ -0,0 +1,68 @@
|
||||
from django.contrib.auth.models import AbstractUser
|
||||
from django.db import models
|
||||
|
||||
class CustomUser(AbstractUser):
|
||||
email = models.EmailField(unique=True)
|
||||
phone = models.CharField(max_length=20, blank=True)
|
||||
|
||||
USERNAME_FIELD = 'email'
|
||||
REQUIRED_FIELDS = ['username']
|
||||
|
||||
@property
|
||||
def role(self):
|
||||
roles = []
|
||||
if hasattr(self, 'studentprofile'):
|
||||
roles.append("student")
|
||||
if hasattr(self, 'instructorprofile'):
|
||||
roles.append("instructor")
|
||||
if hasattr(self, 'adminprofile'):
|
||||
roles.append("admin")
|
||||
return roles
|
||||
|
||||
def save(self, *args, **kwargs):
|
||||
if self.email:
|
||||
self.email = self.email.lower().strip()
|
||||
super().save(*args, **kwargs)
|
||||
|
||||
class Meta:
|
||||
verbose_name = "บัญชีผู้ใช้งาน"
|
||||
verbose_name_plural = "บัญชีผู้ใช้งาน"
|
||||
|
||||
def __str__(self):
|
||||
return self.email
|
||||
|
||||
class BaseRoleProfile(models.Model):
|
||||
user = models.OneToOneField(
|
||||
CustomUser,
|
||||
on_delete=models.CASCADE,
|
||||
)
|
||||
is_active = models.BooleanField(default=True)
|
||||
|
||||
class Meta:
|
||||
abstract = True
|
||||
|
||||
class StudentProfile(BaseRoleProfile):
|
||||
interests = models.TextField(blank=True)
|
||||
|
||||
class Meta:
|
||||
verbose_name = "ผู้เรียน"
|
||||
verbose_name_plural = "ผู้เรียน"
|
||||
|
||||
class InstructorProfile(BaseRoleProfile):
|
||||
bio = models.TextField(blank=True)
|
||||
expertise = models.CharField(max_length=200, blank=True)
|
||||
is_verified = models.BooleanField(default=False)
|
||||
|
||||
class Meta:
|
||||
verbose_name = "ผู้สอน"
|
||||
verbose_name_plural = "ผู้สอน"
|
||||
|
||||
def __str__(self):
|
||||
return f"Instructor: {self.user.email}"
|
||||
|
||||
class AdminProfile(BaseRoleProfile):
|
||||
is_super = models.BooleanField(default=False)
|
||||
|
||||
class Meta:
|
||||
verbose_name = "ผู้ดูแลระบบ"
|
||||
verbose_name_plural = "ผู้ดูแลระบบ"
|
||||
@@ -0,0 +1,3 @@
|
||||
from django.test import TestCase
|
||||
|
||||
# Create your tests here.
|
||||
@@ -0,0 +1,3 @@
|
||||
from django.shortcuts import render
|
||||
|
||||
# Create your views here.
|
||||
Reference in New Issue
Block a user