diff --git a/Main/middleware.py b/Main/middleware.py index d3c1650..3a2c141 100644 --- a/Main/middleware.py +++ b/Main/middleware.py @@ -1,12 +1,31 @@ +""" + This file defines middleware, which is code that runs before a view +""" + from django.shortcuts import redirect from django.conf import settings class UserSetupCheckMiddleware: + """ + This middleware will redirect the user to the user setup page if they're from IIS and need a student_id + """ + def __init__(self, get_response): + """ + This function is run to create a new instance of the middleware + + :param: get_response: A function that gets the response for this request + """ + self.get_response = get_response def __call__(self, request): + """ + This method is run when calling the middleware + If the user doesn't have a student_id, require them to enter one + """ + if "user-setup" not in request.path and request.user.is_authenticated and (request.user.has_usable_password() or settings.DEBUG) and request.user.student_id is None: return redirect('user-setup', pk=request.user.id) else: diff --git a/Users/forms.py b/Users/forms.py index f38f579..e72e87b 100644 --- a/Users/forms.py +++ b/Users/forms.py @@ -1,3 +1,7 @@ +""" + This file defines the forms used in the Users app +""" + from django.conf import settings from django.forms import ModelForm @@ -5,12 +9,32 @@ from .models import User class FinishUserForm(ModelForm): + """ + This form is used to finish setting up a user when syncing them from ActiveDirectory + """ class Meta: + """ + This internal class defines options for the form + + :cvar model: The model we want the form to edit + :cvar fields: The fields on the model we want to edit + """ + model = User fields = ['student_id'] def save(self, commit=True): + """ + This code is run when the form is saved. + It generates the student's email address from their first name, last name, and student id. + + :param commit: Whether to save changes made or not: + :type commit: bool + :returns: The edited user object + :rtype: User + """ + new_user: User = super(FinishUserForm, self).save(commit=commit) new_user.email = f'{new_user.first_name[:3].lower()}{new_user.last_name[:3].lower()}{new_user.student_id[-3:]}@{settings.EMAIL_DOMAIN}' new_user.save() diff --git a/Users/models.py b/Users/models.py index f7cefdb..f1d0895 100644 --- a/Users/models.py +++ b/Users/models.py @@ -13,6 +13,7 @@ class User(AbstractUser): This model represents a User in the database :cvar id: A Unique Identifier for the user + :cvar student_id: The user's BCTC Id :cvar session: The session the student is in (AM or PM) :cvar is_reviewer: Whether the student can review code """ diff --git a/Users/views.py b/Users/views.py index 0fc1f89..47d2f01 100644 --- a/Users/views.py +++ b/Users/views.py @@ -8,6 +8,7 @@ from django.contrib.auth.mixins import LoginRequiredMixin from django.contrib import messages from django.conf import settings from django.views.generic import TemplateView, UpdateView +from django.db.models import QuerySet from .models import User from .forms import FinishUserForm @@ -15,6 +16,17 @@ from Main.views import FormNameMixin, FormAlertMixin class CompleteUserSetupView(LoginRequiredMixin, FormNameMixin, FormAlertMixin, UpdateView): + """ + This view is run when the user wishes to edit their student id + + :cvar template_name: The template to render + :cvar form_name: The name to display in the pageHeader block + :cvar form_class: The class to use when rendering the form + :cvar model: The model we're editing + :cvar success_message: The message that's displayed when the edit is a success + :cvar success_url: The url to redirect to after the form is saved + """ + template_name = "form_base.html" form_name = "Complete User Setup" form_class = FinishUserForm @@ -23,9 +35,22 @@ class CompleteUserSetupView(LoginRequiredMixin, FormNameMixin, FormAlertMixin, U success_url = reverse_lazy('home') def get_queryset(self): + """ + This function defines what users a user can edit the student ids of + We only want a user to be able to edit their own ids + + :returns: The users the user is allowed to edit + :rtype: QuerySet + """ + return User.objects.filter(id=self.request.user.id) def get(self, request, *args, **kwargs): + """ + This function is run when the user makes a GET request + If the user isn't from ActiveDirectory (they're an admin) we don't need to worry about student ids + """ + if request.user.has_usable_password(): if settings.DEBUG: messages.add_message(request, messages.WARNING, "User isn't from IIS. Ignoring because DEBUG is True")