muizenval

Observe mouse traps remotely
Log | Files | Refs

commit 2e7d41db686c0480d0b735d14ed33cf5f2182f68
parent f372660357ab8ec0e62bdb5ead109fbd32150669
Author: Friedel Schön <[email protected]>
Date:   Thu, 19 May 2022 11:06:00 +0200

add mac-connect

Diffstat:
Mserver/forms.py | 9++++++---
Mserver/models.py | 3+++
Mserver/routes.py | 22+++++++++++++++++++++-
Mserver/site.db | 0
Mserver/static/main.css | 4++++
Aserver/templates/admin.html | 92+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Aserver/templates/connect.html | 32++++++++++++++++++++++++++++++++
Mserver/templates/layout.html | 2+-
Mserver/templates/trap.html | 9+++++++--
Mserver/templates/updatetrap.html | 2+-
10 files changed, 167 insertions(+), 8 deletions(-)

diff --git a/server/forms.py b/server/forms.py @@ -68,7 +68,11 @@ class UpdateAccountForm(FlaskForm): raise ValidationError('Deze e-mail bestaat al, log in als dat uw e-mail is') class UpdateTrapForm(FlaskForm): - mac = StringField('Mac-Adres', validators=[ Length(min=16, max=16) ]) + mac = StringField('MAC') name = StringField('Naam') email = StringField('E-mail') - submit = SubmitField('Bewerken') -\ No newline at end of file + submit = SubmitField('Bewerken') + +class ConnectTrapForm(FlaskForm): + mac = StringField('MAC', validators=[ Length(min=16, max=16) ]) + submit = SubmitField('Verbinden') diff --git a/server/models.py b/server/models.py @@ -40,6 +40,9 @@ class Trap(db.Model): def pretty_mac(self): upper = self.mac.upper() return ':'.join([ upper[i] + upper[i+1] for i in range(0, len(upper), 2) ]) + + def owner_class(self): + return User.query.filter_by(id=self.owner).first() def status_color(self): if self.caught: diff --git a/server/routes.py b/server/routes.py @@ -6,9 +6,10 @@ from flask_login import current_user, login_required, login_user, logout_user from PIL import Image from .app import app, bcrypt, db, socket -from .forms import LoginForm, RegistrationForm, UpdateAccountForm, UpdateTrapForm +from .forms import ConnectTrapForm, LoginForm, RegistrationForm, UpdateAccountForm, UpdateTrapForm from .models import Trap, User + @app.route("/api/update_status", methods=['POST', 'GET']) def update_status(): if not request.json: @@ -36,6 +37,7 @@ def search_connect(): return jsonify({ "error": "ok" }) + """ index.html (home-page) route """ @app.route("/") def index(): @@ -154,6 +156,24 @@ def traps(): traps = Trap.query.all() return render_template('trap.html', traps=traps) [email protected]('/traps/connect', methods=['POST', 'GET']) +@login_required +def trap_connect(): + form = ConnectTrapForm() + if form.validate_on_submit() and form.mac.data: + trap = Trap.query.filter_by(mac=form.mac.data.replace(':', '').replace(' ', '')).first() + if not trap: + flash('Muizenval niet gevonden', 'danger') + return redirect(url_for('trap_connect')) + + trap.owner = current_user.id + db.session.commit() + flash('Muizenval toegevoegd!', 'success') + return redirect(url_for('traps')) + + return render_template('connect.html', form=form) + + @app.route('/trap/<trap_id>/update', methods=['POST', 'GET']) @login_required def trap_update(trap_id): diff --git a/server/site.db b/server/site.db Binary files differ. diff --git a/server/static/main.css b/server/static/main.css @@ -102,4 +102,8 @@ a.article-title:hover { .account-heading { font-size: 2.5rem; +} + +.form-code { + font-family: 'Source Code Pro'; } \ No newline at end of file diff --git a/server/templates/admin.html b/server/templates/admin.html @@ -0,0 +1,91 @@ +{% extends "layout.html" %} +{% block content %} +<script type="text/javascript"> + socket.on('trap-change', function (data) { + if (data['user'] == current_user) + location.reload(); + }); +</script> +<article class="media content-section"> + <div class="media-body"> + <h3>These are traps who are waiting to be connected</h3> + </div> +</article> + +{% for trap in connect_traps %} +<article class="media content-section"> + <div class="media-body"> + <p> + <a class="btn btn-secondary btn-sm mt-1 mb-1" + href="{{ url_for('trap_update', trap_id=trap.mac) }}">Bewerken</a> + <a class="btn btn-secondary btn-sm mt-1 mb-1" + href="{{ url_for('trap_delete', trap_id=trap.mac) }}">Verwijderen</a> + </p> + <h3><a class="article-title" href="#"> + <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" fill="{{ trap.status_color() }}" + class="bi bi-circle-fill" viewBox="0 0 20 20"> + <circle cx="10" cy="10" r="10" /> + </svg> + - + {% if trap.name %} + {{ trap.name }} + {% else %} + <code>[{{ trap.pretty_mac() }}]</code> + {% endif %} + </a> + </h3> + {% if trap.name %} + <p> + <code>[{{ trap.pretty_mac() }}]</code> + </p> + {% endif %} + </div> +</article> + +<article class="media content-section"> + <div class="media-body"> + <h3>These are all other traps!</h3> + </div> +</article> + +{% for trap in traps %} +<article class="media content-section"> + <div class="media-body"> + <p> + <a class="btn btn-secondary btn-sm mt-1 mb-1" + href="{{ url_for('trap_update', trap_id=trap.mac) }}">Bewerken</a> + <a class="btn btn-secondary btn-sm mt-1 mb-1" + href="{{ url_for('trap_delete', trap_id=trap.mac) }}">Verwijderen</a> + </p> + <h3><a class="article-title" href="#"> + <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" fill="{{ trap.status_color() }}" + class="bi bi-circle-fill" viewBox="0 0 20 20"> + <circle cx="10" cy="10" r="10" /> + </svg> + - + {% if trap.name %} + {{ trap.name }} + {% else %} + <code>[{{ trap.pretty_mac() }}]</code> + {% endif %} + </a> + </h3> + {% if trap.name %} + <p> + <code>[{{ trap.pretty_mac() }}]</code> + </p> + {% endif %} + </div> + + {#} <div class="media-body"> + <h3>Naam: {{ trap.name }}</h3> + <p> Mac adres: {{ trap.mac }} </p> + {% if trap.caught %} + <p> Status: Gevangen! </p> + {% else %} + <p>Status: Leeg!</p> + {% endif %} + </div>{#} +</article> +{% endfor %} +{% endblock content %} +\ No newline at end of file diff --git a/server/templates/connect.html b/server/templates/connect.html @@ -0,0 +1,31 @@ +{% extends "layout.html" %} +{% block content %} +<div class="content-section"> + <h1>Verbindt een muizenval</h1> + <p>Press the connect-button and enter the MAC-address of your trap:</p> + <form method="POST" action=""> + {{ form.hidden_tag() }} + <fieldset class="form-group"> + <legend class="border-bottom mb-4"> + <h1>{{ legend }}</h1> + </legend> + <div class="form-group"> + {{ form.mac.label(class="form-control-label") }} + {% if form.mac.errors %} + {{ form.mac(class="form-control form-control-lg is-invalid") }} + <div class="invalid-feedback"> + {% for error in form.mac.errors %} + <span>{{ error }}</span> + {% endfor %} + </div> + {% else %} + {{ form.mac(class="form-control form-control-lg form-code") }} + {% endif %} + </div> + </fieldset> + <div class="form-group"> + {{ form.submit(class="btn btn-outline-info") }} + </div> + </form> +</div> +{% endblock content %} +\ No newline at end of file diff --git a/server/templates/layout.html b/server/templates/layout.html @@ -94,7 +94,7 @@ <a href="{{ url_for('traps') }}">Uw muizenvallen</a> </li> <li class="list-group-item list-group-item-light"> - <a href="{{ url_for('register_trap') }}">Muizenval registreren</a> + <a href="{{ url_for('trap_connect') }}">Muizenval verbinden</a> </li> {% if current_user.type == 'admin' %} <li class="list-group-item list-group-item-light"> diff --git a/server/templates/trap.html b/server/templates/trap.html @@ -12,9 +12,9 @@ <div class="media-body"> <p> <a class="btn btn-secondary btn-sm mt-1 mb-1" - href="{{url_for('trap_update', trap_id=trap.mac)}}">Bewerken</a> + href="{{ url_for('trap_update', trap_id=trap.mac) }}">Bewerken</a> <a class="btn btn-secondary btn-sm mt-1 mb-1" - href="{{url_for('trap_delete', trap_id=trap.mac)}}">Verwijderen</a> + href="{{ url_for('trap_delete', trap_id=trap.mac) }}">Verwijderen</a> </p> <h3><a class="article-title" href="#"> <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" fill="{{ trap.status_color() }}" @@ -34,6 +34,11 @@ <code>[{{ trap.pretty_mac() }}]</code> </p> {% endif %} + {% if trap.owner %} + <b> + van {{ trap.owner_class().name }} + </b> + {% endif %} </div> {#} <div class="media-body"> diff --git a/server/templates/updatetrap.html b/server/templates/updatetrap.html @@ -17,7 +17,7 @@ {% endfor %} </div> {% else %} - {{ form.mac(class="form-control form-control-lg") }} + {{ form.mac(disabled=True, class="form-control form-control-lg") }} {% endif %} </div> <div class="form-group">