from django.shortcuts import render,HttpResponse from django.views import View from Fiskars.models import * from django.conf import settings from Fiskars.forms import * import os import xlrd class IndexView(View): def get(self,request): return render(request,'index.html') class UploadView(View): def get(self,request): obj = BalanceSheetForm() return render(request,'upload.html',{'obj':obj}) def post(self,request): if 'F1' in request.POST: result=uploadfile('F1',request) elif 'F2' in request.POST: result=uploadfile('F2',request) elif 'F0' in request.POST: obj=BalanceSheetForm(request.POST) if not obj.is_valid(): msg=obj.non_field_errors() else: msg='msg ok' obj.save() return render(request,'upload.html',{'msg':msg,'obj':obj}) return HttpResponse(result) def uploadfile(key,request): myFile=request.FILES.get('myfile',None) if not myFile: return HttpResponse('no file uploaded') f=open(os.path.join(settings.BASE_DIR,'upload',myFile.name),'wb+') for chunk in myFile.chunks(): f.write(chunk) f.close() wb=xlrd.open_workbook(os.path.join(settings.BASE_DIR,'upload',f.name)).sheet_by_index(0) records = [] err = '' if key=='F1': #balance sheet for i in range(1,wb.nrows): if AccountSheet.objects.filter(code=wb.cell(i,2).value).first() is not None: data=BalanceSheetForm({ 'begin_dr':wb.cell(i,5).value, 'begin_cr':wb.cell(i,6).value, 'happen_dr':wb.cell(i,7).value, 'happen_cr':wb.cell(i,8).value, 'end_dr':wb.cell(i,9).value, 'end_cr':wb.cell(i,10).value, 'accounttype':AccountSheet.objects.filter(code=wb.cell(i,2).value).first().accounttype.type, 'code':AccountSheet.objects.filter(code=wb.cell(i,2).value).first().id,#inquiry 'currency':'CNY', #AccountSheet.objects.filter(code=wb.cell(i,2).value).first().currency, 'group':request.POST.get('group'), #每次上传要改request.POST.get('group') 'period':PeriodSheet.objects.filter(year=wb.cell(i,1).value[-7:-3],month=int(wb.cell(i,1).value[-2:])).first().id }) if not data.is_valid(): err=err+'row'+str(i)+', '+data.non_field_errors()+'.' if i==wb.nrows-1: return err records.append(data) if len(records)>0: for each in records: each.save() return 'upload balance sheet successfully.' else: return 'no records in the uploaded BS file' elif key=='F2': #deprtment cost for a in range(1,wb.nrows): for b in range(5,wb.ncols): if AccountSheet.objects.filter(code=wb.cell(a,0).value).first() is not None and Department.objects.filter(code=wb.cell(0, b).value[:3]).first() is not None: y=request.POST.get('year') m=request.POST.get('month') g=request.POST.get('group') data=DepartmentCostForm({ 'cost':wb.cell(a+1,b).value, 'costaccount':AccountSheet.objects.filter(code=wb.cell(a,0).value).first().id, 'department':Department.objects.filter(code=wb.cell(0,b).value[:3]).first().id, 'period':PeriodSheet.objects.filter(year=2018,month=5).first().id, 'group':'MTD' }) if not data.is_valid(): err=err+'row'+str(a)+', '+data.non_field_errors()+', ' if a==wb.nrows-1: return err records.append(data) if len(records)>0: for each in records: each.save() return 'upload department cost successfully.' else: return 'no records in the uploaded dept cost file.' return 'no such submit button'