adds update & delete for consumables
adds functionality to * update * delete for consumables
This commit is contained in:
parent
60f2e9f4e4
commit
fa4126be66
|
@ -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/<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'])
|
||||
@login_required
|
||||
def get_account_page():
|
||||
|
|
|
@ -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 '<Pitstop odometer="%r" litres="%r" date="%r" vehicle_id="%r">' % \
|
||||
(self.odometer, self.litres, self.date, self.vehicle_id)
|
||||
return '<Pitstop odometer="%r" litres="%r" date="%r" vehicle_id="%r" consumable_id="%r">' % \
|
||||
(self.odometer, self.litres, self.date, self.vehicle_id, self.consumable_id)
|
||||
|
||||
|
||||
class Consumable(db.Model):
|
||||
|
|
|
@ -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!')
|
||||
|
|
|
@ -42,6 +42,14 @@
|
|||
{{ 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>
|
||||
{% 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>
|
||||
</tr>
|
||||
{% endfor %}
|
||||
|
|
|
@ -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 %}
|
|
@ -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 %}
|
Loading…
Reference in New Issue