commit 90545081e670f16bf9f9d8098d4ebd06a680c4f6
parent e64a730b781679393fed0bf22138270d92441454
Author: Friedel Schön <[email protected]>
Date: Thu, 12 May 2022 13:50:38 +0200
adding registration-forms
Diffstat:
7 files changed, 119 insertions(+), 33 deletions(-)
diff --git a/server/app.py b/server/app.py
@@ -3,9 +3,11 @@ from flask_bcrypt import Bcrypt
from flask_login import LoginManager
from flask_sqlalchemy import SQLAlchemy
+
app = Flask(__name__)
app.config['SECRET_KEY'] = 'iot_project'
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///site.db'
+app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)
bcrypt = Bcrypt(app)
login_manager = LoginManager(app)
diff --git a/server/forms.py b/server/forms.py
@@ -1,17 +1,23 @@
-from flask_login import current_user
+import re
from flask_wtf import FlaskForm
from flask_wtf.file import FileAllowed, FileField
-from wtforms import BooleanField, HiddenField, PasswordField, SelectField, StringField, SubmitField, TextAreaField
+from wtforms import BooleanField, HiddenField, PasswordField, SelectField, StringField, SubmitField, IntegerField
from wtforms.validators import DataRequired, Email, EqualTo, Length, ValidationError
-from .models import User
+from .models import User, UserType
""" registration form for register.html """
class RegistrationForm(FlaskForm):
- name = StringField('Naam', validators=[ DataRequired(), Length(min=2, max=20) ])
+ name = StringField('Naam', validators=[ DataRequired(), Length(min=5, max=20) ])
email = StringField('E-Mail', validators=[ DataRequired(), Email() ])
password = PasswordField('Wachtwoord', validators=[ DataRequired() ])
confirm_password = PasswordField('Wachtwoord herhalen', validators=[ DataRequired(), EqualTo('password') ])
+ phone = StringField('Telefoon', validators=[ DataRequired(), Length(min=5) ])
+ street = StringField('Straat', validators=[ DataRequired() ])
+ housenumber = IntegerField('Huisnummer', validators=[ DataRequired() ])
+ postcode = 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 """
@@ -22,7 +28,21 @@ class RegistrationForm(FlaskForm):
""" validates whether e-mail is already in use """
def validate_email(self, email):
if User.query.filter_by(email=email.data).first():
- raise ValidationError('Deze e-mail bestaat al, log in als dat uw e-mail is')
+ raise ValidationError('Deze e-mail bestaat al, log in als dat uw e-mail is.')
+
+ def validate_phone(self, phone):
+ for c in phone.data:
+ if c not in '0123456789 -':
+ raise ValidationError('Dit belnummer is niet geldig.')
+
+ def validate_postcode(self, code):
+ 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):
+ 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 """
diff --git a/server/models.py b/server/models.py
@@ -10,10 +10,11 @@ def load_user(user_id):
class UserType(Enum):
- administrator = 0
- technician = 1
- catcher = 2
- user = 3
+ ADMIN = 0
+ MANAGER = 1
+ TECHNICIAN = 2
+ CATCHER = 3
+ CLIENT = 4
class User(db.Model, UserMixin):
id = db.Column(db.Integer, primary_key=True)
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 Trap, User
+from .models import Trap, User, UserType
""" index.html (home-page) route """
@@ -102,8 +102,10 @@ def account():
@app.route('/dashboard')
@login_required
-def dashboard():
- query = [ current_user ] if current_user.type == 'client' else User.query.filter_by(catcher=current_user.id)
+def dashboard():
+ query = [ current_user ]
+ if current_user.type == UserType.CATCHER:
+ query += list(User.query.filter_by(catcher=current_user.id))
traps = [ trap for user in query for trap in Trap.query.filter_by(owner=user.id) ]
diff --git a/server/templates/index.html b/server/templates/index.html
@@ -8,25 +8,4 @@
</p>
</div>
</article>
-{% for course in courses %}
-<article class="media content-section">
- <div class="media-body">
- <h3><a class="article-title" href="{{url_for('course', course_id=course.id)}}">
- {{ course.name }}
- {{ '(ingescheven)' if course.id in subs }}
- </a></h3>
- <p><i>{{ course.description }}</i></p>
- <p>
- wordt gegeven door
- {% for teacher in teachers if teacher.id == course.teacher_id %}
- {{ teacher.name }},
- {% endfor %}
- </p>
- <p>
- elke {{ ['maandag', 'dinsdag', 'woensdag', 'donderdag', 'vrijdag', 'zaterdag', 'zondag'][course.weekday] }}
- {{ course.start }} uur t/m {{ course.end }} uur op locatie: <b>{{ course.location }}</b>
- </p>
- </div>
-</article>
-{% endfor %}
{% endblock content %}
\ No newline at end of file
diff --git a/server/templates/register.html b/server/templates/register.html
@@ -58,6 +58,84 @@
{{ form.confirm_password(class="form-control form-control-lg") }}
{% endif %}
</div>
+ <div class="form-group">
+ {{ form.phone.label(class="form-control-label") }}
+ {% if form.phone.errors %}
+ {{ form.phone(class="form-control form-control-lg is-invalid") }}
+ <div class="invalid-feedback">
+ {% for error in form.phone.errors %}
+ <span>{{ error }}</span>
+ {% endfor %}
+ </div>
+ {% else %}
+ {{ form.phone(class="form-control form-control-lg") }}
+ {% endif %}
+ </div>
+ <div class="form-group">
+ {{ form.street.label(class="form-control-label") }}
+ {% if form.street.errors %}
+ {{ form.street(class="form-control form-control-lg is-invalid") }}
+ <div class="invalid-feedback">
+ {% for error in form.street.errors %}
+ <span>{{ error }}</span>
+ {% endfor %}
+ </div>
+ {% else %}
+ {{ form.street(class="form-control form-control-lg") }}
+ {% endif %}
+ </div>
+ <div class="form-group">
+ {{ form.housenumber.label(class="form-control-label") }}
+ {% if form.housenumber.errors %}
+ {{ form.housenumber(class="form-control form-control-lg is-invalid") }}
+ <div class="invalid-feedback">
+ {% for error in form.housenumber.errors %}
+ <span>{{ error }}</span>
+ {% endfor %}
+ </div>
+ {% else %}
+ {{ form.housenumber(class="form-control form-control-lg") }}
+ {% endif %}
+ </div>
+ <div class="form-group">
+ {{ form.postcode.label(class="form-control-label") }}
+ {% if form.postcode.errors %}
+ {{ form.postcode(class="form-control form-control-lg is-invalid") }}
+ <div class="invalid-feedback">
+ {% for error in form.postcode.errors %}
+ <span>{{ error }}</span>
+ {% endfor %}
+ </div>
+ {% else %}
+ {{ form.postcode(class="form-control form-control-lg") }}
+ {% endif %}
+ </div>
+ <div class="form-group">
+ {{ form.place.label(class="form-control-label") }}
+ {% if form.place.errors %}
+ {{ form.place(class="form-control form-control-lg is-invalid") }}
+ <div class="invalid-feedback">
+ {% for error in form.place.errors %}
+ <span>{{ error }}</span>
+ {% endfor %}
+ </div>
+ {% else %}
+ {{ 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/test.py b/test.py
@@ -0,0 +1,4 @@
+code = '9939a3'
+
+if len(code) != 6 or not code[0:4].isnumeric() or not code[4:6].isalpha():
+ print('invalid')