adds consumables to database
this adds a new consumables entity to the database and the possibility in the admin page to add these.
This commit is contained in:
parent
56757f29a1
commit
1072142a1b
|
@ -31,13 +31,15 @@ from rollerverbrauch.forms import \
|
|||
SelectVehicleForm, \
|
||||
DeleteAccountForm, \
|
||||
DeletePitStopForm, \
|
||||
EditPitstopForm
|
||||
EditPitstopForm, \
|
||||
CreateConsumableForm
|
||||
|
||||
from rollerverbrauch.entities import \
|
||||
User, \
|
||||
Role, \
|
||||
Pitstop, \
|
||||
Vehicle
|
||||
Vehicle, \
|
||||
Consumable
|
||||
|
||||
# required to activate the filters
|
||||
import rollerverbrauch.filters
|
||||
|
@ -65,6 +67,11 @@ def user_registered_sighandler(app, user, confirm_token):
|
|||
@app.before_first_request
|
||||
def before_first_request():
|
||||
db.create_all()
|
||||
print("""
|
||||
=========================
|
||||
TEST
|
||||
========================
|
||||
""")
|
||||
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()
|
||||
|
@ -105,6 +112,7 @@ def index():
|
|||
def edit_vehicle(vid):
|
||||
vehicle = Vehicle.query.filter(Vehicle.id == vid).first()
|
||||
form = EditVehicleForm()
|
||||
#form.consumables.choices = [(g.id, '%s (%s)' % (g.name, g.unit)) for g in Consumable.query.all()]
|
||||
|
||||
if form.validate_on_submit():
|
||||
if not tools.check_vehicle_name_is_unique(current_user, form.name):
|
||||
|
@ -115,8 +123,10 @@ def edit_vehicle(vid):
|
|||
return redirect(url_for('get_account_page'))
|
||||
|
||||
form.name.default = vehicle.name
|
||||
#form.consumables.default = [g.id for g in vehicle.consumables]
|
||||
#print(form.consumables.name)
|
||||
form.process()
|
||||
return render_template('editVehicleForm.html', form=form)
|
||||
return render_template('editVehicleForm.html', form=form, vehicle=vehicle)
|
||||
|
||||
|
||||
@app.route('/account/delete_vehicle/<int:vid>', methods=['GET', 'POST'])
|
||||
|
@ -287,8 +297,24 @@ def get_manual():
|
|||
@app.route('/admin', methods=['GET'])
|
||||
@roles_required('admin')
|
||||
def get_admin_page():
|
||||
g.data['users'] = User.query.all()
|
||||
return render_template('admin.html', data=g.data)
|
||||
users = User.query.all()
|
||||
consumables = Consumable.query.all()
|
||||
return render_template('admin.html', users=users, consumables=consumables)
|
||||
|
||||
|
||||
@app.route('/admin/create_consumable', methods=['GET', 'POST'])
|
||||
@login_required
|
||||
def create_consumable():
|
||||
form = CreateConsumableForm()
|
||||
|
||||
if form.validate_on_submit():
|
||||
new_consumable = Consumable(form.name.data, form.unit.data)
|
||||
db.session.add(new_consumable)
|
||||
db.session.commit()
|
||||
tools.db_log_add(new_consumable)
|
||||
return redirect(url_for('get_admin_page'))
|
||||
|
||||
return render_template('createConsumableForm.html', form=form)
|
||||
|
||||
|
||||
@app.route('/account', methods=['GET'])
|
||||
|
|
|
@ -5,6 +5,10 @@ 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')))
|
||||
|
||||
vehicles_consumables = db.Table('vehicles_consumables',
|
||||
db.Column('vehicle_id', db.Integer(), db.ForeignKey('vehicle.id')),
|
||||
db.Column('consumable_id', db.Integer(), db.ForeignKey('consumable.id')))
|
||||
|
||||
|
||||
class Role(db.Model, RoleMixin):
|
||||
id = db.Column(db.Integer(), primary_key=True)
|
||||
|
@ -44,6 +48,11 @@ class Vehicle(db.Model):
|
|||
pitstops = db.relationship(
|
||||
'Pitstop'
|
||||
)
|
||||
consumables = db.relationship(
|
||||
'Consumable',
|
||||
secondary=vehicles_consumables,
|
||||
backref=db.backref('consumes', lazy='dynamic')
|
||||
)
|
||||
__table_args__ = (db.UniqueConstraint('owner_id', 'name', name='_owner_name_uniq'),)
|
||||
|
||||
def __init__(self, name):
|
||||
|
@ -68,4 +77,18 @@ class Pitstop(db.Model):
|
|||
self.costs = costs
|
||||
|
||||
def __repr__(self):
|
||||
return '<Pitstop odometer="%r" litres="%r" date="%r" vehicle_id="%r">' % (self.odometer, self.litres, self.date, self.vehicle_id)
|
||||
return '<Pitstop odometer="%r" litres="%r" date="%r" vehicle_id="%r">' % \
|
||||
(self.odometer, self.litres, self.date, self.vehicle_id)
|
||||
|
||||
|
||||
class Consumable(db.Model):
|
||||
id = db.Column(db.Integer, primary_key=True)
|
||||
name = db.Column(db.String(255))
|
||||
unit = db.Column(db.String(255))
|
||||
|
||||
def __init__(self, name, unit):
|
||||
self.name = name
|
||||
self.unit = unit
|
||||
|
||||
def __repr__(self):
|
||||
return '<Consumable name="%s" unit="%s" />' % (self.name, self.unit)
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
from flask_wtf import Form
|
||||
from wtforms import DateField, IntegerField, DecimalField, StringField, SelectField, SubmitField
|
||||
from wtforms import DateField, IntegerField, DecimalField, StringField, SelectField, SubmitField, SelectMultipleField, BooleanField
|
||||
from wtforms.validators import ValidationError, Length
|
||||
from datetime import date
|
||||
|
||||
|
@ -31,6 +31,7 @@ def edit_costs_check(form, field):
|
|||
if costs_check_required and field.data is not None and field.data <= 0:
|
||||
raise ValidationError('Costs must be above 0.01 €.')
|
||||
|
||||
|
||||
class SelectVehicleForm(Form):
|
||||
vehicle = SelectField('Vehicle', coerce=int)
|
||||
submit = SubmitField(label='Do it!')
|
||||
|
@ -50,6 +51,7 @@ class CreatePitstopForm(Form):
|
|||
|
||||
class EditVehicleForm(Form):
|
||||
name = StringField('Name', validators=[Length(1, 255)])
|
||||
#consumables = SelectMultipleField('Consumables')
|
||||
submit = SubmitField(label='Do it!')
|
||||
|
||||
|
||||
|
@ -77,3 +79,8 @@ class EditPitstopForm(Form):
|
|||
self.last_pitstop = last_pitstop
|
||||
|
||||
|
||||
class CreateConsumableForm(Form):
|
||||
name = StringField('Name', validators=[Length(1, 255)])
|
||||
unit = StringField('Unit', validators=[Length(1, 255)])
|
||||
submit = SubmitField(label='Do it!')
|
||||
|
||||
|
|
|
@ -1,18 +1,52 @@
|
|||
{% extends "layout.html" %}
|
||||
|
||||
{% block body %}
|
||||
<div class="col-md-2" ></div>
|
||||
<div class="col-md-8">
|
||||
<div class="panel panel-default">
|
||||
<div class="panel-body">
|
||||
<h3>Admin</h3>
|
||||
We have {{ data.users|length }} users so far:
|
||||
<div class="panel panel-default">
|
||||
<div class="panel-heading">Users</div>
|
||||
<div class="panel-body">
|
||||
We have {{ users|length }} users so far:
|
||||
<ul>
|
||||
{% for user in data.users %}
|
||||
{% for user in users %}
|
||||
<li>{{user.email}}</li>
|
||||
{% endfor %}
|
||||
</ul>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="panel panel-default">
|
||||
<div class="panel-heading">Consumables</div>
|
||||
<div class="panel-body">
|
||||
<a href="{{ url_for('create_consumable') }}" class="btn btn-primary " role="button">
|
||||
<span class="glyphicon glyphicon-plus" aria-hidden="true"></span> create
|
||||
</a>
|
||||
</div>
|
||||
<table class="table table-striped table-bordered">
|
||||
<tbody>
|
||||
<tr>
|
||||
<th>
|
||||
Name
|
||||
</th>
|
||||
<th>
|
||||
Unit
|
||||
</th>
|
||||
<th>
|
||||
Actions
|
||||
</th>
|
||||
</tr>
|
||||
{% for consumable in consumables %}
|
||||
<tr>
|
||||
<td>
|
||||
{{ consumable.name }}
|
||||
</td>
|
||||
<td>
|
||||
{{ consumable.unit }}
|
||||
</td>
|
||||
<td>
|
||||
</td>
|
||||
</tr>
|
||||
{% endfor %}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
|
||||
{% endblock %}
|
||||
|
|
Loading…
Reference in New Issue