-
Notifications
You must be signed in to change notification settings - Fork 0
/
operation.py
149 lines (123 loc) · 5.25 KB
/
operation.py
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import os, random, uuid, pathlib
from flask import Blueprint, Flask, flash, request, redirect, url_for, render_template
from flask_login import login_required, current_user
from werkzeug.utils import secure_filename
from .models import User, Data
from . import UPLOAD_FOLDER,db
operation = Blueprint('operation', __name__)
class sessiondata():
ext = None
session_id = None
ALLOWED_EXTENSIONS = {'txt', 'pdf', 'png', 'jpg', 'jpeg', 'gif','mp3','mp4','wav'}
sesdata = sessiondata()
def cekNomor(nama, ext):
nomor = 1
path = pathlib.Path(os.path.join(
UPLOAD_FOLDER, nama, str(nomor) + '.' + ext))
while path.exists():
nomor = nomor + 1
path = pathlib.Path(os.path.join(
UPLOAD_FOLDER, nama, str(nomor) + '.' + ext))
return nomor
def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
@operation.route('/analyze', methods=['POST','GET'])
@login_required
def analyze():
sesdata.session_id = str(uuid.uuid4())
print(sesdata.session_id)
if request.method == 'POST':
if 'file' not in request.files:
flash('No file part')
return redirect(request.url)
file = request.files['file']
sesdata.ext = file.filename.rsplit('.', 1)[1].lower()
# if user does not select file, browser also
# submit an empty part without filename
if file.filename == '':
flash('No selected file')
return redirect(request.url)
if file and allowed_file(file.filename):
filename = secure_filename("temp" + sesdata.session_id + "." + sesdata.ext)
file.save(os.path.join(
UPLOAD_FOLDER, filename))
return redirect(url_for('operation.newprofile')) #redirect to process page
else:
flash('Incorrect File Extension')
return redirect(url_for('operation.analyze')) #reload the page
return render_template('analyze.html')
@operation.route('/analyze', methods=['POST', 'GET'])
@login_required
def process():
return render_template('analyze.html')
@operation.route('/train', methods=['POST','GET'])
@login_required
def traindata():
sesdata.session_id = str(uuid.uuid4())
print(sesdata.session_id)
if request.method == 'POST':
if 'file' not in request.files:
flash('No file part')
return redirect(request.url)
file = request.files['file']
sesdata.ext = file.filename.rsplit('.', 1)[1].lower()
# if user does not select file, browser also
# submit an empty part without filename
if file.filename == '':
flash('No selected file')
return redirect(request.url)
if file and allowed_file(file.filename):
filename = secure_filename("temp" + sesdata.session_id + "." + sesdata.ext)
file.save(os.path.join(
UPLOAD_FOLDER, filename))
return redirect(url_for('operation.newprofile')) #redirect to process page
else:
flash('Ekstensi File Tidak Cocok')
return redirect(url_for('operation.traindata')) #reload the page
return render_template('train.html')
@operation.route('/newprofile', methods=['GET', 'POST'])
@login_required
def newprofile():
if request.method == 'POST':
global nama, gender, usia, asal
if request.form['action'] == 'Submit':
try:
nama = request.form['1.nama']
gender = request.form['2.gender']
usia = request.form['3.usia']
asal = request.form['4.daerah']
except:
pass
pathnama = pathlib.Path(os.path.join(UPLOAD_FOLDER, nama))
# # Check if name exists
nama_pernah = Data.query.filter_by(nama=nama).first() # if this returns a user, then the email already exists in database
# Check Missing Items
if nama == "" or not nama_pernah:
req = request.form
missing = list()
for k, v in req.items():
print(k, v)
if v == "":
missing.append(k)
if missing:
flash(f"Missing fields for {', '.join(missing)}")
return redirect(request.url)
nomor = str(cekNomor(nama, sesdata.ext))
if not pathnama.exists():
new_profile = Data(nama=nama, gender=gender, usia=usia, asal=asal)
db.session.add(new_profile)
db.session.commit()
os.makedirs(pathnama)
print('data inserted')
os.rename(os.path.join(UPLOAD_FOLDER, "temp" + sesdata.session_id + "." + sesdata.ext), os.path.join(UPLOAD_FOLDER, nama, nomor + '.' + sesdata.ext))
flash("Data Inserted")
return redirect(url_for('main.profilelist'))
elif request.form['action'] == 'Cancel':
flash("Data Is Not Inserted")
try:
os.remove(os.path.join(UPLOAD_FOLDER, "temp" + sesdata.session_id + "." + sesdata.ext))
return redirect(url_for('main.index'))
except:
return redirect(url_for('main.index'))
return render_template('newprofile.html')