adds edit vehicle name
This commit is contained in:
parent
729fee51e9
commit
d19b0a5858
115
app/main.py
115
app/main.py
|
@ -14,8 +14,8 @@ from flask.ext.security import user_registered
|
||||||
from flask.ext.mail import Mail, Message
|
from flask.ext.mail import Mail, Message
|
||||||
from flask_security.core import current_user
|
from flask_security.core import current_user
|
||||||
from flask_wtf import Form
|
from flask_wtf import Form
|
||||||
from wtforms import DateField, IntegerField, DecimalField, SelectField
|
from wtforms import DateField, IntegerField, StringField, DecimalField, SelectField
|
||||||
from wtforms.validators import DataRequired, ValidationError
|
from wtforms.validators import DataRequired, ValidationError, Length
|
||||||
import os
|
import os
|
||||||
|
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
|
@ -99,55 +99,6 @@ class Pitstop(db.Model):
|
||||||
return '<Pitstop %r km, %r l>' % (self.odometer, self.litres)
|
return '<Pitstop %r km, %r l>' % (self.odometer, self.litres)
|
||||||
|
|
||||||
|
|
||||||
user_datastore = SQLAlchemyUserDatastore(db, User, Role)
|
|
||||||
security = Security(app, user_datastore)
|
|
||||||
|
|
||||||
|
|
||||||
@user_registered.connect_via(app)
|
|
||||||
def user_registered_sighandler(app, user, confirm_token):
|
|
||||||
"""
|
|
||||||
Called after a user was created
|
|
||||||
"""
|
|
||||||
role = user_datastore.find_role('user')
|
|
||||||
user_datastore.add_role_to_user(user, role)
|
|
||||||
|
|
||||||
|
|
||||||
@app.before_first_request
|
|
||||||
def before_first_request():
|
|
||||||
db.create_all()
|
|
||||||
user_datastore.find_or_create_role(name='admin', description='Role for administrators')
|
|
||||||
user_datastore.find_or_create_role(name='user', description='Role for all users.')
|
|
||||||
db.session.commit()
|
|
||||||
|
|
||||||
user = User.query.filter(User.email=='someone@example.com').first()
|
|
||||||
if len(user.vehicles) == 0:
|
|
||||||
v = Vehicle('someone')
|
|
||||||
db.session.add(v)
|
|
||||||
user.vehicles.append(v)
|
|
||||||
db.session.commit()
|
|
||||||
|
|
||||||
user = User.query.filter(User.email=='admin@example.com').first()
|
|
||||||
if len(user.vehicles) == 0:
|
|
||||||
v = Vehicle('admin_1')
|
|
||||||
db.session.add(v)
|
|
||||||
user.vehicles.append(v)
|
|
||||||
v = Vehicle('admin_2')
|
|
||||||
db.session.add(v)
|
|
||||||
user.vehicles.append(v)
|
|
||||||
db.session.commit()
|
|
||||||
|
|
||||||
|
|
||||||
@app.before_request
|
|
||||||
def before_request():
|
|
||||||
g.data = {}
|
|
||||||
|
|
||||||
|
|
||||||
@app.route('/')
|
|
||||||
@login_required
|
|
||||||
def index():
|
|
||||||
return redirect(url_for('get_pit_stops'))
|
|
||||||
|
|
||||||
|
|
||||||
def date_check(form, field):
|
def date_check(form, field):
|
||||||
if field.data < form.pitstop.date:
|
if field.data < form.pitstop.date:
|
||||||
raise ValidationError('The new date must after %s' % form.pitstop.date)
|
raise ValidationError('The new date must after %s' % form.pitstop.date)
|
||||||
|
@ -174,6 +125,62 @@ class CreatePitstopForm(Form):
|
||||||
self.pitstop = pitstop
|
self.pitstop = pitstop
|
||||||
|
|
||||||
|
|
||||||
|
class EditVehicleForm(Form):
|
||||||
|
name = StringField('Name', validators=[Length(1, 255)])
|
||||||
|
|
||||||
|
|
||||||
|
user_datastore = SQLAlchemyUserDatastore(db, User, Role)
|
||||||
|
security = Security(app, user_datastore)
|
||||||
|
|
||||||
|
|
||||||
|
@user_registered.connect_via(app)
|
||||||
|
def user_registered_sighandler(app, user, confirm_token):
|
||||||
|
"""
|
||||||
|
Called after a user was created
|
||||||
|
"""
|
||||||
|
role = user_datastore.find_role('user')
|
||||||
|
user_datastore.add_role_to_user(user, role)
|
||||||
|
v = Vehicle('default vehicle')
|
||||||
|
db.session.add(v)
|
||||||
|
user.vehicles.append(v)
|
||||||
|
db.session.commit()
|
||||||
|
|
||||||
|
|
||||||
|
@app.before_first_request
|
||||||
|
def before_first_request():
|
||||||
|
db.create_all()
|
||||||
|
user_datastore.find_or_create_role(name='admin', description='Role for administrators')
|
||||||
|
user_datastore.find_or_create_role(name='user', description='Role for all users.')
|
||||||
|
db.session.commit()
|
||||||
|
|
||||||
|
|
||||||
|
@app.before_request
|
||||||
|
def before_request():
|
||||||
|
g.data = {}
|
||||||
|
|
||||||
|
|
||||||
|
@app.route('/')
|
||||||
|
@login_required
|
||||||
|
def index():
|
||||||
|
return redirect(url_for('get_pit_stops'))
|
||||||
|
|
||||||
|
|
||||||
|
@app.route('/account/edit_vehicle/<int:vid>', methods=['GET', 'POST'])
|
||||||
|
@login_required
|
||||||
|
def edit_vehicle(vid):
|
||||||
|
vehicle = Vehicle.query.filter(Vehicle.id == vid).first()
|
||||||
|
form = EditVehicleForm()
|
||||||
|
|
||||||
|
if form.validate_on_submit():
|
||||||
|
vehicle.name = form.name.data;
|
||||||
|
db.session.commit()
|
||||||
|
return redirect(url_for('get_account_page'))
|
||||||
|
|
||||||
|
form.name.default = vehicle.name
|
||||||
|
form.process()
|
||||||
|
return render_template('editVehicleForm.html', form=form)
|
||||||
|
|
||||||
|
|
||||||
@app.route('/pitstops/createForm', methods=['GET', 'POST'])
|
@app.route('/pitstops/createForm', methods=['GET', 'POST'])
|
||||||
@login_required
|
@login_required
|
||||||
def create_pit_stop_form():
|
def create_pit_stop_form():
|
||||||
|
@ -183,12 +190,9 @@ def create_pit_stop_form():
|
||||||
|
|
||||||
form = CreatePitstopForm()
|
form = CreatePitstopForm()
|
||||||
form.set_pitstop(last_pitstop)
|
form.set_pitstop(last_pitstop)
|
||||||
print(current_user.vehicles)
|
|
||||||
|
|
||||||
form.vehicle.choices = [(g.id, g.name) for g in current_user.vehicles]
|
|
||||||
if form.validate_on_submit():
|
if form.validate_on_submit():
|
||||||
print(form.vehicle.data)
|
v = Vehicle.query.filter(Vehicle.id == form.vehicle.data).first()
|
||||||
v = Vehicle.query.filter(Vehicle.id==form.vehicle.data).first()
|
|
||||||
new_stop = Pitstop(form.odometer.data, form.litres.data, form.date.data)
|
new_stop = Pitstop(form.odometer.data, form.litres.data, form.date.data)
|
||||||
db.session.add(new_stop)
|
db.session.add(new_stop)
|
||||||
v.pitstops.append(new_stop)
|
v.pitstops.append(new_stop)
|
||||||
|
@ -196,6 +200,7 @@ def create_pit_stop_form():
|
||||||
return redirect(url_for('get_pit_stops'))
|
return redirect(url_for('get_pit_stops'))
|
||||||
|
|
||||||
# dynamically set values
|
# dynamically set values
|
||||||
|
form.vehicle.choices = [(g.id, g.name) for g in current_user.vehicles]
|
||||||
form.odometer.default = last_pitstop.odometer
|
form.odometer.default = last_pitstop.odometer
|
||||||
form.litres.default = last_pitstop.litres
|
form.litres.default = last_pitstop.litres
|
||||||
form.date.default = date.today()
|
form.date.default = date.today()
|
||||||
|
|
|
@ -4,4 +4,13 @@
|
||||||
<h1>Account management for {{current_user.email}}</h1>
|
<h1>Account management for {{current_user.email}}</h1>
|
||||||
|
|
||||||
<a href='{{ url_for('security.change_password') }}'>Change password</a>
|
<a href='{{ url_for('security.change_password') }}'>Change password</a>
|
||||||
|
|
||||||
|
<table>
|
||||||
|
{% for vehicle in current_user.vehicles %}
|
||||||
|
<tr>
|
||||||
|
<td>{{vehicle.name}}</td>
|
||||||
|
<td><a href="{{ url_for('edit_vehicle', vid=vehicle.id) }}">edit</a></td>
|
||||||
|
</tr>
|
||||||
|
{% endfor %}
|
||||||
|
</table>
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
|
|
@ -0,0 +1,11 @@
|
||||||
|
{% extends "layout.html" %}
|
||||||
|
|
||||||
|
{% block body %}
|
||||||
|
<form class='form-horizontal' method="POST">
|
||||||
|
{{ form.hidden_tag() }}
|
||||||
|
{{ render_field_with_errors(form.name) }}
|
||||||
|
<input type="submit" value="Go">
|
||||||
|
</form>
|
||||||
|
|
||||||
|
|
||||||
|
{% endblock %}
|
Loading…
Reference in New Issue