管理サイト
ユーザモデル
src/backend/api/online/utils.py
...(略)...
# ↓追加
def mask_email(email: str) -> str:
"""メールアドレスをマスクして表示"""
if not email or "@" not in email:
return email
local, domain = email.split("@", 1)
if len(local) <= 1:
masked_local = "*"
else:
masked_local = local[0] + "*" * (len(local) - 1)
return f"{masked_local}@{domain}"
# ↑追加
src/backend/api/online/models.py
from django.db import models
from .utils import encrypt, hash, decrypt, mask_email # <- mask_emailを追加
import uuid
class User(models.Model):
...(略)...
def get_email(self):
...(略)...
# ↓追加
def masked_email(self):
"""マスクされたメール表示"""
try:
email = self.get_email()
return mask_email(email)
except Exception:
return None
# ↑追加
def __str__(self):
...(略)...
管理画面
src/backend/api/accounts/admin.py
from django.contrib import admin
from api.accounts.models import CustomUser
class CustomUserAdmin(admin.ModelAdmin):
"""カスタムユーザ管理クラス"""
list_display = (
'id',
'username',
'profile_id',
'profile_email_masked',
)
list_display_links = (
'id',
'username',
)
def profile_id(self, obj):
return obj.user.user_id if obj.user else None
profile_id.short_description = "user_id (UUID)"
def profile_email_masked(self, obj):
return obj.user.masked_email() if obj.user else None
profile_email_masked.short_description = "Email (masked)"
admin.site.register(CustomUser, CustomUserAdmin)
src/backend/config/urls.py
from django.contrib import admin
from django.urls import path, include
from django.conf import settings # <- 追加
urlpatterns = [
# path('admin/', admin.site.urls), # <- 削除
path("api/accounts/", include("api.accounts.urls")),
]
# ↓追加
if settings.DEBUG:
urlpatterns += [
path("admin/", admin.site.urls),
]
# ↑追加
スーパーユーザの作成
(recsys_full) backend$ python manage.py createsuperuser
ユーザー名: admin
メールアドレス: admin@recsyslab.org
Password:
Password (again):
Superuser created successfully.
管理サイトの確認
バックエンドサーバが起動している状態で、ブラウザで下記URLにアクセスし、上記で作成したスーパーユーザのユーザ名とパスワードでログインしてください。
管理サイトから、ACCOUNTS > ユーザー(下記URL)にアクセスしてください。
admin とサインアップしたユーザの情報が表示されれば OK。