Files
SmartScanProbeTrack/app/routes/locations.py

79 lines
2.7 KiB
Python

from flask import Blueprint, render_template, request, session, redirect
from app.models import ProbeLocation, Location
locations_bp = Blueprint('locations', __name__)
from datetime import datetime
import json
@locations_bp.route('/')
def locations_index():
"""Show all locations"""
locations = Location.get_all()
return render_template('location_timeline.html',
locations=locations,
user=session)
@locations_bp.route('/new', methods=['GET', 'POST'])
def new_location():
"""Create a new location"""
if request.method == 'POST':
name = request.form.get('name', '').strip()
address = request.form.get('address', '').strip()
contact_name = request.form.get('contact_name', '').strip()
contact_email = request.form.get('contact_email', '').strip()
contact_phone = request.form.get('contact_phone', '').strip()
if not name:
return render_template('location_form.html',
error='Name is required',
user=session)
try:
location = Location.create(
name=name,
address=address,
contact_name=contact_name,
contact_email=contact_email,
contact_phone=contact_phone
)
return redirect('/locations/')
except Exception as e:
return render_template('location_form.html',
error=str(e),
user=session)
return render_template('location_form.html', user=session)
@locations_bp.route('/probe/<probe_id>/locations')
def probe_location_history(probe_id):
"""Show timeline of location assignments for a probe"""
locations = ProbeLocation.get_by_probe(probe_id)
now = datetime.utcnow()
# Prepare data for template
location_data = []
chart_data = {
'labels': [],
'durations': []
}
for loc in locations:
end_date = loc.end_date if loc.end_date else now
duration_days = (end_date - loc.start_date).days
location_data.append({
'location': loc,
'duration_days': duration_days
})
chart_data['labels'].append(str(loc.location_id))
chart_data['durations'].append(duration_days)
return render_template('location_timeline.html',
probe_id=probe_id,
location_data=location_data,
chart_data_json=json.dumps(chart_data),
now=now,
user=session)