From 62199bd5dd7e3da742255bb36fce4ed4b0750d04 Mon Sep 17 00:00:00 2001 From: abhiabhi94 <13880786+abhiabhi94@users.noreply.github.com> Date: Wed, 10 Mar 2021 06:08:38 +0530 Subject: [PATCH] :alembic: chore: Add custom user model to testapp --- testapp/post/admin.py | 16 +++++++++ .../commands/create_initial_data.py | 6 +++- testapp/post/migrations/0001_initial.py | 6 ++-- testapp/post/migrations/0002_post_user.py | 23 +++++++++++++ testapp/post/models.py | 4 +-- testapp/settings.py | 2 ++ .../user_profile/migrations/0001_initial.py | 33 ++++++++++++++++++- testapp/user_profile/models.py | 6 +++- 8 files changed, 87 insertions(+), 9 deletions(-) create mode 100644 testapp/post/admin.py create mode 100644 testapp/post/migrations/0002_post_user.py diff --git a/testapp/post/admin.py b/testapp/post/admin.py new file mode 100644 index 0000000..a5bad02 --- /dev/null +++ b/testapp/post/admin.py @@ -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 diff --git a/testapp/post/management/commands/create_initial_data.py b/testapp/post/management/commands/create_initial_data.py index fb0d18a..f431bf7 100644 --- a/testapp/post/management/commands/create_initial_data.py +++ b/testapp/post/management/commands/create_initial_data.py @@ -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" diff --git a/testapp/post/migrations/0001_initial.py b/testapp/post/migrations/0001_initial.py index 40dcf56..259d66a 100644 --- a/testapp/post/migrations/0001_initial.py +++ b/testapp/post/migrations/0001_initial.py @@ -1,6 +1,6 @@ -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): @@ -8,7 +8,6 @@ class Migration(migrations.Migration): initial = True dependencies = [ - migrations.swappable_dependency(settings.AUTH_USER_MODEL), ] operations = [ @@ -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'], diff --git a/testapp/post/migrations/0002_post_user.py b/testapp/post/migrations/0002_post_user.py new file mode 100644 index 0000000..39fc335 --- /dev/null +++ b/testapp/post/migrations/0002_post_user.py @@ -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), + ), + ] diff --git a/testapp/post/models.py b/testapp/post/models.py index 1340624..53458c2 100644 --- a/testapp/post/models.py +++ b/testapp/post/models.py @@ -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) diff --git a/testapp/settings.py b/testapp/settings.py index 6f9ae02..cf9158c 100644 --- a/testapp/settings.py +++ b/testapp/settings.py @@ -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') diff --git a/testapp/user_profile/migrations/0001_initial.py b/testapp/user_profile/migrations/0001_initial.py index 2ab0314..2a903e5 100644 --- a/testapp/user_profile/migrations/0001_initial.py +++ b/testapp/user_profile/migrations/0001_initial.py @@ -1,6 +1,11 @@ +# 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): @@ -8,10 +13,36 @@ 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=[ diff --git a/testapp/user_profile/models.py b/testapp/user_profile/models.py index e30c142..5171f11 100644 --- a/testapp/user_profile/models.py +++ b/testapp/user_profile/models.py @@ -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)