def destroy(self, request, *args, **kwargs): error_message = None now = TIMEZONE.localize(datetime.datetime.now()) if not (views.REGSTART <= now <= views.REGEND): error_message = "It is not time to enroll." if error_message is not None: return Response({'error': error_message}, status=status.HTTP_400_BAD_REQUEST) return super(RegistrationViewSet, self).destroy(request, *args, **kwargs)
def create(self, request, *args, **kwargs): error_message = None try: selected_course = Course.objects.get(id=request.DATA['section']) except (KeyError, Course.DoesNotExist): error_message = "That course does not exist." else: now = TIMEZONE.localize(datetime.datetime.now()) if not (views.REGSTART <= now <= views.REGEND): error_message = "It is not time to enroll." selected_student = Student.objects.get(netID=request.user) ss_courses = selected_student.course_set.all() # Can't enroll in a class we're already in if selected_course in ss_courses: error_message = "Already enrolled in "+selected_course.title # Can't enroll in a full class if selected_course.is_full(): error_message = selected_course.title+" is at maximum capacity." # Can't enroll in a cancelled class if selected_course.cancelled: error_message = selected_course.title+" is cancelled." # Can't enroll in two section of same class if selected_course.other_section.count() != 0: other_sections = selected_course.other_section.all() my_courses = selected_student.course_set.all() for course in my_courses: for section in other_sections: if course.courseID == section.courseID: error_message = "You are already in "+section.courseID+", which is an alternative section of "+course.title # Can't enroll in a class with time conflicts ssb = selected_student.blocks() overlap = False for blk in selected_course.blocks: if blk in ssb: overlap = True break if overlap: for course in selected_student.course_set.all(): for blk in course.blocks: if blk in selected_course.blocks: error_message = selected_course.title \ +" conflicts with "+course.title+" at " \ +decode(blk) if error_message is not None: return Response({'error': error_message}, status=status.HTTP_400_BAD_REQUEST) # If we've made it to here, we can enroll return super(RegistrationViewSet, self).create(request, *args, **kwargs)