From dc36c9adc19d12e1be76c7217308a33f2b1d41e8 Mon Sep 17 00:00:00 2001 From: riteshjha Date: Sun, 19 May 2024 08:48:38 +0545 Subject: [PATCH 1/2] cookie settings from config --- src/masonite/cookies/Cookie.py | 41 ++++++++++++++++++++-------------- 1 file changed, 24 insertions(+), 17 deletions(-) diff --git a/src/masonite/cookies/Cookie.py b/src/masonite/cookies/Cookie.py index 4ba03b611..43ee3ee7b 100644 --- a/src/masonite/cookies/Cookie.py +++ b/src/masonite/cookies/Cookie.py @@ -1,23 +1,17 @@ +from masonite.configuration import config + class Cookie: - def __init__( - self, - name, - value, - expires=None, - http_only=True, - path="/", - timezone=None, - secure=False, - samesite="Strict", - ): + def __init__(self, name, value, **options): + self.options = options + self.name = name self.value = value - self.http_only = http_only - self.secure = secure - self.expires = expires - self.timezone = timezone - self.samesite = samesite - self.path = path + self.http_only = self.__get_option('http_only', True) + self.secure = self.__get_option('secure', False) + self.expires = self.__get_option('expires', None) + self.timezone = self.__get_option('timezone', None) + self.samesite = self.__get_option('samesite', 'Strict') + self.path = self.__get_option('path', '/') def render(self): response = f"{self.name}={self.value};" @@ -36,3 +30,16 @@ def render(self): response += f"SameSite={self.samesite};" return response + + def __get_option(self, key: str, default:any): + """ + Get cookie options from config/session.py + if option key found in options then it return that + if not found in options then it will fetch from config + if not found in config then use the default value + """ + if key in self.options: + return self.options[key] + else: + cookie = config('session.drivers.cookie') + return cookie[key] if key in cookie else default \ No newline at end of file From 324ed720f19d67daef2159eaeae6cbe865de2d88 Mon Sep 17 00:00:00 2001 From: riteshjha Date: Sun, 16 Jun 2024 08:52:14 +0545 Subject: [PATCH 2/2] fixed linting issue --- src/masonite/cookies/Cookie.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/masonite/cookies/Cookie.py b/src/masonite/cookies/Cookie.py index 43ee3ee7b..a2fb8d3a7 100644 --- a/src/masonite/cookies/Cookie.py +++ b/src/masonite/cookies/Cookie.py @@ -1,5 +1,6 @@ from masonite.configuration import config + class Cookie: def __init__(self, name, value, **options): self.options = options @@ -31,7 +32,7 @@ def render(self): return response - def __get_option(self, key: str, default:any): + def __get_option(self, key: str, default: any): """ Get cookie options from config/session.py if option key found in options then it return that @@ -42,4 +43,4 @@ def __get_option(self, key: str, default:any): return self.options[key] else: cookie = config('session.drivers.cookie') - return cookie[key] if key in cookie else default \ No newline at end of file + return cookie[key] if key in cookie else default