From 44d63111f062b12f6049a42b821b269b6053a8b1 Mon Sep 17 00:00:00 2001 From: Wayne Lambert Date: Tue, 5 Mar 2024 14:36:00 +0000 Subject: [PATCH] Resolve Ruff linting issues --- aa_project/settings/pytest/pytest.py | 1 + apps/api/serializers.py | 5 ++--- apps/blog/models.py | 5 ++--- apps/blog/tests/test_models.py | 9 +-------- apps/blog/views.py | 4 ++-- apps/users/forms.py | 10 ++++------ apps/users/models.py | 9 +++------ apps/users/views.py | 26 +++++++++++++------------- 8 files changed, 28 insertions(+), 41 deletions(-) diff --git a/aa_project/settings/pytest/pytest.py b/aa_project/settings/pytest/pytest.py index 01425fa8..dcf28553 100644 --- a/aa_project/settings/pytest/pytest.py +++ b/aa_project/settings/pytest/pytest.py @@ -54,6 +54,7 @@ }, } + # Eliminates warning about missing staticfiles directory WHITENOISE_AUTOREFRESH = True diff --git a/apps/api/serializers.py b/apps/api/serializers.py index b1634077..ac0c5525 100644 --- a/apps/api/serializers.py +++ b/apps/api/serializers.py @@ -1,5 +1,4 @@ -from django.contrib.auth import get_user_model - +from django.contrib.auth.models import User from rest_framework import serializers from apps.blog.models import Category, Post @@ -10,7 +9,7 @@ class UserSerializer(serializers.ModelSerializer): username = serializers.CharField(required=False, allow_blank=True, read_only=True) class Meta: - model = get_user_model() + model = User fields = ( "username", "first_name", diff --git a/apps/blog/models.py b/apps/blog/models.py index 152d74e5..94b129ad 100644 --- a/apps/blog/models.py +++ b/apps/blog/models.py @@ -1,12 +1,11 @@ import math -from django.contrib.auth import get_user_model +from django.contrib.auth.models import User from django.core.exceptions import ObjectDoesNotExist from django.core.validators import MinLengthValidator from django.db import models from django.template.defaultfilters import slugify from django.urls import reverse - from tinymce.models import HTMLField from apps.blog.managers import PublishedManager @@ -49,7 +48,7 @@ class Post(models.Model): help_text="For bests results, use an image that is 1,200px wide x 600px high", ) status = models.IntegerField(db_index=True, choices=STATUS, default=0) - author = models.ForeignKey(get_user_model(), related_name="author", on_delete=models.CASCADE) + author = models.ForeignKey(User, related_name="author", on_delete=models.CASCADE) categories = models.ManyToManyField( Category, related_name="posts", diff --git a/apps/blog/tests/test_models.py b/apps/blog/tests/test_models.py index e6ee7a25..6c2f835a 100644 --- a/apps/blog/tests/test_models.py +++ b/apps/blog/tests/test_models.py @@ -1,24 +1,17 @@ import datetime -from django.contrib.auth import get_user_model +import pytest from django.db import models from django.shortcuts import reverse from django.utils.text import slugify - -import pytest - from mixer.backend.django import mixer from tinymce.models import HTMLField from apps.blog.models import Category, Post - pytestmark = pytest.mark.django_db(reset_sequences=True) -user_model = get_user_model() - - class TestCategory: def test_name_is_charfield(self): category = mixer.blend(Category, pk=1) diff --git a/apps/blog/views.py b/apps/blog/views.py index 8fe7d91e..357833d0 100644 --- a/apps/blog/views.py +++ b/apps/blog/views.py @@ -1,8 +1,8 @@ from time import perf_counter from django.contrib import messages -from django.contrib.auth import get_user_model from django.contrib.auth.mixins import LoginRequiredMixin, UserPassesTestMixin +from django.contrib.auth.models import User from django.contrib.postgres.search import SearchQuery, SearchRank, SearchVector from django.shortcuts import get_list_or_404, get_object_or_404 from django.urls import reverse, reverse_lazy @@ -93,7 +93,7 @@ class AuthorPostListView(PostView): paginate_orphans = 3 def get_queryset(self): - user = get_object_or_404(get_user_model(), username=self.kwargs["username"]) + user = get_object_or_404(User, username=self.kwargs["username"]) return super().get_queryset().filter(author=user) def get_context_data(self, **kwargs): diff --git a/apps/users/forms.py b/apps/users/forms.py index ecf69758..69e32026 100644 --- a/apps/users/forms.py +++ b/apps/users/forms.py @@ -1,9 +1,7 @@ +from crispy_forms.helper import FormHelper from django import forms -from django.contrib.auth import get_user_model from django.contrib.auth.forms import UserCreationForm -from django.utils.translation import gettext_lazy as _ - -from crispy_forms.helper import FormHelper +from django.contrib.auth.models import User from two_factor.forms import TOTPDeviceForm from two_factor.utils import totp_digits @@ -14,7 +12,7 @@ class UserRegisterForm(UserCreationForm): email = forms.EmailField() class Meta: - model = get_user_model() + model = User fields = ["username", "email", "first_name", "last_name", "password1", "password2"] def clean_username(self): @@ -31,7 +29,7 @@ class UserUpdateForm(forms.ModelForm): email = forms.EmailField() class Meta: - model = get_user_model() + model = User fields = ["username", "email", "first_name", "last_name"] def clean_username(self): diff --git a/apps/users/models.py b/apps/users/models.py index 72480064..f239d603 100644 --- a/apps/users/models.py +++ b/apps/users/models.py @@ -1,13 +1,12 @@ from datetime import timedelta from django.conf import settings -from django.contrib.auth import get_user_model +from django.contrib.auth.models import User from django.core.exceptions import ObjectDoesNotExist from django.db import models from django.template.defaultfilters import slugify from django.urls import reverse from django.utils import timezone - from django_otp.util import random_hex from encrypted_model_fields.fields import EncryptedCharField @@ -29,7 +28,7 @@ class AuthorView(models.IntegerChoices): # Relationship Fields user = models.OneToOneField( - get_user_model(), primary_key=True, related_name="profile", on_delete=models.CASCADE + User, primary_key=True, related_name="profile", on_delete=models.CASCADE ) def __str__(self): @@ -98,9 +97,7 @@ class EmailToken(models.Model): token_expiration_timestamp = models.DateTimeField(null=True, blank=True) # Relationship Fields - user = models.ForeignKey( - get_user_model(), related_name="user_email_tokens", on_delete=models.CASCADE - ) + user = models.ForeignKey(User, related_name="user_email_tokens", on_delete=models.CASCADE) def __repr__(self): return f"" diff --git a/apps/users/views.py b/apps/users/views.py index 1d13ad28..485599a0 100644 --- a/apps/users/views.py +++ b/apps/users/views.py @@ -2,10 +2,10 @@ from django.conf import settings from django.contrib import auth, messages -from django.contrib.auth import get_user_model from django.contrib.auth.forms import AuthenticationForm from django.contrib.auth.hashers import check_password from django.contrib.auth.mixins import UserPassesTestMixin +from django.contrib.auth.models import User from django.contrib.auth.views import ( PasswordResetCompleteView, PasswordResetConfirmView, @@ -20,7 +20,6 @@ from django.utils.safestring import mark_safe from django.views.generic import CreateView, DetailView, TemplateView from django.views.generic.edit import FormView - from shapeshifter.views import MultiModelFormView from two_factor.forms import AuthenticationTokenForm from two_factor.views.core import LoginView, SetupView @@ -51,7 +50,7 @@ class UserRegisterView(CreateView): def user_exists(self, form) -> bool: username = form.data["username"] - user = get_user_model().objects.filter(username=username) + user = User.objects.filter(username=username) if user.exists(): return True @@ -110,7 +109,7 @@ def build_html_content(self, user, token) -> str: }, ) - def email_two_factor_token(self, user: get_user_model(), token): + def email_two_factor_token(self, user: User, token): """Sends email containing current token""" subject = "Your One Time Token" @@ -155,7 +154,8 @@ def login_user(self, user): def handle_email_auth_user(self, user): """Handles the actions for processing an email authenticated user""" - if user_passes_auth := self.authenticate_user(user=user): + user_passes_auth = self.authenticate_user(user=user) + if user_passes_auth: self.login_user(user=user) token = self.retrieve_token_from_db(user) self.email_two_factor_token(user, token) @@ -176,8 +176,8 @@ def post(self, *args, **kwargs): # Scenario 1: The user does not exist in the DB try: - user = get_user_model().objects.get(username=username) - except get_user_model().DoesNotExist: + user = User.objects.get(username=username) + except User.DoesNotExist: self._add_user_does_not_exist_message() return redirect(self.request.path_info) @@ -202,7 +202,7 @@ def post(self, *args, **kwargs): # enable the Django Two-Factor Auth package to handle elif self.steps.current == "token": user_pk = self.request.session["wizard_user_login_view"]["user_pk"] - user = get_user_model().objects.get(pk=user_pk) + user = User.objects.get(pk=user_pk) if user.profile.is_two_factor_auth_by_token: return super().post(*args, **kwargs) @@ -224,7 +224,7 @@ class UserSetupEmailView(TemplateView): template_name = "two_factor/setup_by_email.html" success_url = reverse_lazy("blog:users:setup_email_token") - def store_token_in_db(self, user: get_user_model(), token: str): + def store_token_in_db(self, user: User, token: str): """Creates an email token object in the DB""" EmailToken.objects.create( challenge_email_address=user.email, @@ -235,7 +235,7 @@ def store_token_in_db(self, user: get_user_model(), token: str): user_id=user.id, ) - def build_html_content(self, user: get_user_model(), token: str) -> str: + def build_html_content(self, user: User, token: str) -> str: """ " Specifies the email template and context variables""" return render_to_string( template_name="emails/token.html", @@ -246,7 +246,7 @@ def build_html_content(self, user: get_user_model(), token: str) -> str: }, ) - def email_two_factor_token(self, user: get_user_model(), token: str): + def email_two_factor_token(self, user: User, token: str): """Sends email containing one-time token""" subject = "Your One Time Token" @@ -315,7 +315,7 @@ def build_html_content(self, user, token) -> str: }, ) - def email_two_factor_success(self, user: get_user_model(), token): + def email_two_factor_success(self, user: User, token): """Sends email containing one-time token""" subject = "Two-Factor Authentication Successful" @@ -365,7 +365,7 @@ class ProfileView(TwoFactorAuthUserMixin, DetailView): template_name = "users/profile.html" def get_object(self, queryset=None): - return get_object_or_404(get_user_model(), username=self.kwargs["username"]) + return get_object_or_404(User, username=self.kwargs["username"]) class ProfileUpdateView(TwoFactorAuthUserMixin, UserPassesTestMixin, MultiModelFormView):