|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +# |
| 3 | +# Copyright (C) 2023 Chris Caron <[email protected]> |
| 4 | +# All rights reserved. |
| 5 | +# |
| 6 | +# This code is licensed under the MIT License. |
| 7 | +# |
| 8 | +# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 9 | +# of this software and associated documentation files(the "Software"), to deal |
| 10 | +# in the Software without restriction, including without limitation the rights |
| 11 | +# to use, copy, modify, merge, publish, distribute, sublicense, and / or sell |
| 12 | +# copies of the Software, and to permit persons to whom the Software is |
| 13 | +# furnished to do so, subject to the following conditions : |
| 14 | +# |
| 15 | +# The above copyright notice and this permission notice shall be included in |
| 16 | +# all copies or substantial portions of the Software. |
| 17 | +# |
| 18 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 | +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 | +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE |
| 21 | +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 22 | +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 23 | +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 24 | +# THE SOFTWARE. |
| 25 | +# |
| 26 | +import re |
| 27 | +from django.conf import settings |
| 28 | +from core.themes import SiteTheme, SITE_THEMES |
| 29 | +import datetime |
| 30 | + |
| 31 | + |
| 32 | +class DetectConfigMiddleware: |
| 33 | + """ |
| 34 | + Using the `key=` variable, allow one pre-configure the default |
| 35 | + configuration to use. |
| 36 | +
|
| 37 | + """ |
| 38 | + |
| 39 | + _is_cfg_path = re.compile(r'/cfg/(?P<key>[\w_-]{1,128})') |
| 40 | + |
| 41 | + def __init__(self, get_response): |
| 42 | + """ |
| 43 | + Prepare our initialization |
| 44 | + """ |
| 45 | + self.get_response = get_response |
| 46 | + |
| 47 | + def __call__(self, request): |
| 48 | + """ |
| 49 | + Define our middleware hook |
| 50 | + """ |
| 51 | + |
| 52 | + result = self._is_cfg_path.match(request.path) |
| 53 | + if not result: |
| 54 | + # Our current config |
| 55 | + config = \ |
| 56 | + request.COOKIES.get('key', settings.APPRISE_DEFAULT_CONFIG_ID) |
| 57 | + |
| 58 | + # Extract our key (fall back to our default if not set) |
| 59 | + config = request.GET.get("key", config).strip() |
| 60 | + |
| 61 | + else: |
| 62 | + config = result.group('key') |
| 63 | + |
| 64 | + if not config: |
| 65 | + # Fallback to default config |
| 66 | + config = settings.APPRISE_DEFAULT_CONFIG_ID |
| 67 | + |
| 68 | + # Set our theme to a cookie |
| 69 | + request.default_config_id = config |
| 70 | + |
| 71 | + # Get our response object |
| 72 | + response = self.get_response(request) |
| 73 | + |
| 74 | + # Set our cookie |
| 75 | + max_age = 365 * 24 * 60 * 60 # 1 year |
| 76 | + expires = datetime.datetime.utcnow() + \ |
| 77 | + datetime.timedelta(seconds=max_age) |
| 78 | + |
| 79 | + # Set our cookie |
| 80 | + response.set_cookie('key', config, expires=expires) |
| 81 | + |
| 82 | + # return our response |
| 83 | + return response |
0 commit comments