Skip to content

Commit 1be8f00

Browse files
committed
Added cookie for remembering config id accessed via website
1 parent b1f5333 commit 1be8f00

File tree

3 files changed

+85
-1
lines changed

3 files changed

+85
-1
lines changed

apprise_api/api/context_processors.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ def default_config_id(request):
5353
"""
5454
Returns a unique config identifier
5555
"""
56-
return {'DEFAULT_CONFIG_ID': settings.APPRISE_DEFAULT_CONFIG_ID}
56+
return {'DEFAULT_CONFIG_ID': request.default_config_id}
5757

5858

5959
def unique_config_id(request):

apprise_api/core/middleware/config.py

+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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

apprise_api/core/settings/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
MIDDLEWARE = [
6767
'django.middleware.common.CommonMiddleware',
6868
'core.middleware.theme.AutoThemeMiddleware',
69+
'core.middleware.config.DetectConfigMiddleware',
6970
]
7071

7172
ROOT_URLCONF = 'core.urls'

0 commit comments

Comments
 (0)