Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code format and best practices #4

Merged
merged 17 commits into from
Oct 6, 2024
Merged
666 changes: 666 additions & 0 deletions .pylintrc

Large diffs are not rendered by default.

17 changes: 6 additions & 11 deletions accounts/decorators.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from django.contrib.auth import REDIRECT_FIELD_NAME
from django.contrib.auth.decorators import user_passes_test
from django.shortcuts import redirect


Expand All @@ -21,9 +19,8 @@ def wrapper(request, *args, **kwargs):
if test_func(request.user):
# Call the original function if the user passes the test
return function(request, *args, **kwargs) if function else None
else:
# Redirect to the specified URL if the user fails the test
return redirect(redirect_to)
# Redirect to the specified URL if the user fails the test
return redirect(redirect_to)

return wrapper if function else test_func

Expand All @@ -46,9 +43,8 @@ def wrapper(request, *args, **kwargs):
if test_func(request.user):
# Call the original function if the user passes the test
return function(request, *args, **kwargs) if function else None
else:
# Redirect to the specified URL if the user fails the test
return redirect(redirect_to)
# Redirect to the specified URL if the user fails the test
return redirect(redirect_to)

return wrapper if function else test_func

Expand All @@ -71,8 +67,7 @@ def wrapper(request, *args, **kwargs):
if test_func(request.user):
# Call the original function if the user passes the test
return function(request, *args, **kwargs) if function else None
else:
# Redirect to the specified URL if the user fails the test
return redirect(redirect_to)
# Redirect to the specified URL if the user fails the test
return redirect(redirect_to)

return wrapper if function else test_func
9 changes: 9 additions & 0 deletions accounts/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,15 @@ class StaffAddForm(UserCreationForm):
label="Last Name",
)

gender = forms.CharField(
widget=forms.Select(
choices=GENDERS,
attrs={
"class": "browser-default custom-select form-control",
},
),
)

address = forms.CharField(
max_length=30,
widget=forms.TextInput(
Expand Down
4 changes: 2 additions & 2 deletions accounts/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ def get_picture(self):
return no_picture

def get_absolute_url(self):
return reverse("profile_single", kwargs={"id": self.id})
return reverse("profile_single", kwargs={"user_id": self.id})

def save(self, *args, **kwargs):
super().save(*args, **kwargs)
Expand Down Expand Up @@ -170,7 +170,7 @@ def get_gender_count(cls):
return {"M": males_count, "F": females_count}

def get_absolute_url(self):
return reverse("profile_single", kwargs={"id": self.id})
return reverse("profile_single", kwargs={"user_id": self.id})

def delete(self, *args, **kwargs):
self.student.delete()
Expand Down
2 changes: 1 addition & 1 deletion accounts/signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
)


def post_save_account_receiver(sender, instance=None, created=False, *args, **kwargs):
def post_save_account_receiver(instance=None, created=False, *args, **kwargs):
"""
Send email notification
"""
Expand Down
17 changes: 8 additions & 9 deletions accounts/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,38 +5,37 @@

User = get_user_model()


class AdminRequiredDecoratorTests(TestCase):
def setUp(self):
self.superuser = User.objects.create_superuser(
username='admin', email='[email protected]', password='password'
username="admin", email="[email protected]", password="password"
)
self.user = User.objects.create_user(
username='user', email='[email protected]', password='password'
username="user", email="[email protected]", password="password"
)
self.factory = RequestFactory()

def admin_view(self, request):
return HttpResponse()

def test_admin_required_decorator(self):
# Apply the admin_required decorator to the view function
decorated_view = admin_required(self.admin_view)

request = self.factory.get("/")
request.user = self.user
response = decorated_view(request)
self.assertEqual(response.status_code, 302)


def test_admin_required_decorator_with_redirect(self):
# Apply the admin_required decorator to the view function
decorated_view = admin_required(function=self.admin_view,redirect_to="/login/")
decorated_view = admin_required(function=self.admin_view, redirect_to="/login/")

request = self.factory.get("/")
request.user = self.user
response = decorated_view(request)

# Assert redirection to login page
self.assertEqual(response.status_code, 302)
self.assertEqual(response.url, '/login/')

self.assertEqual(response.url, "/login/")
53 changes: 32 additions & 21 deletions accounts/tests/test_decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,51 +5,51 @@

User = get_user_model()


class AdminRequiredDecoratorTests(TestCase):
def setUp(self):
self.superuser = User.objects.create_superuser(
username='admin', email='[email protected]', password='password'
username="admin", email="[email protected]", password="password"
)
self.user = User.objects.create_user(
username='user', email='[email protected]', password='password'
username="user", email="[email protected]", password="password"
)
self.factory = RequestFactory()

def admin_view(self, request):
return HttpResponse("Admin View Content")

def test_admin_required_decorator_redirects(self):
decorated_view = admin_required(self.admin_view)

request = self.factory.get("/restricted-view")
request.user = self.user
response = decorated_view(request)
self.assertEqual(response.status_code, 302)
self.assertEqual(response.url, "/")


def test_admin_required_decorator_redirects_to_correct_path(self):
decorated_view = admin_required(function=self.admin_view,redirect_to="/login/")
decorated_view = admin_required(function=self.admin_view, redirect_to="/login/")

request = self.factory.get("restricted-view")
request.user = self.user
response = decorated_view(request)

self.assertEqual(response.status_code, 302)
self.assertEqual(response.url, '/login/')
self.assertEqual(response.url, "/login/")

def test_admin_required_decorator_does_not_redirect_superuser(self):
decorated_view = admin_required(self.admin_view)

request = self.factory.get("/restricted-view")
request.user = self.superuser
response = decorated_view(request)
self.assertEqual(response.status_code, 200)
self.assertEqual(response.content, b"Admin View Content")

def test_admin_redirect_decorator_return_correct_response(self):
decorated_view = admin_required(self.admin_view)

request = self.factory.get("/restricted-view")
request.user = self.superuser
response = decorated_view(request)
Expand All @@ -59,10 +59,13 @@ def test_admin_redirect_decorator_return_correct_response(self):
class LecturerRequiredDecoratorTests(TestCase):
def setUp(self):
self.lecturer = User.objects.create_user(
username='lecturer', email='[email protected]', password='password', is_lecturer=True
username="lecturer",
email="[email protected]",
password="password",
is_lecturer=True,
)
self.user = User.objects.create_user(
username='user', email='[email protected]', password='password'
username="user", email="[email protected]", password="password"
)
self.factory = RequestFactory()

Expand All @@ -81,15 +84,17 @@ def test_lecturer_required_decorator_redirects(self):
self.assertEqual(response.url, "/")

def test_lecturer_required_decorator_redirects_to_correct_path(self):
decorated_view = lecturer_required(function=self.lecturer_view, redirect_to="/login/")
decorated_view = lecturer_required(
function=self.lecturer_view, redirect_to="/login/"
)

request = self.factory.get("/restricted-view")
request.user = self.user

response = decorated_view(request)

self.assertEqual(response.status_code, 302)
self.assertEqual(response.url, '/login/')
self.assertEqual(response.url, "/login/")

def test_lecturer_required_decorator_does_not_redirect_lecturer(self):
decorated_view = lecturer_required(self.lecturer_view)
Expand All @@ -112,13 +117,17 @@ def test_lecturer_redirect_decorator_return_correct_response(self):

self.assertIsInstance(response, HttpResponse)


class StudentRequiredDecoratorTests(TestCase):
def setUp(self):
self.student = User.objects.create_user(
username='student', email='[email protected]', password='password', is_student=True
username="student",
email="[email protected]",
password="password",
is_student=True,
)
self.user = User.objects.create_user(
username='user', email='[email protected]', password='password'
username="user", email="[email protected]", password="password"
)
self.factory = RequestFactory()

Expand All @@ -137,15 +146,17 @@ def test_student_required_decorator_redirects(self):
self.assertEqual(response.url, "/")

def test_student_required_decorator_redirects_to_correct_path(self):
decorated_view = student_required(function=self.student_view, redirect_to="/login/")
decorated_view = student_required(
function=self.student_view, redirect_to="/login/"
)

request = self.factory.get("/restricted-view")
request.user = self.user

response = decorated_view(request)

self.assertEqual(response.status_code, 302)
self.assertEqual(response.url, '/login/')
self.assertEqual(response.url, "/login/")

def test_student_required_decorator_does_not_redirect_student(self):
decorated_view = student_required(self.student_view)
Expand All @@ -166,4 +177,4 @@ def test_student_redirect_decorator_return_correct_response(self):

response = decorated_view(request)

self.assertIsInstance(response, HttpResponse)
self.assertIsInstance(response, HttpResponse)
Loading
Loading