forked from dcolish/Cockerel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
classes.py
110 lines (91 loc) · 3.31 KB
/
classes.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
"""
classes.py
-----------------------------
Controller functions for classes. A class object allows you to add associated
lesson plans.
"""
from flask import (
g,
Module,
redirect,
render_template,
request,
url_for,
)
from flatland.out.markup import Generator
from util import login_required
from cockerel.models.schema import db, Classes
from .forms.classes import AddEditClassForm
classes = Module(__name__)
@classes.route('/classes', methods=['GET'])
def index():
"""Shows all classes currently in the system"""
classes = Classes.query.all()
return render_template("/classes/index.html", classes=classes)
@classes.route('/classes/add', methods=['GET', 'POST'])
@login_required
def add():
"""Users can add classes if they are authenticated"""
if request.method == 'POST':
form = AddEditClassForm.from_flat(request.form)
form.validate()
class_section = Classes(form['classname'].value,
form['description'].value,
owner=g.user)
db.session.add(class_section)
db.session.commit()
return redirect(url_for('classes.view',
class_id=class_section.id))
form = AddEditClassForm()
html = Generator()
return render_template('classes/add.html',
form=form,
html=html)
@classes.route('/classes/edit/<int:class_id>', methods=['GET', 'POST'])
@login_required
def edit():
"""Make modifications to a class"""
if request.method == 'POST':
form = AddEditClassForm.from_flat(request.form)
form.validate()
class_section = Classes(form['classname'].value,
form['description'].value,
owner=g.user)
db.session.add(class_section)
db.session.commit()
return redirect(url_for('classes.view',
class_id=class_section.id))
form = AddEditClassForm()
html = Generator()
return render_template('classes/add.html',
form=form,
html=html)
@classes.route('/classes/register/<int:class_id>', methods=['GET'])
@login_required
def register(class_id):
"""As a student user, register for access to a class"""
if class_id and request.method == 'GET':
section = Classes.query.filter_by(id=class_id).first()
section.users.append(g.user)
db.session.commit()
return redirect(url_for('classes.view',
class_id=section.id))
@classes.route('/classes/view/<int:class_id>', methods=['GET'])
def view(class_id):
"""View the lesson plans offered by a specific class. The user must be
either the admin or registered for that class"""
section = Classes.query.filter_by(id=class_id).first()
lessons = section.lessons
if g.user in section.users:
admin = False
registered = True
lessons = section.lessons
elif section.owner == g.user:
admin = registered = True
else:
admin = registered = False
return render_template('classes/view.html',
admin=admin,
class_section=section,
lessons=lessons,
registered=registered)