Source code for import.views

from django.shortcuts import render,redirect
from django.contrib import messages
from openpyxl import load_workbook,Workbook,utils
from openpyxl.utils.cell import coordinate_from_string, column_index_from_string,get_column_letter
from core.models import WineLocationValues,Style,DataLoads,Account
from .functions import write_errors,func_chk,wlv_data_load,create_template, wlv_stream
from .forms import UploadDataForm
from django.http import HttpResponse
from core.functions import addition_check,get_account_name

[docs] def populate_load(request): #Create Audit File # Create wlv template - can be used as a cellar audit file # see https://www.blog.pythonlibrary.org/2021/07/27/creating-spreadsheets-with-openpyxl-and-python/ #Added account 8 June account_name = get_account_name(request) account_id= Account.objects.get(account=account_name) # Block Guest Users from using this function if account_name == 'Guest': print('Guest Account this should redirect',account_name) print('Guest Account this should redirect',account_id) my_message = 'Guests not allowed to create Cellar Audits.' my_flag = 'E' context= ({'my_message' : my_message, 'my_flag' : my_flag, }) messages.error(request, my_message) response = redirect('home') return response if "GET" == request.method: # Create a new workbook wb = Workbook() ws = wb.active # name the worksheet ws.title = 'load' # Add WLV Headers to the worksheet ws['A1'] = 'Location Type' ws['B1'] = 'Location Name' ws['C1'] = 'Location ID' ws['D1'] = 'Wine' ws['E1'] = 'Wine ID' ws['F1'] = 'Quantity' ws['G1'] = 'Style' ws['H1'] = 'Style ID' ws['I1'] = 'Year' ws['J1'] = 'Country' ws['K1'] = 'Size' # Get WLV data for template export temp_export = WineLocationValues.objects.filter(account=account_id.id).order_by('location_type') #iterate through data and export each row for idx, x in enumerate(temp_export): # get the row - remember index starts at 0 row_val = idx+2 # Add a new row ws.insert_rows(row_val) ws_cella = 'A'+ str(row_val) ws[ws_cella] = x.location_type ws_cellb = 'B'+ str(row_val) ws[ws_cellb] = str(x.location) ws_cellc = 'C'+ str(row_val) ws[ws_cellc] = int(x.location_id) ws_celld = 'D'+ str(row_val) ws[ws_celld] = str(x.wine) ws_celle = 'E'+ str(row_val) ws[ws_celle] = int(x.wine_id) ws_cellf = 'F'+ str(row_val) ws[ws_cellf] = int(x.quantity) ws_cellg = 'G'+ str(row_val) ws[ws_cellg] = str(x.style) ws_cellh = 'H'+ str(row_val) ws[ws_cellh] = int(x.style_id) ws_celli = 'I'+ str(row_val) ws[ws_celli] = str(x.vintage) ws_cellj = 'J'+ str(row_val) ws[ws_cellj] = str(x.country) ws_cellk = 'K'+ str(row_val) ws[ws_cellk] = str(x.size) #print(idx,row_val,row,x) # Save the workbook response = HttpResponse(content_type='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet') response['Content-Disposition'] = 'attachment; filename="cellar_list.xlsx"' wb.save(response) #next = '/info/audit' #message="Cellar Audit File created - check downloads" return response
#wlv_stream(request) #return redirect(next) # redirect to put away list after process #return render(request, 'index.html', {})
[docs] def wlv_import(request):#Import the last uploaded Audit if "GET" == request.method: obj = DataLoads.objects.last() # Gets the last upload file_path = obj.excel.path print('file path = ',file_path) workbook =file_path sheet_name ='load' wlv_data_load(workbook,sheet_name) next = '/info/audit' message="Cellar Audit data updated" return redirect(next)
[docs] def upload_excel(request):#Upload the excel file if request.method == 'POST': form=UploadDataForm(request.POST,request.FILES) if form.is_valid(): form.save() message="Cellar Audit file loaded" next = 'wlv_import' messages.success(request, message) print(request.POST) return redirect(next) # redirect to put away list after process else: context = {'form':form} return render(request,'import/uploadform.html',context) context = {'form':UploadDataForm()} return render(request,'import/uploadform.html',context)
[docs] def test(request):#Validate the upload file addition_status,errors = addition_check() print ('value returned from addition_check = ',addition_status) if addition_status == 'errors': m='Errors found in system. These should be fixed asap. Use the link below to create error file ' link = 'true' else: m='no issues' link = 'false' context = {'message':m, 'link':link} return render(request,'import/test.html',context)
[docs] def create_errorfile(request): #passed a list of errors and writes to a response = HttpResponse(content_type='text/plain') response['Content-Disposition'] = 'attachment; filename="audit_error.txt"' addition_status,errors = addition_check() for item in errors: response.write(item) return response