-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackend.py
268 lines (238 loc) · 9.6 KB
/
backend.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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
# A very simple Flask Hello World app for you to get started with...
from flask import Flask, request, jsonify
from flaskext.mysql import MySQL
app = Flask(__name__)
mysql = MySQL()
# Prevent flask jsonify from sorting data
app.config['JSON_SORT_KEYS'] = False
# MySQL configurations
app.config['MYSQL_DATABASE_USER'] = 'smartrecruit'
app.config['MYSQL_DATABASE_PASSWORD'] = 'azerty06'
app.config['MYSQL_CHARSET'] = 'utf-8'
app.config['MYSQL_DATABASE_DB'] = 'smartrecruit$smartrecruit'
app.config['MYSQL_DATABASE_HOST'] = 'smartrecruit.mysql.pythonanywhere-services.com'
mysql.init_app(app)
# Api acces routes
@app.route('/offers')
def getOffers():
applicant = request.args.get('applicant')
try:
cur = mysql.connect().cursor()
jobOffers = []
sql = '''SELECT * FROM offre WHERE id_offre NOT IN (SELECT id_offre FROM candidat_offre WHERE id_candidat=%s) AND id_offre NOT IN (SELECT id_offre FROM favoris WHERE id_candidat=%s)'''
cur.execute(sql, (applicant, applicant))
for row in cur.fetchall():
jobOffer = {'id': row[0],
'position': row[1],
'company': row[2],
'location': row[3],
'datePosted': row[4],
'description': row[5],
'img': row[6]}
jobOffers.append(jobOffer)
return jsonify({'response': 'success', 'results-for': 'offers','job-offers':jobOffers})
except Exception as e:
return jsonify({'error': str(e)})
# Favorite offers
@app.route('/favorites')
def getFavorites():
applicant = request.args.get('applicant')
try:
cur = mysql.connect().cursor()
jobOffers = []
sql = '''SELECT * FROM offre WHERE id_offre IN (SELECT id_offre FROM favoris WHERE id_candidat=%s) AND id_offre NOT IN (SELECT id_offre FROM candidat_offre WHERE id_candidat=%s)'''
cur.execute(sql, (applicant, applicant))
for row in cur.fetchall():
jobOffer = {'id': row[0],
'position': row[1],
'company': row[2],
'location': row[3],
'datePosted': row[4],
'description': row[5],
'img': row[6]}
jobOffers.append(jobOffer)
return jsonify({'response': 'success', 'results-for': 'favorites','job-offers':jobOffers})
except Exception as e:
return jsonify({'error': str(e)})
# Applications for a user
@app.route('/applications')
def getOfferCandidat():
applicant = request.args.get('applicant')
try:
cur = mysql.connect().cursor()
sql = '''SELECT offre.*, etat FROM offre, candidat_offre WHERE offre.id_offre=candidat_offre.id_offre AND id_candidat=%s AND etat<>'SUPPRIME' ORDER BY etat'''
cur.execute(sql, (applicant))
candidatApplications = []
for row in cur.fetchall():
candidatApplication = {'id': row[0],
'position': row[1],
'company': row[2],
'location': row[3],
'datePosted': row[4],
'description': row[5],
'img': row[6],
'etat': row[7]}
candidatApplications.append(candidatApplication)
return jsonify({'response': 'success', 'results-for': 'applications','job-applications':candidatApplications})
except Exception as e:
return jsonify({'error': str(e)})
# update offer status to SUPPRIME, APP_ATT_RDV, APP_RDV_RECU, APP_REF, APP_ACC
@app.route('/updateStatus')
def updateStatus():
status = request.args.get('status')
applicant = request.args.get('applicant')
offer = request.args.get('offer')
try:
cur = mysql.connect().cursor()
sql = '''INSERT INTO candidat_offre (id_candidat, id_offre, etat) VALUES(%s, %s, %s) ON DUPLICATE KEY UPDATE etat=%s, id_candidat=%s, id_offre=%s'''
cur.execute(sql, (applicant, offer, status, status, applicant, offer))
return jsonify({'response':'success'})
except Exception as e:
return jsonify({'error': 'Exception'})
# add an offer to favorites
@app.route('/addFavorite')
def addFavorite():
applicant = request.args.get('applicant')
offer = request.args.get('offer')
try:
cur = mysql.connect().cursor()
sql = '''INSERT INTO favoris (id_candidat, id_offre) VALUES(%s, %s)'''
cur.execute(sql, (applicant, offer))
return jsonify({'response':'success'})
except Exception as e:
return jsonify({'error': 'Exception'})
# Remove offer from favorites
@app.route('/removeFavorite')
def removeFavorite():
applicant = request.args.get('applicant')
offer = request.args.get('offer')
try:
cur = mysql.connect().cursor()
sql = '''DELETE FROM favoris WHERE id_candidat=%s AND id_offre=%s'''
cur.execute(sql, (applicant, offer))
return jsonify({'response':'success'})
except Exception as e:
return jsonify({'error': str(e)})
# Schedule appointment to candidate
@app.route('/scheduleAppointment')
def scheduleAppointment():
applicant = request.args.get('applicant')
recruiter = request.args.get('recruiter')
offer = request.args.get('offer')
date = request.args.get('date')
time = request.args.get('time')
try:
cur = mysql.connect().cursor()
sql = '''INSERT INTO rendez_vous (id_offre, id_candidat, id_recruteur, date, time) VALUES(%s, %s, %s, %s, %s)'''
cur.execute(sql, (offer, applicant, recruiter, date, time))
return jsonify({'response':'success'})
except Exception as e:
return jsonify({'error': str(e)})
# Get applicants for a recruter
@app.route('/appointmentRequests')
def appointmentRequests():
recruiter = request.args.get('recruiter')
try:
cur = mysql.connect().cursor()
sql = '''select o.position, o.localisation, c.* from (select * from candidat_offre where etat='APP_RDV_ATT' and id_offre in (select id_offre from recruteur_offre where id_recruteur=%s)) as c, offre as o where o.id_offre = c.id_offre'''
cur.execute(sql, (recruiter))
appointmentReq = []
for row in cur.fetchall():
req = {'position': row[0],
'location': row[1],
'applicant': row[2],
'offer': row[3]}
appointmentReq.append(req)
return jsonify({'response': 'success', 'results-for': 'appointments','appointments-requests':appointmentReq})
except Exception as e:
return jsonify({'error': str(e)})
# Reject application
@app.route('/rejectApplication')
def rejectApplication():
applicant = request.args.get('applicant')
offer = request.args.get('offer')
try:
cur = mysql.connect().cursor()
sql = '''UPDATE candidat_offre SET etat='APP_REF' WHERE id_candidat=%s AND id_offre=%s'''
cur.execute(sql, (applicant, offer))
return jsonify({'response':'success'})
except Exception as e:
return jsonify({'error': 'Exception'})
#Get offers recruiter
@app.route('/offersRecuiter')
def getOfferseRecruiter():
recruiter= request.args.get('recruiter')
try:
cur = mysql.connect().cursor()
jobOffersRecruiter=[]
sql = '''SELECT * FROM offre WHERE id_offre IN( SELECT id_offre FROM recruteur_offre WHERE id_recruteur=%s) '''
cur.execute(sql, (recruiter))
for row in cur.fetchall():
jobOffer = {'id': row[0],
'position': row[1],
'company': row[2],
'location': row[3],
'datePosted': row[4],
'description': row[5],
'img': row[6]}
jobOffersRecruiter.append(jobOffer)
return jsonify({'response': 'success', 'results-for': 'offersRecuiter','my-offers':jobOffersRecruiter})
except Exception as e:
return jsonify({'error': str(e)})
#Create offer by recruiter
@app.route('/addOffreRecuiter')
def addOffer():
recruiter = request.args.get('recruiter')
position = request.args.get('position')
entreprise = request.args.get('company')
localisation = request.args.get('location')
datePublication = request.args.get('date')
descriptif = request.args.get('desc')
offer = request.args.get('offer')
try:
cur = mysql.connect().cursor()
sql = '''INSERT INTO `offre`(`id_offre`, `position`, `nom_entreprise`, `localisation`, `date_publication`, `descriptif`) VALUES (%s,%s,%s,%s,%s,%s)'''
cur.execute(sql, (offer, position,entreprise, localisation,datePublication,descriptif))
sql2 = ''' INSERT INTO `recruteur_offre`(`id_recruteur`, `id_offre`) VALUES (%s,%s)'''
cur.execute(sql2, (recruiter, offer))
return jsonify({'response':'success'})
except Exception as e:
return jsonify({'error': str(e)})
# Applicant appointments
@app.route('/applicantAppointments')
def getApplicantAppointments():
applicant = request.args.get('applicant')
try:
cur = mysql.connect().cursor()
appointments = []
sql = '''SELECT o.nom_entreprise, o.position, o.localisation, r.date, r.time FROM offre o, rendez_vous r WHERE o.id_offre=r.id_offre and r.id_candidat=%s'''
cur.execute(sql, (applicant))
for row in cur.fetchall():
rdv = {'company': row[0],
'position': row[1],
'location': row[2],
'day': row[3],
'hour': row[4]}
appointments.append(rdv)
return jsonify({'response': 'success', 'results-for': 'appointments','job-appointments':appointments})
except Exception as e:
return jsonify({'error': str(e)})
# Recruiter appointments
@app.route('/recruiterAppointments')
def getRecruiterAppointments():
applicant = request.args.get('recruiter')
try:
cur = mysql.connect().cursor()
appointments = []
sql = '''SELECT o.position, o.localisation, r.id_candidat, r.date, r.time FROM offre o, rendez_vous r WHERE o.id_offre=r.id_offre and r.id_recruteur=%s'''
cur.execute(sql, (applicant))
for row in cur.fetchall():
rdv = {'position': row[0],
'location': row[1],
'applicant': row[2],
'day': row[3],
'hour': row[4]}
appointments.append(rdv)
return jsonify({'response': 'success', 'results-for': 'appointments','job-appointments':appointments})
except Exception as e:
return jsonify({'error': str(e)})