|
1
|
|
|
# coding=utf-8 |
|
|
|
|
|
|
2
|
|
|
from tracim.views.controllers import Controller |
|
3
|
|
|
from pyramid.config import Configurator |
|
4
|
|
|
from pyramid.response import Response |
|
5
|
|
|
from pyramid.exceptions import NotFound |
|
6
|
|
|
from pyramid.httpexceptions import HTTPUnauthorized |
|
7
|
|
|
from pyramid.httpexceptions import HTTPForbidden |
|
8
|
|
|
from pyramid.security import forget |
|
9
|
|
|
|
|
10
|
|
|
|
|
11
|
|
|
class DefaultController(Controller): |
|
|
|
|
|
|
12
|
|
|
|
|
13
|
|
|
@classmethod |
|
14
|
|
|
def notfound_view(cls, request): |
|
|
|
|
|
|
15
|
|
|
request.response.status = 404 |
|
16
|
|
|
return {} |
|
17
|
|
|
|
|
18
|
|
|
@classmethod |
|
19
|
|
|
def forbidden_view(cls, request): |
|
|
|
|
|
|
20
|
|
|
if request.authenticated_userid is None: |
|
21
|
|
|
response = HTTPUnauthorized() |
|
22
|
|
|
response.headers.update(forget(request)) |
|
23
|
|
|
|
|
24
|
|
|
# user is logged in but doesn't have permissions, reject wholesale |
|
25
|
|
|
else: |
|
26
|
|
|
response = HTTPForbidden() |
|
27
|
|
|
return response |
|
28
|
|
|
|
|
29
|
|
|
@classmethod |
|
30
|
|
|
def test_config(cls, request): |
|
|
|
|
|
|
31
|
|
|
try: |
|
32
|
|
|
app_config = request.registry.settings['CFG'] |
|
33
|
|
|
project = app_config.WEBSITE_TITLE |
|
34
|
|
|
except Exception as e: |
|
|
|
|
|
|
35
|
|
|
return Response(e, content_type='text/plain', status=500) |
|
36
|
|
|
return {'project': project} |
|
37
|
|
|
|
|
38
|
|
|
@classmethod |
|
39
|
|
|
def test_admin_page(cls, request): |
|
|
|
|
|
|
40
|
|
|
try: |
|
41
|
|
|
app_config = request.registry.settings['CFG'] |
|
|
|
|
|
|
42
|
|
|
project = 'admin' |
|
43
|
|
|
except Exception as e: |
|
|
|
|
|
|
44
|
|
|
return Response(e, content_type='text/plain', status=500) |
|
45
|
|
|
return {'project': project} |
|
46
|
|
|
|
|
47
|
|
|
@classmethod |
|
48
|
|
|
def test_manager_page(cls, request): |
|
|
|
|
|
|
49
|
|
|
try: |
|
50
|
|
|
app_config = request.registry.settings['CFG'] |
|
|
|
|
|
|
51
|
|
|
project = 'manager' |
|
52
|
|
|
except Exception as e: |
|
|
|
|
|
|
53
|
|
|
return Response(e, content_type='text/plain', status=500) |
|
54
|
|
|
return {'project': project} |
|
55
|
|
|
|
|
56
|
|
|
@classmethod |
|
57
|
|
|
def test_user_page(cls, request): |
|
|
|
|
|
|
58
|
|
|
try: |
|
59
|
|
|
app_config = request.registry.settings['CFG'] |
|
|
|
|
|
|
60
|
|
|
project = 'user' |
|
61
|
|
|
except Exception as e: |
|
|
|
|
|
|
62
|
|
|
return Response(e, content_type='text/plain', status=500) |
|
63
|
|
|
return {'project': project} |
|
64
|
|
|
|
|
65
|
|
|
|
|
66
|
|
|
def bind(self, configurator: Configurator): |
|
67
|
|
|
configurator.add_static_view('static', 'static', cache_max_age=3600) |
|
68
|
|
|
configurator.add_view( |
|
69
|
|
|
self.notfound_view, |
|
70
|
|
|
renderer='tracim:templates/404.jinja2', |
|
71
|
|
|
context=NotFound, |
|
72
|
|
|
) |
|
73
|
|
|
|
|
74
|
|
|
configurator.add_route('test_config', '/') |
|
75
|
|
|
configurator.add_view( |
|
76
|
|
|
self.test_config, |
|
77
|
|
|
route_name='test_config', |
|
78
|
|
|
renderer='tracim:templates/mytemplate.jinja2', |
|
79
|
|
|
) |
|
80
|
|
|
|
|
81
|
|
|
configurator.add_route('test_admin', '/test_admin') |
|
82
|
|
|
configurator.add_view( |
|
83
|
|
|
self.test_admin_page, |
|
84
|
|
|
route_name='test_admin', |
|
85
|
|
|
renderer='tracim:templates/mytemplate.jinja2', |
|
86
|
|
|
permission='admin', |
|
87
|
|
|
) |
|
88
|
|
|
configurator.add_route('test_manager', '/test_manager') |
|
89
|
|
|
configurator.add_view( |
|
90
|
|
|
self.test_user_page, |
|
91
|
|
|
route_name='test_manager', |
|
92
|
|
|
renderer='tracim:templates/mytemplate.jinja2', |
|
93
|
|
|
permission='manager', |
|
94
|
|
|
) |
|
95
|
|
|
configurator.add_route('test_user', '/test_user') |
|
96
|
|
|
configurator.add_view( |
|
97
|
|
|
self.test_user_page, |
|
98
|
|
|
route_name='test_user', |
|
99
|
|
|
renderer='tracim:templates/mytemplate.jinja2', |
|
100
|
|
|
permission='user', |
|
101
|
|
|
) |
|
102
|
|
|
configurator.add_forbidden_view(self.forbidden_view) |
|
103
|
|
|
|
The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:
If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.