/
main.py
222 lines (180 loc) · 6.81 KB
/
main.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
from flask import Flask, render_template, url_for, request, redirect, session, flash, make_response, jsonify
import data_handler
import util
import os
app = Flask(__name__)
app.secret_key = os.urandom(16)
@app.route("/")
def index():
"""
This is a one-pager which shows all the boards and cards
"""
return render_template('index.html')
@app.route("/get-boards")
@util.json_response
def get_boards():
"""
All the boards
"""
return data_handler.get_boards()
@app.route("/get-cards/<int:board_id>")
@util.json_response
def get_cards_for_board(board_id: int):
"""
All cards that belongs to a board
:param board_id: id of the parent board
"""
return data_handler.get_cards_for_board(board_id)
@app.route("/get-status/<int:status_id>")
@util.json_response
def get_status_for_card(status_id: int):
return data_handler.get_card_status(status_id)
@app.route("/get-statuses")
@util.json_response
def get_statuses():
return data_handler.get_statuses()
@app.route('/registration', methods=['GET', 'POST'])
def registration():
if request.method == 'POST':
users = data_handler.get_usernames()
for user in users:
if user['username'] == request.form['username']:
error = 'Username already exists, please choose another one!'
return render_template('registration.html', error=error)
if request.form['username'] and request.form['password']:
password = util.hash_password(request.form['password'])
user = {'username': request.form['username'], 'password': password}
data_handler.add_new_user(user)
flash('Successful registration. Log in to continue.')
return redirect('/login')
else:
error = 'Please, fill in both fields.'
return render_template('registration.html', error=error)
return render_template('registration.html')
@app.route('/login', methods=['GET', 'POST'])
def login():
if request.method == 'POST':
username = request.form['username']
password = request.form['password']
if util.verify_password(password, data_handler.get_password(username)['password']):
session['username'] = username
flash('You were just logged in')
return redirect('/')
else:
error = 'Wrong username or password.'
return render_template('login.html', error=error)
return render_template('login.html')
@app.route('/logout')
def logout():
session.pop('username', None)
flash('You were just logged out')
return redirect(url_for('index'))
@app.route("/rename-save", methods=["POST"])
def rename_save():
boards = data_handler.get_boards()
data = request.get_json()
board_titles = data["titles"].split(",")
for i in range(len(boards)):
boards[i]["title"] = board_titles[i]
data_handler.save_dict_to_boards_csv(boards)
@app.route("/rename-column-save", methods=["POST"])
def rename_column_save():
columns = data_handler.get_statuses()
data = request.get_json()
column_titles = data["titles"].split(",")
for i in range(len(columns)):
columns[i]["title"] = column_titles[i]
data_handler.save_dict_to_statuses_csv(columns)
@app.route("/add_new_board", methods=['POST'])
def add_new_board():
boards = data_handler.get_boards()
max_id = 0
for board in boards:
if int(board['id']) > max_id:
max_id = int(board['id'])
data = request.get_json()
new_board = {'id': max_id + 1, 'title': data['boardName']}
boards.append(new_board)
data_handler.save_dict_to_boards_csv(boards)
return make_response(jsonify({"message": "JSON received"}), 200)
@app.route('/add_new_card', methods=['POST'])
def add_new_card():
cards = data_handler.get_cards()
max_id = 0
for card in cards:
if int(card['id']) > max_id:
max_id = int(card['id'])
data = request.get_json()
new_card = {'id': max_id + 1, 'board_id': data['boardId'], 'title': data['cardName'], 'status_id': 0,
'order': data['position']}
cards.append(new_card)
data_handler.save_dict_to_cards_csv(cards)
return make_response((jsonify({'message': "JSON received"})), 200)
@app.route('/save_card_position', methods=['POST'])
def save_card_position():
cards = data_handler.get_cards()
statuses = data_handler.get_statuses()
data = request.get_json()
for status in statuses:
if status['title'] == data['status']:
status_id = status['id']
for card in cards:
if card['id'] == data['cardId']:
card['board_id'] = data['boardId']
card['status_id'] = status_id
card['order'] = data['position']
data_handler.save_dict_to_cards_csv(cards)
return make_response(jsonify({"message": "JSON received"}), 200)
@app.route("/add-column", methods=['POST'])
def add_column():
data = request.get_json()
data = data["column_titles"].split(",")
statuses = data_handler.get_statuses()
for i in range(len(statuses)):
statuses[i]["title"] = data[i]
statuses.append({"id": len(statuses), "title": "new status"})
data_handler.save_dict_to_statuses_csv(statuses)
@app.route("/rename-card-save", methods=["POST"])
def rename_card_save():
cards = data_handler.get_cards()
data = request.get_json()
card_title = data["titles"].split(",")
for i in range(len(cards)):
cards[i]["title"] = card_title[i]
data_handler.save_dict_to_cards_csv(cards)
@app.route('/del-card', methods=['POST'])
@util.json_response
def del_card():
cards = data_handler.get_cards()
data = request.get_json()
for i in range(len(cards)):
if cards[i]["id"] == data["id"]:
del cards[i]
data_handler.save_dict_to_cards_csv(cards)
return make_response(jsonify({"message": "JSON received"}), 200)
@app.route('/del-board', methods=['POST'])
@util.json_response
def del_board():
boards = data_handler.get_boards()
data = request.get_json()
for i in range(len(boards)):
if boards[i]["id"] == data["id"]:
del boards[i]
data_handler.save_dict_to_boards_csv(boards)
data_handler.del_cards_by_board_id(data["board_id"])
return make_response(jsonify({"message": "JSON received"}), 200)
@app.route('/del-col', methods=['POST'])
@util.json_response
def del_col():
statuses = data_handler.get_statuses()
data = request.get_json()
for i in range(len(statuses)):
if statuses[i]["id"] == data["id"]:
del statuses[i]
data_handler.save_dict_to_statuses_csv(statuses)
data_handler.del_cards_by_col_id(data["col_id"])
return make_response(jsonify({"message": "JSON received"}), 200)
def main():
app.run(debug=True, host='0.0.0.0')
if __name__ == '__main__':
main()