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

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

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

Добавлен: 02.01.2026

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

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

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

37. SQLALCHEMY + URL DISPATCH WIKI TUTORIAL

43word = match.group(1)

44exists = DBSession.query(Page).filter_by(name=word).all()

45if exists:

46

view_url = request.route_url(’view_page’, pagename=word)

47return ’<a href="%s">%s</a>’ % (view_url, word)

48else:

49

add_url = request.route_url(’add_page’, pagename=word)

50

return ’<a href="%s">%s</a>’ % (add_url, word)

51

52content = publish_parts(page.data, writer_name=’html’)[’html_body’]

53content = wikiwords.sub(check, content)

54edit_url = request.route_url(’edit_page’, pagename=pagename)

55return dict(page=page, content=content, edit_url=edit_url,

56

logged_in=authenticated_userid(request))

57

58 @view_config(route_name=’add_page’, renderer=’templates/edit.pt’,

59

permission=’edit’)

60def add_page(request):

61name = request.matchdict[’pagename’]

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

63body = request.params[’body’]

64page = Page(name, body)

65DBSession.add(page)

66return HTTPFound(location = request.route_url(’view_page’,

67

pagename=name))

68save_url = request.route_url(’add_page’, pagename=name)

69page = Page(’’, ’’)

70return dict(page=page, save_url=save_url,

71

logged_in=authenticated_userid(request))

72

73 @view_config(route_name=’edit_page’, renderer=’templates/edit.pt’,

74

permission=’edit’)

75def edit_page(request):

76name = request.matchdict[’pagename’]

77page = DBSession.query(Page).filter_by(name=name).one()

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

79page.data = request.params[’body’]

80DBSession.add(page)

81return HTTPFound(location = request.route_url(’view_page’,

82

pagename=name))

83return dict(

84page=page,

85save_url = request.route_url(’edit_page’, pagename=name),

86logged_in=authenticated_userid(request),

87)

88

482


37.7. ADDING AUTHORIZATION

89@view_config(route_name=’login’, renderer=’templates/login.pt’)

90@forbidden_view_config(renderer=’templates/login.pt’)

91def login(request):

92login_url = request.route_url(’login’)

93referrer = request.url

94if referrer == login_url:

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

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

97message = ’’

98login = ’’

99password = ’’

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

101login = request.params[’login’]

102password = request.params[’password’]

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

104

headers = remember(request,

login)

105

return HTTPFound(location =

came_from,

106

headers = headers)

107

message = ’Failed login’

 

108

109return dict(

110message = message,

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

112came_from = came_from,

113login = login,

114password = password,

115)

116

117@view_config(route_name=’logout’)

118def logout(request):

119headers = forget(request)

120return HTTPFound(location = request.route_url(’view_wiki’),

121

headers = headers)

(Only the highlighted lines need to be added.)

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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

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

<head>

<title>${page.name} - Pyramid tutorial wiki (based on TurboGears 20-Minute Wiki)</title>

483


37. SQLALCHEMY + URL DISPATCH WIKI TUTORIAL

<meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/> <meta name="keywords" content="python web application" />

<meta name="description" content="pyramid web application" /> <link rel="shortcut icon"

href="${request.static_url(’tutorial:static/favicon.ico’)}" /> <link rel="stylesheet"

href="${request.static_url(’tutorial:static/pylons.css’)}" type="text/css" media="screen" charset="utf-8" />

<!--[if lte IE 6]> <link rel="stylesheet"

href="${request.static_url(’tutorial:static/ie6.css’)}" type="text/css" media="screen" charset="utf-8" />

<![endif]-->

</head>

<body>

<div id="wrap">

<div id="top-small">

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

<div>

<img width="220" height="50" alt="pyramid" src="${request.static_url(’tutorial:static/pyramid-small.png’)}" />

</div>

</div>

</div>

<div id="middle">

<div class="middle align-right">

<div id="left" class="app-welcome align-left"> Editing <b><span tal:replace="page.name">Page Name

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

You can return to the

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

</div>

<div id="right" class="app-welcome align-right"> <span tal:condition="logged_in">

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

</span>

</div>

</div>

</div>

<div id="bottom">

<div class="bottom">

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

<textarea name="body" tal:content="page.data" rows="10" cols="60"/><br/>

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

</form>

484


37.7. ADDING AUTHORIZATION

</div>

</div>

</div>

<div id="footer"> <div class="footer"

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

</div>

</body>

</html>

(Only the highlighted lines need to be added.)

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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

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

<head>

<title>${page.name} - Pyramid tutorial wiki (based on TurboGears 20-Minute Wiki)</title>

<meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/> <meta name="keywords" content="python web application" />

<meta name="description" content="pyramid web application" /> <link rel="shortcut icon"

href="${request.static_url(’tutorial:static/favicon.ico’)}" /> <link rel="stylesheet"

href="${request.static_url(’tutorial:static/pylons.css’)}" type="text/css" media="screen" charset="utf-8" />

<!--[if lte IE 6]> <link rel="stylesheet"

href="${request.static_url(’tutorial:static/ie6.css’)}" type="text/css" media="screen" charset="utf-8" />

<![endif]-->

</head>

<body>

<div id="wrap">

<div id="top-small">

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

<div>

<img width="220" height="50" alt="pyramid" src="${request.static_url(’tutorial:static/pyramid-small.png’)}" />

</div>

</div>

</div>

485