def create(request, template_name="manage/student_create.html"): if request.method == "POST": form = StudentForm(request.POST, request.FILES, user=request.user) if form.is_valid(): student = helpers.create_student(form, request) if student.id: messages.success(request, u"学生 %s 成功创建" % student.name) return redirect("manage_student_view", student_id=student.id) else: form = StudentForm(user=request.user) ctx = {"form": form} ctx.update(csrf(request)) return render(request, template_name, ctx)
def update(request, student_id, template_name="manage/student_update.html"): """update a class""" student = get_object_or_404(Student, pk=student_id) if request.method == "POST": form = StudentForm(request.POST, request.FILES, instance=student, user=request.user) if form.is_valid(): # 保存手机信息 form.save() messages.success(request, u"已成功更新学生: %s " % student.name) return redirect("manage_student_view", student_id=student_id) else: # 表单默认手机 form = StudentForm(instance=student, user=request.user) ctx = {"form": form, "student": student} return render(request, template_name, ctx)
def check_import(request, template_name="oa/student_import.html"): schools = helpers.get_schools(request.user) roles_xls = request.FILES.get('students') error_list = [] num = 0 try: school_id = request.POST['school'] school = get_object_or_404(School, pk=school_id) except: school = schools[0] if not roles_xls: messages.error(request, _("Files Missing")) return render(request, template_name, {'schools':schools,'school':school}) try: wb = xlrd.open_workbook(file_contents=roles_xls.read()) s = wb.sheet_by_index(0) except xlrd.biffh.XLRDError: messages.error(request, _("Unsupported format, or corrupt file")) return render(request, template_name, {'schools':schools,'school':school}) try: trans_map = { "group": s.cell(0, 0).value, "name": s.cell(0, 1).value, "mobile": s.cell(0, 2).value } except: trans_map = None xls_cache = [] for row in range(s.nrows)[1:]: num = num + 1 try: mobile = str(int(s.cell(row, 2).value)).strip() except: mobile = '' try: class_name = s.cell(row, 0).value.strip() name = s.cell(row, 1).value.strip() except Exception, e: print e messages.error(request, u"导入的 Excel 文件缺少需要的列, 或者该列数据为空.") return redirect('oa_student_batch_import') try: group = Group.objects.get(name=class_name, school=school) except: error_list.append({'name':name,'mobile':mobile,'row':row,'group':class_name,'msg':'班级名称错误'}) continue if mobile: try: student = Student.objects.get(name=name, user__profile__mobile=mobile,group=group) error_list.append({'name':name,'mobile':mobile,'row':row,'group':class_name,'msg':"该学生已存在"}) continue except: pass initial_data = {"name": name, "mobile": mobile,'group':group.id} form = StudentForm(initial_data) if not form.is_valid(): error_list.append({'name':name,'mobile':mobile,'row':row,'group':class_name,'msg':form.errors}) continue xls_cache.append({'name':name,'mobile':mobile,'group':group.id})