From fa4126be6681cf62035fca75581d4ff88d5e64c2 Mon Sep 17 00:00:00 2001 From: Joachim Lusiardi Date: Mon, 27 Jun 2016 21:35:00 +0200 Subject: [PATCH] adds update & delete for consumables adds functionality to * update * delete for consumables --- app/rollerverbrauch/__init__.py | 59 ++++++++++++++++++- app/rollerverbrauch/entities.py | 9 ++- app/rollerverbrauch/forms.py | 9 +++ app/rollerverbrauch/templates/admin.html | 8 +++ .../templates/deleteConsumableForm.html | 18 ++++++ .../templates/editConsumableForm.html | 19 ++++++ 6 files changed, 117 insertions(+), 5 deletions(-) create mode 100644 app/rollerverbrauch/templates/deleteConsumableForm.html create mode 100644 app/rollerverbrauch/templates/editConsumableForm.html diff --git a/app/rollerverbrauch/__init__.py b/app/rollerverbrauch/__init__.py index 46bd409..1d01bde 100644 --- a/app/rollerverbrauch/__init__.py +++ b/app/rollerverbrauch/__init__.py @@ -33,7 +33,9 @@ from rollerverbrauch.forms import \ DeleteAccountForm, \ DeletePitStopForm, \ EditPitstopForm, \ - CreateConsumableForm + CreateConsumableForm, \ + EditConsumableForm, \ + DeletConsumableForm from rollerverbrauch.entities import \ User, \ @@ -300,10 +302,12 @@ def get_manual(): 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 return render_template('admin.html', users=users, consumables=consumables) -@app.route('/admin/create_consumable', methods=['GET', 'POST']) +@app.route('/admin/consumable/create', methods=['GET', 'POST']) @login_required def create_consumable(): form = CreateConsumableForm() @@ -329,6 +333,57 @@ def create_consumable(): return render_template('createConsumableForm.html', form=form) +@app.route('/admin/consumable/delete/', methods=['GET', 'POST']) +@login_required +def delete_consumable(cid): + consumable = Consumable.query.filter(Consumable.id == cid).first() + if consumable is None: + return redirect(url_for('get_admin_page')) + + form = DeletConsumableForm() + + if form.validate_on_submit(): + db.session.delete(consumable) + db.session.commit() + tools.db_log_delete(consumable) + return redirect(url_for('get_admin_page')) + + return render_template('deleteConsumableForm.html', form=form, consumable=consumable) + + +@app.route('/admin/consumable/edit/', methods=['GET', 'POST']) +@login_required +def edit_consumable(cid): + consumable = Consumable.query.filter(Consumable.id == cid).first() + if consumable is None: + return redirect(url_for('get_admin_page')) + + form = EditConsumableForm() + + form.name.default = consumable.name + form.unit.default = consumable.unit + + # preinitialize the defaults with potentially existing values from a try before + if form.name.data is not None: + form.name.default = form.name.data + if form.unit.data is not None: + form.unit.default = form.unit.data + + if form.validate_on_submit(): + consumable.name = form.name.data + consumable.unit = form.unit.data + try: + db.session.commit() + tools.db_log_update(consumable) + except IntegrityError: + db.session.rollback() + form.name.errors.append('"%s" is not unique.' % (form.name.data)) + return render_template('editConsumableForm.html', form=form) + return redirect(url_for('get_admin_page')) + + return render_template('editConsumableForm.html', form=form) + + @app.route('/account', methods=['GET']) @login_required def get_account_page(): diff --git a/app/rollerverbrauch/entities.py b/app/rollerverbrauch/entities.py index c0b066f..d63a3dc 100644 --- a/app/rollerverbrauch/entities.py +++ b/app/rollerverbrauch/entities.py @@ -66,19 +66,22 @@ class Pitstop(db.Model): id = db.Column(db.Integer, primary_key=True) date = db.Column(db.Date) odometer = db.Column(db.Integer) + consumable_id = db.Column(db.Integer, db.ForeignKey('consumable.id')) + consumable = db.relationship('Consumable') litres = db.Column(db.Numeric(5, 2)) costs = db.Column(db.Numeric(5, 2), default=0) vehicle_id = db.Column(db.Integer, db.ForeignKey('vehicle.id')) - def __init__(self, odometer, litres, date, costs): + def __init__(self, odometer, litres, date, costs, consumable_id): self.odometer = odometer self.litres = litres self.date = date self.costs = costs + self.consumable_id = consumable_id def __repr__(self): - return '' % \ - (self.odometer, self.litres, self.date, self.vehicle_id) + return '' % \ + (self.odometer, self.litres, self.date, self.vehicle_id, self.consumable_id) class Consumable(db.Model): diff --git a/app/rollerverbrauch/forms.py b/app/rollerverbrauch/forms.py index 60efdb9..84c85e9 100644 --- a/app/rollerverbrauch/forms.py +++ b/app/rollerverbrauch/forms.py @@ -84,3 +84,12 @@ class CreateConsumableForm(Form): unit = StringField('Unit', validators=[Length(1, 255)]) submit = SubmitField(label='Do it!') + +class EditConsumableForm(Form): + name = StringField('Name', validators=[Length(1, 255)]) + unit = StringField('Unit', validators=[Length(1, 255)]) + submit = SubmitField(label='Do it!') + + +class DeletConsumableForm(Form): + submit = SubmitField(label='Do it!') diff --git a/app/rollerverbrauch/templates/admin.html b/app/rollerverbrauch/templates/admin.html index 4323d83..e9ec71c 100644 --- a/app/rollerverbrauch/templates/admin.html +++ b/app/rollerverbrauch/templates/admin.html @@ -42,6 +42,14 @@ {{ consumable.unit }} + + edit + + {% if not consumable.in_use %} + + delete + + {% endif %} {% endfor %} diff --git a/app/rollerverbrauch/templates/deleteConsumableForm.html b/app/rollerverbrauch/templates/deleteConsumableForm.html new file mode 100644 index 0000000..40a6e88 --- /dev/null +++ b/app/rollerverbrauch/templates/deleteConsumableForm.html @@ -0,0 +1,18 @@ +{% extends "layout.html" %} + +{% block body %} +
+
+
+
+

Delete vehicle '{{consumable.name}}'?

+
+ {{ form.hidden_tag() }} + {{ render_field_with_errors(form.submit) }} + +
+
+
+
+
+{% endblock %} diff --git a/app/rollerverbrauch/templates/editConsumableForm.html b/app/rollerverbrauch/templates/editConsumableForm.html new file mode 100644 index 0000000..97510d5 --- /dev/null +++ b/app/rollerverbrauch/templates/editConsumableForm.html @@ -0,0 +1,19 @@ +{% extends "layout.html" %} + +{% block body %} +
+
+
+
+

Edit consumable

+
+ {{ form.hidden_tag() }} + {{ render_field_with_errors(form.name) }} + {{ render_field_with_errors(form.unit) }} + {{ render_field_with_errors(form.submit) }} +
+
+
+
+
+{% endblock %}