Как уменьшить метод post с двумя формами и двумя кнопками отправки?
У меня есть две формы и две кнопки submbit. Потому что у меня есть форма submit для pdf и есть submit для excel.
Так я выделяю в views.py формы следующим образом:
def post(self, request):
filter_text = FilterText()
extract_excel_instance = ExtractingTextFromExcel()
types_of_encoding = ["utf8", "cp1252"]
submitted_form = ProfileForm(request.POST, request.FILES)
content = ''
content_excel = ''
if request.POST.get('form_pdf') is not None:
if submitted_form.is_valid() and request.POST:
uploadfile = UploadFile(image=request.FILES["upload_file"])
uploadfile.save()
for encoding_type in types_of_encoding:
with open(os.path.join(settings.MEDIA_ROOT, f"{uploadfile.image}"), 'r', encoding=encoding_type) as f:
if uploadfile.image.path.endswith('.pdf'):
content = filter_text.show_extracted_data_from_file(
uploadfile.image.path)
else:
content = f.read()
return render(request, "main/controle_punt140.html", {
'form': ProfileForm(),
"content": content
})
return render(request, "main/controle_punt140.html", {
"form": submitted_form,
})
if request.POST.get('form_excel') is not None:
if submitted_form.is_valid() and request.POST:
uploadfile = UploadFile(image=request.FILES["upload_file"])
uploadfile.save()
for encoding_type in types_of_encoding:
with open(os.path.join(settings.MEDIA_ROOT, f"{uploadfile.image}"), 'r', encoding=encoding_type) as f:
if uploadfile.image.path.endswith('.excel'):
content_excel = extract_excel_instance.calulate_total_fruit_NorthMidSouth(
uploadfile.image.path)
else:
content_excel = f.read()
return render(request, "main/controle_punt140.html", {
'form': ProfileForm(),
"content_excel": content_excel
})
return render(request, "main/controle_punt140.html", {
"form": submitted_form,
})
Но, как вы можете видеть, это почти идентичный код.
Вопрос: как я могу уменьшить это?