File size: 4,086 Bytes
8c710bc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
05e18e1
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
from flask import Flask, render_template, request, send_file, redirect, url_for, flash
import os
from werkzeug.utils import secure_filename
import corelate

app = Flask(__name__)
app.secret_key = os.urandom(24)

UPLOAD_FOLDER = os.path.dirname(os.path.abspath(__file__))
ALLOWED_EXTENSIONS = {'txt','csv'}

app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
app.config['MAX_CONTENT_LENGTH'] = 16 * 1024 * 1024

def allowed_file(filename):
    return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS

@app.route('/')
def index():
    report1_exists = os.path.exists(corelate.outputfile1)
    report2_exists = os.path.exists(corelate.outputfile2)
    errors_exist = os.path.exists(corelate.errorfile)
    
    report1_data = []
    report2_data = []
    error_data = []
    
    if report1_exists:
        with open(corelate.outputfile1, 'r') as f:
            for line in f:
                parts = line.strip().split('\t')
                if len(parts) >= 3:
                    report1_data.append({
                        'zipcode': parts[0],
                        'customers': parts[1],
                        'distances': parts[2]
                    })
    
    if report2_exists:
        with open(corelate.outputfile2, 'r') as f:
            for line in f:
                parts = line.strip().split('\t')
                if len(parts) >= 2:
                    report2_data.append({
                        'distance': parts[0],
                        'customers': parts[1]
                    })
    
    if errors_exist:
        with open(corelate.errorfile, 'r') as f:
            error_data = f.readlines()
    
    return render_template('index.html', 
                           report1_data=report1_data,
                           report2_data=report2_data,
                           error_data=error_data,
                           report1_exists=report1_exists,
                           report2_exists=report2_exists,
                           errors_exist=errors_exist)

@app.route('/run-script', methods=['GET'])
def run_script():
    try:
        corelate.process_data()
        flash('Script executed successfully!', 'success')
    except Exception as e:
        flash(f'Error running script: {str(e)}', 'error')
    
    return redirect(url_for('index'))

@app.route('/download/<report_type>')
def download_file(report_type):
    if report_type == 'report1':
        return send_file(corelate.outputfile1, as_attachment=True)
    elif report_type == 'report2':
        return send_file(corelate.outputfile2, as_attachment=True)
    elif report_type == 'errors':
        return send_file(corelate.errorfile, as_attachment=True)
    else:
        flash('Invalid report type requested', 'error')
        return redirect(url_for('index'))

@app.route('/upload', methods=['POST'])
def upload_file():
    if 'file' not in request.files:
        flash('No file part', 'error')
        return redirect(url_for('index'))
    
    file = request.files['file']
    file_type = request.form.get('file_type')
    
    if file.filename == '':
        flash('No file selected', 'error')
        return redirect(url_for('index'))
    
    if file and allowed_file(file.filename):
        filename = secure_filename(file.filename)
        
        if file_type == 'zipcodedata':
            target_file = corelate.statedatafile
        elif file_type == 'ziplist':
            target_file = corelate.ziplist
        elif file_type == 'servicecenters':
            target_file = corelate.servicecenters
        else:
            flash('Invalid file type', 'error')
            return redirect(url_for('index'))
        
        file.save(os.path.join(app.config['UPLOAD_FOLDER'], target_file))
        flash(f'File successfully uploaded and replaced {target_file}', 'success')
    else:
        flash('File type not allowed. Please upload .txt files only.', 'error')
    
    return redirect(url_for('index'))

if __name__ == "__main__":
    import os
    port = int(os.environ.get("PORT", "7860"))
    app.run(host="0.0.0.0", port=port, debug=False)