def isCompleted(self): return Record.user_completed( user=get_current_request().user, event="med", program=self.program) \ or Record.user_completed( user=get_current_request().user, event="med_bypass", program=self.program)
def barcodecheckin(self, request, tl, one, two, module, extra, prog): context = {} if request.method == 'POST': results = {'not_found': [], 'existing': [], 'new': [], 'not_student': []} form = OnsiteBarcodeCheckinForm(request.POST) if form.is_valid(): codes=form.cleaned_data['uids'].split() for code in codes: try: result=ESPUser.objects.filter(id=code) except ValueError: results['not_found'].append(code) if len(result) > 1: raise ESPError(False), "Something weird happened, there are two students with ID %s." % code elif len(result) == 0: results['not_found'].append(code) else: student=result[0] if student.isStudent(): existing = Record.user_completed(student, 'attended', prog) if existing: results['existing'].append(code) else: new = Record(user=student, program=prog, event='attended') new.save() results['new'].append(code) else: results['not_student'].append(code) else: results = {} form=OnsiteBarcodeCheckinForm() context['module'] = self context['form'] = form context['results'] = results return render_to_response(self.baseDir()+'barcodecheckin.html', request, context)
def isCheckedIn(self, when=None, teacher=None): if when is None: when = self.now if teacher is None: teacher = self.teacher return Record.user_completed(teacher, self.event, self.program, when, only_today=True)
def checkIn(self, teacher, prog, when=None): """Check teacher into program for the rest of the day (given by 'when'). 'when' defaults to datetime.now().""" if when is None: when = datetime.now() if teacher.getTaughtClassesFromProgram(prog).exists(): endtime = datetime(when.year, when.month, when.day) + timedelta( days=1, seconds=-1) checked_in_already = Record.user_completed(teacher, 'teacher_checked_in', prog, when, only_today=True) if not checked_in_already: Record.objects.create(user=teacher, event='teacher_checked_in', program=prog, time=when) return '%s is checked in until %s.' % (teacher.name(), str(endtime)) else: return '%s has already been checked in until %s.' % ( teacher.name(), str(endtime)) else: return '%s is not a teacher for %s.' % (teacher.name(), prog.niceName())
def createBit(self, extension): if extension == 'Paid': self.updatePaid(True) if Record.user_completed(self.student, extension.lower(), self.program): return False else: Record.objects.create(user=self.student, event=extension.lower(), program=self.program) return True
def createBit(self, extension): if extension == 'Paid': self.updatePaid(True) if Record.user_completed(self.student, extension.lower(), self.program): return False else: Record.objects.create( user = self.student, event = extension.lower(), program = self.program ) return True
def checkIn(self, teacher, prog, when=None): """Check teacher into program for the rest of the day (given by 'when'). 'when' defaults to datetime.now().""" if when is None: when = datetime.now() if teacher.getTaughtClassesFromProgram(prog).exists(): endtime = datetime(when.year, when.month, when.day) + timedelta(days=1, seconds=-1) checked_in_already = Record.user_completed(teacher, 'teacher_checked_in', prog, when, only_today=True) if not checked_in_already: Record.objects.create(user=teacher, event='teacher_checked_in', program=prog, time=when) return '%s is checked in until %s.' % (teacher.name(), str(endtime)) else: return '%s has already been checked in until %s.' % (teacher.name(), str(endtime)) else: return '%s is not a teacher for %s.' % (teacher.name(), prog.niceName())
def barcodecheckin(self, request, tl, one, two, module, extra, prog): context = {} if request.method == 'POST': results = { 'not_found': [], 'existing': [], 'new': [], 'not_student': [] } form = OnsiteBarcodeCheckinForm(request.POST) if form.is_valid(): codes = form.cleaned_data['uids'].split() for code in codes: try: student = ESPUser.objects.get(id=code) except (ValueError, ESPUser.DoesNotExist): results['not_found'].append(code) continue if student.isStudent(): existing = Record.user_completed( student, 'attended', prog) if existing: results['existing'].append(code) else: new = Record(user=student, program=prog, event='attended') new.save() results['new'].append(code) else: results['not_student'].append(code) else: results = {} form = OnsiteBarcodeCheckinForm() context['module'] = self context['form'] = form context['results'] = results return render_to_response(self.baseDir() + 'barcodecheckin.html', request, context)
def user_completed(when=None, only_today=False): return Record.user_completed(self.user, self.event, program, when, only_today)
def survey_view(request, tl, program, instance): try: prog = Program.by_prog_inst(program, instance) except Program.DoesNotExist: raise Http404 user = ESPUser(request.user) if (tl == 'teach' and not user.isTeacher()) or (tl == 'learn' and not user.isStudent()): raise ESPError( False ), 'You need to be a program participant (i.e. student or teacher, not parent or educator) to participate in this survey. Please contact the directors directly if you have additional feedback.' if request.GET.has_key('done'): return render_to_response('survey/completed_survey.html', request, {'prog': prog}) if tl == 'learn': event = "student_survey" else: event = "teacher_survey" if Record.user_completed(user, event, prog): raise ESPError( False ), "You've already filled out the survey. Thanks for responding!" surveys = prog.getSurveys().filter(category=tl).select_related() if request.REQUEST.has_key('survey_id'): try: s_id = int(request.REQUEST['survey_id']) surveys = surveys.filter( id=s_id ) # We want to filter, not get: ID could point to a survey that doesn't exist for this program, or at all except ValueError: pass if len(surveys) < 1: raise ESPError(False), "Sorry, no such survey exists for this program!" if len(surveys) > 1: return render_to_response( 'survey/choose_survey.html', request, { 'surveys': surveys, 'error': request.POST } ) # if request.POST, then we shouldn't have more than one survey any more... survey = surveys[0] if request.POST: response = SurveyResponse() response.survey = survey response.save() r = Record(user=user, event=event, program=prog, time=datetime.datetime.now()) r.save() response.set_answers(request.POST, save=True) return HttpResponseRedirect(request.path + "?done") else: questions = survey.questions.filter(per_class=False).order_by('seq') perclass_questions = survey.questions.filter(per_class=True) classes = sections = timeslots = [] if tl == 'learn': classes = user.getEnrolledClasses(prog, request) timeslots = prog.getTimeSlots().order_by('start') for ts in timeslots: # The order by string really means "title" ts.classsections = prog.sections().filter( meeting_times=ts).exclude( meeting_times__start__lt=ts.start).order_by( 'parent_class__title').distinct() for sec in ts.classsections: if user in sec.students(): sec.selected = True elif tl == 'teach': classes = user.getTaughtClasses(prog) sections = user.getTaughtSections(prog).order_by( 'parent_class__title') context = { 'survey': survey, 'questions': questions, 'perclass_questions': perclass_questions, 'program': prog, 'classes': classes, 'sections': sections, 'timeslots': timeslots, } return render_to_response('survey/survey.html', request, context)
def test_survey(self): # Pick a student and sign them up for a class student = random.choice(self.students) sec = random.choice(self.program.sections()) sec.preregister_student(student) self.assertTrue( self.client.login(username=student.username, password='******'), "Couldn't log in as student %s" % student.username) # Access the survey page - there should be no surveys and we should get an error response = self.client.get('/learn/%s/survey' % self.program.url) self.assertEqual(response.status_code, 500) self.assertIn('no such survey', response.content) # Create a survey (survey, created) = Survey.objects.get_or_create(name='Test Survey', program=self.program, category='learn') (text_qtype, created) = QuestionType.objects.get_or_create(name='yes-no response') (number_qtype, created) = QuestionType.objects.get_or_create(name='numeric rating', is_numeric=True, is_countable=True) (question_base, created) = Question.objects.get_or_create(survey=survey, name='Question1', question_type=text_qtype, per_class=False, seq=0) (question_perclass, created) = Question.objects.get_or_create(survey=survey, name='Question2', question_type=text_qtype, per_class=True, seq=1) (question_number, created) = Question.objects.get_or_create(survey=survey, name='Question3', question_type=number_qtype, per_class=True, seq=2) # Make sure the user is marked as not having completed it self.assertFalse( Record.user_completed(student, 'student_survey', self.program)) # Now we should be able to access the survey response = self.client.get('/learn/%s/survey' % self.program.url) self.assertEqual(response.status_code, 200) self.assertIn('Question1', response.content) self.assertIn('Question2', response.content) self.assertIn('Question3', response.content) # Check that there is a field to select a class for each timeslot for timeslot in self.program.getTimeSlots(): self.assertIn('attendance_%d' % (timeslot.id), response.content) # Fill out the survey with some arbitrary answers sec_timeslot = sec.get_meeting_times()[0] form_settings = { 'attendance_%d' % sec_timeslot.id: '%s' % sec.id, 'question_%d' % question_base.id: 'Yes', 'question_%d_%d' % (question_perclass.id, sec_timeslot.id): 'No', 'question_%d_%d' % (question_number.id, sec_timeslot.id): '3', } # Submit the survey response = self.client.post('/learn/%s/survey' % self.program.url, form_settings, follow=True) self.assertEqual(response.status_code, 200) self.assertEqual(len(response.redirect_chain), 1) self.assertTrue(response.redirect_chain[0][0].endswith( '/learn/%s/survey?done' % self.program.url)) self.assertEqual(response.redirect_chain[0][1], 302) self.assertIn('have been saved', response.content) # Check that we have a SurveyRecord with the right answers associated with it self.assertEqual( SurveyResponse.objects.filter(survey=survey).count(), 1) record = SurveyResponse.objects.filter(survey=survey)[0] self.assertEqual( Answer.objects.filter(question=question_base).count(), 1) answer_base = Answer.objects.filter(question=question_base)[0] self.assertEqual(answer_base.answer, 'Yes') self.assertEqual(answer_base.target, self.program) self.assertEqual( Answer.objects.filter(question=question_perclass).count(), 1) answer_perclass = Answer.objects.filter(question=question_perclass)[0] self.assertEqual(answer_perclass.answer, 'No') self.assertEqual(answer_perclass.target, sec) answer_number = Answer.objects.filter(question=question_number)[0] self.assertEqual(answer_number.answer, '3') self.assertEqual(answer_number.target, sec) # Check that the student is marked as having filled out the survey self.assertTrue( Record.user_completed(student, 'student_survey', self.program)) # Check that we get an error if we try to visit the survey again response = self.client.get('/learn/%s/survey' % self.program.url) self.assertEqual(response.status_code, 500) self.assertIn('already filled out', response.content) # student logs out, teacher logs in self.client.logout() teacher = sec.parent_class.get_teachers()[0] self.client.login(username=teacher.username, password='******') # teacher should see per-class questions only response = self.client.get('/teach/%s/survey/review' % self.program.url) self.assertContains(response, 'Question2') self.assertContains(response, 'Question3') self.assertNotContains(response, 'Question1')
def hasPaid(self): iac = IndividualAccountingController(self.program, self.student) return Record.user_completed(self.student, "paid", self.program) or \ iac.has_paid(in_full=True)
def hasLiability(self): return Record.user_completed(self.student, "liab", self.program)
def hasMedical(self): return Record.user_completed(self.student, "med", self.program)
def survey_view(request, tl, program, instance): try: prog = Program.by_prog_inst(program, instance) except Program.DoesNotExist: raise Http404 user = request.user if (tl == 'teach' and not user.isTeacher()) or (tl == 'learn' and not user.isStudent()): raise ESPError('You need to be a program participant (i.e. student or teacher, not parent or educator) to participate in this survey. Please contact the directors directly if you have additional feedback.', log=False) if 'done' in request.GET: return render_to_response('survey/completed_survey.html', request, {'prog': prog}) if tl == 'learn': event = "student_survey" else: event = "teacher_survey" if Record.user_completed(user, event ,prog): raise ESPError("You've already filled out the survey. Thanks for responding!", log=False) surveys = prog.getSurveys().filter(category = tl).select_related() if 'survey_id' in request.GET: try: s_id = int( request.GET['survey_id'] ) surveys = surveys.filter(id=s_id) # We want to filter, not get: ID could point to a survey that doesn't exist for this program, or at all except ValueError: pass if len(surveys) < 1: raise ESPError("Sorry, no such survey exists for this program!", log=False) if len(surveys) > 1: return render_to_response('survey/choose_survey.html', request, { 'surveys': surveys, 'error': request.POST }) # if request.POST, then we shouldn't have more than one survey any more... survey = surveys[0] if request.POST: response = SurveyResponse() response.survey = survey response.save() r = Record(user=user, event=event, program=prog, time=datetime.datetime.now()) r.save() response.set_answers(request.POST, save=True) return HttpResponseRedirect(request.path + "?done") else: questions = survey.questions.filter(per_class = False).order_by('seq') perclass_questions = survey.questions.filter(per_class = True) classes = sections = timeslots = [] if tl == 'learn': classes = user.getEnrolledClasses(prog) enrolled_secs = user.getEnrolledSections(prog) timeslots = prog.getTimeSlots().order_by('start') for ts in timeslots: # The order by string really means "title" ts.classsections = prog.sections().filter(meeting_times=ts).exclude(meeting_times__start__lt=ts.start).order_by('parent_class__title').distinct() for sec in ts.classsections: if sec in enrolled_secs: sec.selected = True elif tl == 'teach': classes = user.getTaughtClasses(prog) sections = user.getTaughtSections(prog).order_by('parent_class__title') context = { 'survey': survey, 'questions': questions, 'perclass_questions': perclass_questions, 'program': prog, 'classes': classes, 'sections': sections, 'timeslots': timeslots, } return render_to_response('survey/survey.html', request, context)
def test_survey(self): # Pick a student and sign them up for a class student = random.choice(self.students) sec = random.choice(self.program.sections()) sec.preregister_student(student) self.assertTrue( self.client.login( username=student.username, password='******' ), "Couldn't log in as student %s" % student.username ) # Access the survey page - there should be no surveys and we should get an error response = self.client.get('/learn/%s/survey' % self.program.url) self.assertEqual(response.status_code, 500) self.assertIn('no such survey', response.content) # Create a survey (survey, created) = Survey.objects.get_or_create(name='Test Survey', program=self.program, category='learn') (text_qtype, created) = QuestionType.objects.get_or_create(name='yes-no response') (number_qtype, created) = QuestionType.objects.get_or_create(name='numeric rating', is_numeric=True, is_countable=True) (question_base, created) = Question.objects.get_or_create(survey=survey, name='Question1', question_type=text_qtype, per_class=False, seq=0) (question_perclass, created) = Question.objects.get_or_create(survey=survey, name='Question2', question_type=text_qtype, per_class=True, seq=1) (question_number, created) = Question.objects.get_or_create(survey=survey, name='Question3', question_type=number_qtype, per_class=True, seq=2) # Make sure the user is marked as not having completed it self.assertFalse(Record.user_completed(student, 'student_survey', self.program)) # Now we should be able to access the survey response = self.client.get('/learn/%s/survey' % self.program.url) self.assertEqual(response.status_code, 200) self.assertIn('Question1', response.content) self.assertIn('Question2', response.content) self.assertIn('Question3', response.content) # Check that there is a field to select a class for each timeslot for timeslot in self.program.getTimeSlots(): self.assertIn('attendance_%d' % (timeslot.id), response.content) # Fill out the survey with some arbitrary answers sec_timeslot = sec.get_meeting_times()[0] form_settings = { 'attendance_%d' % sec_timeslot.id: '%s' % sec.id, 'question_%d' % question_base.id: 'Yes', 'question_%d_%d' % (question_perclass.id, sec_timeslot.id): 'No', 'question_%d_%d' % (question_number.id, sec_timeslot.id): '3', } # Submit the survey response = self.client.post('/learn/%s/survey' % self.program.url, form_settings, follow=True) self.assertEqual(response.status_code, 200) self.assertEqual(len(response.redirect_chain), 1) self.assertTrue(response.redirect_chain[0][0].endswith('/learn/%s/survey?done' % self.program.url)) self.assertEqual(response.redirect_chain[0][1], 302) self.assertIn('have been saved', response.content) # Check that we have a SurveyRecord with the right answers associated with it self.assertEqual(SurveyResponse.objects.filter(survey=survey).count(), 1) record = SurveyResponse.objects.filter(survey=survey)[0] self.assertEqual(Answer.objects.filter(question=question_base).count(), 1) answer_base = Answer.objects.filter(question=question_base)[0] self.assertEqual(answer_base.answer, 'Yes') self.assertEqual(answer_base.target, self.program) self.assertEqual(Answer.objects.filter(question=question_perclass).count(), 1) answer_perclass = Answer.objects.filter(question=question_perclass)[0] self.assertEqual(answer_perclass.answer, 'No') self.assertEqual(answer_perclass.target, sec) answer_number = Answer.objects.filter(question=question_number)[0] self.assertEqual(answer_number.answer, '3') self.assertEqual(answer_number.target, sec) # Check that the student is marked as having filled out the survey self.assertTrue(Record.user_completed(student, 'student_survey', self.program)) # Check that we get an error if we try to visit the survey again response = self.client.get('/learn/%s/survey' % self.program.url) self.assertEqual(response.status_code, 500) self.assertIn('already filled out', response.content) # student logs out, teacher logs in self.client.logout() teacher = sec.parent_class.get_teachers()[0] self.client.login(username=teacher.username, password='******') # teacher should see per-class questions only response = self.client.get('/teach/%s/survey/review' % self.program.url) self.assertContains(response, 'Question2') self.assertContains(response, 'Question3') self.assertNotContains(response, 'Question1')
def hasAttended(self): return Record.user_completed(self.student, "attended", self.program)