muizenval

Observe mouse traps remotely
Log | Files | Refs

commit bfea7d6d3067524fcff70b967a661af492426d36
parent a798858f7bd5a8cace08d61c9eb69e9cbb9623ca
Author: Friedel Schön <[email protected]>
Date:   Thu, 12 May 2022 19:57:05 +0200

post-merge

Diffstat:
Mserver/forms.py | 10+++++++++-
Mserver/models.py | 2+-
Mserver/routes.py | 64++++++++++++++++++++++++++++++++++++++++++++++++++--------------
Mserver/templates/index.html | 18+++---------------
Aserver/templates/trap.html | 19+++++++++++++++++++
Aserver/templates/updatetrap.html | 54++++++++++++++++++++++++++++++++++++++++++++++++++++++
6 files changed, 136 insertions(+), 31 deletions(-)

diff --git a/server/forms.py b/server/forms.py @@ -1,7 +1,8 @@ import re +from tokenize import String from flask_wtf import FlaskForm from flask_wtf.file import FileAllowed, FileField -from wtforms import BooleanField, HiddenField, PasswordField, SelectField, StringField, SubmitField, IntegerField +from wtforms import BooleanField, HiddenField, PasswordField, StringField, SubmitField, IntegerField from wtforms.validators import DataRequired, Email, EqualTo, Length, ValidationError from .models import User @@ -71,3 +72,9 @@ class UpdateAccountForm(FlaskForm): def validate_email(self, email): if email.data != current_user.email and User.query.filter_by(email=email.data).first(): 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) ]) + name = StringField('Naam') + email = StringField('E-mail') + submit = SubmitField('Bewerken') +\ No newline at end of file diff --git a/server/models.py b/server/models.py @@ -36,7 +36,7 @@ class User(db.Model, UserMixin): class Trap(db.Model): mac = db.Column(db.String(16), primary_key=True, nullable=False) name = db.Column(db.Text) - last_heartbeat = db.Column(db.DateTime, nullable=True, default=0) + last_heartbeat = db.Column(db.DateTime) caught = db.Column(db.Boolean, nullable=False, default=False) owner = db.Column(db.Integer, db.ForeignKey('user.id')) connect_expired = db.Column(db.DateTime) diff --git a/server/routes.py b/server/routes.py @@ -7,21 +7,23 @@ from flask_login import current_user, login_required, login_user, logout_user from PIL import Image from .app import app, bcrypt, db -from .forms import LoginForm, RegistrationForm, UpdateAccountForm +from .forms import LoginForm, RegistrationForm, UpdateAccountForm, UpdateTrapForm from .models import Trap, User, UserType -status = None - @app.route("/api/update_status", methods=['POST', 'GET']) def my_function(): data = request.json - global status - if data is None: - status = "Error" - elif data: - status = "on" - else: - status = "off" + status = False + if data: + if data[0] == "0": + status = False + else: + status = True + mac = data[1:] + val = Trap.query.filter_by(mac=mac).first() + if val: + val.caught = status + db.session.commit() reaction = "congrats" return jsonify(reaction) @@ -31,16 +33,17 @@ def my_function2(): if data is None: status = "Error" elif data: - status = "on" - else: - status = "off" + if not Trap.query.filter_by(mac=data).first(): + trap = Trap(mac=data, caught=False) + db.session.add(trap) + db.session.commit() reaction = data return jsonify(reaction) """ index.html (home-page) route """ @app.route("/") def index(): - return render_template('index.html', status = status) + return render_template('index.html') """ about.html route """ @app.route("/about") @@ -148,6 +151,39 @@ def dashboard(): return render_template('dashboard.html', title='Dashboard', traps=traps) [email protected]('/trap') +@login_required +def trap(): + traps = Trap.query.all() + return render_template('trap.html', traps = traps) + [email protected]('/trap/<trap_id>', methods=['POST', 'GET']) +@login_required +def trapform(trap_id): + form = UpdateTrapForm() + val = Trap.query.filter_by(mac=trap_id).first() + if form.validate_on_submit(): + val.name = form.name.data + email = form.email.data + if email: + user = User.query.filter_by(email = email).first() + val.owner = user.id + db.session.commit() + return redirect('/trap') + elif request.method == 'GET': + form.mac.data = val.mac + form.name.data = val.name + #form.email = val.owner + return render_template('updatetrap.html', form=form) + [email protected]('/trap/delete/<trap_id>') +@login_required +def delete_trap(trap_id): + trap = Trap.query.filter_by(mac=trap_id).first() + db.session.delete(trap) + db.session.commit() + return redirect(url_for('trap')) + """ 404 not found handler """ @app.errorhandler(404) def not_found(error): diff --git a/server/templates/index.html b/server/templates/index.html @@ -2,21 +2,9 @@ {% block content %} <article class="media content-section"> <div class="media-body"> - <h2>Dit zijn ons lessen</h2> - <p> - U kan makkelijk op de titel van een les klikken om in te schrijven! - </p> - </div> -</article> -<article class="media content-section"> - <div class="media-body"> - {% if status == "on" %} - <h1> ON!</h1> - {% elif status == "off" %} - <h1> OFF!</h1> - {% else %} - <h1> ERROR!</h1> - {% endif %} + <h2>Welkom!</h2> + <a class="ml-2" href="{{ url_for('trap') }}">Bekijk de vallen</a> </div> </article> + {% endblock content %} \ No newline at end of file diff --git a/server/templates/trap.html b/server/templates/trap.html @@ -0,0 +1,18 @@ +{% extends "layout.html" %} +{% block content %} +{% for trap in traps %} +<article class="media content-section"> + <div class="media-body"> + <h2><a class="btn btn-secondary btn-sm mt-1 mb-1" href="{{url_for('trapform', trap_id=trap.mac)}}">bewerken</a></h2> + <h2><a class="btn btn-secondary btn-sm mt-1 mb-1" href="{{url_for('delete_trap', trap_id=trap.mac)}}">verwijderen</a></h2> + <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/updatetrap.html b/server/templates/updatetrap.html @@ -0,0 +1,53 @@ +{% extends "layout.html" %} +{% block content %} +<div class="content-section"> + <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") }} + {% endif %} + </div> + <div class="form-group"> + {{ form.name.label(class="form-control-label") }} + {% if form.name.errors %} + {{ form.name(class="form-control form-control-lg is-invalid") }} + <div class="invalid-feedback"> + {% for error in form.name.errors %} + <span>{{ error }}</span> + {% endfor %} + </div> + {% else %} + {{ form.name(class="form-control form-control-lg") }} + {% 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") }} + <div class="invalid-feedback"> + {% for error in form.email.errors %} + <span>{{ error }}</span> + {% endfor %} + </div> + {% else %} + {{ form.email(class="form-control form-control-lg") }} + {% 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