Skip to content

Commit e640e40

Browse files
Nigar KhudiyevaNigar Khudiyeva
Nigar Khudiyeva
authored and
Nigar Khudiyeva
committed
first commit
1 parent 3878c05 commit e640e40

15 files changed

+885
-0
lines changed

Dockerfile

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
FROM python:3.4-alpine
2+
ADD . /code
3+
WORKDIR /code
4+
RUN pip install requests
5+
RUN pip install -r requirements.txt
6+
RUN pip install flask-mysql
7+
RUN pip install pymysql
8+
RUN pip install Flask-Mail
9+
CMD ["python", "app.py"]

__pycache__/database.cpython-34.pyc

2.52 KB
Binary file not shown.

app.py

+300
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,300 @@
1+
from flask import Flask,render_template,session,request,flash
2+
from flask_mail import Mail, Message
3+
from datetime import timedelta
4+
from flaskext.mysql import MySQL
5+
import requests
6+
import json
7+
import os
8+
import datetime
9+
import traceback
10+
11+
app = Flask(__name__)
12+
mysql = MySQL()
13+
14+
15+
# mail notification enable
16+
MAIL_NOTIFICATION = True
17+
18+
# Gmail Configuration
19+
app.config['MAIL_SERVER']='smtp.gmail.com'
20+
app.config['MAIL_PORT'] = 465
21+
app.config['MAIL_USERNAME'] = 'programmerlogs@gmail.com'
22+
app.config['MAIL_PASSWORD'] = 'programmerLOCK8'
23+
app.config['MAIL_USE_TLS'] = False
24+
app.config['MAIL_USE_SSL'] = True
25+
mail = Mail(app)
26+
27+
28+
29+
# MySQL configurations
30+
app.config['MYSQL_DATABASE_USER'] = 'root'
31+
app.config['MYSQL_DATABASE_PASSWORD'] = '123456'
32+
app.config['MYSQL_DATABASE_DB'] = 'mysql'
33+
app.config['MYSQL_DATABASE_HOST'] = 'mysql'
34+
mysql.init_app(app)
35+
36+
def sendemail(message):
37+
try:
38+
39+
if(MAIL_NOTIFICATION):
40+
41+
conn = mysql.connect()
42+
cursor = conn.cursor()
43+
44+
rowcount = cursor.execute("select email from vacation.users ")
45+
rows = cursor.fetchall()
46+
47+
emails = ''
48+
49+
for row in rows:
50+
emails = emails +row[0]+';'
51+
52+
cursor.close()
53+
conn.close()
54+
55+
msg = Message('Vacation Info', sender = 'programmerlogs@gmail.com', recipients = [emails])
56+
msg.body = message
57+
mail.send(msg)
58+
59+
except Exception:
60+
traceback.print_exc()
61+
62+
63+
64+
@app.route('/')
65+
def index():
66+
return render_template('index.html')
67+
68+
@app.route('/user/<access_token>')
69+
def dologin(access_token):
70+
71+
try:
72+
73+
# get access token json data file
74+
json_content=requests.get('https://www.googleapis.com/oauth2/v1/userinfo?alt=json&access_token='+access_token).text
75+
data = json.loads(json_content)
76+
77+
# when access token is valid set info to user session
78+
session['email'] = data['email']
79+
session['name'] = data['name']
80+
session['picture'] = data['picture']
81+
82+
conn = mysql.connect()
83+
cursor = conn.cursor()
84+
85+
rowcount = cursor.execute("select count(*) usercount from vacation.users where email='"+session['email']+"' ")
86+
firstrow = cursor.fetchone()
87+
usercount=firstrow[0]
88+
89+
#if usercount is zero this means user does not exsist in this case, add new user to system on deactive mode and minimum group viewer
90+
if ( usercount == 0):
91+
cursor.execute("insert into vacation.users(email,active,user_group) values ('"+session['email']+"',0,'viewer')")
92+
session['active']=0
93+
session['user_group']='viewer'
94+
conn.commit()
95+
cursor.close()
96+
97+
if( usercount > 0):
98+
cursor.execute("select active,user_group from vacation.users where email='"+session['email']+"'")
99+
firstrow = cursor.fetchone()
100+
session['active']=firstrow[0]
101+
session['user_group']=firstrow[1]
102+
cursor.close()
103+
104+
conn.close()
105+
106+
return render_template('home.html')
107+
except Exception:
108+
traceback.print_exc()
109+
return render_template('error.html')
110+
111+
112+
@app.route('/error')
113+
def goError():
114+
return render_template('error.html')
115+
116+
@app.route('/get_denied_days')
117+
def goDeinedDay():
118+
response_json='['
119+
120+
conn = mysql.connect()
121+
cursor =conn.cursor()
122+
123+
rowcount = cursor.execute("select date_format(date_,'%Y-%m-%d') from vacation.denied_days")
124+
days = cursor.fetchall()
125+
126+
for row in days :
127+
response_json = response_json + '"'+row[0]+'",'
128+
129+
cursor.close()
130+
conn.close()
131+
132+
response_json=response_json[:-1]
133+
response_json=response_json+']'
134+
return response_json
135+
136+
@app.route('/get_vacation_days/<begin_date>')
137+
def getCalendarDays(begin_date):
138+
response_json = '{'
139+
vDate = datetime.datetime.strptime(begin_date, "%d-%m-%Y")
140+
141+
conn = mysql.connect()
142+
cursor = conn.cursor()
143+
144+
for index in range(1,43):
145+
146+
response_json = response_json + '"'+vDate.strftime('%d-%m-%Y')+'":'
147+
148+
rowcount = cursor.execute("select full_name,status from vacation.requests where STR_TO_DATE('"+vDate.strftime('%d-%m-%Y')+"', '%d-%m-%Y') between begin_date and end_date ")
149+
150+
if(rowcount==0):
151+
response_json = response_json + '[],'
152+
153+
if(rowcount > 0):
154+
vRequests = cursor.fetchall();
155+
response_json = response_json + '['
156+
157+
for row in vRequests :
158+
response_json = response_json + '{ "name": "'+row[0]+'", "status": "'+row[1]+'" },'
159+
response_json = response_json[:-1]
160+
response_json = response_json + '],'
161+
vDate = vDate + datetime.timedelta(days=1)
162+
163+
164+
165+
response_json = response_json[:-1]
166+
response_json = response_json + '}'
167+
168+
169+
cursor.close()
170+
conn.close()
171+
172+
return response_json
173+
174+
@app.route('/logout')
175+
def doLogout():
176+
session.clear()
177+
return redirect('https://accounts.google.com/Logout?')
178+
179+
@app.route('/users')
180+
def goUsers():
181+
conn = mysql.connect()
182+
cursor = conn.cursor()
183+
184+
action = request.args.get('action')
185+
186+
if(action == 'activate'):
187+
userEmail = request.args.get('email')
188+
rowcount = cursor.execute("update vacation.users set active=1 where email='"+userEmail+"' ")
189+
flash('User activated successfully')
190+
sendemail('User activated successfully')
191+
conn.commit()
192+
193+
if(action == 'lock'):
194+
userEmail = request.args.get('email')
195+
rowcount = cursor.execute("update vacation.users set active=0 where email='"+userEmail+"' ")
196+
flash('User blocked successfully')
197+
sendemail('User activated successfully')
198+
conn.commit()
199+
200+
if(action == 'change'):
201+
userEmail = request.args.get('email')
202+
userGroup = request.args.get('user_group')
203+
rowcount = cursor.execute("update vacation.users set user_group='"+userGroup+"' where email='"+userEmail+"' ")
204+
flash('User group changed successfully')
205+
sendemail('User changed successfully')
206+
conn.commit()
207+
208+
rowcount = cursor.execute('select email,active,user_group from vacation.users order by active asc ')
209+
users = cursor.fetchall()
210+
211+
cursor.close()
212+
conn.close()
213+
214+
return render_template('users_management.html',users=users)
215+
216+
@app.route('/calendar',methods = ['GET','POST'])
217+
def doCalendar():
218+
conn = mysql.connect()
219+
cursor = conn.cursor()
220+
221+
action = request.args.get('action')
222+
223+
if(action == 'add'):
224+
categoryName = request.form.get('category')
225+
beginDate = request.form.get('begin')
226+
endDate = request.form.get('end')
227+
rowcount = cursor.execute("insert into vacation.requests(email,begin_date,end_date,status,full_name,leave_category) values ('"+session['email']+"',STR_TO_DATE('"+beginDate+"', '%d-%m-%Y'),STR_TO_DATE('"+endDate+"', '%d-%m-%Y'),'pending','"+session['name']+"','"+categoryName+"')")
228+
flash('Your leave request added successfully ')
229+
230+
sendemail('New request created')
231+
conn.commit()
232+
233+
cursor.execute('select * from vacation.categories ')
234+
categories = cursor.fetchall()
235+
236+
237+
238+
cursor.close()
239+
conn.close()
240+
241+
return render_template('calendar.html',categories=categories)
242+
243+
@app.route('/requests')
244+
def goPendingRequest():
245+
conn = mysql.connect()
246+
cursor = conn.cursor()
247+
248+
action = request.args.get('action')
249+
status = request.args.get('status')
250+
251+
if(action=='accepted' or action == 'declined'):
252+
rowcount = cursor.execute("update vacation.requests set status='"+action+"' where id= "+request.args.get('id'))
253+
flash('selected leave request '+action+' successfully ')
254+
sendemail('selected leave request '+action+' successfully ')
255+
conn.commit()
256+
257+
if(status == 'pending'):
258+
cursor.execute("select full_name,leave_category,begin_date,end_date,id,status from vacation.requests where status='pending' order by id desc ")
259+
requests=cursor.fetchall()
260+
else:
261+
cursor.execute("select full_name,leave_category,begin_date,end_date,id,status from vacation.requests where status<>'pending' order by id desc ")
262+
requests=cursor.fetchall()
263+
264+
265+
266+
cursor.close()
267+
conn.close()
268+
269+
return render_template('leave_requests.html',requests=requests)
270+
271+
272+
@app.route('/categories',methods=['GET','POST'])
273+
def goCategories():
274+
#if session set not email attribute redirect to index page to login
275+
#if(session['email']):
276+
# return render_template('index.html')
277+
action = request.args.get('action')
278+
279+
conn = mysql.connect()
280+
cursor = conn.cursor()
281+
282+
if(action == 'add'):
283+
rowcount = cursor.execute("insert into vacation.categories(category) values ('"+request.form.get('category_name')+"') ")
284+
conn.commit()
285+
if(action == 'del'):
286+
rowcount = cursor.execute("delete from vacation.categories where ID= "+request.args.get('id'))
287+
conn.commit()
288+
289+
cursor.execute('select * from vacation.categories ')
290+
categories = cursor.fetchall()
291+
292+
293+
cursor.close()
294+
conn.close()
295+
296+
return render_template('leave_categories.html',categories=categories)
297+
298+
if __name__ == "__main__":
299+
app.secret_key = os.urandom(24)
300+
app.run(host="0.0.0.0", debug=True)

database.sql

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
create DATABASE IF NOT EXISTS vacation;
2+
create TABLE IF NOT EXISTS vacation.categories ( id int NOT NULL AUTO_INCREMENT,category varchar(255) NOT NULL,PRIMARY KEY (ID));
3+
insert into vacation.categories (category) SELECT * FROM ( SELECT 'Annual Leave' ) AS tmp WHERE NOT EXISTS ( SELECT category FROM vacation.categories WHERE category = 'Annual Leave') LIMIT 1;
4+
insert into vacation.categories (category) SELECT * FROM ( SELECT 'Personal/Carer Leave' ) AS tmp WHERE NOT EXISTS ( SELECT category FROM vacation.categories WHERE category = 'Personal/Carer Leave') LIMIT 1;
5+
insert into vacation.categories (category) SELECT * FROM ( SELECT 'Long Service Leave' ) AS tmp WHERE NOT EXISTS ( SELECT category FROM vacation.categories WHERE category = 'Long Service Leave') LIMIT 1 ;
6+
insert into vacation.categories (category) SELECT * FROM ( SELECT 'Time in Lieu' ) AS tmp WHERE NOT EXISTS ( SELECT category FROM vacation.categories WHERE category = 'Time in Lieu') LIMIT 1 ;
7+
insert into vacation.categories (category) SELECT * FROM ( SELECT 'Long Service Leave' ) AS tmp WHERE NOT EXISTS ( SELECT category FROM vacation.categories WHERE category = 'Long Service Leave') LIMIT 1 ;
8+
create table IF NOT EXISTS vacation.users ( email VARCHAR(200) not null, active int not null, check (active in (0,1)),user_group VARCHAR(200), check (user_group in ('viewer','employee','administrator')), primary key (email)) ;
9+
create table IF NOT EXISTS vacation.denied_days( date_ date not null unique ) ;
10+
create table IF NOT EXISTS vacation.requests( email varchar(200) not null, begin_date date not null,end_date date not null,status varchar(200) not null,full_name varchar(200) not null,leave_category varchar(200) not null,id int not null AUTO_INCREMENT,check (status in ('pending','accepted','declined')), primary key (id));
11+
insert into vacation.users(email,active,user_group) values ('muradimanbayli@gmail.com',1,'administrator');
12+
insert INTO vacation.denied_days (date_) VALUES ('2017-10-21');

docker-compose.yml

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
version: '3'
2+
services:
3+
web:
4+
build: .
5+
ports:
6+
- "5000:5000"
7+
volumes:
8+
- .:/code
9+
links:
10+
- db:mysql
11+
db:
12+
image: mysql
13+
environment:
14+
MYSQL_ROOT_PASSWORD: "123456"
15+
DB_HOST: mysqlhost
16+
ports:
17+
- "3306:3306"

requirements.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
flask

0 commit comments

Comments
 (0)