commit 5ac9fc5deff62f780085e7a55f3d8df07eaea697
parent 3cea76fe26ef8af5977d26b85fc32f48aec4408d
Author: Friedel Schön <[email protected]>
Date: Thu, 7 Apr 2022 13:34:48 +0200
merged master -> friedel-dev
Diffstat:
8 files changed, 156 insertions(+), 17 deletions(-)
diff --git a/pgmles/forms.py b/pgmles/forms.py
@@ -1,8 +1,8 @@
from flask_login import current_user
from flask_wtf import FlaskForm
from flask_wtf.file import FileAllowed, FileField
-from wtforms import (BooleanField, PasswordField, StringField, SubmitField,
- TextAreaField)
+from wtforms import (BooleanField, HiddenField, PasswordField, StringField,
+ SubmitField, TextAreaField)
from wtforms.validators import (DataRequired, Email, EqualTo, Length,
ValidationError)
@@ -63,7 +63,23 @@ class UpdateAccountForm(FlaskForm):
'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')
diff --git a/pgmles/models.py b/pgmles/models.py
@@ -1,3 +1,5 @@
+from datetime import datetime
+
from flask_login import UserMixin
from . import db, login_manager
@@ -37,7 +39,7 @@ class Classes(db.Model):
'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/pgmles/routes.py b/pgmles/routes.py
@@ -14,7 +14,11 @@ from .models import Classes, Language, User
def index():
page = request.args.get('page', 1, type=int)
languages = Language.query.all()
- return render_template('index.html', calendar=calendar, 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")
@@ -111,18 +115,51 @@ def new_post():
form=form, legend='New Post')
[email protected]("/post/<int:post_id>")
-def post(post_id):
- #post = Post.query.get_or_404(post_id)
- return render_template('post.html', calendar=calendar, title=post.title, post=post)
[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, 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/pgmles/templates/admin.html b/pgmles/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/pgmles/templates/course.html b/pgmles/templates/course.html
@@ -0,0 +1,30 @@
+{% extends "layout.html" %}
+{% block content %}
+ <article class="media content-section">
+ <div class="media-body">
+ <h2><a class="article-title" href="{{ url_for('course', course_id=course.id)}}">{{ course.name }}</a></h2>
+ <p class="article-content">{{ course.info }}</p>
+ </div>
+ <div class="article-metadata">
+ {% if current_user.is_authenticated %}
+ {% 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 %}
+ </div>
+ </article>
+{% endblock content %}
diff --git a/pgmles/templates/index.html b/pgmles/templates/index.html
@@ -1,10 +1,10 @@
{% extends "layout.html" %}
{% block content %}
- {% for name in languages %}
+ {% for language in languages %}
<article class="media content-section">
<div class="media-body">
- <h1>{{languages.name}}</h1>
- <p> {{languages.info}}</p>
+ <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>
{% endfor %}
diff --git a/pgmles/templates/layout.html b/pgmles/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/pgmles/templates/update_lang.html b/pgmles/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 %}