forked from endegraaf/flask-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroutes.py
104 lines (90 loc) · 2.65 KB
/
routes.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
import pymysql
from flask import render_template
from flask import jsonify
from flask import request
import queries
from app import app, auth
from config import mysql
from model.Vacancy import Vacancy
@app.route('/')
@app.route('/index')
@auth.login_required
def index():
user = {'username': auth.current_user()}
posts = [
{
'author': {'username': 'C. S. Lewis'},
'body': 'The Chronicles of Narnia'
},
{
'author': {'username': 'Tolkien'},
'body': 'LOTR'
}
]
return render_template('index.html', title='Home', user=user, posts=posts)
@app.route('/addvacancy', methods=['POST'])
@auth.login_required
def add_vacancy():
try:
required_fields = ("vacancy_id", "title", "url", "body")
vacancy = Vacancy(**request.json) # unpack json into Employee
if vacancy.all_fields_filled(*required_fields) and request.method == 'POST':
conn = mysql.connect()
cursor = conn.cursor()
cursor.execute(queries.SQL_ADD_VACANCIES_7ST,
tuple(getattr(vacancy, field) for field in required_fields))
conn.commit()
return return_success_vacancy()
else:
return not_found()
except Exception as e:
print(e)
finally:
cursor.close()
conn.close()
@app.route('/getvacancies', methods=['GET'])
@auth.login_required
def get_vacancy():
try:
conn = mysql.connect()
cursor = conn.cursor(pymysql.cursors.DictCursor)
cursor.execute(queries.SQL_GET_VACANCIES)
vacancyRows = cursor.fetchall()
response = jsonify(vacancyRows)
response.status_code = 200
return response
except Exception as e:
print(e)
finally:
cursor.close()
conn.close()
def return_success_vacancy():
response = jsonify('Vacancy added successfully!')
response.status_code = 200
return response
@app.route('/deleteemp/<int:id>', methods=['DELETE'])
@auth.login_required
def delete_emp(id):
try:
conn = mysql.connect()
cursor = conn.cursor()
cursor.execute((queries.SQL_DELETE_EMP), (id,))
conn.commit()
respone = jsonify('Employee deleted successfully!')
respone.status_code = 200
return respone
except Exception as e:
print(e)
finally:
cursor.close()
conn.close()
@app.errorhandler(404)
@auth.login_required
def not_found(error=None):
message = {
'status': 404,
'message': 'Record not found: ' + request.url,
}
respone = jsonify(message)
respone.status_code = 404
return respone