From 868aa018a4653c2aac1ba816ce141809c4998007 Mon Sep 17 00:00:00 2001 From: Tanner Collin Date: Tue, 10 May 2022 01:07:32 +0000 Subject: [PATCH] Add ability to change class instructor --- apiserver/apiserver/api/serializers.py | 14 ++++++++- webclient/src/InstructorClasses.js | 39 ++++++++++++++++++++++++-- 2 files changed, 49 insertions(+), 4 deletions(-) diff --git a/apiserver/apiserver/api/serializers.py b/apiserver/apiserver/api/serializers.py index f777107..f896cec 100644 --- a/apiserver/apiserver/api/serializers.py +++ b/apiserver/apiserver/api/serializers.py @@ -15,6 +15,7 @@ import datetime, time from . import models, fields, utils, utils_ldap, utils_auth, utils_stats from .. import settings, secrets +from .permissions import is_admin_director class UsageSerializer(serializers.ModelSerializer): first_name = serializers.SerializerMethodField() @@ -482,7 +483,7 @@ class SessionSerializer(serializers.ModelSerializer): class Meta: model = models.Session fields = '__all__' - read_only_fields = ['old_instructor', 'instructor'] + read_only_fields = ['old_instructor'] def get_student_count(self, obj): return len([x for x in obj.students.all() if x.attendance_status != 'Withdrawn']) @@ -503,6 +504,17 @@ class SessionSerializer(serializers.ModelSerializer): else: return None + def update(self, instance, validated_data): + if not self.initial_data.get('instructor_id', None): + raise ValidationError(dict(instructor_id='This field is required.')) + + member = get_object_or_404(models.Member, id=self.initial_data['instructor_id']) + if not (is_admin_director(member.user) or member.is_instructor): + raise ValidationError(dict(instructor_id='Member is not an instructor.')) + + validated_data['instructor'] = member.user + return super().update(instance, validated_data) + class SessionListSerializer(SessionSerializer): students = None diff --git a/webclient/src/InstructorClasses.js b/webclient/src/InstructorClasses.js index ad660e0..2754029 100644 --- a/webclient/src/InstructorClasses.js +++ b/webclient/src/InstructorClasses.js @@ -228,7 +228,8 @@ export function InstructorClassAttendance(props) { }; function InstructorClassEditor(props) { - const { input, setInput, error, editing } = props; + const { input, setInput, error, editing, token } = props; + const [editInstructor, setEditInstructor] = useState(false); const handleValues = (e, v) => setInput({ ...input, [v.name]: v.value }); const handleUpload = (e, v) => setInput({ ...input, [v.name]: e.target.files[0] }); @@ -245,6 +246,38 @@ function InstructorClassEditor(props) { return (
+ {editing && + (editInstructor ? + <> + + + + {error.instructor_id && } + + + + Are you sure? +

Only the new instructor will be able to edit this class.

+
+ + : + + ) + } + +

+

Edit Class
- + Submit @@ -402,7 +435,7 @@ export function InstructorClassList(props) {

Documentation: https://wiki.protospace.ca/Be_a_Course_Instructor

- + Submit