1
- import os
2
- import time
3
1
import pandas as pd
4
- import threading
5
- import io
6
- import re
7
-
8
- from werkzeug .utils import secure_filename
9
- from flask import flash , current_app
10
- from datasource_manager import CSV_HEADERS
11
- from datasource_manager import DATASOURCE_MAPPING
12
- from openpyxl import load_workbook
13
- from tempfile import NamedTemporaryFile
2
+ from config import engine
14
3
from donations_importer import validate_import_sfd
4
+ from flask import current_app
5
+ from models import ManualMatches , SalesForceContacts , ShelterluvPeople , Volgistics
15
6
from shifts_importer import validate_import_vs
7
+ from werkzeug .utils import secure_filename
16
8
17
- from constants import RAW_DATA_PATH
18
-
19
- SUCCESS_MSG = 'Uploaded Successfully!'
20
- lock = threading .Lock ()
9
+ SUCCESS_MSG = "Uploaded Successfully!"
21
10
22
11
23
12
def validate_and_arrange_upload (file ):
24
13
current_app .logger .info ("Start uploading file: " + file .filename )
25
14
filename = secure_filename (file .filename )
26
- file_extension = filename .rpartition ('.' )[2 ]
27
- determine_upload_type (file , file_extension )
15
+ file_extension = filename .rpartition ("." )[2 ]
16
+ with engine .begin () as conn :
17
+ determine_upload_type (file , file_extension , conn )
28
18
29
19
30
- def determine_upload_type (file , file_extension ):
31
- df = None
20
+ def determine_upload_type (file , file_extension , conn ):
21
+ # Yes, this method of discovering what kind of file we have by looking at
22
+ # the extension and columns is silly. We'd like to get more of our data from
23
+ # automatically pulling from vendor APIs directly, in which case we'd know
24
+ # what kind of data we had.
25
+ if file_extension == "csv" :
26
+ df = pd .read_csv (file , dtype = "string" )
32
27
33
- if file_extension == 'csv' :
34
- dfs = [pd .read_csv (io .BytesIO (file .stream .read ()), encoding = 'iso-8859-1' )]
35
- file .close ()
36
- else :
37
-
38
- match = re .search ('donat' , file .filename , re .I )
39
-
40
- if match : # It's a SalesForce Donations file
41
- validate_import_sfd (file )
28
+ if {"salesforcecontacts" , "volgistics" , "shelterluvpeople" }.issubset (df .columns ):
29
+ ManualMatches .insert_from_df (df , conn )
30
+ return
31
+ elif {"Animal_ids" , "Internal-ID" }.issubset (df .columns ):
32
+ ShelterluvPeople .insert_from_df (df , conn )
42
33
return
43
- else :
44
- match = re .search ('volunteer' , file .filename , re .I )
45
- if match : # It's a Volgistics file
46
- validate_import_vs (file )
47
- dfs = excel_to_dataframes (file ) # Also need to run Volgistics through match processing
48
- else :
49
- dfs = excel_to_dataframes (file ) # It's a non-Volgistics, non-Shelterluv XLS? file
50
-
51
-
52
-
53
-
54
- found_sources = 0
55
- for df in dfs :
56
- for src_type in CSV_HEADERS :
57
- if set (CSV_HEADERS [src_type ]).issubset (df .columns ):
58
- with lock :
59
- found_sources += 1
60
- filename = secure_filename (file .filename )
61
- now = time .localtime ()
62
- now_date = time .strftime ("%Y-%m-%d--%H-%M-%S" , now )
63
- current_app .logger .info (" -File: " + filename + " Matches files type: " + src_type )
64
- df .to_csv (os .path .join (RAW_DATA_PATH , src_type + '-' + now_date + '.csv' ))
65
- clean_current_folder (src_type )
66
- df .to_csv (os .path .join (RAW_DATA_PATH , src_type + '-' + now_date + '.csv' ))
67
- current_app .logger .info (" -Uploaded successfully as : " + src_type + '-' + now_date + '.' + file_extension )
68
- flash (src_type + " {0} " .format (SUCCESS_MSG ), 'info' )
69
- if found_sources == 0 :
70
- current_app .logger .error ("\n \n !!!!!!! No sources found in upload !!!! \n Uploaded file " + file .filename + " is probably from wrong report \n !!!!!!!!!!!" )
71
-
72
-
73
- def excel_to_dataframes (xls ):
74
- df = []
75
- wb = load_workbook (xls )
76
-
77
- if len (wb .sheetnames ) > 1 :
78
- with NamedTemporaryFile () as tmp :
79
- wb .save (tmp .name )
80
- for sheetname in wb .sheetnames :
81
- for item in DATASOURCE_MAPPING :
82
- if 'sheetname' in DATASOURCE_MAPPING [item ]:
83
- if DATASOURCE_MAPPING [item ]['sheetname' ] == sheetname :
84
- tmp .seek (0 )
85
- df .append (pd .read_excel (tmp .read (), sheetname ))
86
- else :
87
- df .append (pd .read_excel (xls ))
88
-
89
- return df
90
34
35
+ if file_extension == "xlsx" :
36
+ excel_file = pd .ExcelFile (file )
37
+ if {"Master" , "Service" }.issubset (excel_file .sheet_names ):
38
+ # Volgistics
39
+ validate_import_vs (file , conn )
40
+ Volgistics .insert_from_file (excel_file , conn )
41
+ return
91
42
92
- def clean_current_folder (src_type ):
93
- if os .listdir (RAW_DATA_PATH ):
94
- for file_name in os .listdir (RAW_DATA_PATH ):
95
- file_path = os .path .join (RAW_DATA_PATH , file_name )
96
- file_name_striped = file_path .split ('-' )[0 ].split ('/' )[- 1 ]
43
+ df = pd .read_excel (excel_file )
44
+ if "Contact ID 18" in df .columns :
45
+ # Salesforce something-or-other
46
+ if "Amount" in df .columns :
47
+ # Salesforce donations
48
+ validate_import_sfd (file , conn )
49
+ return
50
+ else :
51
+ # Salesforce contacts
52
+ SalesForceContacts .insert_from_file_df (df , conn )
53
+ return
97
54
98
- if file_name_striped == src_type :
99
- current_app .logger .info ('File to remove: ' + file_path )
100
- os .remove (file_path )
101
- current_app .logger .info (" -Removed file: " + file_name + " from Current files folder" )
55
+ current_app .logger .error (f"Don't know how to process file { file .filename } " )
0 commit comments