1
|
|
|
# coding=utf-8 |
|
|
|
|
2
|
|
|
from pyramid.request import Request |
3
|
|
|
|
4
|
|
|
from tracim.models.data import UserRoleInWorkspace |
5
|
|
|
from tracim.views.controllers import Controller |
6
|
|
|
from pyramid.config import Configurator |
|
|
|
|
7
|
|
|
from pyramid.response import Response |
8
|
|
|
from pyramid.exceptions import NotFound |
9
|
|
|
from pyramid.httpexceptions import HTTPUnauthorized |
10
|
|
|
from pyramid.httpexceptions import HTTPForbidden |
11
|
|
|
from pyramid.security import forget, authenticated_userid |
|
|
|
|
12
|
|
|
|
13
|
|
|
from tracim.lib.utils.auth import require_workspace_role |
|
|
|
|
14
|
|
|
|
15
|
|
|
|
16
|
|
|
class DefaultController(Controller): |
|
|
|
|
17
|
|
|
|
18
|
|
|
def notfound_view(self, request): |
|
|
|
|
19
|
|
|
request.response.status = 404 |
20
|
|
|
return {} |
21
|
|
|
|
22
|
|
|
def forbidden_view(self, request): |
|
|
|
|
23
|
|
|
if request.authenticated_userid is None: |
24
|
|
|
response = HTTPUnauthorized() |
25
|
|
|
response.headers.update(forget(request)) |
26
|
|
|
|
27
|
|
|
# user is logged in but doesn't have permissions, reject wholesale |
28
|
|
|
else: |
29
|
|
|
response = HTTPForbidden() |
30
|
|
|
return response |
31
|
|
|
|
32
|
|
|
# TODO - G.M - 10-04-2018 - [cleanup][tempExample] - Drop this method |
|
|
|
|
33
|
|
|
@require_workspace_role(UserRoleInWorkspace.READER) |
34
|
|
|
def test_config(self, request: Request): |
|
|
|
|
35
|
|
|
try: |
36
|
|
|
app_config = request.registry.settings['CFG'] |
37
|
|
|
project = app_config.WEBSITE_TITLE |
38
|
|
|
except Exception as e: |
|
|
|
|
39
|
|
|
return Response(e, content_type='text/plain', status=500) |
40
|
|
|
return {'project': project} |
41
|
|
|
|
42
|
|
|
# TODO - G.M - 10-04-2018 - [cleanup][tempExample] - Drop this method |
|
|
|
|
43
|
|
|
@require_workspace_role(UserRoleInWorkspace.CONTRIBUTOR) |
44
|
|
|
def test_contributor_page(self, request): |
|
|
|
|
45
|
|
|
try: |
46
|
|
|
app_config = request.registry.settings['CFG'] |
|
|
|
|
47
|
|
|
project = 'contributor' |
48
|
|
|
except Exception as e: |
|
|
|
|
49
|
|
|
return Response(e, content_type='text/plain', status=500) |
50
|
|
|
return {'project': project} |
51
|
|
|
|
52
|
|
|
# TODO - G.M - 10-04-2018 - [cleanup][tempExample] - Drop this method |
|
|
|
|
53
|
|
|
def test_admin_page(self, request): |
|
|
|
|
54
|
|
|
try: |
55
|
|
|
app_config = request.registry.settings['CFG'] |
|
|
|
|
56
|
|
|
project = 'admin' |
57
|
|
|
except Exception as e: |
|
|
|
|
58
|
|
|
return Response(e, content_type='text/plain', status=500) |
59
|
|
|
return {'project': project} |
60
|
|
|
|
61
|
|
|
# TODO - G.M - 10-04-2018 - [cleanup][tempExample] - Drop this method |
|
|
|
|
62
|
|
|
def test_manager_page(self, request): |
|
|
|
|
63
|
|
|
try: |
64
|
|
|
app_config = request.registry.settings['CFG'] |
|
|
|
|
65
|
|
|
project = 'manager' |
66
|
|
|
except Exception as e: |
|
|
|
|
67
|
|
|
return Response(e, content_type='text/plain', status=500) |
68
|
|
|
return {'project': project} |
69
|
|
|
|
70
|
|
|
# TODO - G.M - 10-04-2018 - [cleanup][tempExample] - Drop this method |
|
|
|
|
71
|
|
|
def test_user_page(self, request): |
|
|
|
|
72
|
|
|
try: |
73
|
|
|
app_config = request.registry.settings['CFG'] |
|
|
|
|
74
|
|
|
project = 'user' |
75
|
|
|
except Exception as e: |
|
|
|
|
76
|
|
|
return Response(e, content_type='text/plain', status=500) |
77
|
|
|
return {'project': project} |
78
|
|
|
|
79
|
|
|
def bind(self, configurator: Configurator): |
80
|
|
|
# TODO - G.M - 10-04-2018 - [cleanup][tempExample] - Drop static files |
|
|
|
|
81
|
|
|
configurator.add_static_view('static', 'static', cache_max_age=3600) |
82
|
|
|
# TODO - G.M - 10-04-2018 - [cleanup][tempExample] - Do not rely |
|
|
|
|
83
|
|
|
# on static file for 404 view |
84
|
|
|
configurator.add_view( |
85
|
|
|
self.notfound_view, |
86
|
|
|
renderer='tracim:templates/404.jinja2', |
87
|
|
|
context=NotFound, |
88
|
|
|
) |
89
|
|
|
|
90
|
|
|
# TODO - G.M - 10-04-2018 - [cleanup][tempExample] - Drop this method |
|
|
|
|
91
|
|
|
configurator.add_route('test_config', '/') |
92
|
|
|
configurator.add_view( |
93
|
|
|
self.test_config, |
94
|
|
|
route_name='test_config', |
95
|
|
|
renderer='tracim:templates/mytemplate.jinja2', |
96
|
|
|
) |
97
|
|
|
|
98
|
|
|
# TODO - G.M - 10-04-2018 - [cleanup][tempExample] - Drop this method |
|
|
|
|
99
|
|
|
configurator.add_route('test_contributor', '/test_contributor') |
100
|
|
|
configurator.add_view( |
101
|
|
|
self.test_contributor_page, |
102
|
|
|
route_name='test_contributor', |
103
|
|
|
renderer='tracim:templates/mytemplate.jinja2', |
104
|
|
|
) |
105
|
|
|
|
106
|
|
|
# TODO - G.M - 10-04-2018 - [cleanup][tempExample] - Drop this method |
|
|
|
|
107
|
|
|
configurator.add_route('test_admin', '/test_admin') |
108
|
|
|
configurator.add_view( |
109
|
|
|
self.test_admin_page, |
110
|
|
|
route_name='test_admin', |
111
|
|
|
renderer='tracim:templates/mytemplate.jinja2', |
112
|
|
|
) |
113
|
|
|
|
114
|
|
|
# TODO - G.M - 10-04-2018 - [cleanup][tempExample] - Drop this method |
|
|
|
|
115
|
|
|
configurator.add_route('test_manager', '/test_manager') |
116
|
|
|
configurator.add_view( |
117
|
|
|
self.test_user_page, |
118
|
|
|
route_name='test_manager', |
119
|
|
|
renderer='tracim:templates/mytemplate.jinja2', |
120
|
|
|
) |
121
|
|
|
|
122
|
|
|
# TODO - G.M - 10-04-2018 - [cleanup][tempExample] - Drop this method |
|
|
|
|
123
|
|
|
configurator.add_route('test_user', '/test_user') |
124
|
|
|
configurator.add_view( |
125
|
|
|
self.test_user_page, |
126
|
|
|
route_name='test_user', |
127
|
|
|
renderer='tracim:templates/mytemplate.jinja2', |
128
|
|
|
) |
129
|
|
|
|
130
|
|
|
configurator.add_forbidden_view(self.forbidden_view) |
131
|
|
|
|
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.