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'))
|
||||
|
||||
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:
|
||||
form.name.default = form.name.data
|
||||
|
||||
if form.validate_on_submit():
|
||||
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:
|
||||
db.session.commit()
|
||||
tools.db_log_update(vehicle)
|
||||
|
@ -167,13 +177,25 @@ def delete_vehicle(vid):
|
|||
@login_required
|
||||
def create_vehicle():
|
||||
form = EditVehicleForm()
|
||||
form.consumables.choices = [(g.id, g.name) for g in Consumable.query.all()]
|
||||
|
||||
if form.name.data is not None:
|
||||
form.name.default = form.name.data
|
||||
|
||||
if form.consumables.data:
|
||||
form.consumables.default = form.consumables.data
|
||||
|
||||
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
|
||||
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)
|
||||
current_user.vehicles.append(new_vehicle)
|
||||
try:
|
||||
|
@ -274,7 +296,7 @@ def edit_pit_stop_form(pid):
|
|||
if last_pitstop_pos > 0:
|
||||
last_pitstop = vehicle.pitstops[last_pitstop_pos]
|
||||
else:
|
||||
last_pitstop = Pitstop(0, 0, date(1970, 1, 1))
|
||||
last_pitstop = Pitstop(0, 0, date(1970, 1, 1), 0, 0)
|
||||
|
||||
form = EditPitstopForm()
|
||||
form.set_pitstop(last_pitstop)
|
||||
|
@ -320,7 +342,7 @@ def get_admin_page():
|
|||
users = User.query.all()
|
||||
consumables = Consumable.query.all()
|
||||
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)
|
||||
|
||||
|
||||
|
|
|
@ -50,8 +50,7 @@ class Vehicle(db.Model):
|
|||
)
|
||||
consumables = db.relationship(
|
||||
'Consumable',
|
||||
secondary=vehicles_consumables,
|
||||
backref=db.backref('consumes', lazy='dynamic')
|
||||
secondary=vehicles_consumables
|
||||
)
|
||||
__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)
|
||||
name = db.Column(db.String(255), unique=True)
|
||||
unit = db.Column(db.String(255))
|
||||
vehicles = db.relationship(
|
||||
'Vehicle',
|
||||
secondary=vehicles_consumables
|
||||
)
|
||||
|
||||
def __init__(self, name, unit):
|
||||
self.name = name
|
||||
|
|
|
@ -27,7 +27,7 @@ def 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:
|
||||
raise ValidationError('Costs must be above 0.01 €.')
|
||||
|
||||
|
@ -51,7 +51,7 @@ class CreatePitstopForm(Form):
|
|||
|
||||
class EditVehicleForm(Form):
|
||||
name = StringField('Name', validators=[Length(1, 255)])
|
||||
#consumables = SelectMultipleField('Consumables')
|
||||
consumables = SelectMultipleField('Consumables', coerce=int,validators=[])
|
||||
submit = SubmitField(label='Do it!')
|
||||
|
||||
|
||||
|
|
|
@ -36,7 +36,8 @@
|
|||
{{ vehicle.name }}
|
||||
</td>
|
||||
<td>
|
||||
{{ vehicle.pitstops | length }} pitstops
|
||||
{{ vehicle.pitstops | length }} pitstops<br />
|
||||
{{ vehicle.consumables | length }} consumables
|
||||
</td>
|
||||
<td>
|
||||
<a href="{{ url_for('edit_vehicle', vid=vehicle.id) }}" class="btn btn-primary " role="button">
|
||||
|
|
|
@ -29,6 +29,9 @@
|
|||
<th>
|
||||
Unit
|
||||
</th>
|
||||
<th>
|
||||
Used by
|
||||
</th>
|
||||
<th>
|
||||
Actions
|
||||
</th>
|
||||
|
@ -42,14 +45,17 @@
|
|||
{{ consumable.unit }}
|
||||
</td>
|
||||
<td>
|
||||
<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>
|
||||
{{ consumable.vehicles | length }} vehicles
|
||||
</td>
|
||||
<td>
|
||||
{% if not consumable.in_use %}
|
||||
<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
|
||||
</a>
|
||||
{% 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>
|
||||
</tr>
|
||||
{% endfor %}
|
||||
|
|
|
@ -9,6 +9,7 @@
|
|||
<form class='form-horizontal' method="POST">
|
||||
{{ form.hidden_tag() }}
|
||||
{{ render_field_with_errors(form.name) }}
|
||||
{{ render_field_with_errors(form.consumables) }}
|
||||
{{ render_field_with_errors(form.submit) }}
|
||||
</form>
|
||||
</div>
|
||||
|
|
|
@ -9,6 +9,7 @@
|
|||
<form class='form-horizontal' method="POST">
|
||||
{{ form.hidden_tag() }}
|
||||
{{ render_field_with_errors(form.name) }}
|
||||
{{ render_field_with_errors(form.consumables) }}
|
||||
{{ render_field_with_errors(form.submit) }}
|
||||
</form>
|
||||
</div>
|
||||
|
|
Loading…
Reference in New Issue