def create(request): args = {} us = User.objects.exclude(username='******') args['us'] = us if request.method == 'POST' and request.POST.get('user'): args['post'] = True gongzi = Gongzi() u = User.objects.get(id=request.POST.get('user')) gongzi.user = u gongzi.year = request.POST.get('year', datetime.today().year) gongzi.month = request.POST.get('month', datetime.today().month) gongzi.xiangmu = request.POST.get('xiangmu', '') gongzi.yingfa = request.POST.get('yingfa', 0) gongzi.baoxian = request.POST.get('baoxian', 0) gongzi.gongjijin = request.POST.get('gongjijin', 0) gongzi.shuijin = request.POST.get('shuijin', 0) gongzi.shifa = request.POST.get('shifa', 0) gongzi.memo = request.POST.get('memo', '') gongzi.guilei = request.POST.get('guilei', '') gongzi.realname = u.first_name gongzi.idcard = u.last_name gongzi.dateline = datetime.now() gongzi.save() return render(request, 'gongzi/admin/create.html', args) return render(request, 'gongzi/admin/create.html', args)
def upload(request): err = [] this_month = datetime.now().month if request.method == 'POST': month = request.POST.get('month', None) year = request.POST.get('year', None) f = request.FILES.get('file', None) import os if month and year and f: des_path = os.path.abspath('.') + '/gongzi/uploads/upload.xls' des_f = open(des_path, "wb") for chunk in f.chunks(): des_f.write(chunk) des_f.close() bk = xlrd.open_workbook(des_path) sheet = bk.sheet_by_index(0) rows_num = sheet.nrows #cols_num = sheet.ncols #title = [] #rows = [] gs = [] for j in range(1, rows_num): try: g = Gongzi() g.user = User.objects.get(last_name=sheet.cell_value(j, 3)) g.realname = sheet.cell_value(j, 0) g.idcard = sheet.cell_value(j, 2) g.yingfa = smart_float(sheet.cell_value(j, 4)) g.baoxian = smart_float(sheet.cell_value(j, 5)) g.gongjijin = smart_float(sheet.cell_value(j, 6)) g.shuijin = smart_float(sheet.cell_value(j, 7)) g.shifa = smart_float(sheet.cell_value(j, 8)) g.xiangmu = sheet.cell_value(j, 2) g.memo = sheet.cell_value(j, 9) g.guilei = sheet.cell_value(j, 10) g.month = month g.year = year g.dateline = datetime.today() gs.append(g) except User.DoesNotExist: err.append(u'第%s行中身份证号在数据库中找不到对应人员,错误数据%s' % (j + 1, sheet.cell_value(j, 2))) if not err: Gongzi.objects.bulk_create(gs) else: err.append(u'表单每项都不能为空') return render(request, 'gongzi/admin/upload.html', {'month': this_month, 'err': err, 'post': True}) return render(request, 'gongzi/admin/upload.html', {'month': this_month, 'err': err})