def _test(self, state, discarded, expected): template = Template(r'{% load training_progress %}' r'{% progress_label p %}') training_progress = TrainingProgress(state=state, discarded=discarded) context = Context({'p': training_progress}) got = template.render(context) self.assertEqual(got, expected)
def training_progress(request): homework_form = SendHomeworkForm() # Add information about instructor training progress to request.user. request.user = Person.objects \ .annotate_with_instructor_eligibility() \ .prefetch_related(Prefetch( 'badges', to_attr='instructor_badges', queryset=Badge.objects.instructor_badges()), ).get(pk=request.user.pk) progresses = request.user.trainingprogress_set.filter(discarded=False) last_swc_homework = progresses.filter( requirement__name='SWC Homework').order_by('-created_at').first() request.user.swc_homework_in_evaluation = (last_swc_homework is not None and last_swc_homework.state == 'n') last_dc_homework = progresses.filter( requirement__name='DC Homework').order_by('-created_at').first() request.user.dc_homework_in_evaluation = (last_dc_homework is not None and last_dc_homework.state == 'n') last_lc_homework = progresses.filter( requirement__name='LC Homework').order_by('-created_at').first() request.user.lc_homework_in_evaluation = (last_lc_homework is not None and last_lc_homework.state == 'n') if request.method == 'POST': homework_form = SendHomeworkForm(data=request.POST) if homework_form.is_valid(): # read homework type from POST hw_type = homework_form.cleaned_data['requirement'] # create "empty" progress object and fill out progress = TrainingProgress( trainee=request.user, state='n', # not evaluated yet requirement=hw_type, ) # create virtual form to validate and save form = SendHomeworkForm(data=request.POST, instance=progress) if form.is_valid(): form.save() messages.success( request, "Your homework submission will be " "evaluated soon.") return redirect(reverse('training-progress')) context = { 'title': 'Your training progress', 'homework_form': homework_form, } return render(request, 'dashboard/training_progress.html', context)
def training_progress(request): homework_form = SendHomeworkForm() # Add information about instructor training progress to request.user. request.user = ( Person.objects.annotate_with_instructor_eligibility().prefetch_related( Prefetch( "badges", to_attr="instructor_badges", queryset=Badge.objects.instructor_badges(), ), ).get(pk=request.user.pk)) progresses = request.user.trainingprogress_set.filter(discarded=False) last_swc_homework = (progresses.filter( requirement__name="SWC Homework").order_by("-created_at").first()) request.user.swc_homework_in_evaluation = (last_swc_homework is not None and last_swc_homework.state == "n") last_dc_homework = (progresses.filter( requirement__name="DC Homework").order_by("-created_at").first()) request.user.dc_homework_in_evaluation = (last_dc_homework is not None and last_dc_homework.state == "n") last_lc_homework = (progresses.filter( requirement__name="LC Homework").order_by("-created_at").first()) request.user.lc_homework_in_evaluation = (last_lc_homework is not None and last_lc_homework.state == "n") if request.method == "POST": homework_form = SendHomeworkForm(data=request.POST) if homework_form.is_valid(): # read homework type from POST hw_type = homework_form.cleaned_data["requirement"] # create "empty" progress object and fill out progress = TrainingProgress( trainee=request.user, state="n", # not evaluated yet requirement=hw_type, ) # create virtual form to validate and save form = SendHomeworkForm(data=request.POST, instance=progress) if form.is_valid(): form.save() messages.success( request, "Your homework submission will be " "evaluated soon.") return redirect(reverse("training-progress")) context = { "title": "Your training progress", "homework_form": homework_form, } return render(request, "dashboard/training_progress.html", context)
def test_basic(self): self._test( progress=TrainingProgress( state='p', evaluated_by=self.spiderman, trainee=self.ironman, created_at=datetime(2016, 5, 1, 16, 00), requirement=TrainingRequirement(name='Discussion')), expected='Passed Discussion<br />' 'evaluated by Peter Q. Parker<br />' 'on Sunday 01 May 2016 at 16:00.', )
def test_no_mentor_or_examiner_assigned(self): self._test( progress=TrainingProgress( state='p', evaluated_by=None, trainee=self.ironman, created_at=datetime(2016, 5, 1, 16, 00), requirement=TrainingRequirement(name='Discussion'), ), expected='Passed Discussion<br />' 'submitted<br />' 'on Sunday 01 May 2016 at 16:00.', )
def test_discarded(self): self._test( progress=TrainingProgress( state="p", discarded=True, evaluated_by=self.spiderman, trainee=self.ironman, created_at=datetime(2016, 5, 1, 16, 00), requirement=TrainingRequirement(name="Discussion"), ), expected="Discarded Passed Discussion<br />" "evaluated by Peter Q. Parker<br />" "on Sunday 01 May 2016 at 16:00.", )
def test_notes(self): self._test( progress=TrainingProgress( state="p", evaluated_by=self.spiderman, trainee=self.ironman, created_at=datetime(2016, 5, 1, 16, 00), requirement=TrainingRequirement(name="Discussion"), notes="Additional notes", ), expected="Passed Discussion<br />" "evaluated by Peter Q. Parker<br />" "on Sunday 01 May 2016 at 16:00.<br />" "Notes: Additional notes", )
def all_trainees(request): filter = TraineeFilter( request.GET, queryset=Person.objects.annotate_with_instructor_eligibility( ).prefetch_related( Prefetch('task_set', to_attr='training_tasks', queryset=Task.objects.filter(role__name='learner', event__tags__name='TTT')), 'training_tasks__event', 'trainingrequest_set', 'trainingprogress_set', 'trainingprogress_set__requirement', 'trainingprogress_set__evaluated_by', ).annotate( is_swc_instructor=Sum( Case(When(badges__name='swc-instructor', then=1), default=0, output_field=IntegerField())), is_dc_instructor=Sum( Case(When(badges__name='dc-instructor', then=1), default=0, output_field=IntegerField())), is_lc_instructor=Sum( Case(When(badges__name='lc-instructor', then=1), default=0, output_field=IntegerField())), ).order_by('family', 'personal')) trainees = get_pagination_items(request, filter.qs) if request.method == 'POST' and 'discard' in request.POST: # Bulk discard progress of selected trainees form = BulkAddTrainingProgressForm() discard_form = BulkDiscardProgressesForm(request.POST) if discard_form.is_valid(): for trainee in discard_form.cleaned_data['trainees']: TrainingProgress.objects.filter(trainee=trainee)\ .update(discarded=True) messages.success( request, 'Successfully discarded progress of ' 'all selected trainees.') # Raw uri contains GET parameters from django filters. We use it # to preserve filter settings. return redirect(request.get_raw_uri()) elif request.method == 'POST' and 'submit' in request.POST: # Bulk add progress to selected trainees instance = TrainingProgress(evaluated_by=request.user) form = BulkAddTrainingProgressForm(request.POST, instance=instance) discard_form = BulkDiscardProgressesForm() if form.is_valid(): for trainee in form.cleaned_data['trainees']: TrainingProgress.objects.create( trainee=trainee, evaluated_by=request.user, requirement=form.cleaned_data['requirement'], state=form.cleaned_data['state'], discarded=False, event=form.cleaned_data['event'], url=form.cleaned_data['url'], notes=form.cleaned_data['notes'], ) messages.success( request, 'Successfully changed progress of ' 'all selected trainees.') return redirect(request.get_raw_uri()) else: # GET request # If the user filters by training, we want to set initial values for # "requirement" and "training" fields. training_id = request.GET.get('training', None) or None try: initial = { 'event': Event.objects.get(pk=training_id), 'requirement': TrainingRequirement.objects.get(name='Training') } except Event.DoesNotExist: # or there is no `training` GET parameter initial = None form = BulkAddTrainingProgressForm(initial=initial) discard_form = BulkDiscardProgressesForm() context = { 'title': 'Trainees', 'all_trainees': trainees, 'swc': Badge.objects.get(name='swc-instructor'), 'dc': Badge.objects.get(name='dc-instructor'), 'lc': Badge.objects.get(name='lc-instructor'), 'filter': filter, 'form': form, 'discard_form': discard_form } return render(request, 'trainings/all_trainees.html', context)
def all_trainees(request): filter = TraineeFilter( request.GET, queryset=all_trainees_queryset(), ) trainees = get_pagination_items(request, filter.qs) if request.method == "POST" and "discard" in request.POST: # Bulk discard progress of selected trainees form = BulkAddTrainingProgressForm() discard_form = BulkDiscardProgressesForm(request.POST) if discard_form.is_valid(): for trainee in discard_form.cleaned_data["trainees"]: TrainingProgress.objects.filter(trainee=trainee).update( discarded=True) messages.success( request, "Successfully discarded progress of " "all selected trainees.") # Raw uri contains GET parameters from django filters. We use it # to preserve filter settings. return redirect(request.get_raw_uri()) elif request.method == "POST" and "submit" in request.POST: # Bulk add progress to selected trainees instance = TrainingProgress(evaluated_by=request.user) form = BulkAddTrainingProgressForm(request.POST, instance=instance) discard_form = BulkDiscardProgressesForm() if form.is_valid(): for trainee in form.cleaned_data["trainees"]: TrainingProgress.objects.create( trainee=trainee, evaluated_by=request.user, requirement=form.cleaned_data["requirement"], state=form.cleaned_data["state"], discarded=False, event=form.cleaned_data["event"], url=form.cleaned_data["url"], notes=form.cleaned_data["notes"], ) messages.success( request, "Successfully changed progress of " "all selected trainees.") return redirect(request.get_raw_uri()) else: # GET request # If the user filters by training, we want to set initial values for # "requirement" and "training" fields. training_id = request.GET.get("training", None) or None try: initial = { "event": Event.objects.get(pk=training_id), "requirement": TrainingRequirement.objects.get(name="Training"), } except Event.DoesNotExist: # or there is no `training` GET parameter initial = None form = BulkAddTrainingProgressForm(initial=initial) discard_form = BulkDiscardProgressesForm() context = { "title": "Trainees", "all_trainees": trainees, "swc": Badge.objects.get(name="swc-instructor"), "dc": Badge.objects.get(name="dc-instructor"), "lc": Badge.objects.get(name="lc-instructor"), "filter": filter, "form": form, "discard_form": discard_form, } return render(request, "trainings/all_trainees.html", context)
def training_progress(request): swc_form = SendHomeworkForm(submit_name='swc-submit') dc_form = SendHomeworkForm(submit_name='dc-submit') # Add information about instructor training progress to request.user. request.user = Person.objects \ .annotate_with_instructor_eligibility() \ .prefetch_related(Prefetch( 'badges', to_attr='instructor_badges', queryset=Badge.objects.instructor_badges()), ).get(pk=request.user.pk) progresses = request.user.trainingprogress_set.filter(discarded=False) last_swc_homework = progresses.filter( requirement__name='SWC Homework').order_by('-created_at').first() request.user.swc_homework_in_evaluation = (last_swc_homework is not None and last_swc_homework.state == 'n') last_dc_homework = progresses.filter( requirement__name='DC Homework').order_by('-created_at').first() request.user.dc_homework_in_evaluation = (last_dc_homework is not None and last_dc_homework.state == 'n') if request.method == 'POST' and 'swc-submit' in request.POST: requirement = TrainingRequirement.objects.get(name='SWC Homework') progress = TrainingProgress( trainee=request.user, state='n', # not-evaluated yet requirement=requirement) swc_form = SendHomeworkForm(data=request.POST, instance=progress, submit_name='swc-submit') dc_form = SendHomeworkForm(submit_name='dc-submit') if swc_form.is_valid(): swc_form.save() messages.success( request, 'Your homework submission will be ' 'evaluated soon.') return redirect(reverse('training-progress')) elif request.method == 'POST' and 'dc-submit' in request.POST: requirement = TrainingRequirement.objects.get(name='DC Homework') progress = TrainingProgress( trainee=request.user, state='n', # not-evaluated yet requirement=requirement) swc_form = SendHomeworkForm(submit_name='swc-submit') dc_form = SendHomeworkForm(data=request.POST, instance=progress, submit_name='dc-submit') if dc_form.is_valid(): dc_form.save() messages.success( request, 'Your homework submission will be ' 'evaluated soon.') return redirect(reverse('training-progress')) else: # GET request pass context = { 'title': 'Your training progress', 'swc_form': swc_form, 'dc_form': dc_form, } return render(request, 'dashboard/training_progress.html', context)
def _setUpInstructors(self): # prepare data # 1 SWC/DC/LC instructor self.instructor1 = Person.objects.create( personal="Instructor1", family="Instructor1", email="*****@*****.**", username="******", ) Award.objects.create(person=self.instructor1, badge=self.swc_instructor, awarded=date(2014, 1, 1)) self.instructor2 = Person.objects.create( personal="Instructor2", family="Instructor2", email="*****@*****.**", username="******", ) Award.objects.create(person=self.instructor2, badge=self.dc_instructor, awarded=date(2014, 1, 1)) self.instructor3 = Person.objects.create( personal="Instructor3", family="Instructor3", email="*****@*****.**", username="******", ) Award.objects.create(person=self.instructor3, badge=self.lc_instructor, awarded=date(2014, 1, 1)) # 1 combined instructor (SWC-DC-LC) self.instructor4 = Person.objects.create( personal="Instructor4", family="Instructor4", email="*****@*****.**", username="******", ) Award.objects.create(person=self.instructor4, badge=self.swc_instructor, awarded=date(2014, 1, 1)) Award.objects.create(person=self.instructor4, badge=self.dc_instructor, awarded=date(2014, 1, 1)) Award.objects.create(person=self.instructor4, badge=self.lc_instructor, awarded=date(2014, 1, 1)) # 1 eligible trainee with no instructor badges self.trainee1 = Person.objects.create( personal="Trainee1", family="Trainee1", email="*****@*****.**", username="******", ) TrainingProgress.objects.bulk_create([ TrainingProgress( trainee=self.trainee1, evaluated_by=None, requirement=self.training, state="p", # passed ), TrainingProgress( trainee=self.trainee1, evaluated_by=None, requirement=self.discussion, state="p", ), TrainingProgress( trainee=self.trainee1, evaluated_by=None, requirement=self.swc_homework, state="p", ), TrainingProgress( trainee=self.trainee1, evaluated_by=None, requirement=self.dc_homework, state="p", ), TrainingProgress( trainee=self.trainee1, evaluated_by=None, requirement=self.lc_demo, state="p", ), ]) # 1 eligible trainee with instructor badge self.trainee2 = Person.objects.create( personal="Trainee2", family="Trainee2", email="*****@*****.**", username="******", ) TrainingProgress.objects.bulk_create([ TrainingProgress( trainee=self.trainee2, evaluated_by=None, requirement=self.training, state="p", # passed ), TrainingProgress( trainee=self.trainee2, evaluated_by=None, requirement=self.discussion, state="p", ), TrainingProgress( trainee=self.trainee2, evaluated_by=None, requirement=self.dc_homework, state="p", ), TrainingProgress( trainee=self.trainee2, evaluated_by=None, requirement=self.swc_demo, state="p", ), TrainingProgress( trainee=self.trainee2, evaluated_by=None, requirement=self.lc_demo, state="p", ), ]) Award.objects.create(person=self.trainee2, badge=self.lc_instructor, awarded=date(2014, 1, 1)) # 1 non-eligible trainee self.trainee3 = Person.objects.create( personal="Trainee3", family="Trainee3", email="*****@*****.**", username="******", ) TrainingProgress.objects.bulk_create([ TrainingProgress( trainee=self.trainee3, evaluated_by=None, requirement=self.training, state="p", # passed ), TrainingProgress( trainee=self.trainee3, evaluated_by=None, requirement=self.discussion, state="f", # failed notes="Failed", ), TrainingProgress( trainee=self.trainee3, evaluated_by=None, requirement=self.lc_homework, state="p", ), TrainingProgress( trainee=self.trainee3, evaluated_by=None, requirement=self.lc_demo, state="p", ), ])