Beispiel #1
0
    def post(self, request):
        # 上传的文件都在request.FILES里面获取,所以这里要多传一个这个参数
        excel_form = UploadExcelForm(request.POST, request.FILES)
        next_url = request.GET.get('next', '')
        if excel_form.is_valid():
            excel_file = excel_form.cleaned_data["excel_file"]
            case_file = File()
            case_file.excel_file = excel_file
            case_file.save()

            excel_file_name = os.path.join(MEDIA_ROOT,
                                           case_file.excel_file.name)
            wb = openpyxl.load_workbook(excel_file_name)
            sheet = wb.get_active_sheet()
            for i in range(1, sheet.max_row):
                record = Record()

                record.asin = sheet.cell(row=i + 1, column=1).value
                record.c_price = sheet.cell(row=i + 1, column=2).value
                record.purchase_cost = sheet.cell(row=i + 1, column=3).value
                record.product_profit = sheet.cell(row=i + 1, column=4).value
                record.product_upload_time = sheet.cell(row=i + 1,
                                                        column=5).value
                # record.brush_number = sheet.cell(row=i + 1, column=6).value
                record.sale_30_number = sheet.cell(row=i + 1, column=6).value
                record.sale_7_number = sheet.cell(row=i + 1, column=7).value
                record.record_src = sheet.cell(row=i + 1, column=8).value
                record.now_score = sheet.cell(row=i + 1, column=9).value
                record.now_review_number = sheet.cell(row=i + 1,
                                                      column=10).value
                review_type = sheet.cell(row=i + 1, column=11).value
                record.review_type = review_type
                if review_type == "留评":
                    record.direct_review = '0'
                    record.free_review_number = '0'
                    record.feedback = '0'
                elif review_type == "直评":
                    record.review_number = '0'
                    record.free_review_number = '0'
                    record.feedback = '0'
                elif review_type == "免评":
                    record.review_number = '0'
                    record.direct_review = '0'
                    record.feedback = '0'
                elif review_type == "feedback":
                    record.review_number = '0'
                    record.direct_review = '0'
                    record.free_review_number = '0'

                record.user = request.user
                record.save()

            messages.add_message(request,
                                 messages.SUCCESS,
                                 "文件上传成功",
                                 extra_tags='success')
            return redirect(next_url)
        else:
            messages.add_message(request,
                                 messages.ERROR,
                                 "表单验证错误",
                                 extra_tags='danger')
            return redirect(next_url)