ВУЗ: Не указан

Категория: Не указан

Дисциплина: Не указана

Добавлен: 02.01.2026

Просмотров: 2738

Скачиваний: 0

ВНИМАНИЕ! Если данный файл нарушает Ваши авторские права, то обязательно сообщите нам.

36.7. ADDING AUTHORIZATION

89def login(request):

90login_url = request.resource_url(request.context, ’login’)

91referrer = request.url

92if referrer == login_url:

93referrer = ’/’ # never use the login form itself as came_from

94came_from = request.params.get(’came_from’, referrer)

95message = ’’

96login = ’’

97password = ’’

98if ’form.submitted’ in request.params:

99login = request.params[’login’]

100password = request.params[’password’]

101if USERS.get(login) == password:

102

headers = remember(request,

login)

103

return HTTPFound(location =

came_from,

104

headers = headers)

105

message = ’Failed login’

 

106

107return dict(

108message = message,

109url = request.application_url + ’/login’,

110came_from = came_from,

111login = login,

112password = password,

113)

114

115@view_config(context=’.models.Wiki’, name=’logout’)

116def logout(request):

117headers = forget(request)

118return HTTPFound(location = request.resource_url(request.context),

119

headers = headers)

Our edit.pt template will look something like this when we’re done:

1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"

2"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

3

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"

4

xmlns:tal="http://xml.zope.org/namespaces/tal">

5

<head>

6

<title>${page.__name__} - Pyramid tutorial wiki (based on

7TurboGears 20-Minute Wiki)</title>

8 <meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/>

9<meta name="keywords" content="python web application" />

10<meta name="description" content="pyramid web application" />

11<link rel="shortcut icon"

12href="/static/favicon.ico" />

427


36. ZODB + TRAVERSAL WIKI TUTORIAL

13<link rel="stylesheet"

14href="/static/pylons.css"

15type="text/css" media="screen" charset="utf-8" />

16<!--[if lte IE 6]>

17<link rel="stylesheet"

18href="/static/ie6.css"

19type="text/css" media="screen" charset="utf-8" />

20<![endif]-->

21</head>

22<body>

23<div id="wrap">

24<div id="top-small">

25<div class="top-small align-center">

26<div>

27<img width="220" height="50" alt="pyramid"

28src="/static/pyramid-small.png" />

29</div>

30</div>

31</div>

32<div id="middle">

33<div class="middle align-right">

34<div id="left" class="app-welcome align-left">

35Editing <b><span tal:replace="page.__name__">Page Name

36Goes Here</span></b><br/>

37You can return to the

38<a href="${request.application_url}">FrontPage</a>.<br/>

39</div>

40<div id="right" class="app-welcome align-right">

41<span tal:condition="logged_in">

42

<a href="${request.application_url}/logout">Logout</a>

43</span>

44</div>

45</div>

46</div>

47<div id="bottom">

48<div class="bottom">

49<form action="${save_url}" method="post">

50<textarea name="body" tal:content="page.data" rows="10"

51

cols="60"/><br/>

52<input type="submit" name="form.submitted" value="Save"/>

53</form>

54</div>

55</div>

56</div>

57<div id="footer">

58<div class="footer"

428


36.7. ADDING AUTHORIZATION

59>© Copyright 2008-2011, Agendaless Consulting.</div>

60</div>

61</body>

62</html>

Our view.pt template will look something like this when we’re done:

1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"

2"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

3

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"

4

xmlns:tal="http://xml.zope.org/namespaces/tal">

5

<head>

6

<title>${page.__name__} - Pyramid tutorial wiki (based on

7TurboGears 20-Minute Wiki)</title>

8 <meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/>

9<meta name="keywords" content="python web application" />

10<meta name="description" content="pyramid web application" />

11<link rel="shortcut icon"

12href="/static/favicon.ico" />

13<link rel="stylesheet"

14href="/static/pylons.css"

15type="text/css" media="screen" charset="utf-8" />

16<!--[if lte IE 6]>

17<link rel="stylesheet"

18href="/static/ie6.css"

19type="text/css" media="screen" charset="utf-8" />

20<![endif]-->

21</head>

22<body>

23<div id="wrap">

24<div id="top-small">

25<div class="top-small align-center">

26<div>

27<img width="220" height="50" alt="pyramid"

28src="/static/pyramid-small.png" />

29</div>

30</div>

31</div>

32<div id="middle">

33<div class="middle align-right">

34<div id="left" class="app-welcome align-left">

35Viewing <b><span tal:replace="page.__name__">Page Name

36Goes Here</span></b><br/>

37You can return to the

38<a href="${request.application_url}">FrontPage</a>.<br/>

39</div>

429



36. ZODB + TRAVERSAL WIKI TUTORIAL

40<div id="right" class="app-welcome align-right">

41<span tal:condition="logged_in">

42<a href="${request.application_url}/logout">Logout</a>

43</span>

44</div>

45</div>

46</div>

47<div id="bottom">

48<div class="bottom">

49<div tal:replace="structure content">

50Page text goes here.

51</div>

52<p>

53<a tal:attributes="href edit_url" href="">

54Edit this page

55</a>

56</p>

57</div>

58</div>

59</div>

60<div id="footer">

61<div class="footer"

62>© Copyright 2008-2011, Agendaless Consulting.</div>

63</div>

64</body>

65</html>

36.7.10 View the Application in a Browser

We can finally examine our application in a browser. The views we’ll try are as follows:

Visiting http://localhost:6543/ in a browser invokes the view_wiki view. This always redirects to the view_page view of the FrontPage page resource. It is executable by any user.

Visiting http://localhost:6543/FrontPage/ in a browser invokes the view_page view of the FrontPage Page resource. This is because it’s the default view (a view without a name) for Page resources. It is executable by any user.

Visiting http://localhost:6543/FrontPage/edit_page in a browser invokes the edit view for the FrontPage Page resource. It is executable by only the editor user. If a different user (or the anonymous user) invokes it, a login form will be displayed. Supplying the credentials with the username editor, password editor will show the edit page form being displayed.

430