programmeerles-ouderen

Teach programming to seniors
Log | Files | Refs

commit e26f34979925cc50d644d8729ee567f7b6878afc
parent ba6b02b1c87a2a43bc99de634e696f456081c2a2
Author: Friedel Schön <[email protected]>
Date:   Thu,  7 Apr 2022 14:54:30 +0200

demo

Diffstat:
Mpgmles/__init__.py | 4+---
Mpgmles/forms.py | 2+-
Mpgmles/routes.py | 21++++++++++++---------
Mpgmles/site.db | 0
Mpgmles/templates/course.html | 2+-
5 files changed, 15 insertions(+), 14 deletions(-)

diff --git a/pgmles/__init__.py b/pgmles/__init__.py @@ -1,5 +1,3 @@ -from typing import cast - from flask import Flask from flask_bcrypt import Bcrypt from flask_login import LoginManager @@ -13,7 +11,7 @@ app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///site.db' db = SQLAlchemy(app) bcrypt = Bcrypt(app) login_manager = LoginManager(app) -login_manager.login_view = cast(None, 'login') +login_manager.login_view = 'login' login_manager.login_message_category = 'info' calendar = Calendar() diff --git a/pgmles/forms.py b/pgmles/forms.py @@ -76,7 +76,7 @@ class SubscribeForm(FlaskForm): class UnsubscribeForm(FlaskForm): lang_id = HiddenField() - submit = SubmitField('Unsubsribe') + submit = SubmitField('Unsubscribe') class PostForm(FlaskForm): # redundant diff --git a/pgmles/routes.py b/pgmles/routes.py @@ -6,7 +6,8 @@ from flask_login import current_user, login_required, login_user, logout_user from PIL import Image from . import app, bcrypt, calendar, db -from .forms import LoginForm, PostForm, RegistrationForm, UpdateAccountForm +from .forms import (LoginForm, PostForm, RegistrationForm, SubscribeForm, + UnsubscribeForm, UpdateAccountForm) from .models import Classes, Language, User @@ -140,25 +141,27 @@ def update_lang(lang_id): def course(course_id): form = SubscribeForm() form2 = UnsubscribeForm() - subscription = Classes.query.filter_by( - user_id=current_user.id, language_id=course_id).first() - show = True - if subscription: - show = False - if form.validate_on_submit() and show == True: + subscription = None + if current_user.is_authenticated: + subscription = Classes.query.filter_by( + user_id=current_user.id, language_id=course_id).first() + + if form.validate_on_submit() and not subscription: course = Classes(user_id=current_user.id, language_id=course_id, teacher_id=1, location="hier") db.session.add(course) db.session.commit() flash('You have subscribed to this course!', 'success') return redirect(url_for('account')) - if form2.validate_on_submit() and show == False: + + if form2.validate_on_submit() and subscription: db.session.delete(subscription) db.session.commit() flash('You been have Unsubscribed to this course!', 'success') return redirect(url_for('account')) + course = Language.query.get_or_404(course_id) - return render_template('course.html', calendar=calendar, title=course.name, course=course, form=form, form2=form2, show=show) + return render_template('course.html', calendar=calendar, title=course.name, course=course, form=form, form2=form2, show=not subscription) @app.route("/course/<int:post_id>/update", methods=['GET', 'POST']) diff --git a/pgmles/site.db b/pgmles/site.db Binary files differ. diff --git a/pgmles/templates/course.html b/pgmles/templates/course.html @@ -14,7 +14,7 @@ {{ form.submit(class="btn btn-outline-info") }} </div> </form> - {%else%} + {% else %} <form method="POST" action=""> {{ form2.hidden_tag() }} <div class="form-group">