def test_default_all_system(self): today = util.today() y = today.year term = util.get_term(today) sec = Section(year=y, term=term, number=1, start_date=util.today()) sec2 = Section(year=y, term=term, number=2, start_date=util.today()) sec3 = Section(year=2019, term=term, number=3, start_date=util.today()) partner.db.session.add(sec) partner.db.session.add(sec2) partner.db.session.add(sec3) partner.db.session.commit() response = self.app.get('/rest/sections') assert 200 == response.status_code json_data = response.get_json() assert 2 == len(json_data) s1 = json_data[0] s2 = json_data[1] assert s1['number'] == sec.number assert s1['year'] == sec.year assert s1['term'] == sec.term assert s1['start_date'] == util.date_to_mdy(sec.start_date) assert s2['number'] == sec2.number assert s2['year'] == sec2.year assert s2['term'] == sec2.term assert s2['start_date'] == util.date_to_mdy(sec2.start_date)
def attendance_csv(): secId = request.args.get('secId') if secId: r = Roster.query.filter_by( section_id=secId).first_or_404() #type: Roster sec = Section.query.filter_by(id=r.section_id).first_or_404() csv = AttendanceMgr.generate_attendance(r.sorted_students(), sec.start_date, util.today()) filename = 'lab' + str( sec.number) + '_' + sec.title + '_' + util.today().strftime( '%y%m%d' + '.csv') return Response( csv, mimetype="text/csv", headers={"Content-Disposition": "attachment;filename=" + filename})
def sections(): app.logger.debug("Inside sections") headers = request.headers print(headers) sec_id = request.args.get('id') year = request.args.get('year') term = request.args.get('term') date = request.args.get( 'date' ) # an optional parameter which, if given, will include attendance data for the date if date: date = util.parse_date(date) else: date = util.today() if not year: year = date.year if not term: term = util.get_term(date) if sec_id: sections = Section.query.filter_by(id=sec_id).all() else: sections = Section.query.filter_by(year=year, term=term).all() sections = SectionMgr.get_sections_with_roster_data(sections, date) return jsonify(sections)
def roster_csv (): secId = request.args.get('secId') if secId: r = models.Roster.query.filter_by(section_id=secId).first_or_404() #type: Roster sec = models.Section.query.filter_by(id=r.section_id).first_or_404() else: flash('Must provide secId argument. Select a lab section') return redirect(url_for('roster_admin')) year = util.get_current_year() term = util.get_current_term() sec = models.Section.query.filter_by(year=year, term=term).first_or_404() r = models.Roster.query.filter_by(section_id=sec.id).first_or_404() csv = AttendanceMgr.generate_attendance(r.sorted_students(), sec.start_date, util.today()) filename = 'lab' + str(sec.number) + '_' + sec.title + '_' + util.today().strftime('%y%m%d' + '.csv') return Response(csv, mimetype="text/csv", headers={"Content-Disposition": "attachment;filename=" + filename})
def test_get_by_term(self): today = util.today() y = today.year sec = Section(year=y, term='spring', number=1, start_date=today) sec2 = Section(year=2019, term='fall', number=2, start_date=util.today()) partner.db.session.add(sec) partner.db.session.add(sec2) partner.db.session.commit() t = sec.term response = self.app.get(f'/rest/sections?term={t}') assert 200 == response.status_code json_data = response.get_json() assert 1 == len(json_data) s1 = json_data[0] assert s1['number'] == sec.number assert s1['year'] == sec.year assert s1['term'] == sec.term assert s1['start_date'] == util.date_to_mdy(sec.start_date)
def test_get_by_id(self): sec = Section(year=2020, term='spring', number=1, start_date=util.today()) sec2 = Section(year=2019, term='fall', number=2, start_date=util.today()) partner.db.session.add(sec) partner.db.session.add(sec2) partner.db.session.commit() id = sec.id # lookup by id does not use default term or year response = self.app.get(f'/rest/sections?id={id}') assert 200 == response.status_code json_data = response.get_json() assert 1 == len(json_data) s1 = json_data[0] assert s1['number'] == sec.number assert s1['year'] == sec.year assert s1['term'] == sec.term assert s1['start_date'] == util.date_to_mdy(sec.start_date)
def _create_section(secj, file, term, year): start_date = secj.get('start_date') if start_date: start_date = util.str_to_date(start_date) else: start_date = util.today() newsec = Section(title=secj.get('short_title', ''), number=secj.get('number', 0), term=term, year=year, start_date=start_date) db.session.add(newsec) if secj.get('fileIncluded'): RosterToDb.process_roster_file_upload(file, newsec) return newsec
def test_get_by_year(self): today = util.today() y = today.year term = util.get_term(today) sec = Section(year=y, term=term, number=1, start_date=today) sec2 = Section(year=2019, term='fall', number=2, start_date=util.today()) partner.db.session.add(sec) partner.db.session.add(sec2) partner.db.session.commit() yr = sec.year # today's month will be used to figure out a term for default in lookup response = self.app.get(f'/rest/sections?year{yr}') assert 200 == response.status_code json_data = response.get_json() # its no longer 2019 so we should only get back one section for today's year assert 1 == len(json_data) s1 = json_data[0] assert s1['number'] == sec.number assert s1['year'] == sec.year assert s1['term'] == sec.term assert s1['start_date'] == util.date_to_mdy(sec.start_date)
def roster_admin(): tab = request.form.get('tab') year = request.form.get('year') or request.args.get('year') if not year: year = util.get_current_year() term = request.form.get('term') or request.args.get('term') if not term: term = util.get_current_term() section = None dt = request.args.get('date') # mm/dd/yyyy format if not dt: date = util.today() dt = util.date_to_mdy(date) if request.method == 'POST': if tab == 'section': return process_admin_section_form(year, term, dt, request) else: sec_id = request.form.get('labId') if not sec_id: flash('Please select a section') return redirect(url_for('roster_admin', term=request.form.get('term'), year=request.form.get('year'))) section = Section.query.filter_by(id=sec_id).first_or_404() section.start_date = util.mdy_to_date(request.form.get('startDate')) file = request.files['file'] # if user does not select file, browser also # submit an empty part without filename # if file.filename == '': # flash('No selected file') # return redirect(request.url) process_roster_file_upload(file, section) db.session.commit() return redirect(url_for('roster_admin', section_id=section.id)) #TODO alter rosters_page to take a roster as input rather than meeting_time # return redirect(url_for('rosters_page',meeting_time='wed1')) # show the roster page for the created roster else: sec_id = request.args.get('section_id') if sec_id: section = Section.query.filter_by(id=sec_id).first_or_404() dt = util.date_to_mdy(section.start_date) if section.start_date else dt sections = Section.query.filter_by(year=year, term=term).all() return render_template('admin.html', title='admin', tab='rosters', year=year, term=term, section=section, dt=dt, sections=sections)
def rosters_page(): year = util.get_current_year() term = util.get_current_term() lab_num = request.args.get('lab_number') dt = request.args.get('date') # mm/dd/yyyy format if not dt: date = util.today() dt = util.date_to_mdy(date) else: date = util.parse_date(dt) year = date.year sections = models.Section.query.filter_by(year=year, term=term).all() sec = models.Section.query.filter_by(year=year, term=term, number=lab_num).first() if not sec: # TODO set error message so that 404 indicates missing resource is the sections for term/year sec = models.Section.query.filter_by(year=year, term=term).first_or_404() r = models.Roster.query.filter_by(section_id=sec.id).first_or_404("Roster not found for lab {}. You need to create it from spreadsheet in admin page".format(sec.number)) students = r.sorted_students() AttendanceMgr.set_attendance_status(students, date) groups = GroupGenerator().get_existing_groups(r, date) return render_template('roster.html', title='Attendance', dt=dt, section=sec, sections=sections, groups=groups, roster=r, students=students, num_students=len(list(r.students)))
def rosters(): year = request.args.get('year') term = request.args.get('term') number = request.args.get('number') date = request.args.get( 'date' ) # an optional parameter which, if given, will include attendance data for the date if date: date = util.parse_date(date) else: date = util.today() if not year: year = date.year if not term: term = util.get_term(date) if not number: number = 1 sec = Section.query.filter_by(year=year, term=term, number=number).first_or_404() roster = sec.roster students = roster.sorted_students() AttendanceMgr.set_attendance_status(students, date) return jsonify(sec.roster.to_dict(students))
def get_groups(): sec_id = request.args.get('secId') dt = request.args.get('date') format = request.args.get('format') sec = Section.query.filter_by(id=sec_id).first_or_404() r = sec.roster if dt: date = util.parse_date(dt) else: date = util.today() if format == 'csv': csv = GroupGenerator().get_groups_csv( r.id, date) # list of csv strings where each holds students in a group filename = 'labgroups' + str( sec.number) + '_' + sec.title + '_' + date.strftime('%y%m%d' + '.csv') return Response( csv, mimetype="text/csv", headers={"Content-Disposition": "attachment;filename=" + filename}) else: groups = Group.query.filter_by(roster_id=r.id, date=date).all() return jsonify([g.to_dict() for g in groups])
def rosters_post(): json = request.get_json() if not json: return abort(400) sec_id = json.get('secId') dt = json.get('date') if not dt or not sec_id: return abort(400) sec = Section.query.filter_by(id=sec_id).first_or_404() r = sec.roster if dt: date = util.mdy_to_date(dt) else: date = util.today() students = json.get('students', []) # caller is sending a sorted list status_codes = [s['status'] for s in students] name_edits = [True if s.get('edited') else False for s in students] if True in name_edits: names = [s['full_name'] for s in students] # names = [s['preferred_fname'] + ' ' + s['last_name'] for s in students ] AttendanceMgr.update_student_names2(r, name_edits, names) AttendanceMgr.update_attendance(r, date, status_codes) db.session.commit() return jsonify({})