commit fab1d389126af796e95aec70838849598506d3cd
parent 10a58b6c4c71ddf5deb670e1741b07b4fd0900a2
Author: xd <[email protected]>
Date: Fri, 8 Apr 2022 15:12:38 +0200
Added update, delete and extra information to the homescreen
Diffstat:
8 files changed, 277 insertions(+), 15 deletions(-)
diff --git a/pgmles/forms.py b/pgmles/forms.py
@@ -2,7 +2,7 @@ from flask_login import current_user
from flask_wtf import FlaskForm
from flask_wtf.file import FileAllowed, FileField
from wtforms import (BooleanField, HiddenField, PasswordField, StringField,
- SubmitField, TextAreaField)
+ SubmitField, TextAreaField, SelectField)
from wtforms.validators import (DataRequired, Email, EqualTo, Length,
ValidationError)
@@ -68,6 +68,18 @@ class LanguageForm(FlaskForm):
info = TextAreaField('Info', validators=[DataRequired()])
submit = SubmitField('Update')
+class NewCourseForm(FlaskForm):
+ name = StringField('Title', validators=[
+ DataRequired(), Length(min=1, max=100)])
+ description = TextAreaField('Description', validators=[DataRequired()])
+ teacher_id = SelectField('Teacher', validators=[DataRequired()], coerce=int)
+ weekday = StringField('Weekday', validators=[DataRequired()])
+ start = StringField('Start', validators=[DataRequired()])
+ end = StringField('End', validators=[DataRequired()])
+ location = StringField('Location', validators=[
+ DataRequired(), Length(min=1, max=100)])
+ submit = SubmitField('Add')
+
class SubscribeForm(FlaskForm):
lang_id = HiddenField()
diff --git a/pgmles/routes.py b/pgmles/routes.py
@@ -7,7 +7,7 @@ from PIL import Image
from . import app, bcrypt, calendar, db
from .forms import (LanguageForm, LoginForm, PostForm, RegistrationForm,
- SubscribeForm, UnsubscribeForm, UpdateAccountForm)
+ SubscribeForm, UnsubscribeForm, UpdateAccountForm, NewCourseForm)
from .models import Course, CourseMember, User
@@ -15,12 +15,13 @@ from .models import Course, CourseMember, User
def index():
courses = Course.query.all()
subscriptions = []
+ teachers = [[teacher.id, teacher.username] for teacher in User.query.filter_by(type='teacher')]
if current_user.is_authenticated:
subscriptions = [cm.course_id for cm in CourseMember.query.filter_by(
user_id=current_user.id)]
# for coursemember in members:
-# course = Course
- return render_template('index.html', calendar=calendar, courses=courses, subs=subscriptions)
+# course[] = Course.id
+ return render_template('index.html', calendar=calendar, courses=courses, subs=subscriptions, teachers=teachers)
@app.route("/about")
@@ -108,20 +109,45 @@ def admin():
courses = Course.query.all()
return render_template('admin.html', calendar=calendar, title='Administration Page', courses=courses)
[email protected]("/admin/new_course", methods=['GET', 'POST'])
+def new_course():
+ form = NewCourseForm()
+ form.teacher_id.choices = [(g.id, g.username) for g in User.query.filter_by(type='teacher')]
+ if form.validate_on_submit():
+ course = Course(name=form.name.data, description=form.description.data,\
+ teacher_id=form.teacher_id.data, weekday=form.weekday.data,\
+ start=form.start.data, end=form.end.data, location=form.location.data)
+ db.session.add(course)
+ db.session.commit()
+ flash('The course has been created!', 'success')
+ return redirect(url_for('admin'))
+ return render_template('new_course.html', calendar=calendar, title='New Course', form=form)
+
@app.route("/admin/update/<int:course_id>", methods=['GET', 'POST'])
def update_lang(course_id):
- form = LanguageForm()
+ form = NewCourseForm()
+ form.teacher_id.choices = [(g.id, g.username) for g in User.query.filter_by(type='teacher')]
course = Course.query.get_or_404(course_id)
if form.validate_on_submit():
course.name = form.name.data
- course.description = form.info.data
+ course.description = form.description.data
+ course.teacher_id = form.teacher_id.data
+ course.weekday = form.weekday.data
+ course.start = form.start.data
+ course.end = form.end.data
+ course.location = form.location.data
db.session.commit()
flash('The course has been updated!', 'success')
return redirect(url_for('admin'))
elif request.method == 'GET':
form.name.data = course.name
- form.info.data = course.description
+ form.description.data = course.description
+ form.teacher_id.data = course.teacher_id
+ form.weekday.data = course.weekday
+ form.start.data = course.start
+ form.end.data = course.end
+ form.location.data = course.location
return render_template('update_lang.html', calendar=calendar, form=form, legend='Update Language')
@@ -129,6 +155,7 @@ def update_lang(course_id):
def course(course_id):
form = SubscribeForm()
form2 = UnsubscribeForm()
+ teachers = [[teacher.id, teacher.username] for teacher in User.query.filter_by(type='teacher')]
subscribed = None
if current_user.is_authenticated:
subscribed = CourseMember.query.filter_by(
@@ -148,4 +175,11 @@ def course(course_id):
return redirect(url_for('account'))
course = Course.query.get_or_404(course_id)
- return render_template('course.html', calendar=calendar, title=course.name, course=course, form=form, form2=form2, show=not subscribed)
+ return render_template('course.html', calendar=calendar, title=course.name, course=course, form=form, form2=form2, show=not subscribed, teachers=teachers)
+
[email protected]("/delete_course/<int:course_id>", methods=['GET','POST'])
+def delete_course(course_id):
+ course = Course.query.get_or_404(course_id)
+ db.session.delete(course)
+ db.session.commit()
+ return redirect(url_for('index'))
+\ No newline at end of file
diff --git a/pgmles/site.db b/pgmles/site.db
Binary files differ.
diff --git a/pgmles/templates/admin.html b/pgmles/templates/admin.html
@@ -1,5 +1,6 @@
{% extends "layout.html" %}
{% block content %}
+<a class="btn btn-secondary btn-sm mt-1 mb-1" href="{{ url_for('new_course') }}">New Course</a>
{% for course in courses %}
<article class="media content-section">
<div class="media-body">
@@ -10,5 +11,24 @@
</div>
</div>
</article>
+ <!-- Modal -->
+ <div class="modal fade" id="deleteModal" tabindex="-1" role="dialog" aria-labelledby="deleteModalLabel" aria-hidden="true">
+ <div class="modal-dialog" role="document">
+ <div class="modal-content">
+ <div class="modal-header">
+ <h5 class="modal-title" id="deleteModalLabel">Delete Post?</h5>
+ <button type="button" class="close" data-dismiss="modal" aria-label="Close">
+ <span aria-hidden="true">×</span>
+ </button>
+ </div>
+ <div class="modal-footer">
+ <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
+ <form action="{{ url_for('delete_course', course_id=course.id) }}" method="POST">
+ <input class="btn btn-danger" type="submit" value="Delete">
+ </form>
+ </div>
+ </div>
+ </div>
+ </div>
{% endfor %}
{% endblock content %}
diff --git a/pgmles/templates/course.html b/pgmles/templates/course.html
@@ -2,8 +2,21 @@
{% 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.description }}</p>
+ <h2><a class="article-title" href="{{url_for('course', course_id=course.id)}}">
+ {{course.name}}
+ {% if course.id in subs %}(subscribed){% endif %}
+ </a></h2>
+ <p> {{course.description}}</p>
+ <p>De Cursus {{course.name}} wordt gegeven door
+ {%for x in teachers%}
+ {%if x[0] == course.teacher_id%}
+ {{x[1]}}
+ {%else%}
+ Missing Data
+ {%endif%}
+ {%endfor%}
+ </p>
+ <p>Start elke {{course.weekday}} vanaf {{course.start}} tot {{course.end}} op locatie: {{course.location}}</p>
</div>
<div class="article-metadata">
{% if current_user.is_authenticated %}
diff --git a/pgmles/templates/index.html b/pgmles/templates/index.html
@@ -8,6 +8,16 @@
{% if course.id in subs %}(subscribed){% endif %}
</a></h2>
<p> {{course.description}}</p>
+ <p>De Cursus {{course.name}} wordt gegeven door
+ {%for x in teachers%}
+ {%if x[0] == course.teacher_id%}
+ {{x[1]}}
+ {%else%}
+ Missing Data
+ {%endif%}
+ {%endfor%}
+ </p>
+ <p>Start elke {{course.weekday}} vanaf {{course.start}} tot {{course.end}} op locatie: {{course.location}}</p>
</div>
</article>
{% endfor %}
diff --git a/pgmles/templates/new_course.html b/pgmles/templates/new_course.html
@@ -0,0 +1,106 @@
+{% 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.description.label(class="form-control-label") }}
+ {% if form.description.errors %}
+ {{ form.description(class="form-control form-control-lg is-invalid") }}
+ <div class="invalid-feedback">
+ {% for error in form.description.errors %}
+ <span>{{ error }}</span>
+ {% endfor %}
+ </div>
+ {% else %}
+ {{ form.description(class="form-control form-control-lg") }}
+ {% endif %}
+ </div>
+ <div class="form-group">
+ {{ form.teacher_id.label(class="form-control-label") }}
+ {% if form.teacher_id.errors %}
+ {{ form.teacher_id(class="form-control form-control-lg is-invalid") }}
+ <div class="invalid-feedback">
+ {% for error in form.teacher_id.errors %}
+ <span>{{ error }}</span>
+ {% endfor %}
+ </div>
+ {% else %}
+ {{ form.teacher_id(class="form-control form-control-lg") }}
+ {% endif %}
+ </div>
+ <div class="form-group">
+ {{ form.weekday.label(class="form-control-label") }}
+ {% if form.weekday.errors %}
+ {{ form.weekday(class="form-control form-control-lg is-invalid") }}
+ <div class="invalid-feedback">
+ {% for error in form.weekday.errors %}
+ <span>{{ error }}</span>
+ {% endfor %}
+ </div>
+ {% else %}
+ {{ form.weekday(class="form-control form-control-lg") }}
+ {% endif %}
+ </div>
+ <div class="form-group">
+ {{ form.start.label(class="form-control-label") }}
+ {% if form.start.errors %}
+ {{ form.start(class="form-control form-control-lg is-invalid") }}
+ <div class="invalid-feedback">
+ {% for error in form.start.errors %}
+ <span>{{ error }}</span>
+ {% endfor %}
+ </div>
+ {% else %}
+ {{ form.start(class="form-control form-control-lg") }}
+ {% endif %}
+ </div>
+ <div class="form-group">
+ {{ form.end.label(class="form-control-label") }}
+ {% if form.end.errors %}
+ {{ form.end(class="form-control form-control-lg is-invalid") }}
+ <div class="invalid-feedback">
+ {% for error in form.end.errors %}
+ <span>{{ error }}</span>
+ {% endfor %}
+ </div>
+ {% else %}
+ {{ form.end(class="form-control form-control-lg") }}
+ {% endif %}
+ </div>
+ <div class="form-group">
+ {{ form.location.label(class="form-control-label") }}
+ {% if form.location.errors %}
+ {{ form.location(class="form-control form-control-lg is-invalid") }}
+ <div class="invalid-feedback">
+ {% for error in form.location.errors %}
+ <span>{{ error }}</span>
+ {% endfor %}
+ </div>
+ {% else %}
+ {{ form.location(class="form-control form-control-lg") }}
+ {% endif %}
+ </div>
+ </div>
+ </fieldset>
+ <div class="form-group">
+ {{ form.submit(class="btn btn-outline-info") }}
+ </div>
+ </form>
+</div>
+{% endblock content %}
diff --git a/pgmles/templates/update_lang.html b/pgmles/templates/update_lang.html
@@ -19,18 +19,84 @@
{% 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") }}
+ {{ form.description.label(class="form-control-label") }}
+ {% if form.description.errors %}
+ {{ form.description(class="form-control form-control-lg is-invalid") }}
<div class="invalid-feedback">
- {% for error in form.info.errors %}
+ {% for error in form.description.errors %}
<span>{{ error }}</span>
{% endfor %}
</div>
{% else %}
- {{ form.info(class="form-control form-control-lg") }}
+ {{ form.description(class="form-control form-control-lg") }}
{% endif %}
</div>
+ <div class="form-group">
+ {{ form.teacher_id.label(class="form-control-label") }}
+ {% if form.teacher_id.errors %}
+ {{ form.teacher_id(class="form-control form-control-lg is-invalid") }}
+ <div class="invalid-feedback">
+ {% for error in form.teacher_id.errors %}
+ <span>{{ error }}</span>
+ {% endfor %}
+ </div>
+ {% else %}
+ {{ form.teacher_id(class="form-control form-control-lg") }}
+ {% endif %}
+ </div>
+ <div class="form-group">
+ {{ form.weekday.label(class="form-control-label") }}
+ {% if form.weekday.errors %}
+ {{ form.weekday(class="form-control form-control-lg is-invalid") }}
+ <div class="invalid-feedback">
+ {% for error in form.weekday.errors %}
+ <span>{{ error }}</span>
+ {% endfor %}
+ </div>
+ {% else %}
+ {{ form.weekday(class="form-control form-control-lg") }}
+ {% endif %}
+ </div>
+ <div class="form-group">
+ {{ form.start.label(class="form-control-label") }}
+ {% if form.start.errors %}
+ {{ form.start(class="form-control form-control-lg is-invalid") }}
+ <div class="invalid-feedback">
+ {% for error in form.start.errors %}
+ <span>{{ error }}</span>
+ {% endfor %}
+ </div>
+ {% else %}
+ {{ form.start(class="form-control form-control-lg") }}
+ {% endif %}
+ </div>
+ <div class="form-group">
+ {{ form.end.label(class="form-control-label") }}
+ {% if form.end.errors %}
+ {{ form.end(class="form-control form-control-lg is-invalid") }}
+ <div class="invalid-feedback">
+ {% for error in form.end.errors %}
+ <span>{{ error }}</span>
+ {% endfor %}
+ </div>
+ {% else %}
+ {{ form.end(class="form-control form-control-lg") }}
+ {% endif %}
+ </div>
+ <div class="form-group">
+ {{ form.location.label(class="form-control-label") }}
+ {% if form.location.errors %}
+ {{ form.location(class="form-control form-control-lg is-invalid") }}
+ <div class="invalid-feedback">
+ {% for error in form.location.errors %}
+ <span>{{ error }}</span>
+ {% endfor %}
+ </div>
+ {% else %}
+ {{ form.location(class="form-control form-control-lg") }}
+ {% endif %}
+ </div>
+ </div>
</fieldset>
<div class="form-group">
{{ form.submit(class="btn btn-outline-info") }}