def post(self): try: # try to create student from form values student = Student() student.studentid = int(self.request.get('studentid').strip()) student.first_name = self.request.get('first_name').strip().title() student.last_name = self.request.get('last_name').strip().title() student.ucinetid = self.request.get('ucinetid').strip().lower() student.email = self.request.get('email').strip().lower() student.lab = int(self.request.get('lab').strip()) student.quarter = int(self.request.get('quarter').strip()) student.year = int(self.request.get('year').strip()) student.active = True except Exception, e: return self.redirect('/admin?message=' + 'There was a problem adding that student: ' + str(e))
def post(self): try: file = self.request.get('the_file') file = file.strip().split('\n') quarter = int(self.request.get('quarter')) year = int(self.request.get('year')) num_labs = 0 # students to be uploaded students = [] # to check for duplicates in roster student_cache = set() # grab students in DB (active and not active) existing_students = StudentModel.get_students_by_active_status( quarter, year).fetch() existing_students += StudentModel.get_students_by_active_status( quarter, year, active=False).fetch() existing_students = dict( map(lambda x: (int(x.studentid), x), existing_students)) for row in file: row = row.split(',') # save highest lab number found if int(row[5]) > num_labs: num_labs = int(row[5]) # grab student ID from current row of CSV roster studentid = int(row[0].strip()) # if student w/ same studentid has already been found in roster, skip... if studentid in student_cache: continue # ...else, add them to 'seen' cache student_cache.add(studentid) # if student in DB, skip to next student... if studentid in existing_students: existing_student = existing_students[studentid] # ...unless they're inactive; if so, activate them... if not existing_student.active: existing_student.active = True students.append(existing_student) # ...or if they've switched labs; if so, update lab num... if existing_student.lab != int(row[5]): existing_student.lab = int(row[5]) students.append(existing_student) # ...to see which students have dropped the class, remove active students from existing_students del existing_students[studentid] # ...and continue to next student continue # create student object student = Student() student.studentid = studentid student.last_name = row[1].strip().title() student.last_name = row[1].strip('"').title() student.first_name = row[2].strip().title() student.first_name = row[2].strip('"').title() student.ucinetid = row[3].lower().strip() student.email = row[4].lower().strip() if not student.email.endswith("@uci.edu"): student.email = student.email + "@uci.edu" student.lab = int(row[5]) student.quarter = quarter student.year = year student.active = True students.append(student) # deactivate students who have dropped (students left in existing_students) for dropped in existing_students.values(): dropped.active = False students.append(dropped) # save student objects... ndb.put_multi(students) # ...save num_labs... setting = Setting.query().get() setting = setting if setting else Setting() setting.num_labs = num_labs setting.put() # ...and render the response return self.redirect('/admin/roster/view?message=' + 'Successfully uploaded new roster') except Exception, e: return self.redirect('/admin?message=' + 'There was a problem uploading the roster: ' + str(e))
def post(self): try: file = self.request.get('the_file') file = file.strip().split('\n') quarter = int(self.request.get('quarter')) year = int(self.request.get('year')) num_labs = 0 # students to be uploaded students = [] # to check for duplicates in roster student_cache = set() # grab students in DB (active and not active) existing_students = StudentModel.get_students_by_active_status(quarter, year).fetch() existing_students += StudentModel.get_students_by_active_status(quarter, year, active=False).fetch() existing_students = dict(map(lambda x: (int(x.studentid), x), existing_students)) for row in file: row = row.split(',') # save highest lab number found if int(row[5]) > num_labs: num_labs = int(row[5]) # grab student ID from current row of CSV roster studentid = int(row[0].strip()) # if student w/ same studentid has already been found in roster, skip... if studentid in student_cache: continue # ...else, add them to 'seen' cache student_cache.add(studentid) # if student in DB, skip to next student... if studentid in existing_students: existing_student = existing_students[studentid] # ...unless they're inactive; if so, activate them... if not existing_student.active: existing_student.active = True students.append(existing_student) # ...or if they've switched labs; if so, update lab num... if existing_student.lab != int(row[5]): existing_student.lab = int(row[5]) students.append(existing_student) # ...to see which students have dropped the class, remove active students from existing_students del existing_students[studentid] # ...and continue to next student continue # create student object student = Student() student.studentid = studentid student.last_name = row[1].strip().title() student.last_name = row[1].strip('"').title() student.first_name = row[2].strip().title() student.first_name = row[2].strip('"').title() student.ucinetid = row[3].lower().strip() student.email = row[4].lower().strip() student.lab = int(row[5]) student.quarter = quarter student.year = year student.active = True students.append(student) # deactivate students who have dropped (students left in existing_students) for dropped in existing_students.values(): dropped.active = False students.append(dropped) # save student objects... ndb.put_multi(students) # ...save num_labs... setting = Setting.query().get() setting = setting if setting else Setting() setting.num_labs = num_labs setting.put() # ...and render the response return self.redirect('/admin/roster/view?message=' + 'Successfully uploaded new roster') except Exception, e: return self.redirect('/admin?message=' + 'There was a problem uploading the roster: ' + str(e))