diff --git a/aleksis/core/decorators.py b/aleksis/core/decorators.py index bd87884fbdace4a4277e9d81503a89c89924563d..06a62f5a36112fc942449a724dc1156d778437d1 100644 --- a/aleksis/core/decorators.py +++ b/aleksis/core/decorators.py @@ -1,8 +1,15 @@ from typing import Callable -from django.contrib.auth.decorators import user_passes_test +from django.contrib.auth.decorators import login_required, user_passes_test def admin_required(function: Callable = None) -> Callable: actual_decorator = user_passes_test(lambda u: u.is_active and u.is_superuser) return actual_decorator(function) + + +def person_required(function: Callable = None) -> Callable: + """ Requires a logged-in user which is linked to a person. """ + + actual_decorator = user_passes_test(lambda u: user.person is not None) + return actual_decorator(login_required(function)) diff --git a/aleksis/core/views.py b/aleksis/core/views.py index 09e0a42677c961fe1b48e9ab074df3614dc8825b..a320980e51657381d2e358a4314a6ef1e1af7ae9 100644 --- a/aleksis/core/views.py +++ b/aleksis/core/views.py @@ -8,7 +8,7 @@ from django.utils.translation import ugettext_lazy as _ from django_tables2 import RequestConfig -from .decorators import admin_required +from .decorators import admin_required, person_required from .forms import ( EditGroupForm, EditPersonForm, @@ -21,23 +21,17 @@ from .tables import GroupsTable, PersonsTable from .util import messages -@login_required +@person_required def index(request: HttpRequest) -> HttpResponse: context = {} - if request.user.is_authenticated: - activities = request.user.person.activities.all()[:5] - - notifications = ( - request.user.person.notifications.all()[:5] - ) - unread_notifications = ( - request.user.person.notifications.all().filter(read=False) - ) + activities = request.user.person.activities.all()[:5] + notifications = request.user.person.notifications.all()[:5] + unread_notifications = request.user.person.notifications.all().filter(read=False) - context["activities"] = activities - context["notifications"] = notifications - context["unread_notifications"] = unread_notifications + context["activities"] = activities + context["notifications"] = notifications + context["unread_notifications"] = unread_notifications return render(request, "core/index.html", context)