diff --git a/app/rollerverbrauch/__init__.py b/app/rollerverbrauch/__init__.py index 544fbe0..6811f1c 100644 --- a/app/rollerverbrauch/__init__.py +++ b/app/rollerverbrauch/__init__.py @@ -25,12 +25,15 @@ mail = Mail(app) from rollerverbrauch.forms import \ - CreatePitstopForm + CreatePitstopForm, \ + EditVehicleForm, \ + DeleteVehicleForm from rollerverbrauch.entities import \ User, \ Role, \ - Pitstop + Pitstop, \ + Vehicle user_datastore = SQLAlchemyUserDatastore(db, User, Role) @@ -44,6 +47,10 @@ def user_registered_sighandler(app, user, confirm_token): """ 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 @@ -65,6 +72,51 @@ 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('/account/delete_vehicle/', methods=['GET', 'POST']) +@login_required +def delete_vehicle(vid): + vehicle = Vehicle.query.filter(Vehicle.id == vid).first() + form = DeleteVehicleForm() + + if form.validate_on_submit(): + db.session.delete(vehicle) + db.session.commit() + return redirect(url_for('get_account_page')) + + return render_template('deleteVehicleForm.html', form=form, vehicle=vehicle) + + +@app.route('/account/create_vehicle', methods=['GET', 'POST']) +@login_required +def create_vehicle(): + form = EditVehicleForm() + + if form.validate_on_submit(): + new_vehicle = Vehicle(form.name.data) + db.session.add(new_vehicle) + current_user.vehicles.append(new_vehicle) + db.session.commit() + return redirect(url_for('get_account_page')) + + return render_template('createVehicleForm.html', form=form) + + @app.route('/pitstops/createForm', methods=['GET', 'POST']) @login_required def create_pit_stop_form(): @@ -74,13 +126,16 @@ def create_pit_stop_form(): form = CreatePitstopForm() form.set_pitstop(last_pitstop) + form.vehicle.choices = [(g.id, g.name) for g in current_user.vehicles] + if form.validate_on_submit(): + 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) db.session.commit() return redirect(url_for('get_pit_stops')) - # dynamically set values form.odometer.default = last_pitstop.odometer form.litres.default = last_pitstop.litres form.date.default = date.today() @@ -144,7 +199,6 @@ def get_statistics(): g.data['averageListresUsed'] = average_litres_used return render_template('statistics.html', data=g.data) - def prepare_pit_stops(pss): pitstops = [] for pitstop_index in range(0, len(pss)): @@ -162,5 +216,4 @@ def prepare_pit_stops(pss): curr_date = curr['date'] curr['days'] = (curr_date - last_date).days pitstops.reverse() - return pitstops - + return pitstops \ No newline at end of file diff --git a/app/rollerverbrauch/entities.py b/app/rollerverbrauch/entities.py index f1dba99..8c40b1c 100644 --- a/app/rollerverbrauch/entities.py +++ b/app/rollerverbrauch/entities.py @@ -2,8 +2,8 @@ from rollerverbrauch import db from flask.ext.security import UserMixin, RoleMixin roles_users = db.Table('roles_users', - db.Column('user_id', db.Integer(), db.ForeignKey('user.id')), - db.Column('role_id', db.Integer(), db.ForeignKey('role.id'))) + db.Column('user_id', db.Integer(), db.ForeignKey('user.id')), + db.Column('role_id', db.Integer(), db.ForeignKey('role.id'))) class Role(db.Model, RoleMixin): @@ -24,6 +24,9 @@ class User(db.Model, UserMixin): password = db.Column(db.String(255)) active = db.Column(db.Boolean()) confirmed_at = db.Column(db.DateTime()) + vehicles = db.relationship( + 'Vehicle' + ) roles = db.relationship( 'Role', secondary=roles_users, @@ -34,11 +37,27 @@ class User(db.Model, UserMixin): return '' % self.email +class Vehicle(db.Model): + id = db.Column(db.Integer, primary_key=True) + owner_id = db.Column(db.Integer, db.ForeignKey('user.id')) + name = db.Column(db.String(255), unique=True) + pitstops = db.relationship( + 'Pitstop' + ) + + def __init__(self, name): + self.name = name + + def __repr__(self): + return '' % self.name + + class Pitstop(db.Model): id = db.Column(db.Integer, primary_key=True) date = db.Column(db.Date) odometer = db.Column(db.Integer) - litres = db.Column(db.Numeric(5,2)) + litres = db.Column(db.Numeric(5, 2)) + vehicle_id = db.Column(db.Integer, db.ForeignKey('vehicle.id')) def __init__(self, odometer, litres, date): self.odometer = odometer @@ -46,5 +65,4 @@ class Pitstop(db.Model): self.date = date def __repr__(self): - return '' % (self.odometer, self.litres) - + return '' % (self.odometer, self.litres) \ No newline at end of file diff --git a/app/rollerverbrauch/forms.py b/app/rollerverbrauch/forms.py index 1a317be..a5402b2 100644 --- a/app/rollerverbrauch/forms.py +++ b/app/rollerverbrauch/forms.py @@ -1,6 +1,6 @@ from flask_wtf import Form -from wtforms import DateField, IntegerField, DecimalField -from wtforms.validators import ValidationError +from wtforms import DateField, IntegerField, DecimalField, StringField, SelectField +from wtforms.validators import ValidationError, Length def date_check(form, field): @@ -19,6 +19,7 @@ def litres_check(form, field): class CreatePitstopForm(Form): + vehicle = SelectField('Vehicle', coerce=int) date = DateField('Date of Pitstop', validators=[date_check]) odometer = IntegerField('Odometer (km)', validators=[odometer_check]) litres = DecimalField('Litres (l)', places=1, validators=[litres_check]) @@ -28,3 +29,13 @@ class CreatePitstopForm(Form): self.pitstop = pitstop +class EditVehicleForm(Form): + name = StringField('Name', validators=[Length(1, 255)]) + + +class DeleteVehicleForm(Form): + pass + + + +