diff --git a/app/main.py b/app/main.py index b7de3dd..3db885a 100644 --- a/app/main.py +++ b/app/main.py @@ -14,8 +14,8 @@ from flask.ext.security import user_registered from flask.ext.mail import Mail, Message from flask_security.core import current_user from flask_wtf import Form -from wtforms import DateField, IntegerField, DecimalField, SelectField -from wtforms.validators import DataRequired, ValidationError +from wtforms import DateField, IntegerField, StringField, DecimalField, SelectField +from wtforms.validators import DataRequired, ValidationError, Length import os app = Flask(__name__) @@ -99,55 +99,6 @@ class Pitstop(db.Model): return '' % (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): if field.data < form.pitstop.date: raise ValidationError('The new date must after %s' % form.pitstop.date) @@ -174,6 +125,62 @@ class CreatePitstopForm(Form): 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/', 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']) @login_required def create_pit_stop_form(): @@ -183,12 +190,9 @@ def create_pit_stop_form(): form = CreatePitstopForm() 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(): - 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) db.session.add(new_stop) v.pitstops.append(new_stop) @@ -196,6 +200,7 @@ def create_pit_stop_form(): return redirect(url_for('get_pit_stops')) # dynamically set values + form.vehicle.choices = [(g.id, g.name) for g in current_user.vehicles] form.odometer.default = last_pitstop.odometer form.litres.default = last_pitstop.litres form.date.default = date.today() diff --git a/app/templates/account.html b/app/templates/account.html index de46b10..d111351 100644 --- a/app/templates/account.html +++ b/app/templates/account.html @@ -4,4 +4,13 @@

Account management for {{current_user.email}}

Change password + + + {% for vehicle in current_user.vehicles %} + + + + + {% endfor %} +
{{vehicle.name}}edit
{% endblock %} diff --git a/app/templates/editVehicleForm.html b/app/templates/editVehicleForm.html new file mode 100644 index 0000000..09360ac --- /dev/null +++ b/app/templates/editVehicleForm.html @@ -0,0 +1,11 @@ +{% extends "layout.html" %} + +{% block body %} +
+ {{ form.hidden_tag() }} + {{ render_field_with_errors(form.name) }} + +
+ + +{% endblock %}