forked from Ravichandran-S-06/Reagent_Inventory
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
235 lines (192 loc) · 7.29 KB
/
app.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
from flask import Flask, json, request, jsonify
from flask_cors import CORS
import sqlite3
from datetime import datetime, timedelta
import smtplib
import getpass
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email import encoders
app = Flask(__name__)
CORS(app)
def get_db_connection():
conn = sqlite3.connect('reagents.db')
conn.row_factory = sqlite3.Row
return conn
def init_db():
conn = get_db_connection()
conn.execute('''CREATE TABLE IF NOT EXISTS reagents
(id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
packingtype TEXT,
quantity FLOAT,
quantity_measure TEXT,
source TEXT,
expiry DATE,
setAlert INTEGER,
setQuantity INTEGER,
last_updated DATE)''')
conn.commit()
conn.close()
init_db()
@app.route('/send-email', methods=['POST'])
def send_email():
data = request.json
sender_email = "[email protected]"
receiver_email = "[email protected]"
# data.get('to_email')
password = "fxyornljpwuvjbqm" # It's recommended to use environment variables or a secure method to store passwords
# Create the email body
message = MIMEMultipart()
message["From"] = sender_email
message["To"] = receiver_email
message["Subject"] = "ReAgent REPORT"
#db data in json format
db_data = data['dbData']
reagent_name = None
for item in db_data:
if item['name'] == "HCL":
reagent_name = item['name']
break # Exit the loop once HCL is found
# Add the body to the email
# message.attach(MIMEText(f"Dear Sir/Madam,\n\nPlease find the attached report
# for the reagent {reagent_name}.\n\nThank you,\nReAgent Team", "plain"))
# Email body
# email_body = "Dear Sir/Madam,\n\nPlease find the attached report for the reagent " + reagent_name + "\n\nThank you,\nReAgent Team"
# email_body = f"""
# <html>
# <body>
# <p>Dear Sir/Madam,</p>
# <p>Please find the attached report for the reagent <b style='color:green;'>{reagent_name}</b>.</p>
# <p>Thank you,<br>ReAgent Team</p>
# </body>
# </html>
# """
# Connect to the SQLite database
conn = sqlite3.connect('D:\\Reagent_Inventory\\reagents.db')
cursor = conn.cursor()
# Fetch all records from the reagents table
cursor.execute("SELECT name,quantity,expiry FROM reagents")
rows = cursor.fetchall()
# Start building the HTML table
html_table = """
<table border="1">
<tr>
<th>Name</th>
<th>Quantity</th>
<th>Expiration Date</th>
</tr>
"""
# Loop through the rows and add them to the table
for row in rows:
html_table += f"""
<tr>
<td>{row[0]}</td>
<td>{row[1]}</td>
<td>{row[2]}</td>
</tr>
"""
# Close the table HTML
html_table += "</table>"
# Close the database connection
conn.close()
# Include the table in your email body
email_body = f"""
<!DOCTYPE html>
<html>
<head>
<style>
body {{ font-family: Arial, sans-serif; }}
table {{ border-collapse: collapse; width: 100%; }}
th, td {{ border: 1px solid #ddd; padding: 8px; }}
th {{ background-color: #f2f2f2; }}
</style>
</head>
<body>
<p>Dear Sir/Madam,</p>
<p>Please find below the report for the reagents:</p>
{html_table}
<p>Thank you,<br>ReAgent Team</p>
</body>
</html>
"""
message.attach(MIMEText(email_body, "html")) # plain for text
# Specify the file path here
file_path = "D:\\Reagent_Inventory\\reagents.db"
attachment = open(file_path, "rb")
part = MIMEBase('application', 'octet-stream')
part.set_payload((attachment).read())
encoders.encode_base64(part)
part.add_header('Content-Disposition', "attachment; filename= " + file_path.split('/')[-1])
message.attach(part)
# Sending the email
try:
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login(sender_email, password)
text = message.as_string()
server.sendmail(sender_email, receiver_email, text)
server.quit()
return jsonify({"message": "Email sent successfully"}), 200
except Exception as e:
return jsonify({"error": str(e)}), 500
@app.route('/reagents', methods=['GET', 'POST'])
def reagents():
conn = get_db_connection()
if request.method == 'POST':
data = request.json
current_date = datetime.now().strftime('%Y-%m-%d')
conn.execute('INSERT INTO reagents (name,packingtype, quantity, quantity_measure, source, expiry, setAlert,setQuantity, last_updated) VALUES (?,?, ?, ?, ?, ?, ?, ?,?)',
(data['name'],data['packingtype'], data['quantity'], data['quantity_measure'], data['source'], data['expiry'], data['setAlert'],data['setQuantity'], current_date))
conn.commit()
return jsonify({"message": "Reagent added successfully"}), 201
reagents = conn.execute('SELECT * FROM reagents').fetchall()
conn.close()
return jsonify([dict(ix) for ix in reagents])
@app.route('/reagents/<int:id>', methods=['GET', 'PUT', 'DELETE'])
def reagent(id):
conn = get_db_connection()
if request.method == 'PUT':
data = request.json
current_date = datetime.now().strftime('%Y-%m-%d')
conn.execute('UPDATE reagents SET name=?,packingtype=?, quantity=?, quantity_measure=?, source=?, expiry=?, setAlert=?,setQuantity = ?, last_updated=? WHERE id=?',
(data['name'],data['packingtype'], data['quantity'], data['quantity_measure'], data['source'], data['expiry'],data['setAlert'], data['setQuantity'], current_date, id))
conn.commit()
conn.close()
return jsonify({"message": "Reagent updated successfully"})
elif request.method == 'DELETE':
conn.execute('DELETE FROM reagents WHERE id=?', (id,))
conn.commit()
conn.close()
return jsonify({"message": "Reagent deleted successfully"})
# Fetch reagent details
reagent = conn.execute('SELECT * FROM reagents WHERE id=?', (id,)).fetchone()
conn.close()
if not reagent:
return jsonify({"error": "Reagent not found"}), 404
return jsonify(dict(reagent))
@app.route('/reagents/expiring-soon', methods=['GET'])
def expiring_soon():
conn = get_db_connection()
two_months_from_now = datetime.now() + timedelta(days=60)
reagents = conn.execute('''
SELECT * FROM reagents
WHERE expiry <= ?
ORDER BY expiry ASC
''', (two_months_from_now.strftime('%Y-%m-%d'),)).fetchall()
conn.close()
return jsonify([dict(ix) for ix in reagents])
@app.route('/api/search', methods=['POST'])
def search_reagents():
query = request.json.get('query', '')
# Perform search logic (example: simple substring search)
conn = get_db_connection()
results = []
for row in conn.execute('SELECT * FROM reagents'):
if query.lower() in row['name'].lower():
results.append(dict(row))
conn.close()
return jsonify(results)
if __name__ == '__main__':
app.run(debug=True)