-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.py
226 lines (191 loc) · 7.6 KB
/
index.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
from flask import Flask, render_template, request, redirect, session, url_for
from flask_mail import Mail, Message
import json
import sqlite3
import os
from dotenv import load_dotenv
import threading
from google.oauth2 import service_account
from googleapiclient.discovery import build
from datetime import datetime, timedelta
import random
load_dotenv()
app = Flask(__name__)
@app.context_processor
def inject_stage_and_region():
return dict(domain=os.getenv('Domain_OR_IP'))
app.secret_key = os.getenv('APP_SECRET_KEY')
recipients = [recipient.strip() for recipient in os.getenv('APPLICATION_RECIPIENTS').split(',')]
app.config['MAIL_SERVER'] = os.getenv('MAIL_SERVER')
app.config['MAIL_PORT'] = os.getenv('MAIL_PORT')
app.config['MAIL_USE_SSL'] = os.getenv('MAIL_USE_TLS')
app.config['MAIL_USERNAME'] = os.getenv('MAIL_USERNAME')
app.config['MAIL_PASSWORD'] = os.getenv('MAIL_PASSWORD')
app.config['MAIL_DEFAULT_SENDER'] = os.getenv('MAIL_DEFAULT_SENDER')
mail = Mail(app)
def send_async_email(app, msg):
with app.app_context():
mail.send(msg)
def send_email(msg):
thr = threading.Thread(target=send_async_email, args=[app, msg])
thr.start()
# Load the credentials for the service account
credentials = service_account.Credentials.from_service_account_file(
os.getenv('SERVICE_ACCOUNT_JSON'),
scopes=['https://www.googleapis.com/auth/calendar']
)
service = build('calendar', 'v3', credentials=credentials)
def create_database():
if not os.path.exists('leave_applications.db'):
conn = sqlite3.connect('leave_applications.db')
c = conn.cursor()
c.execute("CREATE TABLE leave_applications (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, email TEXT, leave_days TEXT, leave_period TEXT, reason TEXT, status TEXT)")
c.execute("CREATE TABLE users (id INTEGER PRIMARY KEY AUTOINCREMENT, email TEXT, password TEXT)")
c.execute("INSERT INTO users (email, password) Values (?,?)",(os.getenv('DEFAULT_USER'),os.getenv('DEFAULT_USER_PASSWORD')))
conn.commit()
conn.close()
create_database()
def get_db():
conn = sqlite3.connect('leave_applications.db')
conn.row_factory = sqlite3.Row
return conn
# function to update the leave request status
def update_status(email, status):
conn = get_db()
conn.execute('UPDATE leave_applications SET status = ? WHERE email = ?', [status, email])
conn.commit()
conn.close()
def get_status(email):
conn = get_db()
cursor = conn.cursor()
status = cursor.execute('Select status from leave_applications WHERE email = ?', [email])
row = cursor.fetchone()
conn.close()
return(row[0])
def get_dates(email):
conn = get_db()
cursor = conn.cursor()
status = cursor.execute('Select leave_period from leave_applications WHERE email = ?', [email])
row = cursor.fetchone()
conn.close()
return(row[0])
def get_leave_rec(email):
conn = get_db()
cursor = conn.cursor()
status = cursor.execute('Select * from leave_applications WHERE email = ?', [email])
row = cursor.fetchone()
conn.close()
return(row)
def create_event(name, start_date, end_date, reason, service):
start_date = datetime.strptime(start_date, '%d-%m-%Y')
end_date = datetime.strptime(end_date, '%d-%m-%Y')
event = {
'summary': name+" is on Leave",
'description': reason,
'colorId': random.choice(['1','2','3','4','5','6','7','8','9','10','11']),
'start': {
'dateTime': start_date.isoformat(),
'timeZone': 'UTC',
},
'end': {
'dateTime': end_date.isoformat(),
'timeZone': 'UTC',
},
}
event = service.events().insert(calendarId=os.getenv('CALENDAR_ID'), body=event).execute()
return f"Event created: {event.get('htmlLink')}"
def get_st_en_date(date_str):
date_range = date_str.split(" to ")
return date_range[0],date_range[1]
@app.route('/', methods=['GET', 'POST'])
def index():
if request.method == 'POST':
name = request.form['name']
email = request.form['email']
leave_days = request.form['leave_days']
leave_period= request.form['leave_period']
reason = request.form['reason']
conn=get_db()
conn.execute("INSERT INTO leave_applications (name, email, leave_days, leave_period, reason) VALUES (?, ?, ?, ?, ?)", (name, email, leave_days, leave_period, reason))
conn.commit()
conn.close()
context={
"name": name,
"email": email,
"leave_days":leave_days,
"leave_period": leave_period,
"reason":reason,
}
message = Message(
subject='New Leave Request Recieved | FlaskyLMS',
recipients=recipients,
html=render_template(template_name_or_list="leave_notification.html", **context))
send_email(message)
return render_template('thankyou.html')
return render_template('lform.html')
@app.route('/leave_applications')
def leave_applications():
if 'user' not in session:
return redirect(url_for('login'))
c = get_db().cursor()
c.execute("SELECT * FROM leave_applications")
data = c.fetchall()
c.close()
return render_template('application_table.html', data=[row[1:] for row in data])
@app.route('/login', methods=['GET', 'POST'])
def login():
error_message = ''
if request.method == 'POST':
username = request.form['email']
password = request.form['password']
c = get_db().cursor()
c.execute("SELECT * FROM users WHERE email = ? AND password = ?",(username,password))
data = c.fetchall()
c.close()
# Check if the credentials are valid
if data:
# Store the user ID in a session variable
session['user'] = username
return redirect(url_for('leave_applications'))
else:
error_message = 'Invalid username or password'
# Render the login page with an error message if not present
return render_template('login.html', error_message=error_message)
@app.route('/logout')
def logout():
# Remove the session variables
session.pop('user', None)
session.pop('is_authenticated', None)
return redirect(url_for('login'))
@app.route('/submit', methods=['POST'])
def submit():
data = request.form.get("data")
data = json.loads(data)
for item in data:
email = item["email"]
status = item["status"]
db_status = get_status(email)
print("db status for ", email, " is ", db_status)
if db_status not in ['approved','rejected']:
update_status(email, status)
data=get_leave_rec(email)
context={
"name": data[1],
"leave_period":data[4],
"status": data[6],
}
message = Message(
subject='Your Leave Request Status | FlaskyLMS',
recipients=[email],
html=render_template(template_name_or_list="emp_leave_notification.html", **context))
send_email(message)
#Create Event in Calendar
if data[6] == 'approved':
start_date,end_date = get_st_en_date(data[4])
thr = threading.Thread(target = create_event, args = [data[1], start_date, end_date, data[5], service])
thr.start()
else:
print("nothing to send")
return "submitted"
if __name__ == '__main__':
app.run(host='0.0.0.0', debug=True)