commit f372660357ab8ec0e62bdb5ead109fbd32150669
parent fb03702c23ae09de49e49069ab07d5688201fbef
Author: Friedel Schön <[email protected]>
Date: Wed, 18 May 2022 13:01:37 +0200
remove catcher
Diffstat:
13 files changed, 87 insertions(+), 47 deletions(-)
diff --git a/add-user.py b/add-user.py
@@ -8,9 +8,6 @@ from server.models import User, UserType
users = [
(UserType.CLIENT, 'Boer Herman', '[email protected]'),
- (UserType.CATCHER, 'Vanger Kees', '[email protected]'),
- (UserType.TECHNICIAN, 'Technicus Jos', '[email protected]'),
- (UserType.MANAGER, 'Manager Peter', '[email protected]'),
(UserType.ADMIN, 'Administrator Ralf', '[email protected]'),
]
diff --git a/readme.md b/readme.md
@@ -13,7 +13,7 @@ $ git clone https://github.com/friedelschoen/muizenval.tk/
**Alle afhankelijkheden installeren:**
```
-$ pip3 install flask wtforms flask_sqlalchemy flask-wtf email_validator flask-bcrypt flask-login pillow
+$ pip3 install flask wtforms flask_sqlalchemy flask-wtf email_validator flask-bcrypt flask-login pillow flask_socketio simple-websocket
```
**De server runnen:**
diff --git a/run-server.py b/run-server.py
@@ -1,4 +1,4 @@
-from server.app import app
+from server.app import socket, app
if __name__ == '__main__':
- app.run(host="0.0.0.0", port=5000, debug=True)
+ socket.run(app, "0.0.0.0", 5000, debug=True)
diff --git a/server/app.py b/server/app.py
@@ -2,7 +2,7 @@ from flask import Flask
from flask_bcrypt import Bcrypt
from flask_login import LoginManager
from flask_sqlalchemy import SQLAlchemy
-
+from flask_socketio import SocketIO
app = Flask(__name__)
app.config['SECRET_KEY'] = 'iot_project'
@@ -10,6 +10,7 @@ app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///site.db'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)
bcrypt = Bcrypt(app)
+socket = SocketIO(app)
login_manager = LoginManager(app)
login_manager.login_view = 'login'
login_manager.login_message_category = 'info'
diff --git a/server/forms.py b/server/forms.py
@@ -16,9 +16,8 @@ class RegistrationForm(FlaskForm):
phone = StringField('Telefoon', validators=[ DataRequired(), Length(min=5) ])
street = StringField('Straat', validators=[ DataRequired() ])
housenumber = IntegerField('Huisnummer', validators=[ DataRequired() ])
- postcode = StringField('Postcode', validators=[ DataRequired() ])
+ zipcode = StringField('Postcode', validators=[ DataRequired() ])
place = StringField('Plaats', validators=[ DataRequired() ])
- catcher_code = StringField('VangerCode', validators=[ DataRequired(), Length(min=5, max=5) ])
submit = SubmitField('Registeren')
""" validates whether name is already in use """
@@ -40,11 +39,6 @@ class RegistrationForm(FlaskForm):
if len(code.data) != 6 or not code.data[0:4].isnumeric() or not code.data[4:6].isalpha():
raise ValidationError('De postcode is niet geldig.')
- def validate_catcher_code(self, code):
- pass
- #if not User.query.filter_by(type=UserType.CATCHER, catcher_code=code.data).first():
- # raise ValidationError('De rattenvanger is niet bekennt, hebt u de code juist ingevoerd?')
-
""" login form for login.html """
class LoginForm(FlaskForm):
diff --git a/server/models.py b/server/models.py
@@ -11,10 +11,7 @@ def load_user(user_id):
class UserType(Enum):
ADMIN = 0
- MANAGER = 1
- TECHNICIAN = 2
- CATCHER = 3
- CLIENT = 4
+ CLIENT = 1
class User(db.Model, UserMixin):
id = db.Column(db.Integer, primary_key=True)
@@ -26,8 +23,7 @@ class User(db.Model, UserMixin):
phone = db.Column(db.Text, nullable=False)
address = db.Column(db.Text)
- contact = db.Column(db.Integer, db.ForeignKey('user.id')) # set if technician, catcher, user
- catcher_code = db.Column(db.String(5)) # set if catcher
+ contact = db.Column(db.Integer, db.ForeignKey('user.id')) # set if user
def contact_class(self):
return User.query.filter_by(id=self.contact).first()
diff --git a/server/routes.py b/server/routes.py
@@ -5,19 +5,23 @@ from flask import flash, redirect, render_template, request, url_for, abort, req
from flask_login import current_user, login_required, login_user, logout_user
from PIL import Image
-from .app import app, bcrypt, db
+from .app import app, bcrypt, db, socket
from .forms import LoginForm, RegistrationForm, UpdateAccountForm, UpdateTrapForm
-from .models import Trap, User, UserType
+from .models import Trap, User
@app.route("/api/update_status", methods=['POST', 'GET'])
def update_status():
if not request.json:
return jsonify({ "error": "invalid-json" })
- val = Trap.query.filter_by(mac=request.json['mac']).first()
- if not val:
+ trap = Trap.query.filter_by(mac=request.json['mac']).first()
+ if not trap:
return jsonify({ "error": "not-found" })
- val.caught = request.json['status']
+
+ trap.caught = request.json['status']
db.session.commit()
+
+ socket.emit('trap-change', { 'user': trap.owner })
+
return jsonify({ "error": "ok" })
@app.route("/api/search_connect", methods=['POST', 'GET'])
@@ -29,6 +33,7 @@ def search_connect():
trap = Trap(mac=request.json['mac'], caught=False)
db.session.add(trap)
db.session.commit()
+
return jsonify({ "error": "ok" })
""" index.html (home-page) route """
diff --git a/server/site.db b/server/site.db
Binary files differ.
diff --git a/server/static/logo.svg b/server/static/logo.svg
@@ -0,0 +1,46 @@
+<?xml version="1.0" encoding="UTF-8" standalone="no"?>
+<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 20010904//EN"
+"http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd">
+<svg
+ xmlns:dc="http://purl.org/dc/elements/1.1/"
+ xmlns:cc="http://creativecommons.org/ns#"
+ xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
+ xmlns:svg="http://www.w3.org/2000/svg"
+ xmlns="http://www.w3.org/2000/svg"
+ version="1.1"
+ width="176"
+ height="91"
+ id="svg1">
+ <path
+ d="m 1.6218838,0.91587741 c 0.538,-0.09 13.9530002,6.991 22.9700002,9.4379996 16.079,4.109 30.756,16.476 37.85,23.58 0.526,0.608 6.093,-4.168 11.919,-3.824 7.958,0.482 32.581996,7.406 35.871996,2.691 1.995,-2.768 1.852,-6.164 4.341,-7.555 1.34,-0.375 -4.981,-11.936 1.737,-15.64 4.79,-2.3939996 8.849,11.009 10.079,12.511 0.551,0.672 3.474,0.836 3.128,-0.695 -0.375,-1.663 -5.103,-14.5239996 0.695,-16.6819996 9.417,-3.729 7.748,16.4559996 7.993,17.0299996 0.521,1.222 32.797,2.164 34.754,16.682 1.132,7.99 -25.858,18.317 -28.498,20.157 -2.639,1.84 -2.419,4.783 -1.39,6.604 1.183,1.975 9.046,8.641 7.994,11.816 -0.723,1.834 -15.398,-4.888 -16.335,-3.475 -2.137,2.699 10.683,9.455 6.256,14.249 -1.755,1.148 -21.947,-8.454 -21.896,-8.341 -0.542,0.206 -28.531996,5.113 -50.045996,-1.391 -0.758,-0.214 -13.808,12.606 -15.292,12.165 -2.029,-0.744 2.991,-11.15 2.433,-13.207 -0.912,-1.146 -9.251,4.653 -9.731,1.738 -0.574,-3.831 7.318,-8.235 6.951,-10.774 -2.035,-14.424 0.909,-22.847 5.908,-29.888 1.667,-2.592 -19.592,-18.646 -34.983,-24.982 C 8.9408838,6.7868774 0.70688377,1.4168774 1.6218838,0.91587741 z"
+ id="path14"
+ style="fill:#020202;stroke:#000002;stroke-width:1pt" />
+ <path
+ d="m 139.93424,49.143277 c -0.169,-0.59 10.503,-7.045 12.048,-5.561 2.781,1.008 -11.95,6.31 -12.048,5.561 z"
+ id="path7"
+ style="fill:#ffffff;stroke:#000000;stroke-width:0.6pt" />
+ <path
+ d="m 145.03124,55.013277 c -0.94,-0.568 6.389,-9.114 8.187,-8.187 2.625,2.608 -8.31,8.39 -8.187,8.187 z"
+ id="path8"
+ style="fill:#ffffff;stroke:#000000;stroke-width:0.6pt" />
+ <path
+ d="m 173.91624,15.470277 c -0.891,-0.737 -7.878,10.287 -6.333,11.431 0.929,0.695 6.731,-11.038 6.333,-11.431 z"
+ id="path13"
+ style="fill:#020202;stroke:#000000;stroke-width:0.6pt" />
+ <ellipse
+ cx="288.86475"
+ cy="374.58655"
+ rx="3.012037"
+ ry="3.398196"
+ transform="matrix(0.9253,0,0,0.933788,-124.97146,-318.53572)"
+ id="ellipse17"
+ style="fill:#ffffff;stroke:#000000;stroke-width:0.6pt" />
+ <path
+ d="m 165.88414,13.925647 c -0.441,-0.512 -2.781,11.368 -1.699,11.894 0.929,0.695 2.547,-11.276 1.699,-11.894 z"
+ id="path18"
+ style="fill:#020202;stroke:#000000;stroke-width:0.6pt" />
+ <path
+ d="m 136.05934,30.003277 c -2.771,1.959 1.521,13.045 8.58,6.572 -5.546,0.163 -7.396,-1.461 -8.58,-6.572 z"
+ id="path21"
+ style="fill:#ffffff;stroke:#000000;stroke-width:0.6pt" />
+</svg>
+\ No newline at end of file
diff --git a/server/templates/dashboard.html b/server/templates/dashboard.html
@@ -30,9 +30,9 @@
<code>[{{ trap.pretty_mac() }}]</code>
</p>
{% endif %}
- <p>
- onderhouden door <b>{{ home.catcher_class().name }}</b>
- </p>
+ {#} <p>
+ onderhouden door <b>{{ home.contact_class().name }}</b>
+ </p>{#}
<p>
<a class="article-title" href="#">
{{ home.street }} {{ home.number }}<br>
diff --git a/server/templates/layout.html b/server/templates/layout.html
@@ -15,6 +15,17 @@
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link rel="stylesheet"
href="https://fonts.googleapis.com/css2?family=Source+Code+Pro&family=Source+Sans+Pro:ital,wght@0,400;0,600;1,400;1,600&display=swap">
+
+ <script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/4.0.1/socket.io.js"
+ integrity="sha512-q/dWJ3kcmjBLU4Qc47E4A9kTB4m3wuTY7vkFJDTZKjTs8jhyGQnaUrxa0Ytd0ssMZhbNua9hE+E7Qv1j+DyZwA=="
+ crossorigin="anonymous"></script>
+
+ <script type="text/javascript" charset="utf-8">
+ var socket = io();
+ var current_user = {{ current_user.id if current_user.is_authenticated else none | tojson }};
+// socket.on('connect', function () { });
+ </script>
+
{% if title %}
<title>muizenval.tk - {{ title }}</title>
{% else %}
@@ -82,18 +93,13 @@
<li class="list-group-item list-group-item-light">
<a href="{{ url_for('traps') }}">Uw muizenvallen</a>
</li>
- {% if current_user.type == 'catcher' %}
<li class="list-group-item list-group-item-light">
- <a href="#">Uw cliënten</a>
+ <a href="{{ url_for('register_trap') }}">Muizenval registreren</a>
</li>
- {% endif %}
{% if current_user.type == 'admin' %}
<li class="list-group-item list-group-item-light">
<a href="#">Gebruikers bewerken</a>
</li>
- <li class="list-group-item list-group-item-light">
- <a href="#">Statusaanvragen</a>
- </li>
{% endif %}
</ul>
</p>
diff --git a/server/templates/register.html b/server/templates/register.html
@@ -123,19 +123,6 @@
{{ form.place(class="form-control form-control-lg") }}
{% endif %}
</div>
- <div class="form-group">
- {{ form.catcher_code.label(class="form-control-label") }}
- {% if form.catcher_code.errors %}
- {{ form.catcher_code(class="form-control form-control-lg is-invalid") }}
- <div class="invalid-feedback">
- {% for error in form.catcher_code.errors %}
- <span>{{ error }}</span>
- {% endfor %}
- </div>
- {% else %}
- {{ form.catcher_code(class="form-control form-control-lg") }}
- {% endif %}
- </div>
</fieldset>
<div class="form-group">
{{ form.submit(class="btn btn-outline-info") }}
diff --git a/server/templates/trap.html b/server/templates/trap.html
@@ -1,5 +1,12 @@
{% extends "layout.html" %}
{% block content %}
+<script type="text/javascript">
+ socket.on('trap-change', function (data) {
+ if (data['user'] == current_user)
+ location.reload();
+ });
+</script>
+
{% for trap in traps %}
<article class="media content-section">
<div class="media-body">