Skip to content

Commit

Permalink
setup django admin
Browse files Browse the repository at this point in the history
  • Loading branch information
ytliuSVN committed Nov 9, 2024
1 parent e51f49b commit c57e43f
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 2 deletions.
33 changes: 31 additions & 2 deletions app/core/admin.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,32 @@
from django.contrib import admin # noqa
"""
Django admin customization
"""
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin as BaseUserAdmin
from django.utils.translation import gettext_lazy as _

# Register your models here.
from core import models


class UserAdmin(BaseUserAdmin):
"""Define the admin pages for users."""
ordering = ['id']
list_display = ['email', 'name']
fieldsets = (
(None, {'fields': ('email', 'password')}),
(
_('Permissions'),
{
'fields': (
'is_active',
'is_staff',
'is_superuser',
)
}
),
(_('Important dates'), {'fields': ('last_login',)}),
)
readonly_fields = ['last_login']


admin.site.register(models.User, UserAdmin)
37 changes: 37 additions & 0 deletions app/core/tests/test_admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
"""
Tests for the Django admin modifications.
"""
from django.test import TestCase
from django.contrib.auth import get_user_model
from django.urls import reverse
from django.test import Client


class AdminSiteTests(TestCase):

def setUp(self):
self.client = Client()
self.admin_user = get_user_model().objects.create_superuser(
'[email protected]',
'test123'
)
self.client.force_login(self.admin_user)
self.user = get_user_model().objects.create_user(
'[email protected]',
'test123',
name='Test User'
)

def test_users_list(self):
url = reverse('admin:core_user_changelist')
res = self.client.get(url)

self.assertContains(res, self.user.name)
self.assertContains(res, self.user.email)

def test_edit_user_page(self):
"""Test the edit user page works."""
url = reverse('admin:core_user_change', args=[self.user.id])
res = self.client.get(url)

self.assertEqual(res.status_code, 200)

0 comments on commit c57e43f

Please sign in to comment.