commit e64a730b781679393fed0bf22138270d92441454
parent 5a786e21cac2f84fb3c51ce8ea4b65a4cecd1f3b
Author: Friedel Schon <[email protected]>
Date: Thu, 12 May 2022 11:00:00 +0200
adding users
Diffstat:
8 files changed, 60 insertions(+), 28 deletions(-)
diff --git a/create-db.py b/create-db.py
@@ -0,0 +1,5 @@
+from server.app import db
+import server.models
+
+db.create_all()
+db.session.commit()
diff --git a/readme.md b/readme.md
@@ -1,5 +1,7 @@
# REPOSITORY VOOR ONS IOT-PROJECT (5GRONINGEN)
+> Hamdi Hassan, Loes Hoogstra, Gerco van Woudenberg, Friedel Schon
+
## De server runnen
Dit is een dev-server, dus run je met `debug=True`-flag!
diff --git a/server/models.py b/server/models.py
@@ -1,5 +1,4 @@
-from datetime import datetime
-
+from enum import Enum
from flask_login import UserMixin
from .app import db, login_manager
@@ -10,27 +9,40 @@ def load_user(user_id):
return User.query.get(int(user_id))
+class UserType(Enum):
+ administrator = 0
+ technician = 1
+ catcher = 2
+ user = 3
+
class User(db.Model, UserMixin):
id = db.Column(db.Integer, primary_key=True)
- type = db.Column(db.String(7), nullable=False, default="guest") # guest, client, catcher, admin
+ type = db.Column(db.Enum(UserType))
email = db.Column(db.String(120), unique=True, nullable=False)
name = db.Column(db.String(20), unique=True, nullable=False)
password = db.Column(db.String(60), nullable=False)
image_file = db.Column(db.String(20), nullable=False, default='default.jpg')
+ phone = db.Column(db.Text, nullable=False)
+ address = db.Column(db.Text)
+
+ manager = db.Column(db.Integer, db.ForeignKey('user.id')) # set if technician, catcher, user
+ catcher_code = db.Column(db.String(5)) # set if catcher
+ catcher = db.Column(db.Integer, db.ForeignKey('user.id')) # set if user
-class Home(db.Model):
- id = db.Column(db.Integer, primary_key=True)
- owner = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
- catcher = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
- street = db.Column(db.Text, nullable=False)
- number = db.Column(db.Integer, nullable=False)
- zipcode = db.Column(db.Text, nullable=False)
- place = db.Column(db.Text, nullable=False)
class Trap(db.Model):
id = db.Column(db.Integer, primary_key=True)
mac = db.Column(db.String(16), unique=True, nullable=False)
name = db.Column(db.Text)
- home = db.Column(db.Integer, db.ForeignKey('home.id'), nullable=False)
- last_heartbeat = db.Column(db.Integer, nullable=True, default=0)
+ owner = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
+ last_heartbeat = db.Column(db.DateTime, nullable=True, default=0)
caught = db.Column(db.Boolean, nullable=False, default=False)
+
+ def pretty_mac(self):
+ upper = self.mac.upper()
+ return ':'.join([ upper[i] + upper[i+1] for i in range(0, len(upper), 2) ])
+
+ def status_color(self):
+ if self.caught:
+ return '#f4a900'
+ return 'currentColor'
diff --git a/server/routes.py b/server/routes.py
@@ -10,7 +10,7 @@ from datetime import datetime
from .app import app, bcrypt, db
from .forms import LoginForm, RegistrationForm, UpdateAccountForm
-from .models import Home, Trap, User
+from .models import Trap, User
""" index.html (home-page) route """
@@ -103,11 +103,10 @@ def account():
@app.route('/dashboard')
@login_required
def dashboard():
- traps = []
- for home in Home.query.filter((Home.owner == current_user.id) | (Home.catcher == current_user.id)):
- for trap in Trap.query.filter_by(home=home.id):
- traps.append((home, trap))
- print(traps)
+ query = [ current_user ] if current_user.type == 'client' else User.query.filter_by(catcher=current_user.id)
+
+ traps = [ trap for user in query for trap in Trap.query.filter_by(owner=user.id) ]
+
return render_template('dashboard.html', title='Dashboard', traps=traps)
""" 404 not found handler """
diff --git a/server/site.db b/server/site.db
Binary files differ.
diff --git a/server/site.db-journal b/server/site.db-journal
Binary files differ.
diff --git a/server/templates/dashboard.html b/server/templates/dashboard.html
@@ -2,7 +2,7 @@
{% block content %}
<article class="media content-section">
<div class="media-body">
- <h2>Dit zijn uw muizenvallen</h2>
+ <h2><b>Dit zijn uw muizenvallen</b></h2>
<p>
Klik op de titel van een muizenval om de instellingen in te zien!
</p>
@@ -12,19 +12,32 @@
<article class="media content-section">
<div class="media-body">
<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 }} <sub><code>{{ trap.mac }}</code></sub>
+ {{ trap.name }}
{% else %}
- <code>{{ trap.mac }}</code>
+ <code>[{{ trap.pretty_mac() }}]</code>
{% endif %}
- </a></h3>
+ <small>van {{ home.owner_class().name }}</small>
+ </a>
+ </h3>
+ {% if trap.name %}
<p>
- {{ home.adress }}
+ <code>[{{ trap.pretty_mac() }}]</code>
</p>
+ {% endif %}
<p>
- {% if trap.caught %}
- <b>(geactiveerd)</b>
- {% endif %}
+ onderhouden door <b>{{ home.catcher_class().name }}</b>
+ </p>
+ <p>
+ <a class="article-title" href="#">
+ {{ home.street }} {{ home.number }}<br>
+ {{ home.zipcode }} {{ home.place }}
+ </a>
</p>
</div>
</article>
diff --git a/server/templates/layout.html b/server/templates/layout.html
@@ -28,10 +28,11 @@
<nav class="navbar navbar-expand-md fixed-top">
<div class="container">
<a class="navbar-brand mr-4" href="/">
- <code>muizenval.io</code>
+ <b><code>muizenval.io</code></b>
</a>
<div class="collapse navbar-collapse" id="navbarToggle">
<div class="navbar-nav mr-auto">
+ <a class="nav-item nav-link" href="#">Shop</a>
<a class="nav-item nav-link" href="{{ url_for('about') }}">Over ons</a>
</div>
<!-- Navbar Right Side -->