| 1 |  |  | # -*- coding: utf-8 -*- | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | from tracim.models import User | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | from tracim.models.data import UserRoleInWorkspace | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | CALENDAR_PERMISSION_READ = 'r' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | CALENDAR_PERMISSION_WRITE = 'w' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | class Calendar(object): | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |     def __init__(self, related_object, path): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |         self._related_object = related_object | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |         self._path = path | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |     @property | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |     def related_object(self): | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |         return self._related_object | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     def user_can_read(self, user: User) -> bool: | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |         raise NotImplementedError() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     def user_can_write(self, user: User) -> bool: | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |         raise NotImplementedError() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | class UserCalendar(Calendar): | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     def user_can_write(self, user: User) -> bool: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |         return self._related_object.user_id == user.user_id | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     def user_can_read(self, user: User) -> bool: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |         return self._related_object.user_id == user.user_id | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  | class WorkspaceCalendar(Calendar): | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     _workspace_rights = { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         UserRoleInWorkspace.NOT_APPLICABLE: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |             [], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         UserRoleInWorkspace.READER: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |             [CALENDAR_PERMISSION_READ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         UserRoleInWorkspace.CONTRIBUTOR: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |             [CALENDAR_PERMISSION_READ, CALENDAR_PERMISSION_WRITE], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |         UserRoleInWorkspace.CONTENT_MANAGER: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |             [CALENDAR_PERMISSION_READ, CALENDAR_PERMISSION_WRITE], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         UserRoleInWorkspace.WORKSPACE_MANAGER: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |             [CALENDAR_PERMISSION_READ, CALENDAR_PERMISSION_WRITE], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     def user_can_write(self, user: User) -> bool: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         role = user.get_role(self._related_object) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         return CALENDAR_PERMISSION_WRITE in self._workspace_rights[role] | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 52 |  |  |     def user_can_read(self, user: User) -> bool: | 
            
                                                        
            
                                    
            
            
                | 53 |  |  |         role = user.get_role(self._related_object) | 
            
                                                        
            
                                    
            
            
                | 54 |  |  |         return CALENDAR_PERMISSION_READ in self._workspace_rights[role] | 
            
                                                        
            
                                    
            
            
                | 55 |  |  |  | 
            
                        
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.