Skip to content

Commit

Permalink
Fix 'flake8' issues in application.
Browse files Browse the repository at this point in the history
  • Loading branch information
brucestull committed Oct 28, 2023
1 parent 5526640 commit 39cd1b0
Show file tree
Hide file tree
Showing 21 changed files with 236 additions and 139 deletions.
1 change: 1 addition & 0 deletions .flake8
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
[flake8]
extend-ignore = F403,F405
exclude = migrations
statistics = True
max-line-length = 88
1 change: 1 addition & 0 deletions Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ coverage = "==7.2.6"
djangorestframework-simplejwt = "*"
flake8 = "*"
isort = "*"
pymongo = "*"

[dev-packages]

Expand Down
98 changes: 97 additions & 1 deletion Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 9 additions & 11 deletions accounts/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,30 @@

from accounts.models import CustomUser

class CustomUserCreationForm(UserCreationForm):

class CustomUserCreationForm(UserCreationForm):
class Meta:
model = CustomUser
fields = (
'username',
'email',
"username",
"email",
# 'first_name',
# 'last_name',
)

class CustomUserChangeForm(UserChangeForm):

class CustomUserChangeForm(UserChangeForm):
class Meta:
model = CustomUser
fields = (
'username',
'email',
'first_name',
'last_name',

"username",
"email",
"first_name",
"last_name",
# 'is_staff', # This field should only be available to
# some adminstrative role. There would be some sort of page
# where the andminstrative role can change the role of a user.

# 'is_superuser', # This field should only be available to
# some adminstrative role. There would be some sort of page
# where the andminstrative role can change the role of a user.
)
)
4 changes: 3 additions & 1 deletion accounts/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

class CustomUser(AbstractUser):
"""
A CustomUser class is added so we can add functionality later. It's more convenient then not to add CustomUser at beginning of project before database migrations are started.
A CustomUser class is added so we can add functionality later. It's more convenient
then not to add CustomUser at beginning of project before database migrations are
started.
"""

registration_accepted = models.BooleanField(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@

from accounts.models import CustomUser

A_TEST_USERNAME = 'ACustomUser'
A_TEST_USERNAME = "ACustomUser"


class CustomUserModelTest(TestCase):
"""
Expand All @@ -17,30 +18,31 @@ def setUpTestData(cls):
This specific function name `setUpTestData` is required by Django.
"""
user = CustomUser.objects.create(
cls.user = CustomUser.objects.create(
username=A_TEST_USERNAME,
)

def test_has_registration_accepted_field(self):
"""
`CustomUser` model should have `registration_accepted` field.
"""
self.assertTrue(hasattr(CustomUser, 'registration_accepted'))
self.assertTrue(hasattr(CustomUser, "registration_accepted"))

def test_registration_accepted_field_is_boolean_field(self):
"""
`CustomUser` model `registration_accepted` field should be `BooleanField`.
"""
user = CustomUser.objects.get(id=1)
registration_accepted_field = user._meta.get_field('registration_accepted')
registration_accepted_field = user._meta.get_field("registration_accepted")
self.assertIsInstance(registration_accepted_field, models.BooleanField)

def test_registration_accepted_default_attribute_is_false(self):
"""
`CustomUser` model `registration_accepted` field `default` attribute should be `False`.
`CustomUser` model `registration_accepted` field `default` attribute should be
`False`.
"""
user = CustomUser.objects.get(id=1)
registration_accepted_field = user._meta.get_field('registration_accepted')
registration_accepted_field = user._meta.get_field("registration_accepted")
self.assertFalse(registration_accepted_field.default)

def test_dunder_string_method(self):
Expand Down
9 changes: 5 additions & 4 deletions accounts/tests/test_views.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
from django.test import TestCase
from django.urls import reverse_lazy
from django.urls import reverse

from accounts.models import CustomUser
from accounts.forms import CustomUserCreationForm, CustomUserChangeForm
from accounts.forms import CustomUserChangeForm

A_TEST_USERNAME = "ACustomUser"
A_TEST_PASSWORD = "a_test_password"
Expand All @@ -20,7 +19,8 @@

CUSTOM_LOGIN_VIEW_URL = "/accounts/login/"
CUSTOM_LOGIN_VIEW_NAME = "login"
# Django provides a default login view with a default template location so we don't need to specify it.
# Django provides a default login view with a default template location so we don't
# need to specify it.
# CUSTOM_LOGIN_VIEW_TEMPLATE = "registration/login.html"

USER_UPDATE_VIEW_URL = "/accounts/1/edit/"
Expand Down Expand Up @@ -161,7 +161,8 @@ def setUpTestData(cls):

def test_url_redirects_for_non_authenticated_user(self):
"""
`accounts` update view should redirect to the `login` view for non-authenticated user.
`accounts` update view should redirect to the `login` view for non-authenticated
user.
"""
response = self.client.get(
reverse(
Expand Down
3 changes: 1 addition & 2 deletions accounts/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
urlpatterns = [
# Try to override 'login' view.
path("login/", CustomLoginView.as_view(), name="login"),

path("signup/", SignUpView.as_view(), name="signup"),
path("<int:pk>/edit/", UserUpdateView.as_view(), name="edit-profile"),
]
]
32 changes: 18 additions & 14 deletions accounts/views.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from django.urls import reverse_lazy
from django.views.generic.edit import CreateView, UpdateView
from django.contrib.auth.views import LoginView
from django.contrib.auth.mixins import LoginRequiredMixin, UserPassesTestMixin
from django.contrib.auth.mixins import LoginRequiredMixin

from accounts.forms import CustomUserCreationForm, CustomUserChangeForm
from accounts.models import CustomUser
Expand All @@ -12,50 +12,54 @@ class SignUpView(CreateView):
"""
View for user to create a new account.
"""

form_class = CustomUserCreationForm
success_url = reverse_lazy('login')
template_name = 'registration/signup.html'
success_url = reverse_lazy("login")
template_name = "registration/signup.html"

def get_context_data(self, **kwargs):
"""
Add the site name to the context.
"""
context = super().get_context_data(**kwargs)
context['the_site_name'] = THE_SITE_NAME
# Add 'hide_signup_link' to the context so that the signup link is hidden on the signup page but shown on other pages.
context['hide_signup_link'] = True
context["the_site_name"] = THE_SITE_NAME
# Add 'hide_signup_link' to the context so that the signup link is hidden on
# the signup page but shown on other pages.
context["hide_signup_link"] = True
return context


class CustomLoginView(LoginView):
"""
Override the default login view. This will allow us to add the site name to the context and then display it on the page.
Override the default login view. This will allow us to add the site name to the
context and then display it on the page.
"""

def get_context_data(self, **kwargs):
"""
Get the parent `context` and add the site name to the it.
"""
context = super().get_context_data(**kwargs)
context['the_site_name'] = THE_SITE_NAME
context['hide_login_link'] = True
context["the_site_name"] = THE_SITE_NAME
context["hide_login_link"] = True
return context


class UserUpdateView(LoginRequiredMixin, UpdateView):
"""
View for user to update an existing account.
"""

model = CustomUser
form_class = CustomUserChangeForm
success_url = reverse_lazy('login')
template_name ='registration/update.html'
success_url = reverse_lazy("login")
template_name = "registration/update.html"

def get_context_data(self, **kwargs):
"""
Add the site name to the context.
"""
context = super().get_context_data(**kwargs)
context['the_site_name'] = THE_SITE_NAME
context['hide_edit_profile_link'] = True
return context
context["the_site_name"] = THE_SITE_NAME
context["hide_edit_profile_link"] = True
return context
3 changes: 0 additions & 3 deletions api/admin.py
Original file line number Diff line number Diff line change
@@ -1,3 +0,0 @@
from django.contrib import admin

# Register your models here.
3 changes: 0 additions & 3 deletions api/models.py
Original file line number Diff line number Diff line change
@@ -1,3 +0,0 @@
from django.db import models

# Create your models here.
1 change: 1 addition & 0 deletions api/permissions.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from rest_framework.permissions import BasePermission


class IsRegistrationAccepted(BasePermission):
def has_permission(self, request, view):
return request.user.registration_accepted
Expand Down
Loading

0 comments on commit 39cd1b0

Please sign in to comment.