integrate services into rollerverbrauch
This commit is contained in:
parent
dd11419305
commit
71412bf487
120
app/__init__.py
120
app/__init__.py
@ -24,7 +24,10 @@ from .forms import \
|
||||
CreateConsumableForm, \
|
||||
EditConsumableForm, \
|
||||
DeletConsumableForm, \
|
||||
SelectConsumableForm
|
||||
SelectConsumableForm, \
|
||||
CreateServiceForm, \
|
||||
DeleteServiceForm, \
|
||||
EditServiceForm
|
||||
|
||||
|
||||
app = Flask(__name__)
|
||||
@ -38,7 +41,8 @@ from .entities import \
|
||||
Role, \
|
||||
Pitstop, \
|
||||
Vehicle, \
|
||||
Consumable
|
||||
Consumable, \
|
||||
Service
|
||||
|
||||
# required to activate the filters
|
||||
from .filters import *
|
||||
@ -367,10 +371,120 @@ def edit_pit_stop_form(pid):
|
||||
return render_template('editPitStopForm.html', form=form, vehicle=vehicle, messages=messages)
|
||||
|
||||
|
||||
def pitstop_service_key(x):
|
||||
return x.odometer
|
||||
|
||||
|
||||
@app.route('/pitstops', methods=['GET'])
|
||||
@login_required
|
||||
def get_pit_stops():
|
||||
return render_template('pitstops.html', user=current_user)
|
||||
user = {
|
||||
'vehicles': []
|
||||
}
|
||||
for vehicle in current_user.vehicles:
|
||||
data = []
|
||||
for pitstop in vehicle.pitstops:
|
||||
data.append(pitstop)
|
||||
for service in vehicle.services:
|
||||
data.append(service)
|
||||
v = {
|
||||
'id': vehicle.id,
|
||||
'name': vehicle.name,
|
||||
'data': sorted(data, key=pitstop_service_key)
|
||||
}
|
||||
user['vehicles'].append(v)
|
||||
|
||||
return render_template('pitstops.html', user=user)
|
||||
|
||||
|
||||
@app.route('/service/vehicle/<int:vid>/create', methods=['GET', 'POST'])
|
||||
@login_required
|
||||
def create_service_for_vehicle(vid):
|
||||
vehicle = Vehicle.query.get(vid)
|
||||
if vehicle is None or vehicle not in current_user.vehicles:
|
||||
return redirect(url_for('get_account_page'))
|
||||
|
||||
form = CreateServiceForm()
|
||||
last_pitstop = tools.get_latest_pitstop_for_vehicle(vid)
|
||||
form.set_pitstop(last_pitstop)
|
||||
form.same_odometer_allowed = True
|
||||
|
||||
form.preinit_with_data()
|
||||
|
||||
if form.validate_on_submit():
|
||||
new_service = Service(form.date.data, form.odometer.data, vid, form.costs.data, form.description.data)
|
||||
db.session.add(new_service)
|
||||
vehicle.services.append(new_service)
|
||||
db.session.commit()
|
||||
print(new_service)
|
||||
return redirect(url_for('get_account_page'))
|
||||
|
||||
form.process()
|
||||
return render_template('createServiceForm.html', form=form, vehicle=vehicle, messages=[])
|
||||
|
||||
|
||||
@app.route('/service/delete/<int:sid>', methods=['GET', 'POST'])
|
||||
@login_required
|
||||
def delete_service_form(sid):
|
||||
service = Service.query.filter(Service.id == sid).first()
|
||||
if service is None:
|
||||
return redirect(url_for('get_pit_stops'))
|
||||
vehicle = Vehicle.query.filter(Vehicle.id == service.vehicle_id).first()
|
||||
if vehicle not in current_user.vehicles:
|
||||
return redirect(url_for('get_pit_stops'))
|
||||
|
||||
form = DeleteServiceForm()
|
||||
if form.validate_on_submit():
|
||||
db.session.delete(service)
|
||||
db.session.commit()
|
||||
tools.db_log_delete(service)
|
||||
return redirect(url_for('get_pit_stops', _anchor='v' + str(vehicle.id)))
|
||||
|
||||
return render_template('deleteServiceForm.html', form=form, service=service )
|
||||
|
||||
|
||||
@app.route('/service/edit/<int:sid>', methods=['GET', 'POST'])
|
||||
@login_required
|
||||
def edit_service_form(sid):
|
||||
edit_service = Service.query.get(sid)
|
||||
if edit_service is None:
|
||||
return redirect(url_for('get_pit_stops'))
|
||||
|
||||
vehicle = Vehicle.query.filter(Vehicle.id == edit_service.vehicle_id).first()
|
||||
if vehicle not in current_user.vehicles:
|
||||
return redirect(url_for('get_pit_stops'))
|
||||
|
||||
# last_pitstop_pos = vehicle.pitstops.index(edit_service) - 1
|
||||
# if last_pitstop_pos > 0:
|
||||
# last_pitstop = vehicle.pitstops[last_pitstop_pos]
|
||||
# else:
|
||||
# last_pitstop = Pitstop(0, 0, date(1970, 1, 1), 0, 0)
|
||||
last_pitstop = Pitstop(0, 0, date(1970, 1, 1), 0, 0)
|
||||
|
||||
form = EditServiceForm()
|
||||
form.set_pitstop(last_pitstop)
|
||||
|
||||
if form.validate_on_submit():
|
||||
edit_service.costs = form.costs.data
|
||||
edit_service.date = form.date.data
|
||||
edit_service.description = form.description.data
|
||||
edit_service.odometer = form.odometer.data
|
||||
db.session.commit()
|
||||
tools.db_log_update(edit_service)
|
||||
return redirect(url_for('get_pit_stops', _anchor='v' + str(vehicle.id)))
|
||||
|
||||
form.odometer.default = edit_service.odometer
|
||||
form.description.default = edit_service.description
|
||||
form.date.default = edit_service.date
|
||||
form.costs.default = edit_service.costs
|
||||
form.process()
|
||||
messages = {
|
||||
# 'date': 'Date must be between %s and %s (including).' % (str(last_pitstop.date), str(date.today())),
|
||||
# 'odometer': 'Odometer must be greater than %s km.' % (str(last_pitstop.odometer))
|
||||
}
|
||||
if edit_service.costs is not None and edit_service.costs > 0:
|
||||
messages['costs'] = 'Costs must be higher than 0.01 €.'
|
||||
return render_template('editServiceForm.html', form=form, vehicle=vehicle, messages=messages)
|
||||
|
||||
|
||||
@app.route('/manual', methods=['GET'])
|
||||
|
@ -40,6 +40,12 @@
|
||||
{{ vehicle.consumables | length }} consumables
|
||||
</td>
|
||||
<td>
|
||||
<a href="{{ url_for('create_service_for_vehicle', vid=vehicle.id) }}" id="pitstop_{{loop.index}}" class="btn btn-primary " role="button">
|
||||
<span class="glyphicon glyphicon-wrench" aria-hidden="true"></span> add service
|
||||
</a>
|
||||
<a href="{{ url_for('select_consumable_for_new_pitstop', vid=vehicle.id) }}" id="pitstop_{{loop.index}}" class="btn btn-primary " role="button">
|
||||
<span class="glyphicon glyphicon-plus" aria-hidden="true"></span> add pitstop
|
||||
</a>
|
||||
<a href="{{ url_for('edit_vehicle', vid=vehicle.id) }}" id="edit_vehicle_{{loop.index}}" class="btn btn-primary " role="button">
|
||||
<span class="glyphicon glyphicon-pencil" aria-hidden="true"></span> edit
|
||||
</a>
|
||||
|
@ -1,9 +1,95 @@
|
||||
{% extends "layout.html" %}
|
||||
{% extends "layout.html" %}
|
||||
|
||||
{% macro pitstop(field, vindex, loop) -%}
|
||||
<div class="panel panel-default">
|
||||
<div class="panel-body">
|
||||
<div style="text-align: left; font-size: 20px;">
|
||||
<span class="glyphicon glyphicon-filter" aria-hidden="true" style="border: 1px solid black; padding: 5px 5px 3px; border-radius: 5px;"></span>
|
||||
</div>
|
||||
<table class="table table-striped table-bordered table-condensed">
|
||||
<tr>
|
||||
<th>Date</th>
|
||||
<td id="vehicle_{{vindex}}_pitstop_{{loop.index}}_date">{{field.date}}</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th>Odometer</th>
|
||||
<td id="vehicle_{{vindex}}_pitstop_{{loop.index}}_odo">{{field.odometer}} km</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th>{{ field.consumable.name }}</th>
|
||||
<td id="vehicle_{{vindex}}_pitstop_{{loop.index}}_anmount">{{field.amount}} {{ field.consumable.unit }}</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th>Costs</th>
|
||||
<td id="vehicle_{{vindex}}_pitstop_{{loop.index}}_cost">
|
||||
{% if field.costs %}
|
||||
{{field.costs}} €
|
||||
{% else %}
|
||||
-- €
|
||||
{% endif %}
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
{% if loop.first %}
|
||||
<a id="vehicle_{{vindex}}_edit_pitstop_{{loop.index}}" href="{{ url_for('edit_pit_stop_form', pid=field.id) }}" class="btn btn-primary">
|
||||
<span class="glyphicon glyphicon-pencil" aria-hidden="true"></span> edit
|
||||
</a>
|
||||
<a id="vehicle_{{vindex}}_delete_pitstop_{{loop.index}}" href="{{ url_for('delete_pit_stop_form', pid=field.id) }}" class="btn btn-primary btn-warning ">
|
||||
<span class="glyphicon glyphicon-trash" aria-hidden="true"></span> delete
|
||||
</a>
|
||||
{% endif %}
|
||||
</div>
|
||||
</div>
|
||||
{%- endmacro %}
|
||||
|
||||
{% macro service(field, vindex, loop) -%}
|
||||
<div class="panel panel-default">
|
||||
<div class="panel-body">
|
||||
<div style="text-align: left; font-size: 20px;">
|
||||
<span class="glyphicon glyphicon-wrench" aria-hidden="true" style="border: 1px solid black; padding: 5px 5px 3px; border-radius: 5px;"></span>
|
||||
</div>
|
||||
<table class="table table-striped table-bordered table-condensed">
|
||||
<tr>
|
||||
<th>Date</th>
|
||||
<td id="vehicle_{{vindex}}_pitstop_{{loop.index}}_date">{{field.date}}</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th>Odometer</th>
|
||||
<td id="vehicle_{{vindex}}_pitstop_{{loop.index}}_desc">{{field.odometer}} km</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th>Description</th>
|
||||
<td id="vehicle_{{vindex}}_pitstop_{{loop.index}}_anmount">{{field.description}}</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th>Costs</th>
|
||||
<td id="vehicle_{{vindex}}_pitstop_{{loop.index}}_cost">
|
||||
{% if field.costs %}
|
||||
{{field.costs}} €
|
||||
{% else %}
|
||||
-- €
|
||||
{% endif %}
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
{% if loop.first %}
|
||||
<a id="vehicle_{{vindex}}_edit_pitstop_{{loop.index}}" href="{{ url_for('edit_service_form', sid=field.id) }}" class="btn btn-primary">
|
||||
<span class="glyphicon glyphicon-pencil" aria-hidden="true"></span> edit
|
||||
</a>
|
||||
<a id="vehicle_{{vindex}}_delete_pitstop_{{loop.index}}" href="{{ url_for('delete_service_form', sid=field.id) }}" class="btn btn-primary btn-warning ">
|
||||
<span class="glyphicon glyphicon-trash" aria-hidden="true"></span> delete
|
||||
</a>
|
||||
{% endif %}
|
||||
</div>
|
||||
</div>
|
||||
{%- endmacro %}
|
||||
|
||||
|
||||
{% block body %}
|
||||
<div class="col-md-2" ></div>
|
||||
<div class="col-md-8">
|
||||
<ul id="tabs" class="nav nav-tabs" data-tabs="tabs">
|
||||
{% for vehicle in current_user.vehicles %}
|
||||
{% for vehicle in user.vehicles %}
|
||||
<li {% if loop.first %}class="active" {%endif %}>
|
||||
<a href="#v{{vehicle.id}}" id="i{{vehicle.id}}" data-toggle="tab">
|
||||
{{ vehicle.name }}
|
||||
@ -12,48 +98,18 @@
|
||||
{% endfor %}
|
||||
</ul>
|
||||
<div id="my-tab-content" class="tab-content">
|
||||
{% for vehicle in current_user.vehicles %}
|
||||
{% for vehicle in user.vehicles %}
|
||||
{% set vehicleloop = loop %}
|
||||
<div class="tab-pane {% if loop.first %}active{% endif %}" id="v{{vehicle.id}}">
|
||||
<h3>{{vehicle.name}}</h3>
|
||||
{% if vehicle.pitstops %}
|
||||
{% for pitstop in vehicle.pitstops|reverse %}
|
||||
<div class="panel panel-default">
|
||||
<div class="panel-body">
|
||||
<table class="table table-striped table-bordered table-condensed">
|
||||
<tr>
|
||||
<th>Date</th>
|
||||
<td id="vehicle_{{vehicleloop.index}}_pitstop_{{loop.index}}_date">{{pitstop.date}}</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th>Odometer</th>
|
||||
<td id="vehicle_{{vehicleloop.index}}_pitstop_{{loop.index}}_odo">{{pitstop.odometer}} km</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th>{{ pitstop.consumable.name }}</th>
|
||||
<td id="vehicle_{{vehicleloop.index}}_pitstop_{{loop.index}}_anmount">{{pitstop.amount}} {{ pitstop.consumable.unit }}</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th>Costs</th>
|
||||
<td id="vehicle_{{vehicleloop.index}}_pitstop_{{loop.index}}_cost">
|
||||
{% if pitstop.costs %}
|
||||
{{pitstop.costs}} €
|
||||
{% else %}
|
||||
-- €
|
||||
{% endif %}
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
{% if loop.first %}
|
||||
<a id="vehicle_{{vehicleloop.index}}_edit_pitstop_{{loop.index}}" href="{{ url_for('edit_pit_stop_form', pid=pitstop.id) }}" class="btn btn-primary">
|
||||
<span class="glyphicon glyphicon-pencil" aria-hidden="true"></span> edit
|
||||
</a>
|
||||
<a id="vehicle_{{vehicleloop.index}}_delete_pitstop_{{loop.index}}" href="{{ url_for('delete_pit_stop_form', pid=pitstop.id) }}" class="btn btn-primary btn-warning ">
|
||||
<span class="glyphicon glyphicon-trash" aria-hidden="true"></span> delete
|
||||
</a>
|
||||
{% endif %}
|
||||
</div>
|
||||
</div>
|
||||
{% if vehicle.data %}
|
||||
{% for data in vehicle.data|reverse %}
|
||||
{% if 'Pitstop' in data.__class__.__name__ %}
|
||||
{{ pitstop(data, vehicleloop.index, loop) }}
|
||||
{% endif %}
|
||||
{% if 'Service' in data.__class__.__name__ %}
|
||||
{{ service(data, vehicleloop.index, loop) }}
|
||||
{% endif %}
|
||||
{% endfor %}
|
||||
{% else %}
|
||||
<div class="alert alert-warning" role="alert">
|
||||
|
Loading…
Reference in New Issue
Block a user