Skip to content

Commit

Permalink
⚗️ chore: Add custom user model to testapp
Browse files Browse the repository at this point in the history
  • Loading branch information
abhiabhi94 committed Mar 10, 2021
1 parent 8a3c35e commit 62199bd
Show file tree
Hide file tree
Showing 8 changed files with 87 additions and 9 deletions.
16 changes: 16 additions & 0 deletions testapp/post/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from django.apps import apps
from django.contrib import admin


class ModelAdmin(admin.ModelAdmin):
def __init__(self, model, admin_site):
self.list_display = [field.name for field in model._meta.fields]
super().__init__(model, admin_site)


models = apps.get_models()
for model in models:
try:
admin.site.register(model, ModelAdmin)
except admin.sites.AlreadyRegistered:
pass
6 changes: 5 additions & 1 deletion testapp/post/management/commands/create_initial_data.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
from django.contrib.auth.models import User, Group
from django.contrib.auth.models import Group
from django.core.management.base import BaseCommand
from django.contrib.auth import get_user_model

from testapp.post.models import Post


User = get_user_model()


class Command(BaseCommand):
help = "Generate initial data"

Expand Down
6 changes: 2 additions & 4 deletions testapp/post/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
from django.conf import settings
# Generated by Django 3.1.1 on 2021-03-09 07:33

from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

initial = True

dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
]

operations = [
Expand All @@ -21,7 +20,6 @@ class Migration(migrations.Migration):
('date', models.DateTimeField(auto_now_add=True)),
('editdate', models.DateTimeField(auto_now=True)),
('slug', models.SlugField(unique=True)),
('user', models.ForeignKey(default=None, on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
],
options={
'ordering': ['-editdate', '-date'],
Expand Down
23 changes: 23 additions & 0 deletions testapp/post/migrations/0002_post_user.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Generated by Django 3.1.1 on 2021-03-09 07:33

from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

initial = True

dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('post', '0001_initial'),
]

operations = [
migrations.AddField(
model_name='post',
name='user',
field=models.ForeignKey(default=None, on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL),
),
]
4 changes: 2 additions & 2 deletions testapp/post/models.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
from django.db import models
from django.urls import reverse
from django.template.defaultfilters import slugify
from django.contrib.auth.models import User
from django.contrib.contenttypes.fields import GenericRelation
from django.conf import settings

from flag.models import Flag


class Post(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE, default=None)
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, default=None)
title = models.CharField(max_length=150)
body = models.TextField()
date = models.DateTimeField(auto_now_add=True)
Expand Down
2 changes: 2 additions & 0 deletions testapp/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,8 @@
LOGIN_REDIRECT_URL = '/'
LOGOUT_REDIRECT_URL = LOGIN_URL

AUTH_USER_MODEL = 'user_profile.User'

EMAIL_HOST_USER = os.environ.get('EMAIL_HOST_USER', 'user@domain')
DEFAULT_FROM_EMAIL = EMAIL_HOST_USER
EMAIL_HOST_PASSWORD = os.environ.get('EMAIL_HOST_PASSWORD', 'password')
Expand Down
33 changes: 32 additions & 1 deletion testapp/user_profile/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,48 @@
# Generated by Django 3.1.1 on 2021-03-09 07:33

from django.conf import settings
import django.contrib.auth.models
import django.contrib.auth.validators
from django.db import migrations, models
import django.db.models.deletion
import django.utils.timezone


class Migration(migrations.Migration):

initial = True

dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('auth', '0012_alter_user_first_name_max_length'),
]

operations = [
migrations.CreateModel(
name='User',
fields=[
('id', models.AutoField(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')),
('email', models.EmailField(blank=True, max_length=254, verbose_name='email address')),
('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')),
('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': 'user',
'verbose_name_plural': 'users',
'abstract': False,
},
managers=[
('objects', django.contrib.auth.models.UserManager()),
],
),
migrations.CreateModel(
name='UserProfile',
fields=[
Expand Down
6 changes: 5 additions & 1 deletion testapp/user_profile/models.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
from django.db import models
from django.contrib.auth.models import User
from django.contrib.auth.models import AbstractUser
from django.db.models.signals import post_save
from django.dispatch import receiver
from django.urls import reverse


class User(AbstractUser):
pass


class UserProfile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
display_name = models.CharField(max_length=15, null=True, blank=True)
Expand Down

0 comments on commit 62199bd

Please sign in to comment.