-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathviews.py
More file actions
126 lines (95 loc) · 3.41 KB
/
views.py
File metadata and controls
126 lines (95 loc) · 3.41 KB
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
"""
=================
learnpython.views
=================
View functions for Learn Python web-site.
"""
import operator
import socket
from flask import flash, render_template, redirect, request, url_for
from flask.ext.babel import lazy_gettext as _
from ordereddict import OrderedDict
from learnpython import forms
from learnpython.app import app, pages
def contacts(name=None):
"""
Contacts form or subscribe page.
Add ability to send email with question about cources or subscribe to
lessons.
"""
messages = {
'contacts': {
'error': _('Cannot send message due to mail server problems. '
'Please, try again later!'),
'success': _('Your message has been sent! Thanks for your '
'feedback!'),
},
'subscribe': {
'error': _('Cannot apply subscription due to mail server '
'problems. Please, try again later!'),
'success': _('You successfully subscribed to Learn Python '
'courses! We will send you all necessary information '
'later!')
}
}
name = name or 'contacts'
form_klass = getattr(forms, name.title() + 'Form')
page_obj = pages.get(name)
if request.method == 'POST':
form = form_klass(request.form)
if form.validate():
try:
form.send()
except socket.error:
flash(messages[name]['error'], 'error')
else:
flash(messages[name]['success'])
return redirect(url_for('status', next=request.path))
else:
form = form_klass()
context = {'form': form, 'is_{0}'.format(name): True, 'page': page_obj}
return render_template('contacts.html', **context)
def error(err):
"""
Error page.
Prettify "Does Not Exist" and "Server Error" errors handling by displaying
fancy page.
"""
code = getattr(err, 'code', 500)
return render_template('error.html', error=err), code
def flows(archive=None):
"""
Flows page.
Show information of all available flows, which available in courses.
"""
prefix = 'flows/' if not archive else 'archive/{0}'.format(archive)
data = sorted(filter(lambda page: page.path.startswith(prefix), pages),
key=operator.itemgetter('order'))
data = map(lambda page: (page.path.replace(prefix, ''), page), data)
context = {'archive': archive, 'flows': OrderedDict(data)}
return render_template('flows.html', **context)
def page(name):
"""
Flat page.
Show content of flat page, which named ``name``. Content of flat page would
be rendered with ReStructuredText markup.
If page not found - show "Does Not Exist" error page.
"""
page_obj = pages.get_or_404(name)
context = {'is_{0}'.format(name): True, 'page': page_obj}
return render_template('page.html', **context)
def status():
"""
Status page.
Helper page, which displays after user sent message or has been subscribed.
"""
next_page = request.args.get('next', request.referrer or url_for('index'))
return render_template('status.html', next=next_page)
def subscribe():
"""
Subscribe page.
Disable ability of new subscribers if ``ALLOW_SUBSCRIBERS`` is False.
"""
if app.config['ALLOW_SUBSCRIBERS']:
return contacts('subscribe')
return page('nosubscribe')