-
Notifications
You must be signed in to change notification settings - Fork 881
/
views.py
119 lines (99 loc) · 3.79 KB
/
views.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
111
112
113
114
115
116
117
118
119
from docutils.core import publish_parts
import re
from pyramid.httpexceptions import HTTPFound
from pyramid.view import (
view_config,
forbidden_view_config,
)
from pyramid.security import (
authenticated_userid,
remember,
forget,
)
from .security import USERS
from .models import Page
# regular expression used to find WikiWords
wikiwords = re.compile(r"\b([A-Z]\w+[A-Z]+\w+)")
@view_config(context='.models.Wiki', permission='view')
def view_wiki(context, request):
return HTTPFound(location=request.resource_url(context, 'FrontPage'))
@view_config(context='.models.Page',
renderer='templates/view.pt', permission='view')
def view_page(context, request):
wiki = context.__parent__
def check(match):
word = match.group(1)
if word in wiki:
page = wiki[word]
view_url = request.resource_url(page)
return '<a href="%s">%s</a>' % (view_url, word)
else:
add_url = request.application_url + '/add_page/' + word
return '<a href="%s">%s</a>' % (add_url, word)
content = publish_parts(context.data, writer_name='html')['html_body']
content = wikiwords.sub(check, content)
edit_url = request.resource_url(context, 'edit_page')
logged_in = authenticated_userid(request)
return dict(page = context, content = content, edit_url = edit_url,
logged_in = logged_in)
@view_config(name='add_page', context='.models.Wiki',
renderer='templates/edit.pt',
permission='edit')
def add_page(context, request):
name = request.subpath[0]
if 'form.submitted' in request.params:
body = request.params['body']
page = Page(body)
page.__name__ = name
page.__parent__ = context
context[name] = page
return HTTPFound(location = request.resource_url(page))
save_url = request.resource_url(context, 'add_page', name)
page = Page('')
page.__name__ = name
page.__parent__ = context
logged_in = authenticated_userid(request)
return dict(page = page, save_url = save_url, logged_in = logged_in)
@view_config(name='edit_page', context='.models.Page',
renderer='templates/edit.pt',
permission='edit')
def edit_page(context, request):
if 'form.submitted' in request.params:
context.data = request.params['body']
return HTTPFound(location = request.resource_url(context))
logged_in = authenticated_userid(request)
return dict(page = context,
save_url = request.resource_url(context, 'edit_page'),
logged_in = logged_in)
@view_config(context='.models.Wiki', name='login',
renderer='templates/login.pt')
@forbidden_view_config(renderer='templates/login.pt')
def login(request):
login_url = request.resource_url(request.context, 'login')
referrer = request.url
if referrer == login_url:
referrer = '/' # never use the login form itself as came_from
came_from = request.params.get('came_from', referrer)
message = ''
login = ''
password = ''
if 'form.submitted' in request.params:
login = request.params['login']
password = request.params['password']
if USERS.get(login) == password:
headers = remember(request, login)
return HTTPFound(location = came_from,
headers = headers)
message = 'Failed login'
return dict(
message = message,
url = request.application_url + '/login',
came_from = came_from,
login = login,
password = password,
)
@view_config(context='.models.Wiki', name='logout')
def logout(request):
headers = forget(request)
return HTTPFound(location = request.resource_url(request.context),
headers = headers)