-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathviews_martial_art.py
93 lines (74 loc) · 3.19 KB
/
views_martial_art.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
from flask import render_template, request, redirect, session, flash, url_for, send_from_directory
from models.martial_arts import MartialArts
from objects.martialArtForm import MartialArtForm
from app import db, app, UPLOAD_PATH
from helpers import find_image, delete_file
import time
@app.route('/')
def index():
martialList = MartialArts.query.order_by(MartialArts.id)
return render_template('index.html', martialArts=martialList, title='Martial Arts')
@app.route('/new')
def newMartialArt():
if 'user_logged' not in session or session['user_logged'] == None:
return redirect(url_for('login',next_page=url_for('newMartialArt')))
form = MartialArtForm()
return render_template('new.html', title='New Martial Art', form=form)
@app.route('/martialCreate', methods=['POST', ])
def martialCreate():
form = MartialArtForm(request.form)
if not form.validate_on_submit():
return redirect(url_for('newMartialArt'))
name = form.name.data
category = form.category.data
points = form.points.data
martial_art = MartialArts.query.filter_by(name=name).first()
if martial_art:
flash('Martial art already in database!')
return redirect(url_for('index'))
new_martial_art = MartialArts(name=name, category=category, points=points)
db.session.add(new_martial_art)
db.session.commit()
file = request.files['cape_file']
timestamp = time.time()
file.save(f'{UPLOAD_PATH}/cape_{new_martial_art.id}-{timestamp}.jpg')
flash('Martial art added with sucess!')
return redirect(url_for('index'))
@app.route('/edit/<int:id>')
def edit(id):
if 'user_logged' not in session or session['user_logged'] == None:
return redirect(url_for('login',next_page=url_for('edit', id=id)))
martial_art = MartialArts.query.filter_by(id=id).first()
form = MartialArtForm()
form.name.data = martial_art.name
form.category.data = martial_art.category
form.points.data = martial_art.points
martial_cape = find_image(id)
return render_template('edit.html', title='Edit Martial Art', id=id, martial_cape=martial_cape, form=form)
@app.route('/martialUpdate', methods=['POST', ])
def martialUpdate():
form = MartialArtForm(request.form)
if form.validate_on_submit:
martial_art = MartialArts.query.filter_by(id=request.form['id']).first()
martial_art.name = form.name.data
martial_art.category = form.category.data
martial_art.points = form.points.data
db.session.add(martial_art)
db.session.commit()
file = request.files['cape_file']
timestamp = time.time()
delete_file(martial_art.id)
file.save(f'{UPLOAD_PATH}/cape_{martial_art.id}-{timestamp}.jpg')
flash('Martial art updated with sucess!')
return redirect(url_for('index'))
@app.route('/delete/<int:id>')
def delete(id):
if 'user_logged' not in session or session['user_logged'] == None:
return redirect(url_for('login'))
MartialArts.query.filter_by(id=id).delete()
db.session.commit()
flash('Martial art Deleted with sucess!')
return redirect(url_for('index'))
@app.route('/uploads/<file_name>')
def image(file_name):
return send_from_directory('uploads', file_name)