consumables for vehicles can be edited
This commit is contained in:
parent
fe4236eead
commit
907d0435d1
|
@ -120,12 +120,22 @@ def edit_vehicle(vid):
|
||||||
return redirect(url_for('get_account_page'))
|
return redirect(url_for('get_account_page'))
|
||||||
|
|
||||||
form = EditVehicleForm()
|
form = EditVehicleForm()
|
||||||
|
form.consumables.choices = [(g.id, g.name) for g in Consumable.query.all()]
|
||||||
|
|
||||||
|
if not form.consumables.data:
|
||||||
|
form.consumables.default = [g.id for g in vehicle.consumables]
|
||||||
|
|
||||||
if form.name.data is not None:
|
if form.name.data is not None:
|
||||||
form.name.default = form.name.data
|
form.name.default = form.name.data
|
||||||
|
|
||||||
if form.validate_on_submit():
|
if form.validate_on_submit():
|
||||||
vehicle.name = form.name.data
|
vehicle.name = form.name.data
|
||||||
|
# we cannot delete consumables where there are pitstops for => report error
|
||||||
|
vehicle.consumables = []
|
||||||
|
for consumable_id in form.consumables.data:
|
||||||
|
consumable = Consumable.query.get(consumable_id)
|
||||||
|
if consumable is not None:
|
||||||
|
vehicle.consumables.append(consumable)
|
||||||
try:
|
try:
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
tools.db_log_update(vehicle)
|
tools.db_log_update(vehicle)
|
||||||
|
@ -167,13 +177,25 @@ def delete_vehicle(vid):
|
||||||
@login_required
|
@login_required
|
||||||
def create_vehicle():
|
def create_vehicle():
|
||||||
form = EditVehicleForm()
|
form = EditVehicleForm()
|
||||||
|
form.consumables.choices = [(g.id, g.name) for g in Consumable.query.all()]
|
||||||
|
|
||||||
if form.name.data is not None:
|
if form.name.data is not None:
|
||||||
form.name.default = form.name.data
|
form.name.default = form.name.data
|
||||||
|
|
||||||
|
if form.consumables.data:
|
||||||
|
form.consumables.default = form.consumables.data
|
||||||
|
|
||||||
if form.validate_on_submit():
|
if form.validate_on_submit():
|
||||||
|
if len(form.consumables.data) == 0:
|
||||||
|
form.consumables.errors.append('At least one consumable must be selected.')
|
||||||
|
return render_template('createVehicleForm.html', form=form)
|
||||||
|
|
||||||
vehicle_name = form.name.data
|
vehicle_name = form.name.data
|
||||||
new_vehicle = Vehicle(vehicle_name)
|
new_vehicle = Vehicle(vehicle_name)
|
||||||
|
for consumable_id in form.consumables.data:
|
||||||
|
consumable = Consumable.query.get(consumable_id)
|
||||||
|
if consumable is not None:
|
||||||
|
new_vehicle.consumables.append(consumable)
|
||||||
db.session.add(new_vehicle)
|
db.session.add(new_vehicle)
|
||||||
current_user.vehicles.append(new_vehicle)
|
current_user.vehicles.append(new_vehicle)
|
||||||
try:
|
try:
|
||||||
|
@ -274,7 +296,7 @@ def edit_pit_stop_form(pid):
|
||||||
if last_pitstop_pos > 0:
|
if last_pitstop_pos > 0:
|
||||||
last_pitstop = vehicle.pitstops[last_pitstop_pos]
|
last_pitstop = vehicle.pitstops[last_pitstop_pos]
|
||||||
else:
|
else:
|
||||||
last_pitstop = Pitstop(0, 0, date(1970, 1, 1))
|
last_pitstop = Pitstop(0, 0, date(1970, 1, 1), 0, 0)
|
||||||
|
|
||||||
form = EditPitstopForm()
|
form = EditPitstopForm()
|
||||||
form.set_pitstop(last_pitstop)
|
form.set_pitstop(last_pitstop)
|
||||||
|
@ -320,7 +342,7 @@ def get_admin_page():
|
||||||
users = User.query.all()
|
users = User.query.all()
|
||||||
consumables = Consumable.query.all()
|
consumables = Consumable.query.all()
|
||||||
for consumable in consumables:
|
for consumable in consumables:
|
||||||
consumable.in_use = Pitstop.query.filter(Pitstop.consumable_id == consumable.id).count() > 0
|
consumable.in_use = len(consumable.vehicles) > 0
|
||||||
return render_template('admin.html', users=users, consumables=consumables)
|
return render_template('admin.html', users=users, consumables=consumables)
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -50,8 +50,7 @@ class Vehicle(db.Model):
|
||||||
)
|
)
|
||||||
consumables = db.relationship(
|
consumables = db.relationship(
|
||||||
'Consumable',
|
'Consumable',
|
||||||
secondary=vehicles_consumables,
|
secondary=vehicles_consumables
|
||||||
backref=db.backref('consumes', lazy='dynamic')
|
|
||||||
)
|
)
|
||||||
__table_args__ = (db.UniqueConstraint('owner_id', 'name', name='_owner_name_uniq'),)
|
__table_args__ = (db.UniqueConstraint('owner_id', 'name', name='_owner_name_uniq'),)
|
||||||
|
|
||||||
|
@ -88,6 +87,10 @@ class Consumable(db.Model):
|
||||||
id = db.Column(db.Integer, primary_key=True)
|
id = db.Column(db.Integer, primary_key=True)
|
||||||
name = db.Column(db.String(255), unique=True)
|
name = db.Column(db.String(255), unique=True)
|
||||||
unit = db.Column(db.String(255))
|
unit = db.Column(db.String(255))
|
||||||
|
vehicles = db.relationship(
|
||||||
|
'Vehicle',
|
||||||
|
secondary=vehicles_consumables
|
||||||
|
)
|
||||||
|
|
||||||
def __init__(self, name, unit):
|
def __init__(self, name, unit):
|
||||||
self.name = name
|
self.name = name
|
||||||
|
|
|
@ -27,7 +27,7 @@ def costs_check(form, field):
|
||||||
|
|
||||||
|
|
||||||
def edit_costs_check(form, field):
|
def edit_costs_check(form, field):
|
||||||
costs_check_required = (form.costs.default is not None and form.costs.default > 0)
|
costs_check_required = (form.costs.default is not None and form.costs.default > 0)
|
||||||
if costs_check_required and field.data is not None and field.data <= 0:
|
if costs_check_required and field.data is not None and field.data <= 0:
|
||||||
raise ValidationError('Costs must be above 0.01 €.')
|
raise ValidationError('Costs must be above 0.01 €.')
|
||||||
|
|
||||||
|
@ -51,7 +51,7 @@ class CreatePitstopForm(Form):
|
||||||
|
|
||||||
class EditVehicleForm(Form):
|
class EditVehicleForm(Form):
|
||||||
name = StringField('Name', validators=[Length(1, 255)])
|
name = StringField('Name', validators=[Length(1, 255)])
|
||||||
#consumables = SelectMultipleField('Consumables')
|
consumables = SelectMultipleField('Consumables', coerce=int,validators=[])
|
||||||
submit = SubmitField(label='Do it!')
|
submit = SubmitField(label='Do it!')
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -36,7 +36,8 @@
|
||||||
{{ vehicle.name }}
|
{{ vehicle.name }}
|
||||||
</td>
|
</td>
|
||||||
<td>
|
<td>
|
||||||
{{ vehicle.pitstops | length }} pitstops
|
{{ vehicle.pitstops | length }} pitstops<br />
|
||||||
|
{{ vehicle.consumables | length }} consumables
|
||||||
</td>
|
</td>
|
||||||
<td>
|
<td>
|
||||||
<a href="{{ url_for('edit_vehicle', vid=vehicle.id) }}" class="btn btn-primary " role="button">
|
<a href="{{ url_for('edit_vehicle', vid=vehicle.id) }}" class="btn btn-primary " role="button">
|
||||||
|
|
|
@ -29,6 +29,9 @@
|
||||||
<th>
|
<th>
|
||||||
Unit
|
Unit
|
||||||
</th>
|
</th>
|
||||||
|
<th>
|
||||||
|
Used by
|
||||||
|
</th>
|
||||||
<th>
|
<th>
|
||||||
Actions
|
Actions
|
||||||
</th>
|
</th>
|
||||||
|
@ -42,14 +45,17 @@
|
||||||
{{ consumable.unit }}
|
{{ consumable.unit }}
|
||||||
</td>
|
</td>
|
||||||
<td>
|
<td>
|
||||||
<a href="{{ url_for('edit_consumable', cid=consumable.id) }}" class="btn btn-primary " role="button">
|
{{ consumable.vehicles | length }} vehicles
|
||||||
<span class="glyphicon glyphicon-pencil" aria-hidden="true"></span> edit
|
</td>
|
||||||
</a>
|
<td>
|
||||||
{% if not consumable.in_use %}
|
{% if not consumable.in_use %}
|
||||||
<a href="{{ url_for('delete_consumable', cid=consumable.id) }}" class="btn btn-primary btn-warning " role="button">
|
<a href="{{ url_for('delete_consumable', cid=consumable.id) }}" class="btn btn-primary btn-warning " role="button">
|
||||||
<span class="glyphicon glyphicon-trash" aria-hidden="true"></span> delete
|
<span class="glyphicon glyphicon-trash" aria-hidden="true"></span> delete
|
||||||
</a>
|
</a>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
<a href="{{ url_for('edit_consumable', cid=consumable.id) }}" class="btn btn-primary " role="button">
|
||||||
|
<span class="glyphicon glyphicon-pencil" aria-hidden="true"></span> edit
|
||||||
|
</a>
|
||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
|
|
|
@ -9,6 +9,7 @@
|
||||||
<form class='form-horizontal' method="POST">
|
<form class='form-horizontal' method="POST">
|
||||||
{{ form.hidden_tag() }}
|
{{ form.hidden_tag() }}
|
||||||
{{ render_field_with_errors(form.name) }}
|
{{ render_field_with_errors(form.name) }}
|
||||||
|
{{ render_field_with_errors(form.consumables) }}
|
||||||
{{ render_field_with_errors(form.submit) }}
|
{{ render_field_with_errors(form.submit) }}
|
||||||
</form>
|
</form>
|
||||||
</div>
|
</div>
|
||||||
|
|
|
@ -9,6 +9,7 @@
|
||||||
<form class='form-horizontal' method="POST">
|
<form class='form-horizontal' method="POST">
|
||||||
{{ form.hidden_tag() }}
|
{{ form.hidden_tag() }}
|
||||||
{{ render_field_with_errors(form.name) }}
|
{{ render_field_with_errors(form.name) }}
|
||||||
|
{{ render_field_with_errors(form.consumables) }}
|
||||||
{{ render_field_with_errors(form.submit) }}
|
{{ render_field_with_errors(form.submit) }}
|
||||||
</form>
|
</form>
|
||||||
</div>
|
</div>
|
||||||
|
|
Loading…
Reference in New Issue