Skip to content

Commit

Permalink
added user admin interface
Browse files Browse the repository at this point in the history
  • Loading branch information
bcg0006 committed Apr 22, 2024
1 parent 9c190b5 commit d73e983
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 2 deletions.
32 changes: 30 additions & 2 deletions app/core/admin.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,31 @@
from django.contrib import admin # noqa
"""Django Admin customization for the core app."""
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin as BaseUserAdmin
from django.utils.translation import gettext_lazy as _
from core import models

# Register your models here.
class UserAdmin(BaseUserAdmin):
"""Customize the user admin page."""
ordering = ['id']
list_display = ['email', 'name']

fieldsets = (
(None, {'fields': ('email', 'password')}),
('Personal Info', {'fields': ('name',)}),
(
_('Permissions'),
{
'fields': ('is_active', 'is_staff', 'is_superuser'),
}
),
('Important dates', {'fields': ('last_login',)}),
)
add_fieldsets = (
(None, {

'classes': ('wide',),
'fields': ('email', 'password1', 'password2'),
}),
)

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

class AdminSiteTests(TestCase):
"""Test the admin site"""

def setUp(self):
"""Create user & client for testing"""
self.client = Client()
self.admin_user = get_user_model().objects.create_superuser(
email='admin@example.com',
password='password123',
)
self.client.force_login(self.admin_user)
self.user = get_user_model().objects.create_user(
email='user@example.com',
password='password123',
name='Test user full name',
)

def test_users_list(self):
"""Test users are listed on user page"""
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_user_change_page(self):
"""Test user edit page works"""
url = reverse('admin:core_user_change', args=[self.user.id])
res = self.client.get(url)

self.assertEqual(res.status_code, 200)

def test_create_user_page(self):
"""Test create user page works"""
url = reverse('admin:core_user_add')
res = self.client.get(url)

self.assertEqual(res.status_code, 200)

0 comments on commit d73e983

Please sign in to comment.