diff --git a/ogn_python/model/device.py b/ogn_python/model/device.py index fc7d868..16a2413 100644 --- a/ogn_python/model/device.py +++ b/ogn_python/model/device.py @@ -1,5 +1,7 @@ -from ogn_python import db +from sqlalchemy.ext.hybrid import hybrid_property +from ogn_python import db +from .device_info import DeviceInfo class Device(db.Model): __tablename__ = 'devices' @@ -25,3 +27,11 @@ class Device(db.Model): self.software_version, self.hardware_version, self.real_address) + + @hybrid_property + def info(self): + query = db.session.query(DeviceInfo) \ + .filter(DeviceInfo.address == self.address) \ + .order_by(DeviceInfo.address_origin) + + return query.first() diff --git a/ogn_python/model/device_info.py b/ogn_python/model/device_info.py index 6060506..76fe440 100644 --- a/ogn_python/model/device_info.py +++ b/ogn_python/model/device_info.py @@ -17,7 +17,7 @@ class DeviceInfo(db.Model): address_origin = db.Column(db.SmallInteger) - # Relations + # Relations (deprecated) device_id = db.Column(db.Integer, db.ForeignKey('devices.id', ondelete='SET NULL'), index=True) device = db.relationship('Device', foreign_keys=[device_id], backref=db.backref('infos', order_by='DeviceInfo.address_origin.asc()')) diff --git a/ogn_python/routes.py b/ogn_python/routes.py index 3a095d7..d37650b 100644 --- a/ogn_python/routes.py +++ b/ogn_python/routes.py @@ -20,8 +20,8 @@ def devices(): return render_template('devices.html', devices=devices) -@app.route('/device.html', methods=['GET', 'POST']) -def device(): +@app.route('/device_detail.html', methods=['GET', 'POST']) +def device_detail(): device_id = request.args.get('id') device = db.session.query(Device) \ .filter(Device.id == device_id) \ @@ -166,12 +166,11 @@ def logbook(): filters.append(Logbook.device_id == sel_device_id) if len(filters) > 0: - entries = db.session.query(Logbook, Device, DeviceInfo) \ + logbook = db.session.query(Logbook) \ .filter(*filters) \ - .filter(db.and_(Logbook.device_id == Device.id, Device.address == DeviceInfo.address)) \ .order_by(Logbook.reftime) else: - entries = None + logbook = None return render_template('logbook.html', title='Logbook', @@ -181,7 +180,7 @@ def logbook(): airports=airports, sel_date=sel_date, dates=dates, - entries=entries) + logbook=logbook) @app.route('/statistics.html') def statistics(): diff --git a/ogn_python/templates/devices.html b/ogn_python/templates/devices.html index 9e0a03b..ca9551f 100644 --- a/ogn_python/templates/devices.html +++ b/ogn_python/templates/devices.html @@ -17,10 +17,10 @@ {% for device in devices %}
{{ device.software_version }}
{% else %}{{ device.software_version }}{% endif %}{% else %} - {% endif %}Nr. | @@ -41,19 +41,16 @@Landing | AGL | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
{{ loop.index }} | -{{ device_info.registration }} | -{{ device_info.aircraft }} | -{% if logbook.takeoff_timestamp is not none %} {{ logbook.takeoff_timestamp.strftime('%H:%M') }} {% endif %} | -{% if logbook.takeoff_track is not none %} {{ '%02d' | format(logbook.takeoff_track/10) }} {% endif %} | -{% if logbook.landing_timestamp is not none %} {{ logbook.landing_timestamp.strftime('%H:%M') }} {% endif %} | -{% if logbook.landing_track is not none %} {{ '%02d' | format(logbook.landing_track/10) }} {% endif %} | -{% if logbook.max_altitude is not none %} {{ logbook.max_altitude }} {% endif %} | +{% if entry.device.info is none %}{{ entry.device.address }}{% else %}{{ entry.device.info.registration }}{% endif %} | +{% if entry.device.info is none %}(unknown){% else %}{{ entry.device.info.aircraft }}{% endif %} | +{% if entry.takeoff_timestamp is not none %} {{ entry.takeoff_timestamp.strftime('%H:%M') }} {% endif %} | +{% if entry.takeoff_track is not none %} {{ '%02d' | format(entry.takeoff_track/10) }} {% endif %} | +{% if entry.landing_timestamp is not none %} {{ entry.landing_timestamp.strftime('%H:%M') }} {% endif %} | +{% if entry.landing_track is not none %} {{ '%02d' | format(entry.landing_track/10) }} {% endif %} | +{% if entry.max_altitude is not none %} {{ entry.max_altitude }} {% endif %} |