finished modules merge

This commit is contained in:
Joachim Lusiardi 2016-04-23 23:24:25 +02:00
parent 7bc5422687
commit c941e1bccf
3 changed files with 95 additions and 13 deletions

View File

@ -25,12 +25,15 @@ mail = Mail(app)
from rollerverbrauch.forms import \ from rollerverbrauch.forms import \
CreatePitstopForm CreatePitstopForm, \
EditVehicleForm, \
DeleteVehicleForm
from rollerverbrauch.entities import \ from rollerverbrauch.entities import \
User, \ User, \
Role, \ Role, \
Pitstop Pitstop, \
Vehicle
user_datastore = SQLAlchemyUserDatastore(db, User, Role) user_datastore = SQLAlchemyUserDatastore(db, User, Role)
@ -44,6 +47,10 @@ def user_registered_sighandler(app, user, confirm_token):
""" """
role = user_datastore.find_role('user') role = user_datastore.find_role('user')
user_datastore.add_role_to_user(user, role) 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 @app.before_first_request
@ -65,6 +72,51 @@ def index():
return redirect(url_for('get_pit_stops')) 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('/account/delete_vehicle/<int:vid>', 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']) @app.route('/pitstops/createForm', methods=['GET', 'POST'])
@login_required @login_required
def create_pit_stop_form(): def create_pit_stop_form():
@ -74,13 +126,16 @@ def create_pit_stop_form():
form = CreatePitstopForm() form = CreatePitstopForm()
form.set_pitstop(last_pitstop) form.set_pitstop(last_pitstop)
form.vehicle.choices = [(g.id, g.name) for g in current_user.vehicles]
if form.validate_on_submit(): 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) 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)
db.session.commit() db.session.commit()
return redirect(url_for('get_pit_stops')) return redirect(url_for('get_pit_stops'))
# dynamically set values
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()
@ -144,7 +199,6 @@ def get_statistics():
g.data['averageListresUsed'] = average_litres_used g.data['averageListresUsed'] = average_litres_used
return render_template('statistics.html', data=g.data) return render_template('statistics.html', data=g.data)
def prepare_pit_stops(pss): def prepare_pit_stops(pss):
pitstops = [] pitstops = []
for pitstop_index in range(0, len(pss)): for pitstop_index in range(0, len(pss)):
@ -163,4 +217,3 @@ def prepare_pit_stops(pss):
curr['days'] = (curr_date - last_date).days curr['days'] = (curr_date - last_date).days
pitstops.reverse() pitstops.reverse()
return pitstops return pitstops

View File

@ -2,8 +2,8 @@ from rollerverbrauch import db
from flask.ext.security import UserMixin, RoleMixin from flask.ext.security import UserMixin, RoleMixin
roles_users = db.Table('roles_users', roles_users = db.Table('roles_users',
db.Column('user_id', db.Integer(), db.ForeignKey('user.id')), db.Column('user_id', db.Integer(), db.ForeignKey('user.id')),
db.Column('role_id', db.Integer(), db.ForeignKey('role.id'))) db.Column('role_id', db.Integer(), db.ForeignKey('role.id')))
class Role(db.Model, RoleMixin): class Role(db.Model, RoleMixin):
@ -24,6 +24,9 @@ class User(db.Model, UserMixin):
password = db.Column(db.String(255)) password = db.Column(db.String(255))
active = db.Column(db.Boolean()) active = db.Column(db.Boolean())
confirmed_at = db.Column(db.DateTime()) confirmed_at = db.Column(db.DateTime())
vehicles = db.relationship(
'Vehicle'
)
roles = db.relationship( roles = db.relationship(
'Role', 'Role',
secondary=roles_users, secondary=roles_users,
@ -34,11 +37,27 @@ class User(db.Model, UserMixin):
return '<User %r>' % self.email return '<User %r>' % 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 '<Vehicle %r>' % self.name
class Pitstop(db.Model): class Pitstop(db.Model):
id = db.Column(db.Integer, primary_key=True) id = db.Column(db.Integer, primary_key=True)
date = db.Column(db.Date) date = db.Column(db.Date)
odometer = db.Column(db.Integer) 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): def __init__(self, odometer, litres, date):
self.odometer = odometer self.odometer = odometer
@ -47,4 +66,3 @@ class Pitstop(db.Model):
def __repr__(self): def __repr__(self):
return '<Pitstop %r km, %r l>' % (self.odometer, self.litres) return '<Pitstop %r km, %r l>' % (self.odometer, self.litres)

View File

@ -1,6 +1,6 @@
from flask_wtf import Form from flask_wtf import Form
from wtforms import DateField, IntegerField, DecimalField from wtforms import DateField, IntegerField, DecimalField, StringField, SelectField
from wtforms.validators import ValidationError from wtforms.validators import ValidationError, Length
def date_check(form, field): def date_check(form, field):
@ -19,6 +19,7 @@ def litres_check(form, field):
class CreatePitstopForm(Form): class CreatePitstopForm(Form):
vehicle = SelectField('Vehicle', coerce=int)
date = DateField('Date of Pitstop', validators=[date_check]) date = DateField('Date of Pitstop', validators=[date_check])
odometer = IntegerField('Odometer (km)', validators=[odometer_check]) odometer = IntegerField('Odometer (km)', validators=[odometer_check])
litres = DecimalField('Litres (l)', places=1, validators=[litres_check]) litres = DecimalField('Litres (l)', places=1, validators=[litres_check])
@ -28,3 +29,13 @@ class CreatePitstopForm(Form):
self.pitstop = pitstop self.pitstop = pitstop
class EditVehicleForm(Form):
name = StringField('Name', validators=[Length(1, 255)])
class DeleteVehicleForm(Form):
pass