adds update & delete for consumables

adds functionality to
* update
* delete
for consumables
This commit is contained in:
Joachim Lusiardi 2016-06-27 21:35:00 +02:00
parent 60f2e9f4e4
commit fa4126be66
6 changed files with 117 additions and 5 deletions

View File

@ -33,7 +33,9 @@ from rollerverbrauch.forms import \
DeleteAccountForm, \ DeleteAccountForm, \
DeletePitStopForm, \ DeletePitStopForm, \
EditPitstopForm, \ EditPitstopForm, \
CreateConsumableForm CreateConsumableForm, \
EditConsumableForm, \
DeletConsumableForm
from rollerverbrauch.entities import \ from rollerverbrauch.entities import \
User, \ User, \
@ -300,10 +302,12 @@ def get_manual():
def get_admin_page(): def get_admin_page():
users = User.query.all() users = User.query.all()
consumables = Consumable.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) 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 @login_required
def create_consumable(): def create_consumable():
form = CreateConsumableForm() form = CreateConsumableForm()
@ -329,6 +333,57 @@ def create_consumable():
return render_template('createConsumableForm.html', form=form) return render_template('createConsumableForm.html', form=form)
@app.route('/admin/consumable/delete/<int:cid>', 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/<int:cid>', 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']) @app.route('/account', methods=['GET'])
@login_required @login_required
def get_account_page(): def get_account_page():

View File

@ -66,19 +66,22 @@ class Pitstop(db.Model):
id = db.Column(db.Integer, primary_key=True) id = db.Column(db.Integer, primary_key=True)
date = db.Column(db.Date) date = db.Column(db.Date)
odometer = db.Column(db.Integer) 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)) litres = db.Column(db.Numeric(5, 2))
costs = db.Column(db.Numeric(5, 2), default=0) costs = db.Column(db.Numeric(5, 2), default=0)
vehicle_id = db.Column(db.Integer, db.ForeignKey('vehicle.id')) 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.odometer = odometer
self.litres = litres self.litres = litres
self.date = date self.date = date
self.costs = costs self.costs = costs
self.consumable_id = consumable_id
def __repr__(self): def __repr__(self):
return '<Pitstop odometer="%r" litres="%r" date="%r" vehicle_id="%r">' % \ return '<Pitstop odometer="%r" litres="%r" date="%r" vehicle_id="%r" consumable_id="%r">' % \
(self.odometer, self.litres, self.date, self.vehicle_id) (self.odometer, self.litres, self.date, self.vehicle_id, self.consumable_id)
class Consumable(db.Model): class Consumable(db.Model):

View File

@ -84,3 +84,12 @@ class CreateConsumableForm(Form):
unit = StringField('Unit', validators=[Length(1, 255)]) unit = StringField('Unit', validators=[Length(1, 255)])
submit = SubmitField(label='Do it!') 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!')

View File

@ -42,6 +42,14 @@
{{ consumable.unit }} {{ consumable.unit }}
</td> </td>
<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>
{% 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 %}
</td> </td>
</tr> </tr>
{% endfor %} {% endfor %}

View File

@ -0,0 +1,18 @@
{% 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>Delete vehicle '{{consumable.name}}'?</h3>
<form class='form-horizontal' method="POST">
{{ form.hidden_tag() }}
{{ render_field_with_errors(form.submit) }}
</form>
</div>
</div>
</div>
<div class="col-md-2" ></div>
{% endblock %}

View File

@ -0,0 +1,19 @@
{% 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>Edit consumable </h3>
<form class='form-horizontal' method="POST">
{{ form.hidden_tag() }}
{{ render_field_with_errors(form.name) }}
{{ render_field_with_errors(form.unit) }}
{{ render_field_with_errors(form.submit) }}
</form>
</div>
</div>
</div>
<div class="col-md-2" ></div>
{% endblock %}