-
Notifications
You must be signed in to change notification settings - Fork 0
/
EmpApp.py
110 lines (81 loc) · 3.01 KB
/
EmpApp.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
#!/usr/bin/env python3
from flask import Flask, render_template, request
from pymysql import connections
import os
import boto3
from config import *
app = Flask(__name__)
bucket = custombucket
region = customregion
db_conn = connections.Connection(
host=customhost,
port=3306,
user=customuser,
password=custompass,
db=customdb
)
output = {}
table = 'employees'
@app.route("/", methods=['GET', 'POST'])
def home():
return render_template('AddEmp.html')
@app.route("/about", methods=['GET'])
def about():
return render_template('www.intellipaat.com')
@app.route("/addempoutput", methods=['POST'])
def addempoutput():
return render_template('AddEmp.html')
@app.route("/getempoutput", methods=['GET'])
def getempoutput():
return render_template('GetEmpOutput.html')
@app.route("/getemp", methods=['GET', 'POST'])
def getemp():
cursor = db_conn.cursor()
cursor.execute("SELECT * FROM employees WHERE emp_id=%s", (id))
cursor.fetchone()
db_conn.commit()
return render_template('GetEmp.html')
@app.route("/addemp", methods=['POST'])
def AddEmp():
emp_id = request.form.get("emp_id")
first_name = request.form.get("first_name")
last_name = request.form.get("last_name")
pri_skill = request.form.get("pri_skill")
location = request.form.get("location")
emp_image_file = request.files.get("emp_image_file")
cursor = db_conn.cursor()
cursor.execute(
"CREATE TABLE IF NOT EXISTS employees(emp_id int UNSIGNED PRIMARY KEY NOT NULL, first_name VARCHAR(20), last_name VARCHAR(20), pri_skill VARCHAR(20), location VARCHAR(30));")
db_conn.commit()
insert_sql = "INSERT INTO employees VALUES (%s, %s, %s, %s, %s)"
cursor = db_conn.cursor()
if emp_image_file.filename == "":
return "Please select a file"
try:
cursor.execute(insert_sql, (emp_id, first_name, last_name, pri_skill, location))
db_conn.commit()
emp_name = "" + first_name + " " + last_name
# Uplaod image file in S3 #
emp_image_file_name_in_s3 = "emp-id-" + str(emp_id) + "_image_file"
s3 = boto3.resource('s3')
try:
print("Data inserted in MySQL RDS... uploading image to S3...")
s3.Bucket(custombucket).put_object(Key=emp_image_file_name_in_s3, Body=emp_image_file)
bucket_location = boto3.client('s3').get_bucket_location(Bucket=custombucket)
s3_location = (bucket_location['LocationConstraint'])
if s3_location is None:
s3_location = ''
else:
s3_location = '-' + s3_location
object_url = "https://s3{0}.amazonaws.com/{1}/{2}".format(
s3_location,
custombucket,
emp_image_file_name_in_s3)
except Exception as e:
return str(e)
finally:
cursor.close()
print("all modification done...")
return render_template('AddEmpOutput.html', name=emp_name)
if __name__ == '__main__':
app.run(host='0.0.0.0', port=80, debug=True)