| 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 |  |  | from tracim.lib.utils.auth import MANAGE_CONTENT_PERM | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | from tracim.lib.utils.auth import MANAGE_WORKSPACE_PERM | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | from tracim.lib.utils.auth import MANAGE_GLOBAL_PERM | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | from tracim.lib.utils.auth import READ_PERM | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | from tracim.lib.utils.auth import CONTRIBUTE_PERM | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | from tracim.lib.utils.auth import ADMIN_PERM | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | from tracim.lib.utils.auth import USER_PERM | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | class DefaultController(Controller): | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     @classmethod | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     def notfound_view(cls, request): | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |         request.response.status = 404 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |         return {} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     @classmethod | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     def forbidden_view(cls, request): | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |         if request.authenticated_userid is None: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |             response = HTTPUnauthorized() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |             response.headers.update(forget(request)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |         # user is logged in but doesn't have permissions, reject wholesale | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |         else: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |             response = HTTPForbidden() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         return response | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     @classmethod | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     def test_config(cls, request): | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         try: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |             app_config = request.registry.settings['CFG'] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |             project = app_config.WEBSITE_TITLE | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |         except Exception as e: | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |             return Response(e, content_type='text/plain', status=500) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         return {'project': project} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |     @classmethod | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     def test_contributor_page(cls, request): | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         try: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |             app_config = request.registry.settings['CFG'] | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |             project = 'contributor' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         except Exception as e: | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |             return Response(e, content_type='text/plain', status=500) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         return {'project': project} | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |     @classmethod | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |     def test_admin_page(cls, request): | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |         try: | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |             app_config = request.registry.settings['CFG'] | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |             project = 'admin' | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |         except Exception as e: | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |             return Response(e, content_type='text/plain', status=500) | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |         return {'project': project} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |     @classmethod | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     def test_manager_page(cls, request): | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         try: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |             app_config = request.registry.settings['CFG'] | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |             project = 'manager' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         except Exception as e: | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |             return Response(e, content_type='text/plain', status=500) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         return {'project': project} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |     @classmethod | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |     def test_user_page(cls, request): | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         try: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |             app_config = request.registry.settings['CFG'] | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |             project = 'user' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |         except Exception as e: | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |             return Response(e, content_type='text/plain', status=500) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |         return {'project': project} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |     def bind(self, configurator: Configurator): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |         configurator.add_static_view('static', 'static', cache_max_age=3600) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |         configurator.add_view( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |             self.notfound_view, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |             renderer='tracim:templates/404.jinja2', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |             context=NotFound, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |         ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |         configurator.add_route('test_config', '/') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |         configurator.add_view( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |             self.test_config, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |             route_name='test_config', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |             renderer='tracim:templates/mytemplate.jinja2', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |         ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |         configurator.add_route('test_contributor', '/test_contributor') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |         configurator.add_view( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |             self.test_contributor_page, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |             route_name='test_contributor', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |             renderer='tracim:templates/mytemplate.jinja2', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |             permission=CONTRIBUTE_PERM, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |         ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |         configurator.add_route('test_admin', '/test_admin') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |         configurator.add_view( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |             self.test_admin_page, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |             route_name='test_admin', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |             renderer='tracim:templates/mytemplate.jinja2', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |             permission=ADMIN_PERM, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |         ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |         configurator.add_route('test_manager', '/test_manager') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |         configurator.add_view( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |             self.test_user_page, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |             route_name='test_manager', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |             renderer='tracim:templates/mytemplate.jinja2', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |             permission=MANAGE_GLOBAL_PERM, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |         ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |         configurator.add_route('test_user', '/test_user') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |         configurator.add_view( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |             self.test_user_page, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |             route_name='test_user', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |             renderer='tracim:templates/mytemplate.jinja2', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |             permission=USER_PERM, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |         ) | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 125 |  |  |         configurator.add_forbidden_view(self.forbidden_view) | 
            
                                                        
            
                                    
            
            
                | 126 |  |  |  | 
            
                        
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.