commit 76d95e2224e7b02a14913b7ce83edd28d9d65bfe
parent e3c00358411f7e9e52e59e34004b391972080cc5
Author: Friedel Schön <[email protected]>
Date: Sun, 17 Apr 2022 11:37:57 +0200
added function-documentation
Diffstat:
4 files changed, 36 insertions(+), 15 deletions(-)
diff --git a/pgmles/forms.py b/pgmles/forms.py
@@ -6,6 +6,7 @@ from wtforms.validators import DataRequired, Email, EqualTo, Length, ValidationE
from .models import User
+""" registration form for register.html """
class RegistrationForm(FlaskForm):
username = StringField('Naam', validators=[ DataRequired(), Length(min=2, max=20) ])
email = StringField('E-Mail', validators=[ DataRequired(), Email() ])
@@ -13,15 +14,18 @@ class RegistrationForm(FlaskForm):
confirm_password = PasswordField('Wachtwoord herhalen', validators=[ DataRequired(), EqualTo('password') ])
submit = SubmitField('Registeren')
+ """ validates whether username is already in use """
def validate_username(self, username):
if User.query.filter_by(username=username.data).first():
raise ValidationError('Deze gebruikersnaam bestaat al, kies een andere.')
+ """ 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')
+""" login form for login.html """
class LoginForm(FlaskForm):
email = StringField('E-Mail', validators=[ DataRequired(), Email() ])
password = PasswordField('Wachtwoord', validators=[ DataRequired() ])
@@ -29,6 +33,7 @@ class LoginForm(FlaskForm):
submit = SubmitField('Inloggen')
+""" update account form for account.html """
class UpdateAccountForm(FlaskForm):
username = StringField('Naam', validators=[ DataRequired(), Length(min=2, max=20) ])
email = StringField('E-Mail', validators=[ DataRequired(), Email() ])
@@ -37,14 +42,17 @@ class UpdateAccountForm(FlaskForm):
picture = FileField('Profielfoto bewerken', validators=[ FileAllowed(['jpg', 'png']) ])
submit = SubmitField('Bewerken')
+ """ validates whether username is already in use """
def validate_username(self, username):
if username.data != current_user.username and User.query.filter_by(username=username.data).first():
raise ValidationError('Deze gebruikersnaam bestaat al, kies een andere.')
+ """ validates whether e-mail is already in use """
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')
+""" update/new course form for new_course.html """
class NewCourseForm(FlaskForm):
name = StringField('Naam', validators=[ DataRequired(), Length(min=1, max=100) ])
description = TextAreaField('Beschrijving', validators=[ DataRequired() ])
@@ -55,20 +63,23 @@ class NewCourseForm(FlaskForm):
location = StringField('Locatie', validators=[ DataRequired(), Length(min=1, max=100) ])
submit = SubmitField('Bewerken')
-
+""" subscribe form for course.html """
class SubscribeForm(FlaskForm):
lang_id = HiddenField()
submit = SubmitField('Inschrijven')
+""" unsubscribe form for course.html """
class UnsubscribeForm(FlaskForm):
lang_id = HiddenField()
submit = SubmitField('Uitschrijven')
+""" search form for admin.html """
class SearchForm(FlaskForm):
username = StringField('Naam', validators=[ DataRequired(), Length(min=2, max=20)])
submit = SubmitField('Zoeken')
+""" account-settings form for admin_user.html """
class AdminForm(FlaskForm):
type = SelectField('Type', choices=[('client', 'Klant'), ('teacher', 'Docent'), ('admin', 'Administrator')])
submit = SubmitField('Bewerken')
diff --git a/pgmles/models.py b/pgmles/models.py
@@ -4,11 +4,12 @@ from flask_login import UserMixin
from .server import db, login_manager
+""" function to load a user from database """
@login_manager.user_loader
def load_user(user_id):
return User.query.get(int(user_id))
-
+""" user-struct for 'user'-database """
class User(db.Model, UserMixin):
id = db.Column(db.Integer, primary_key=True)
type = db.Column(db.String(6), nullable=False, default="client")
@@ -17,10 +18,7 @@ class User(db.Model, UserMixin):
image_file = db.Column(db.String(20), nullable=False, default='default.jpg')
password = db.Column(db.String(60), nullable=False)
- def __repr__(self):
- return f"User('{self.username}', '{self.email}', '{self.image_file}')"
-
-
+""" course-struct for 'course'-database """
class Course(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(100), nullable=False)
@@ -31,10 +29,7 @@ class Course(db.Model):
end = db.Column(db.String(10), nullable=False, default=datetime.utcnow)
location = db.Column(db.String(120), nullable=False)
- def __repr__(self):
- return f"Course('{self.id}', '{self.name}', '{self.description}')"
-
-
+""" course-member-struct for 'coursemember'-database """
class CourseMember(db.Model):
id = db.Column(db.Integer, primary_key=True)
course_id = db.Column(db.Integer, db.ForeignKey('course.id'), nullable=False)
diff --git a/pgmles/routes.py b/pgmles/routes.py
@@ -12,6 +12,7 @@ from .forms import LoginForm, NewCourseForm, AdminForm, RegistrationForm, Search
from .models import Course, CourseMember, User
+""" calendar-function to calculate days, etc. for calendar """
def make_calendar():
weekdays = list(enumerate(['Ma', 'Di', 'Wo', 'Do', 'Vr', 'Za', 'Zo']))
@@ -29,7 +30,7 @@ def make_calendar():
return { 'weekdays': weekdays, 'rows': rows }
-
+""" index.html (home-page) route """
@app.route("/")
def index():
courses = Course.query.all()
@@ -39,13 +40,16 @@ def index():
subscriptions = [ cm.course_id for cm in CourseMember.query.filter_by(user_id=current_user.id) ]
return render_template('index.html', calendar=make_calendar(), courses=courses, subs=subscriptions, teachers=teachers)
+""" about.html route """
@app.route("/about")
def about():
return render_template('about.html', calendar=make_calendar(), title='Over ons')
+""" register.html route """
@app.route("/register", methods=['GET', 'POST'])
def register():
if current_user.is_authenticated:
+ flash('U bent al ingelogd', 'warning')
return redirect('/')
form = RegistrationForm()
if form.validate_on_submit():
@@ -57,10 +61,11 @@ def register():
return redirect(url_for('login'))
return render_template('register.html', calendar=make_calendar(), title='Registeren', form=form)
-
+""" login.html route """
@app.route("/login", methods=['GET', 'POST'])
def login():
if current_user.is_authenticated:
+ flash('U bent al ingelogd', 'warning')
return redirect('/')
form = LoginForm()
if form.validate_on_submit():
@@ -73,11 +78,13 @@ def login():
flash('Kon niet inloggen, is uw e-mail en wachtwoord juist?', 'danger')
return render_template('login.html', calendar=make_calendar(), title='Inloggen', form=form)
+""" logout route """
@app.route("/logout")
def logout():
logout_user()
return redirect('/')
+""" save-picture function for account.html """
def save_picture(form_picture):
random_hex = secrets.token_hex(8)
_, f_ext = os.path.splitext(form_picture.filename)
@@ -91,6 +98,7 @@ def save_picture(form_picture):
return picture_fn
+""" account.html route """
@app.route("/account", methods=[ 'GET', 'POST' ])
@login_required
def account():
@@ -112,7 +120,7 @@ def account():
image_file = url_for('static', filename='profile_pics/' + current_user.image_file)
return render_template('account.html', calendar=make_calendar(), title='Profiel', image_file=image_file, form=form)
-
+""" course_overview.html route """
@app.route("/course_overview")
@login_required
def course_overview():
@@ -121,7 +129,7 @@ def course_overview():
courses = [ (c, User.query.filter_by(id=c.id).first() ) for c in Course.query.all() ]
return render_template('course_overview.html', calendar=make_calendar(), title='Lesoverzicht', courses=courses)
-
+""" new_course.html route """
@app.route("/course_overview/new_course", methods=['GET', 'POST'])
@login_required
def new_course():
@@ -137,7 +145,7 @@ def new_course():
return redirect(url_for('course_overview'))
return render_template('new_course.html', calendar=make_calendar(), title='Nieuwe les', form=form)
-
+""" new_course.html (update course) route """
@app.route("/course_overview/course_update/<int:course_id>", methods=['GET', 'POST'])
@login_required
def update_course(course_id):
@@ -167,6 +175,7 @@ def update_course(course_id):
form.location.data = course.location
return render_template('new_course.html', calendar=make_calendar(), form=form, legend='Update Language')
+""" course.html (course-info) route """
@app.route("/course/<int:course_id>", methods=[ 'GET', 'POST' ])
def course(course_id):
sub_form = SubscribeForm()
@@ -192,6 +201,7 @@ def course(course_id):
course = Course.query.get_or_404(course_id)
return render_template('course.html', calendar=make_calendar(), title=course.name, course=course, sub_form=sub_form, unsub_form=unsub_form, subscribed=subscribed is not None, teachers=teachers)
+""" delete-course route """
@app.route("/delete_course/<int:course_id>", methods=['GET','POST'])
@login_required
def delete_course(course_id):
@@ -202,6 +212,7 @@ def delete_course(course_id):
db.session.commit()
return redirect(url_for('course_overview'))
+""" admin.html route """
@app.route("/admin", methods=['GET','POST'])
@login_required
def admin():
@@ -217,6 +228,7 @@ def admin():
return redirect(url_for('admin_user', user_id= user.id))
return render_template('admin.html', calendar=make_calendar(), form=form)
+""" account-admin route """
@app.route("/admin/<int:user_id>", methods=['GET','POST'])
@login_required
def admin_user(user_id):
@@ -234,6 +246,7 @@ def admin_user(user_id):
form.type.data = user.type
return render_template('admin_user.html', calendar=make_calendar(), form=form, user=user, image_file=image_file)
+""" delete-user route """
@app.route("/delete_user/<int:user_id>", methods=['GET','POST'])
@login_required
def delete_user(user_id):
@@ -245,6 +258,7 @@ def delete_user(user_id):
flash(f'De gebruiker {user.username} werd verwijdert', 'success')
return redirect(url_for('admin'))
+""" reset user's password route """
@app.route("/reset_user/<int:user_id>", methods=['GET','POST'])
@login_required
def reset_user(user_id):
diff --git a/pgmles/server.py b/pgmles/server.py
@@ -12,4 +12,5 @@ login_manager = LoginManager(app)
login_manager.login_view = 'login'
login_manager.login_message_category = 'info'
+# to run 'routes.py' and make the routes available
from .routes import *