Skip to content

Commit

Permalink
Replace import User model with auth.get_user_model. Hence changing Us…
Browse files Browse the repository at this point in the history
…er and Task model and reapplying migrations
  • Loading branch information
alllexxx1 committed Feb 26, 2024
1 parent 49470c1 commit 5e2bfdd
Show file tree
Hide file tree
Showing 20 changed files with 85 additions and 134 deletions.
56 changes: 0 additions & 56 deletions task_manager/fixtures/auth.json

This file was deleted.

4 changes: 2 additions & 2 deletions task_manager/forms.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from task_manager.users.models import User
from django.contrib.auth import get_user_model
from django.contrib.auth.forms import AuthenticationForm


class LoginUserForm(AuthenticationForm):
class Meta:
model = User
model = get_user_model()
fields = ['username', 'password']
2 changes: 1 addition & 1 deletion task_manager/labels/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Generated by Django 5.0.2 on 2024-02-23 17:15
# Generated by Django 5.0.2 on 2024-02-26 13:23

from django.db import migrations, models

Expand Down
6 changes: 3 additions & 3 deletions task_manager/labels/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
from task_manager.custom_utils import load_fixture
from task_manager.labels.models import Label
from task_manager.labels.forms import LabelCreateForm
from task_manager.users.models import User
from django.contrib.auth import get_user_model


class LabelCRUDTestCase(TestCase):
fixtures = ['users.json', 'auth.json', 'labels.json']
fixtures = ['users.json', 'labels.json']

def setUp(self):
self.user = User.objects.get(pk=3)
self.user = get_user_model().objects.get(pk=3)
self.client.force_login(self.user)
self.labels = load_fixture('labels_to_create.json')

Expand Down
5 changes: 5 additions & 0 deletions task_manager/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,10 +183,15 @@
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'


# Default auth settings

AUTH_USER_MODEL = 'users.User'

LOGIN_REDIRECT_URL = 'index'
LOGOUT_REDIRECT_URL = 'index'


# Shell_plus settings
SHELL_PLUS = 'ipython'
SHELL_PLUS_PRINT_SQL = True

Expand Down
2 changes: 1 addition & 1 deletion task_manager/statuses/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Generated by Django 5.0.2 on 2024-02-23 17:15
# Generated by Django 5.0.2 on 2024-02-26 13:23

from django.db import migrations, models

Expand Down
6 changes: 3 additions & 3 deletions task_manager/statuses/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
from task_manager.custom_utils import load_fixture
from task_manager.statuses.models import Status
from task_manager.statuses.forms import StatusCreateForm
from task_manager.users.models import User
from django.contrib.auth import get_user_model


class StatusCRUDTestCase(TestCase):
fixtures = ['users.json', 'auth.json', 'statuses.json']
fixtures = ['users.json', 'statuses.json']

def setUp(self):
self.user = User.objects.get(pk=1)
self.user = get_user_model().objects.get(pk=1)
self.client.force_login(self.user)
self.statuses = load_fixture('statuses_to_create.json')

Expand Down
4 changes: 2 additions & 2 deletions task_manager/tasks/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
)
from django.utils.translation import gettext as _
from task_manager.tasks.models import Task
from task_manager.users.models import User
from django.contrib.auth import get_user_model
from task_manager.statuses.models import Status
from task_manager.labels.models import Label

Expand All @@ -33,7 +33,7 @@ class Meta:
executor = ModelChoiceFilter(
label=_('Executor'),
field_name='executor',
queryset=User.objects.all()
queryset=get_user_model().objects.all()
)

labels = ModelChoiceFilter(
Expand Down
4 changes: 2 additions & 2 deletions task_manager/tasks/forms.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from django import forms
from django.utils.translation import gettext as _
from task_manager.tasks.models import Task
from task_manager.users.models import User
from django.contrib.auth import get_user_model
from task_manager.statuses.models import Status
from task_manager.labels.models import Label

Expand All @@ -27,7 +27,7 @@ class Meta:
label=_('Status'),
)
executor = forms.ModelChoiceField(
queryset=User.objects.all(),
queryset=get_user_model().objects.all(),
label=_('Executor'),
required=False
)
Expand Down
2 changes: 1 addition & 1 deletion task_manager/tasks/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Generated by Django 5.0.2 on 2024-02-23 17:15
# Generated by Django 5.0.2 on 2024-02-26 13:23

from django.db import migrations, models

Expand Down
13 changes: 7 additions & 6 deletions task_manager/tasks/migrations/0002_initial.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Generated by Django 5.0.2 on 2024-02-23 17:15
# Generated by Django 5.0.2 on 2024-02-26 13:23

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


Expand All @@ -12,19 +13,19 @@ class Migration(migrations.Migration):
('labels', '0001_initial'),
('statuses', '0001_initial'),
('tasks', '0001_initial'),
('users', '0001_initial'),
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
]

operations = [
migrations.AddField(
model_name='task',
name='assignee',
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, to='users.user'),
name='author',
field=models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name='tasks', to=settings.AUTH_USER_MODEL),
),
migrations.AddField(
model_name='task',
name='author',
field=models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name='tasks', to='users.user'),
name='executor',
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, to=settings.AUTH_USER_MODEL),
),
migrations.AddField(
model_name='task',
Expand Down

This file was deleted.

14 changes: 11 additions & 3 deletions task_manager/tasks/models.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,23 @@
from django.db import models
from task_manager.users.models import User
from django.conf import settings
from task_manager.statuses.models import Status
from task_manager.labels.models import Label


class Task(models.Model):
name = models.CharField(max_length=150, unique=True)
description = models.TextField(blank=True)
executor = models.ForeignKey(User, on_delete=models.PROTECT, null=True, blank=True)
executor = models.ForeignKey(
settings.AUTH_USER_MODEL,
on_delete=models.PROTECT,
null=True, blank=True
)
status = models.ForeignKey(Status, on_delete=models.PROTECT)
author = models.ForeignKey(User, on_delete=models.PROTECT, related_name='tasks')
author = models.ForeignKey(
settings.AUTH_USER_MODEL,
on_delete=models.PROTECT,
related_name='tasks'
)
labels = models.ManyToManyField(Label, through='TaskLabel', blank=True)
created_at = models.DateTimeField(auto_now_add=True)

Expand Down
10 changes: 5 additions & 5 deletions task_manager/tasks/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
from django.urls import reverse
from task_manager.custom_utils import load_fixture
from task_manager.tasks.models import Task
from task_manager.users.models import User
from django.contrib.auth import get_user_model


class TaskCRUDTestCase(TestCase):
fixtures = ['users.json', 'auth.json', 'statuses.json', 'tasks.json']
fixtures = ['users.json', 'statuses.json', 'tasks.json']

def setUp(self):
self.user = User.objects.get(pk=1)
self.user = get_user_model().objects.get(pk=1)
self.client.force_login(self.user)
self.tasks = load_fixture('tasks_to_create.json')

Expand Down Expand Up @@ -71,10 +71,10 @@ def test_delete_task(self):


class FilterFormTestCase(TestCase):
fixtures = ['users.json', 'auth.json', 'statuses.json', 'tasks.json']
fixtures = ['users.json', 'statuses.json', 'tasks.json']

def setUp(self):
self.user = User.objects.get(pk=1)
self.user = get_user_model().objects.get(pk=1)
self.client.force_login(self.user)

def test_filter_form(self):
Expand Down
4 changes: 2 additions & 2 deletions task_manager/tasks/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
)
from django_filters.views import FilterView
from task_manager.tasks.filters import TasksFilter
from task_manager.users.models import User
from django.contrib.auth import get_user_model


class TasksView(AuthRequiredMixin, FilterView):
Expand All @@ -39,7 +39,7 @@ class CreateTaskView(AuthRequiredMixin, SuccessMessageMixin, CreateView):

def form_valid(self, form):
user = self.request.user
form.instance.author = User.objects.get(pk=user.pk)
form.instance.author = get_user_model().objects.get(pk=user.pk)
return super().form_valid(form)


Expand Down
4 changes: 2 additions & 2 deletions task_manager/users/forms.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from django.contrib.auth.forms import UserCreationForm
from task_manager.users.models import User
from django.contrib.auth import get_user_model


class UserCreateForm(UserCreationForm):

class Meta:
model = User
model = get_user_model()
fields = ['first_name', 'last_name', 'username']
21 changes: 16 additions & 5 deletions task_manager/users/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# Generated by Django 5.0.2 on 2024-02-23 17:15
# Generated by Django 5.0.2 on 2024-02-26 13:23

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


Expand All @@ -18,14 +18,25 @@ class Migration(migrations.Migration):
migrations.CreateModel(
name='User',
fields=[
('user_ptr', models.OneToOneField(auto_created=True, on_delete=django.db.models.deletion.CASCADE, parent_link=True, primary_key=True, serialize=False, to=settings.AUTH_USER_MODEL)),
('id', models.BigAutoField(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,
},
bases=('auth.user',),
managers=[
('objects', django.contrib.auth.models.UserManager()),
],
Expand Down
4 changes: 2 additions & 2 deletions task_manager/users/models.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from django.contrib.auth.models import User
from django.contrib.auth.models import AbstractUser


class User(User):
class User(AbstractUser):

def __str__(self):
return self.get_full_name()
Loading

0 comments on commit 5e2bfdd

Please sign in to comment.