programmeerles-ouderen

Teach programming to seniors
Log | Files | Refs

commit 32313533ff8ab8eda3c54752c3651354a0ef187d
parent d852d0b63359c232dd8b569778216a31b47bc8fd
Author: xd <[email protected]>
Date:   Thu,  7 Apr 2022 13:26:16 +0200

Update lang toegevoegd en admin page

Diffstat:
Mflaskblog/forms.py | 17+++++++++++++++--
Mflaskblog/models.py | 2+-
Mflaskblog/routes.py | 54++++++++++++++++++++++++++++++++++++++++++++++--------
Mflaskblog/site.db | 0
Aflaskblog/templates/admin.html | 14++++++++++++++
Mflaskblog/templates/course.html | 18+++++++++++++++---
Mflaskblog/templates/home.html | 2+-
Mflaskblog/templates/layout.html | 2+-
Aflaskblog/templates/update_lang.html | 40++++++++++++++++++++++++++++++++++++++++
9 files changed, 133 insertions(+), 16 deletions(-)

diff --git a/flaskblog/forms.py b/flaskblog/forms.py @@ -1,7 +1,7 @@ from flask_wtf import FlaskForm from flask_wtf.file import FileField, FileAllowed from flask_login import current_user -from wtforms import StringField, PasswordField, SubmitField, BooleanField, TextAreaField +from wtforms import StringField, PasswordField, SubmitField, BooleanField, TextAreaField, HiddenField from wtforms.validators import DataRequired, Length, Email, EqualTo, ValidationError from flaskblog.models import User @@ -53,7 +53,20 @@ class UpdateAccountForm(FlaskForm): user = User.query.filter_by(email=email.data).first() if user: raise ValidationError('That email is taken. Please choose a different one.') -class PostForm(FlaskForm): +class LanguageForm(FlaskForm): + name = StringField('Name', validators=[DataRequired()]) + info = TextAreaField('Info', validators=[DataRequired()]) + submit = SubmitField('Update') + +class SubscribeForm(FlaskForm): + lang_id = HiddenField() + submit = SubmitField('Subscribe') + +class UnsubscribeForm(FlaskForm): + lang_id = HiddenField() + submit = SubmitField('Unsubsribe') + +class PostForm(FlaskForm):# redundant title = StringField('Title', validators=[DataRequired()]) content = TextAreaField('Content', validators=[DataRequired()]) submit = SubmitField('Post') \ No newline at end of file diff --git a/flaskblog/models.py b/flaskblog/models.py @@ -32,7 +32,7 @@ class Classes(db.Model): user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False) language_id = db.Column(db.Integer, db.ForeignKey('language.id'), nullable=False) teacher_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False) - start = db.Column(db.DateTime, nullable=False) + start = db.Column(db.DateTime, nullable=False, default=datetime.utcnow) location = db.Column(db.String(120), nullable=False) def __repr__(self): diff --git a/flaskblog/routes.py b/flaskblog/routes.py @@ -3,7 +3,7 @@ import secrets from PIL import Image from flask import render_template, url_for, flash, redirect, request, abort from flaskblog import app, db, bcrypt -from flaskblog.forms import RegistrationForm, LoginForm, UpdateAccountForm, PostForm +from flaskblog.forms import RegistrationForm, LoginForm, UpdateAccountForm, LanguageForm, PostForm, SubscribeForm, UnsubscribeForm from flaskblog.models import User, Language, Classes from flask_login import login_user, current_user, logout_user, login_required @@ -13,7 +13,11 @@ from flask_login import login_user, current_user, logout_user, login_required def home(): page = request.args.get('page', 1, type=int) languages = Language.query.all() - return render_template('home.html', languages=languages) + subs = Classes.query.filter_by(user_id=current_user.id) + lijst = [] + for sub in subs: + lijst.append(sub.language_id) + return render_template('home.html', languages=languages, subs=lijst, subscribed="subscribed") @app.route("/about") @@ -102,17 +106,51 @@ def new_post(): return render_template('create_post.html', title='New Post', form=form, legend='New Post') [email protected]("/course/<int:course_id>") [email protected]("/admin") +def admin(): + languages = Language.query.all() + return render_template('admin.html', title = 'Administration Page', languages = languages) + [email protected]("/admin/update/<int:lang_id>", methods=['GET', 'POST']) +def update_lang(lang_id): + form = LanguageForm() + lang = Language.query.get_or_404(lang_id) + if form.validate_on_submit(): + lang.name = form.name.data + lang.info = form.info.data + db.session.commit() + flash('The course has been updated!', 'success') + return redirect(url_for('home', lang_id=lang.id)) + elif request.method == 'GET': + form.name.data = lang.name + form.info.data = lang.info + return render_template('update_lang.html', form=form, legend='Update Language') + [email protected]("/course/<int:course_id>", methods=['GET', 'POST']) 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: + 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: + 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', title=course.name, course=course) + return render_template('course.html', title=course.name, course=course, form=form, form2=form2, show=show) [email protected]("/post/<int:post_id>/update", methods=['GET', 'POST']) [email protected]("/course/<int:post_id>/update", methods=['GET', 'POST']) @login_required def update_post(post_id): - #post = Post.query.get_or_404(post_id) - if post.author != current_user: - abort(403) form = PostForm() if form.validate_on_submit(): post.title = form.title.data diff --git a/flaskblog/site.db b/flaskblog/site.db Binary files differ. diff --git a/flaskblog/templates/admin.html b/flaskblog/templates/admin.html @@ -0,0 +1,14 @@ +{% extends "layout.html" %} +{% block content %} + {% for language in languages %} + <article class="media content-section"> + <div class="media-body"> + <h2><a class="article-title" href="{{url_for('course', course_id=language.id)}}">{{language.name}}</a></h2> + <div> + <a class="btn btn-secondary btn-sm mt-1 mb-1" href="{{ url_for('update_lang', lang_id = language.id) }}">Update</a> + <button type="button" class="btn btn-danger btn-sm m-1" data-toggle="modal" data-target="#deleteModal">Delete</button> + </div> + </div> + </article> + {% endfor %} +{% endblock content %} diff --git a/flaskblog/templates/course.html b/flaskblog/templates/course.html @@ -7,9 +7,21 @@ </div> <div class="article-metadata"> {% if current_user.is_authenticated %} - <div> - <a class="btn btn-secondary btn-sm mt-1 mb-1" href="{{url_for('course', course_id=course.id)}}">Sign Up</a> - </div> + {% if show %} + <form method="POST" action=""> + {{ form.hidden_tag() }} + <div class="form-group"> + {{ form.submit(class="btn btn-outline-info") }} + </div> + </form> + {%else%} + <form method="POST" action=""> + {{ form2.hidden_tag() }} + <div class="form-group"> + {{ form2.submit(class="btn btn-outline-info") }} + </div> + </form> + {%endif%} {% else %} <p> Please Log in to subscribe to course</p> {% endif %} diff --git a/flaskblog/templates/home.html b/flaskblog/templates/home.html @@ -3,7 +3,7 @@ {% for language in languages %} <article class="media content-section"> <div class="media-body"> - <h2><a class="article-title" href="{{url_for('course', course_id=language.id)}}">{{language.name}}</a></h2> + <h2><a class="article-title" href="{{url_for('course', course_id=language.id)}}">{{language.name}} {%if language.id in subs%}({{subscribed}}){%endif%}</a></h2> <p> {{language.info}}</p> </div> </article> diff --git a/flaskblog/templates/layout.html b/flaskblog/templates/layout.html @@ -32,7 +32,7 @@ <!-- Navbar Right Side --> <div class="navbar-nav"> {% if current_user.is_authenticated %} - <a class="nav-item nav-link" href="{{ url_for('new_post') }}">New Post</a> + <a class="nav-item nav-link" href="{{ url_for('admin') }}">Admin Page</a> <a class="nav-item nav-link" href="{{ url_for('account') }}">Account</a> <a class="nav-item nav-link" href="{{ url_for('logout') }}">Logout</a> {% else %} diff --git a/flaskblog/templates/update_lang.html b/flaskblog/templates/update_lang.html @@ -0,0 +1,40 @@ +{% extends "layout.html" %} +{% block content %} +<div class="content-section"> + <form method="POST" action=""> + {{ form.hidden_tag() }} + <fieldset class="form-group"> + <legend class="border-bottom mb-4">{{ legend }}</legend> + <div class="form-group"> + {{ form.name.label(class="form-control-label") }} + {% if form.name.errors %} + {{ form.name(class="form-control form-control-lg is-invalid") }} + <div class="invalid-feedback"> + {% for error in form.name.errors %} + <span>{{ error }}</span> + {% endfor %} + </div> + {% else %} + {{ form.name(class="form-control form-control-lg") }} + {% endif %} + </div> + <div class="form-group"> + {{ form.info.label(class="form-control-label") }} + {% if form.info.errors %} + {{ form.info(class="form-control form-control-lg is-invalid") }} + <div class="invalid-feedback"> + {% for error in form.info.errors %} + <span>{{ error }}</span> + {% endfor %} + </div> + {% else %} + {{ form.info(class="form-control form-control-lg") }} + {% endif %} + </div> + </fieldset> + <div class="form-group"> + {{ form.submit(class="btn btn-outline-info") }} + </div> + </form> +</div> +{% endblock content %}