commit a86a1ff61afde2db1bc5c128261eb5f70c44b99a
parent 605f9ff6d2110a6b310ad94c65a37ef373c657b0
Author: Friedel Schön <[email protected]>
Date: Thu, 19 May 2022 18:12:49 +0200
added map + location-edit
Diffstat:
7 files changed, 74 insertions(+), 18 deletions(-)
diff --git a/server/forms.py b/server/forms.py
@@ -70,7 +70,7 @@ class UpdateAccountForm(FlaskForm):
class UpdateTrapForm(FlaskForm):
mac = StringField('MAC')
name = StringField('Naam')
- email = StringField('E-mail')
+ location = StringField('Locatie')
submit = SubmitField('Bewerken')
class ConnectTrapForm(FlaskForm):
diff --git a/server/models.py b/server/models.py
@@ -51,3 +51,6 @@ class Trap(db.Model):
if self.caught:
return '#f4a900'
return 'currentColor'
+
+ def dict(self):
+ return { c.name: getattr(self, c.name) for c in self.__table__.columns }
diff --git a/server/routes.py b/server/routes.py
@@ -163,7 +163,7 @@ def traps():
else:
query = Trap.query.filter_by(owner=current_user.id)
- trap_json = [ { c.name: str(getattr(trap, c.name)) for c in trap.__table__.columns } for trap in query ]
+ trap_json = [ trap.dict() for trap in query ]
return render_template('trap.html', traps=query, trap_json=trap_json)
@@ -192,9 +192,9 @@ def trap_update(trap_id):
trap = Trap.query.filter_by(mac=trap_id).first()
if form.validate_on_submit():
trap.name = form.name.data
- if form.email.data:
- user = User.query.filter_by(email=form.email.data).first()
- trap.owner = user.id
+ print(form.location.data)
+ if form.location.data:
+ trap.location_lat, trap.location_lon = form.location.data.split(' ', 2)
db.session.commit()
return redirect(url_for('traps'))
elif not trap:
diff --git a/server/site.db b/server/site.db
Binary files differ.
diff --git a/server/templates/trap.html b/server/templates/trap.html
@@ -46,6 +46,19 @@
{% endfor %}
<script type="text/javascript">
+ function chunkString(str, len) {
+ const size = Math.ceil(str.length / len)
+ const r = Array(size)
+ let offset = 0
+
+ for (let i = 0; i < size; i++) {
+ r[i] = str.substr(offset, len)
+ offset += len
+ }
+
+ return r
+ }
+
socket.on('trap-change', function (data) {
if (data['user'] == current_user)
location.reload();
@@ -65,8 +78,13 @@
attribution: '© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
}).addTo(map);
-// L.marker([51.5, -0.09]).addTo(map)
- // .bindPopup('A pretty CSS3 popup.<br> Easily customizable.')
- // .openPopup();
+ for (var index in traps) {
+ var trap = traps[index];
+ if (trap.location_lat && trap.location_lon) {
+ L.marker([trap.location_lat, trap.location_lon])
+ .addTo(map)
+ .bindPopup(`[${chunkString(trap.mac, 2).join(':')}] ${trap.name || ''}`);
+ }
+ }
</script>
{% endblock content %}
\ No newline at end of file
diff --git a/server/templates/updatetrap.html b/server/templates/updatetrap.html
@@ -34,23 +34,59 @@
{% endif %}
</div>
<div class="form-group">
- {{ form.email.label(class="form-control-label") }}
- {% if form.email.errors %}
- {{ form.email(class="form-control form-control-lg is-invalid") }}
+ {{ form.location.label(class="form-control-label") }}
+ {% if form.location.errors %}
+ {{ form.location(class="form-control form-control-lg is-invalid") }}
<div class="invalid-feedback">
- {% for error in form.email.errors %}
+ {% for error in form.location.errors %}
<span>{{ error }}</span>
{% endfor %}
</div>
{% else %}
- {{ form.email(class="form-control form-control-lg") }}
+ {{ form.location(id='location-input', readonly=True, class="form-control form-control-lg") }}
{% endif %}
</div>
</fieldset>
+ <p>
+ <div id="trap-map"></div>
+ </p>
<div class="form-group">
{{ form.submit(class="btn btn-outline-info") }}
<a class="btn btn btn-danger" href="{{ url_for('trap_delete', trap_id=trap.mac) }}">Verwijderen</a>
</div>
</form>
</div>
+<script type="text/javascript">
+ var trap = {{ trap.dict() | tojson }};
+
+ var map = L.map('trap-map').setView([52.283333, 5.666667], 7);
+
+ L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
+ attribution: '© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
+ }).addTo(map);
+
+
+ let marker = null;
+
+ function setMarker(locArg) {
+ var loc = L.latLng(locArg);
+ if (marker) {
+ marker.setLatLng(loc);
+ } else {
+ marker = L.marker(loc).addTo(map);
+ }
+
+ document.getElementById('location-input').value = `${loc.lat} ${loc.lng}`;
+ }
+
+ if (trap.location_lat && trap.location_lon) {
+ setMarker([trap.location_lat, trap.location_lon]);
+ }
+
+ function onMapClick(e) {
+ setMarker(e.latlng);
+ }
+
+ map.on('click', onMapClick);
+</script>
{% endblock content %}
\ No newline at end of file
diff --git a/test-client.py b/test-client.py
@@ -1,12 +1,11 @@
import requests, random
-"""#mac = ''.join([ random.choice('0123456789ABCDEF') for _ in range(16) ])
-mac = '2C5C9A4DBA95D559'
+mac = ''.join([ random.choice('0123456789ABCDEF') for _ in range(16) ])
res = requests.post('http://0.0.0.0:5000/api/search_connect', json={ 'mac': mac })
print('MAC:', mac)
print('Answer:', res.content)
-"""
-mac = '90FD852087386BE9'
-res = requests.post('http://0.0.0.0:5000/api/update_status', json={ 'mac': mac, 'status': False })
+
+#mac = '90FD852087386BE9'
+#res = requests.post('http://0.0.0.0:5000/api/update_status', json={ 'mac': mac, 'status': False })