def remove_student(request: HttpRequest, course_id:str, student_id:str): try: course = Course.objects.get(id=course_id) course.unenroll(student_id) except Course.DoesNotExist: return db_error(_('Requested course does not exist.')) except Course.IsEnrolled: return db_error(_('Requested student is not enrolled in this course.')) return redirect('course', course_id)
def edit_slot(request: HttpRequest, course_id): try: course = Course.objects.get(id=course_id) except Course.DoesNotExist: return db_error(_('Requested course does not exist.')) schedule = course.schedule if schedule.is_weekly(): form_type = forms.WeeklySlotForm else: form_type = forms.DateForm if request.method == 'POST': form = form_type(request.POST) if form.is_valid(): slot = form.save(commit=False) slot.schedule = schedule slot.save() return redirect('course-edit-slot', course_id) else: form = form_type() return render( request, 'course/time.html', { 'title': _('Edit Schedule'), 'form': form, 'schedule': schedule, 'course_id': course_id, 'target': 'course-edit-slot', })
def course_overview(request, subjectname): user = request.user try: active_subject = subject.Subject.objects.get(name=subjectname) except subject.Subject.DoesNotExist: return db_error(_('Requested subject does not exist.')) if user.is_authenticated(): cl = filter( lambda c: c.active or c.is_teacher(user.userinformation), active_subject.course_set.all() ) else: cl = active_subject.course_set.filter(active=True) return render( request, 'subject/info.html', { 'title': subjectname, 'subject': active_subject, 'course_list': cl, 'user_is_verified_student': user.userinformation.is_verified_student() if user.is_authenticated() else False, 'target': reverse('subject', args=(subjectname,)) } )
def add(request, course_id): user = request.user stud = user.userinformation try: course = Course.objects.get(id=course_id) except Course.DoesNotExist: return db_error(_('Requested course does not exist.')) session = request.session if course.is_participant(stud): session['enroll-error'] = _('You are already enrolled in this course.') elif course.student_only and not stud.is_student(): session['enroll-error'] = _('Sorry, this course is for students only.') elif course.student_only and stud.is_pending_student(): session['enroll-error'] = _( 'Please verify your student status to enroll for this course.') elif not course.joinable: session['enroll-error'] = _('Sorry, this course is full.') else: if 'enroll-error' in session: del session['enroll-error'] course.participants.add(stud) # redirect to course overview or specified target return redirect_unless_target(request, 'register-course-done', course_id)
def contact_form(request:HttpRequest, user_id:int): try: user = User.objects.get(id=user_id) except User.DoesNotExist: return db_error(_('Requested user does not exist.')) assert isinstance(user, User) if request.method == 'POST': form = ContactForm(request.POST) if form.is_valid(): user.email_user( subject="[CM contact form] " + form.subject, message=form.content, from_email=form.data['sender'] ) return redirect('index') else: form = ContactForm() return render( request, 'contact-form.html', { 'form': form, 'user': user } )
def course_overview(request, subjectname): user = request.user try: active_subject = subject.Subject.objects.get(name=subjectname) except subject.Subject.DoesNotExist: return db_error(_('Requested subject does not exist.')) if user.is_authenticated(): cl = filter( lambda c: c.visible or c.is_teacher(user.userinformation), active_subject.course_set.filter(archiving='t') ) else: cl = active_subject.course_set.filter(visible=True, archiving='t') return render( request, 'subject/info.html', { 'title': subjectname, 'subject': active_subject, 'course_list': cl, 'target': reverse('subject', args=(subjectname,)) } )
def contact_teachers(request, course_id): if request.method == 'POST': form = ContactForm(request.POST) if form.is_valid(): try: course = Course.objects.get(id=course_id) except Course.DoesNotExist: return db_error(_('Requested course does not exist.')) subject = "[CM contact form] " + form.subject content = form.content + CONTACT_FOOTER + request.user.email for teacher in course.teacher.all(): teacher.user.email_user(subject, content) return redirect('contact-teachers-done', course_id) else: form = ContactForm() return render( request, 'course/contact-teachers.html', { 'title': _('Notify Course'), 'form': form, 'course_id': course_id } )
def contact_form(request: HttpRequest, user_id: int): try: user = User.objects.get(id=user_id) except User.DoesNotExist: return db_error(_('Requested user does not exist.')) assert isinstance(user, User) if request.method == 'POST': form = ContactForm(request.POST) if form.is_valid(): user.email_user( subject="[CM contact form] " + form.subject, message=form.content, from_email=form.cleaned_data['sender'] ) return redirect('index') else: form = ContactForm() return render( request, 'contact-form.html', { 'form': form, 'user': user } )
def edit_course(request: HttpRequest, course_id: str): """ Edit form for changing a course and handler for submitted data. :param request: request object :param course_id: id for the course :return: """ try: current_course = Course.objects.get(id=course_id) except Course.DoesNotExist: return db_error(_('Requested course does not exist.')) if request.method == "POST": form = CourseForm(request.POST, instance=current_course) if form.is_valid(): current_course.save() return redirect('course', course_id) else: form = CourseForm(instance=current_course) return render( request, 'course/edit.html', { 'title': _('Edit course'), 'form': form, 'course_id': course_id, 'allowed_tags': html_clean.DESCR_ALLOWED_TAGS, 'course_is_active': current_course.active, } )
def profile(request, user_id=None): if user_id is None: if request.user.is_authenticated(): user = request.user template = 'user/profile.html' is_own = True teacher = user.userinformation.teacher.filter(archiving='t') attend = user.userinformation.course_set.filter(archiving='t') else: return redirect('login') else: try: user = User.objects.get(id=user_id) is_own = request.user.id == user.id except User.DoesNotExist: return db_error(_('This user does not exist')) template = 'user/public-profile.html' return render( request, template, { 'course_list_show_subject': True, 'profiled_user': user, 'is_own': is_own, 'title': '{} {}'.format(user.first_name, user.last_name) })
def attendee_list(request, course_id): if 'slots' in request.GET: try: course = Course.objects.get(id=course_id) except Course.DoesNotExist: return db_error(_('Requested course does not exist.')) # handle empty string (== no number) as input try: slots = int(request.GET['slots']) except ValueError: slots = 0 return render( request, 'course/attendee-list.html', { 'attendees': course.participants.all(), 'slots': range(slots) } ) return render( request, 'course/attendee-list-select.html', { 'course_id': course_id } )
def course(request: HttpRequest, course_id: str): """ Controller for single course info page :param request: request object :param course_id: id for the course :return: """ try: current_course = Course.objects.get(id=course_id) if hasattr(request.user, 'userinformation'): user = request.user.userinformation if isinstance(user, UserInformation): context = current_course.as_context(user) else: context = current_course.as_context() else: context = current_course.as_context() session = request.session if 'enroll-error' in session: context['error'] = session['enroll-error'] del session['enroll-error'] return render( request, 'course/info.html', context ) except Course.DoesNotExist: return db_error(_('Requested course does not exist.'))
def profile(request, user_id=None): if user_id is None: if request.user.is_authenticated(): user = request.user template = 'user/profile.html' is_own = True else: return redirect('login') else: try: user = User.objects.get(id=user_id) is_own = request.user.id == user.id except User.DoesNotExist: return db_error(_('This user does not exist')) template = 'user/public-profile.html' return render( request, template, { 'course_list_show_subject': True, 'profiled_user': user, 'is_own': is_own, 'title': '{} {}'.format(user.first_name, user.last_name) } )
def course(request: HttpRequest, course_id: str): """ Controller for single course info page :param request: request object :param course_id: id for the course :return: """ try: current_course = Course.objects.get(id=course_id) if hasattr(request.user, 'userinformation'): user = request.user.userinformation if isinstance(user, UserInformation): context = current_course.as_context(user) else: context = current_course.as_context() else: context = current_course.as_context() return render( request, 'course/info.html', context ) except Course.DoesNotExist: return db_error(_('Requested course does not exist.'))
def edit(request, subjectname): try: subj = subject.Subject.objects.get(name=subjectname) except subject.Subject.DoesNotExist: return db_error(_('Requested subject does not exist.')) if request.method == 'POST': form = SubjectForm(request.POST, instance=subj) if 'cancel' not in request.POST and form.is_valid(): subj.save() return redirect('subject', subj.name) else: form = SubjectForm(instance=subj) return render( request, 'subject/edit.html', { 'title': subj.name, 'form': form, 'subject': subj } )
def edit(request: HttpRequest, news_id: str): """ Edit form for changing a news and handler for submitted data. :param request: request object :param news_id: id for the news :return: """ try: cur_news = news.News.objects.get(id=news_id) except news.News.DoesNotExist: return db_error(_('Requested News does not exist.')) if request.method == 'POST': form = NewsForm(request.POST, instance=cur_news) if form.is_valid(): cur_news.save() return redirect('index') else: form = NewsForm(instance=cur_news) return render(request, 'news/edit.html', { 'title': _('Edit News'), 'create': False, 'form': form, 'news_id': news_id })
def notify(request: HttpRequest, course_id): if request.method == 'POST': form = NotifyCourseForm(request.POST) if form.is_valid(): try: course = Course.objects.get(id=course_id) except Course.DoesNotExist: return db_error(_('Requested course does not exist.')) email = request.user.email show_sender = form.cleaned_data['show_sender'] and email if show_sender: content = form.content + CONTACT_FOOTER + email else: content = form.content + BLANK_FOOTER for student in itertools.chain(course.participants.all(), course.teacher.all()): student.user.email_user("[iFSR Course Manager] " + form.subject, content) return redirect('notify-course-done', course_id) else: form = NotifyCourseForm() return render( request, 'course/notify.html', { 'title': _('Notify Course'), 'form': form, 'course_id': course_id } )
def participants_list(request, course_id): try: current_course = Course.objects.get(id=course_id) return render(request, 'course/participants.html', {'course': current_course}) except Course.DoesNotExist: return db_error(_('Requested course does not exist.'))
def delete(request, subjectname): try: subj = subject.Subject.objects.get(name=subjectname) except subject.Subject.DoesNotExist: return db_error(_('Requested subject does not exist.')) subj.delete() return redirect('subject-overview')
def wrapped(request, poll_name, *args, **kwargs): try: cur_poll = Poll.objects.get(url=poll_name) except Poll.DoesNotExist: return db_error(_('This poll does not seem to exist, sorry.')) if cur_poll.is_owner(request.user.userinformation): return func(request, poll_name, *args, **kwargs) else: return redirect(reverse('login') + '?next=' + request.path)
def wrapped(request, course_id, *args, **kwargs): try: curr_course = Course.objects.get(id=course_id) if not isinstance(course_id, Course) else course_id except Course.DoesNotExist: return db_error(_('This course does not seem to exist, sorry.')) if has_teacher_permissions(request.user.userinformation, curr_course): return func(request, course_id, *args, **kwargs) else: return redirect(reverse('login')+'?next='+request.path)
def wrapped(request, poll_name, *args, **kwargs): try: cur_poll = Poll.objects.get(url=poll_name) except Poll.DoesNotExist: return db_error(_('This poll does not seem to exist, sorry.')) if cur_poll.is_owner(request.user.userinformation): return func(request, poll_name, *args, **kwargs) else: return redirect(reverse('login')+'?next='+request.path)
def vote(request, poll_name): """ Form for voting on a poll and handling the submitted results. """ try: poll = Poll.objects.get(url=poll_name) except Poll.DoesNotExist: return db_error(_('This poll does not exist')) if 'token' in request.GET: token = request.GET['token'] elif 'token' in request.POST: token = request.POST['token'] else: return redirect('poll-require-token', poll_name) def fail_token_verify(message): return render( request, 'polls/token/require.html', { 'target': reverse('poll-vote', args=(poll_name,)), 'error': _(message) } ) try: token = Token.objects.get(token=token, poll=poll) except Token.DoesNotExist: return fail_token_verify('This token is invalid (for this poll).') if token.used: return fail_token_verify('This token has already been used, please obtain a new one.') if request.method == 'POST': vote_interface = PollInterface(poll, request.POST) if vote_interface.is_valid(): token.used = True token.save() vote_interface.save() return redirect('poll-view', poll.url) else: vote_interface = PollInterface(poll) return render( request, 'polls/poll/vote.html', { 'vote_interface': vote_interface, 'token': token.token, 'poll': poll } )
def delete(request, course_id): try: course = Course.objects.get(id=course_id) except Course.DoesNotExist: return db_error(_('Requested course does not exist.')) subj = course.subject.name course.delete() return redirect('subject', subj)
def remove_slot(request, course_id, slot_id): try: course = Course.objects.get(id=course_id) except Course.DoesNotExist: return db_error( 'The specified course does not exist. Please try again. ' 'If this error persists, contact an administrator and include the url "{}"' ''.format(request.path)) try: course.schedule.slots.get(id=slot_id).delete() except (WeeklySlot.DoesNotExist, DateSlot.DoesNotExist): return db_error( 'The slot you\'re trying to remove does not exist. Please try again. ' 'If this error persists, contact an administrator and include the url "{}"' ''.format(request.path)) return redirect_unless_target(request, 'course-edit-slot', course_id)
def remove_teacher(request, course_id, teacher_id): try: curr_course = Course.objects.get(id=course_id) user = User.objects.get(id=teacher_id) curr_course.teacher.remove(user.userinformation) remove_perm( 'change_course', user, curr_course ) remove_perm( 'delete_course', user, curr_course ) except Course.DoesNotExist: return db_error(_('Requested course does not exist.')) except UserInformation.DoesNotExist: return db_error(_('Requested student does not exist.')) return redirect_unless_target(request, 'course', course_id)
def participants_list(request, course_id): try: current_course = Course.objects.get(id=course_id) return render( request, 'course/participants.html', {'course': current_course} ) except Course.DoesNotExist: return db_error(_('Requested course does not exist.'))
def remove_teacher(request, course_id, teacher_id): try: curr_course = Course.objects.get(id=course_id) userinfo = UserInformation.objects.get(id=teacher_id) curr_course.teacher.remove(userinfo) remove_perm( 'change_course', userinfo.user, curr_course ) remove_perm( 'delete_course', userinfo.user, curr_course ) except Course.DoesNotExist: return db_error(_('Requested course does not exist.')) except UserInformation.DoesNotExist: return db_error(_('Requested student does not exist.')) return redirect_unless_target(request, 'course', course_id)
def remove_question(request, poll_name, qlink_id, question_id): """ Remove the link between a question and a poll (also discards its results). """ poll = Poll.objects.get(url=poll_name) qlink = QLink.objects.get(id=qlink_id) if qlink.poll == poll and qlink.question.id == int(question_id): qlink.delete() return redirect('poll-edit-questions', poll.url) else: return db_error('Inconsistent query. Id\'s do not match.')
def remove_slot(request, course_id, slot_id): try: course = Course.objects.get(id=course_id) except Course.DoesNotExist: return db_error( 'The specified course does not exist. Please try again. ' 'If this error persists, contact an administrator and include the url "{}"' ''.format(request.path) ) try: course.schedule.slots.get(id=slot_id).delete() except (WeeklySlot.DoesNotExist, DateSlot.DoesNotExist): return db_error( 'The slot you\'re trying to remove does not exist. Please try again. ' 'If this error persists, contact an administrator and include the url "{}"' ''.format(request.path) ) return redirect_unless_target(request, 'course', course_id)
def vote(request, poll_name): """ Form for voting on a poll and handling the submitted results. """ try: poll = Poll.objects.get(url=poll_name) except Poll.DoesNotExist: return db_error(_('This poll does not exist')) if 'token' in request.GET: token = request.GET['token'] elif 'token' in request.POST: token = request.POST['token'] else: return redirect('poll-require-token', poll_name) def fail_token_verify(message): return render( request, 'polls/token/require.html', { 'target': reverse('poll-vote', args=(poll_name, )), 'error': _(message) }) try: token = Token.objects.get(token=token, poll=poll) except Token.DoesNotExist: return fail_token_verify('This token is invalid (for this poll).') if token.used: return fail_token_verify( 'This token has already been used, please obtain a new one.') if request.method == 'POST': vote_interface = PollInterface(poll, request.POST) if vote_interface.is_valid(): token.used = True token.save() vote_interface.save() return redirect('poll-view', poll.url) else: vote_interface = PollInterface(poll) return render(request, 'polls/poll/vote.html', { 'vote_interface': vote_interface, 'token': token.token, 'poll': poll })
def delete(request: HttpRequest, news_id: str): """ Delete a news entry. :param request: request object :param news_id: id for the news :return: """ try: cur_news = news.News.objects.get(id=news_id) except news.News.DoesNotExist: return db_error(_('Requested News does not exist.')) cur_news.delete() return redirect('index')
def enroll_response(request, course_id, action=None): session = request.session try: course = Course.objects.get(id=course_id) except Course.DoesNotExist: return db_error(_('Requested course does not exist.')) context = course.as_context(request.user) context['action'] = action context['subject'] = course.subject.name context['course_id'] = course_id if 'enroll-error' in session: context['error'] = session['enroll-error'] del session['enroll-error'] return render(request, 'enroll/response.html', context)
def remove(request, course_id): stud = request.user.userinformation try: course = Course.objects.get(id=course_id) except Course.DoesNotExist: return db_error(_('Requested course does not exist.')) ps = course.participants if course.is_participant(stud): ps.remove(stud) else: request.session['enroll-error'] = _('You do not seem to be enrolled in this course.') return redirect('unregister-course-done', course_id) # redirect to course overview or specified target return redirect_unless_target(request, 'unregister-course-done', course_id)
def remove(request, course_id): stud = request.user.userinformation try: course = Course.objects.get(id=course_id) except Course.DoesNotExist: return db_error(_('Requested course does not exist.')) ps = course.participants if course.is_participant(stud): ps.remove(stud) else: request.session['enroll-error'] = _( 'You do not seem to be enrolled in this course.') return redirect('unregister-course-done', course_id) # redirect to course overview or specified target return redirect_unless_target(request, 'unregister-course-done', course_id)
def add_teacher(request, course_id): context = { 'title': _('Edit Teachers'), 'course_id': course_id, 'target': reverse('add-teacher', args=(course_id,)) } try: curr_course = Course.objects.get(id=course_id) except Course.DoesNotExist: return db_error(_('Requested course does not exist.')) if request.method == 'POST': form = AddTeacherForm(request.POST) if form.is_valid(): try: user = User.objects.get(username=form.cleaned_data['username']) curr_course.teacher.add(user.userinformation) assign_perm( 'change_course', user, curr_course ) assign_perm( 'delete_course', user, curr_course ) return redirect('add-teacher', course_id) except User.DoesNotExist: context['error'] = _('The username you entered does not exist in ' 'my database, sorry :(') else: form = AddTeacherForm() context['form'] = form context['teachers'] = curr_course.teacher return render( request, 'course/teacher.html', context )
def add(request, course_id): user = request.user stud = user.userinformation try: course = Course.objects.get(id=course_id) except Course.DoesNotExist: return db_error(_('Requested course does not exist.')) session = request.session if course.is_participant(stud): session['enroll-error'] = _('You are already enrolled in this course.') else: if 'enroll-error' in session: del session['enroll-error'] course.enroll(stud) # redirect to course overview or specified target return redirect_unless_target(request, 'course', course_id)
def add_teacher(request, course_id): context = { 'title': _('Edit Teachers'), 'course_id': course_id, 'target': reverse('add-teacher', args=(course_id,)) } try: curr_course = Course.objects.get(id=course_id) except Course.DoesNotExist: return db_error(_('Requested course does not exist.')) if request.method == 'POST': form = AddTeacherForm(request.POST) if form.is_valid(): try: user = User.objects.get(username=form.cleaned_data['username']) curr_course.teacher.add(user.userinformation) assign_perm( 'change_course', user, curr_course ) assign_perm( 'delete_course', user, curr_course ) return redirect('add-teacher', course_id) except User.DoesNotExist: context['error'] = _('The username you entered does not exist.') else: form = AddTeacherForm() context['form'] = form context['teachers'] = curr_course.teacher return render( request, 'course/teachers.html', context )
def bump_question(request, poll_name, qlink_id, question_id, up=False): """ Move a question up or down in the poll. """ poll = Poll.objects.get(url=poll_name) qlink = QLink.objects.get(id=qlink_id) if qlink.poll == poll and qlink.question.id == int(question_id): try: old_obj = qlink.next_higher_to() if up else qlink.next_lower_to() pos = qlink.position qlink.position = old_obj.position old_obj.position = pos qlink.save() old_obj.save() except QLink.DoesNotExist: pass return redirect('poll-edit-questions', poll.url) else: return db_error('Inconsistent query.')
def attendee_list(request, course_id): if 'slots' in request.GET: try: course = Course.objects.get(id=course_id) except Course.DoesNotExist: return db_error(_('Requested course does not exist.')) # handle empty string (== no number) as input try: slots = int(request.GET['slots']) except ValueError: slots = 0 return render(request, 'course/attendee-list.html', { 'attendees': course.participants.all(), 'slots': range(slots) }) return render(request, 'course/attendee-list-select.html', {'course_id': course_id})
def edit_course(request: HttpRequest, course_id: str): """ Edit form for changing a course and handler for submitted data. :param request: request object :param course_id: id for the course :return: """ try: current_course = Course.objects.get(id=course_id) current_schedule = Schedule.objects.get(course_id=course_id) except Course.DoesNotExist: return db_error(_('Requested course does not exist.')) if request.method == "POST": form = CourseForm(request.POST, instance=current_course) if form.is_valid(): current_course.save() current_schedule.set_type(request.POST['schedule_type']) current_schedule.save() return redirect('course', course_id) else: # FIXME(feliix42): Manually setting the start & end date here is required beacuse I just can't get django to format the date correctly in the Form setup form = CourseForm(instance=current_course,initial={ 'schedule_type':current_schedule.get_type(), 'start_time': current_course.start_time.strftime('%d.%m.%Y'), 'end_time': current_course.end_time.strftime('%d.%m.%Y') }) return render( request, 'course/edit.html', { 'title': _('Edit course'), 'form': form, 'create': False, 'course_id': course_id, 'allowed_tags': html_clean.DESCR_ALLOWED_TAGS, 'course_is_active': current_course.active, } )
def toggle(request: HttpRequest, course_id: str, active: bool): """ Toggle course status (active/inactive) :param request: request information :param course_id: :param active: active/inactive :return: """ try: curr_course = Course.objects.get(id=course_id) except Course.DoesNotExist: return db_error(_('Requested course does not exist.')) curr_course.active = active if not active: curr_course.participants.clear() curr_course.queue.clear() curr_course.save() return redirect('course', course_id)
def notify(request: HttpRequest, course_id): if request.method == 'POST': form = NotifyCourseForm(request.POST) if form.is_valid(): try: course = Course.objects.get(id=course_id) except Course.DoesNotExist: return db_error(_('Requested course does not exist.')) email = request.user.email data = form.cleaned_data show_sender = data.get('show_sender', False) and email subject = data['subject'] content = data['content'] for student in itertools.chain(course.participants.all(), course.teacher.all()): if show_sender: student.user.email_user(subject, content, email) else: student.user.email_user(subject, content) return redirect('notify-course-done', course_id) else: form = NotifyCourseForm() return render( request, 'course/notify.html', { 'title': _('Notify Course'), 'form': form, 'course_id': course_id } )
def edit_slot(request: HttpRequest, course_id): try: course = Course.objects.get(id=course_id) except Course.DoesNotExist: return db_error(_('Requested course does not exist.')) schedule = course.schedule if schedule.is_weekly(): form_type = forms.WeeklySlotForm else: form_type = forms.DateForm if request.method == 'POST': form = form_type(request.POST) if form.is_valid(): slot = form.save(commit=False) slot.schedule = schedule slot.save() return redirect('course-edit-slot', course_id) else: form = form_type() return render( request, 'course/time.html', { 'title': _('Edit Schedule'), 'form': form, 'schedule': schedule, 'course_id': course_id, 'target': 'course-edit-slot', } )
def profile(request, user_id=None): attend = [] if user_id is None: if request.user.is_authenticated: user = request.user # require consenting to privacy policy if not user.userinformation.accepted_privacy_policy: return redirect('privacy-policy-updated') template = 'user/profile.html' is_own = True # construct a list of attended courses that are not yet archived attend = [(p.course, p.course.position_in_queue(user.userinformation)) for p in user.userinformation.participation_set.all() if p.course.archiving == 't'] else: return redirect('login') else: try: user = User.objects.get(id=user_id) is_own = request.user.id == user.id except User.DoesNotExist: return db_error(_('This user does not exist')) template = 'user/public_profile.html' return render( request, template, { 'course_list_show_subject': True, 'profiled_user': user, 'is_own': is_own, 'attend': attend, 'title': '{} {}'.format(user.first_name, user.last_name) })
def add(request, course_id): user = request.user stud = user.userinformation try: course = Course.objects.get(id=course_id) except Course.DoesNotExist: return db_error(_('Requested course does not exist.')) session = request.session if course.is_participant(stud): session['enroll-error'] = _('You are already enrolled in this course.') elif course.student_only and not stud.is_student(): session['enroll-error'] = _('Sorry, this course is for students only.') elif course.student_only and stud.is_pending_student(): session['enroll-error'] = _('Please verify your student status to enroll for this course.') elif not course.joinable: session['enroll-error'] = _('Sorry, this course is full.') else: if 'enroll-error' in session: del session['enroll-error'] course.participants.add(stud) # redirect to course overview or specified target return redirect_unless_target(request, 'register-course-done', course_id)
def notify(request: HttpRequest, course_id): if request.method == 'POST': form = NotifyCourseForm(request.POST) if form.is_valid(): try: course = Course.objects.get(id=course_id) except Course.DoesNotExist: return db_error(_('Requested course does not exist.')) email = request.user.email data = form.cleaned_data show_sender = data.get('show_sender', False) and email subject = data['subject'] content = data['content'] for student in itertools.chain(course.participants.all(), course.teacher.all()): if show_sender: student.user.email_user(subject, content, email) else: student.user.email_user(subject, content) return redirect('notify-course-done', course_id) else: form = NotifyCourseForm() return render(request, 'course/notify.html', { 'title': _('Notify Course'), 'form': form, 'course_id': course_id })