From 907d0435d1dbd0eff22490f097dd6673e48ad258 Mon Sep 17 00:00:00 2001 From: Joachim Lusiardi Date: Tue, 28 Jun 2016 23:33:24 +0200 Subject: [PATCH] consumables for vehicles can be edited --- app/rollerverbrauch/__init__.py | 26 +++++++++++++++++-- app/rollerverbrauch/entities.py | 7 +++-- app/rollerverbrauch/forms.py | 4 +-- app/rollerverbrauch/templates/account.html | 3 ++- app/rollerverbrauch/templates/admin.html | 12 ++++++--- .../templates/createVehicleForm.html | 1 + .../templates/editVehicleForm.html | 1 + 7 files changed, 44 insertions(+), 10 deletions(-) diff --git a/app/rollerverbrauch/__init__.py b/app/rollerverbrauch/__init__.py index 8f30bb6..57fd72a 100644 --- a/app/rollerverbrauch/__init__.py +++ b/app/rollerverbrauch/__init__.py @@ -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) diff --git a/app/rollerverbrauch/entities.py b/app/rollerverbrauch/entities.py index d63a3dc..0a9c06e 100644 --- a/app/rollerverbrauch/entities.py +++ b/app/rollerverbrauch/entities.py @@ -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 diff --git a/app/rollerverbrauch/forms.py b/app/rollerverbrauch/forms.py index 84c85e9..ffc33ac 100644 --- a/app/rollerverbrauch/forms.py +++ b/app/rollerverbrauch/forms.py @@ -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!') diff --git a/app/rollerverbrauch/templates/account.html b/app/rollerverbrauch/templates/account.html index cc1ed3a..43a0d33 100644 --- a/app/rollerverbrauch/templates/account.html +++ b/app/rollerverbrauch/templates/account.html @@ -36,7 +36,8 @@ {{ vehicle.name }} - {{ vehicle.pitstops | length }} pitstops + {{ vehicle.pitstops | length }} pitstops
+ {{ vehicle.consumables | length }} consumables diff --git a/app/rollerverbrauch/templates/admin.html b/app/rollerverbrauch/templates/admin.html index e9ec71c..8bee9ef 100644 --- a/app/rollerverbrauch/templates/admin.html +++ b/app/rollerverbrauch/templates/admin.html @@ -29,6 +29,9 @@ Unit + + Used by + Actions @@ -42,14 +45,17 @@ {{ consumable.unit }} - - edit - + {{ consumable.vehicles | length }} vehicles + + {% if not consumable.in_use %} delete {% endif %} + + edit + {% endfor %} diff --git a/app/rollerverbrauch/templates/createVehicleForm.html b/app/rollerverbrauch/templates/createVehicleForm.html index 2dce35b..9e5ced5 100644 --- a/app/rollerverbrauch/templates/createVehicleForm.html +++ b/app/rollerverbrauch/templates/createVehicleForm.html @@ -9,6 +9,7 @@
{{ form.hidden_tag() }} {{ render_field_with_errors(form.name) }} + {{ render_field_with_errors(form.consumables) }} {{ render_field_with_errors(form.submit) }}
diff --git a/app/rollerverbrauch/templates/editVehicleForm.html b/app/rollerverbrauch/templates/editVehicleForm.html index 8d2827f..c5b18dd 100644 --- a/app/rollerverbrauch/templates/editVehicleForm.html +++ b/app/rollerverbrauch/templates/editVehicleForm.html @@ -9,6 +9,7 @@
{{ form.hidden_tag() }} {{ render_field_with_errors(form.name) }} + {{ render_field_with_errors(form.consumables) }} {{ render_field_with_errors(form.submit) }}