first iteration of vehicles

This commit is contained in:
Joachim Lusiardi 2016-04-19 06:47:10 +02:00
parent 2e0fc3b772
commit baca41619d
2 changed files with 50 additions and 5 deletions

View File

@ -7,11 +7,12 @@ from flask import url_for
from flask_sqlalchemy import SQLAlchemy from flask_sqlalchemy import SQLAlchemy
from flask.ext.security import Security, SQLAlchemyUserDatastore, \ from flask.ext.security import Security, SQLAlchemyUserDatastore, \
UserMixin, RoleMixin, login_required, utils UserMixin, RoleMixin, login_required, utils
from flask.ext.security.core import current_user
import uuid import uuid
import hashlib import hashlib
from functools import wraps from functools import wraps
from flask_wtf import Form from flask_wtf import Form
from wtforms import DateField, IntegerField, DecimalField from wtforms import DateField, IntegerField, DecimalField, SelectField
from wtforms.validators import DataRequired, ValidationError from wtforms.validators import DataRequired, ValidationError
import os import os
@ -49,6 +50,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,
@ -56,7 +60,22 @@ class User(db.Model, UserMixin):
) )
def __repr__(self): def __repr__(self):
return '<User %r>' % self.username 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):
@ -64,6 +83,7 @@ class Pitstop(db.Model):
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
@ -99,6 +119,23 @@ def before_first_request():
user_datastore.add_role_to_user('admin@example.com', 'admin') user_datastore.add_role_to_user('admin@example.com', 'admin')
db.session.commit() 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 @app.before_request
def before_request(): def before_request():
@ -127,6 +164,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])
@ -145,9 +183,15 @@ 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()
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'))

View File

@ -3,6 +3,7 @@
{% block body %} {% block body %}
<form class='form-horizontal' method="POST"> <form class='form-horizontal' method="POST">
{{ form.hidden_tag() }} {{ form.hidden_tag() }}
{{ render_field_with_errors(form.vehicle) }}
{{ render_field_with_errors(form.date) }} {{ render_field_with_errors(form.date) }}
{{ render_field_with_errors(form.odometer) }} {{ render_field_with_errors(form.odometer) }}
{{ render_field_with_errors(form.litres) }} {{ render_field_with_errors(form.litres) }}