|
19 | 19 | import tornado.websocket |
20 | 20 |
|
21 | 21 |
|
| 22 | +_COMMON_APP_ATTRIBUTES = ( |
| 23 | + "state", |
| 24 | + "subs", |
| 25 | + "sources", |
| 26 | + "port", |
| 27 | + "env_path", |
| 28 | + "storage", |
| 29 | + "login_enabled", |
| 30 | +) |
| 31 | + |
| 32 | +_WEB_APP_ATTRIBUTES = _COMMON_APP_ATTRIBUTES + ( |
| 33 | + "max_text_lines", |
| 34 | + "max_old_content", |
| 35 | + "max_image_history", |
| 36 | +) |
| 37 | + |
| 38 | +_SOCKET_APP_ATTRIBUTES = _COMMON_APP_ATTRIBUTES + ("readonly",) |
| 39 | + |
| 40 | + |
| 41 | +def _copy_app_attributes(handler, app, attrs, store_app=False): |
| 42 | + if app is None: |
| 43 | + return |
| 44 | + |
| 45 | + if store_app: |
| 46 | + handler.app = app |
| 47 | + |
| 48 | + for attr in attrs: |
| 49 | + setattr(handler, attr, getattr(app, attr)) |
| 50 | + |
| 51 | + |
22 | 52 | class BaseWebSocketHandler(tornado.websocket.WebSocketHandler): |
23 | 53 | """ |
24 | 54 | Implements any required overriden functionality from the basic tornado |
25 | 55 | websocket handler. Also contains some shared logic for all WebSocketHandler |
26 | 56 | classes. |
27 | 57 | """ |
28 | 58 |
|
| 59 | + def initialize(self, app=None): |
| 60 | + """Common initialization shared by WebSocket handlers.""" |
| 61 | + _copy_app_attributes(self, app, _SOCKET_APP_ATTRIBUTES, store_app=True) |
| 62 | + |
29 | 63 | def get_current_user(self): |
30 | 64 | """ |
31 | 65 | This method determines the self.current_user |
@@ -54,17 +88,7 @@ def initialize(self, app=None): |
54 | 88 | The ``app`` parameter defaults to ``None`` so that handlers |
55 | 89 | registered without an ``app`` dict (e.g. HealthHandler) still work. |
56 | 90 | """ |
57 | | - if app is not None: |
58 | | - self.state = app.state |
59 | | - self.subs = app.subs |
60 | | - self.sources = app.sources |
61 | | - self.port = app.port |
62 | | - self.env_path = app.env_path |
63 | | - self.storage = app.storage |
64 | | - self.login_enabled = app.login_enabled |
65 | | - self.max_text_lines = app.max_text_lines |
66 | | - self.max_old_content = app.max_old_content |
67 | | - self.max_image_history = app.max_image_history |
| 91 | + _copy_app_attributes(self, app, _WEB_APP_ATTRIBUTES) |
68 | 92 |
|
69 | 93 | def is_authorized(self): |
70 | 94 | """Update access time and validate authentication for protected methods.""" |
|
0 commit comments