-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflask_slurmreserve.py
146 lines (102 loc) · 4.5 KB
/
flask_slurmreserve.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
from slurmreserve import *
import datetime
import time
import mimetypes
from flask import Flask, redirect, request, render_template
app = Flask(__name__)
app.config['LDAP_PROVIDER_URL'] = ""
mimetypes.add_type('image/svg+xml', '.svg')
@app.route('/')
def showPartitions():
partitions = get_partition("test")
return render_template('partitions.html', partitions=partitions)
@app.route('/partitions/<string:partition>/reservations')
def showReservations(partition):
reservations = get_all_reservations(partition);
if reservations:
return render_template('reservations.html', partition=partition, reservations=reservations)
return render_template('reservations.html', partition=partition)
@app.route('/partitions/<string:partition>/reservations/filter/<string:filter_string>')
def showFilteredReservations(partition, filter_string):
reservations = get_all_reservations(partition);
if reservations:
filtered_list = []
for d in reservations:
if(filter_string in d["users"] or filter_string in d["name"]):
filtered_list.append(d)
return render_template('reservations.html', partition=partition, reservations=filtered_list, filter_string=filter_string)
return render_template('reservations.html', partition=partition)
@app.route('/partitions/<string:partition>/reservations/new', methods=['GET', 'POST'])
def newReservations(partition):
now = datetime.datetime.now().strftime("%Y-%m-%dT%H:%M:%S");
error = False
if request.method == 'POST':
dic = create_dict()
dic['name'] = request.form['name']
dic['node_cnt'] = (int)(request.form['node_cnt'])
try:
dic['start_time'] = (int)(time.mktime(datetime.datetime.strptime(request.form['start_time'], "%Y-%m-%dT%H:%M:%S").timetuple()))
dic['end_time'] = (int)(time.mktime(datetime.datetime.strptime(request.form['end_time'], "%Y-%m-%dT%H:%M:%S").timetuple()))
except:
print("Can't parse time.")
error = True
dic["users"] = request.form['users'].replace(" ", "")
dic["partition"] = partition
try:
res_id = create_reservation(dic)
except ValueError as e:
error = True
if error:
return render_template('new.html', partition=partition, error="value error", name=dic['name'], node_cnt=dic['node_cnt'], now=now,
start_time=request.form['start_time'], end_time=request.form['end_time'], users=dic["users"])
return redirect('/partitions/' + partition + '/reservations')
return render_template('new.html', partition=partition, now=now, start_time=now, end_time=now)
@app.route('/partitions/<string:partition>/reservations/<string:res_id>/edit', methods=['GET', 'POST'])
def editReservations(partition, res_id):
error = False
dic = {}
startTime = ""
endTime = ""
if request.method == 'POST':
dic = create_dict()
dic['name'] = res_id
dic['node_cnt'] = (int)(request.form['node_cnt'])
dic["users"] = request.form['users'].replace(" ", "")
try:
startTime = request.form['start_time']
if request.args.has_key('start_time'):
dic['start_time'] = (int)(time.mktime(datetime.datetime.strptime(startTime, "%Y-%m-%dT%H:%M:%S").timetuple()))
endTime = request.form['end_time']
dic['end_time'] = (int)(time.mktime(datetime.datetime.strptime(request.form['end_time'], "%Y-%m-%dT%H:%M:%S").timetuple()))
except:
print("Can't parse time.")
try:
res = update_reservation(dic)
print(res);
if res == 0:
return redirect('/partitions/' + partition + '/reservations')
except ValueError as e:
print("Error on edit")
error = True
reservation = dic
reservation['start_time'] = startTime
reservation['end_time'] = endTime
showStartTimeInput = True
if not error:
reservation = get_reservation(partition, res_id);
reservation['users'] = ",".join(reservation['users'])
try:
startTime = datetime.datetime.strptime(reservation['start_time'], "%Y-%m-%dT%H:%M:%S")
showStartTimeInput = (startTime > datetime.datetime.now())
except:
print("Can't parse time.")
now = datetime.datetime.now().strftime("%Y-%m-%dT%H:%M:%S");
return render_template('edit.html', partition=partition, error=error, res_id=res_id, reservation=reservation, showStart=showStartTimeInput, now=now)
@app.route('/partitions/<string:partition>/reservations/<string:res_id>/delete', methods=['POST'])
def deleteReservations(partition, res_id):
if request.method == 'POST':
try:
delete_reservation(res_id,"test")
except ValueError as e:
return "Error on delete"
return "delete"