From 0cd6fb33a466fa6ceacc1379f0478a5e74e91e11 Mon Sep 17 00:00:00 2001 From: Aniket Sarkar Date: Tue, 2 Nov 2021 14:14:52 +0530 Subject: [PATCH 1/3] fixed documentation --- docs/index.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/index.md b/docs/index.md index fdfbc16..7e707f8 100644 --- a/docs/index.md +++ b/docs/index.md @@ -2,7 +2,7 @@ -[![MIT licensed](https://img.shields.io/github/license/marktennyson/Flask-Tortoise)](https://raw.githubusercontent.com/marktennyson/Flask-Tortoise/master/LICENSE) [![GitHub stars](https://img.shields.io/github/stars/marktennyson/Flask-Tortoise.svg)](https://github.com/marktennyson/Flask-Tortoise/stargazers) [![GitHub forks](https://img.shields.io/github/forks/marktennyson/Flask-Tortoise.svg)](https://github.com/marktennyson/Flask-Tortoise/network) [![GitHub issues](https://img.shields.io/github/issues-raw/marktennyson/Flask-Tortoise)](https://github.com/marktennyson/Flask-Tortoise/issues) [![Downloads](https://pepy.tech/badge/Flask-Tortoise)](https://pepy.tech/project/Flask-Tortoise) +[![MIT licensed](https://img.shields.io/github/license/marktennyson/Flask-Express)](https://raw.githubusercontent.com/marktennyson/Flask-Express/master/LICENSE) [![GitHub stars](https://img.shields.io/github/stars/marktennyson/Flask-Express.svg)](https://github.com/marktennyson/Flask-Express/stargazers) [![GitHub forks](https://img.shields.io/github/forks/marktennyson/Flask-Express.svg)](https://github.com/marktennyson/Flask-Express/network) [![GitHub issues](https://img.shields.io/github/issues-raw/marktennyson/Flask-Express)](https://github.com/marktennyson/Flask-Express/issues) [![Downloads](https://pepy.tech/badge/Flask-Express)](https://pepy.tech/project/Flask-Express) ## Introduction From 75e29eac8c280680cd6d3399c3904e99d51d0923 Mon Sep 17 00:00:00 2001 From: Aniket Sarkar Date: Thu, 2 Dec 2021 11:57:35 +0530 Subject: [PATCH 2/3] added the feature to set the downloadable name for attachment file. --- CHANGELOG.md | 5 ++++- flask_express/_helper.py | 4 ++-- flask_express/response.py | 18 ++++++++++++++++-- 3 files changed, 22 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 92c6280..875d665 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -23,4 +23,7 @@ - Added more documentation. - docstring typo fixed at `request.Request` class. - Added more test cases. -- Changed the License from **GPL** to **MIT**. \ No newline at end of file +- Changed the License from **GPL** to **MIT**. + +## 0.1.4 (Upcoming) +- Added `download_name` features to the `Response.attachment` method to set the downloadable name of the attachment file. \ No newline at end of file diff --git a/flask_express/_helper.py b/flask_express/_helper.py index f2600e1..ac1ca75 100644 --- a/flask_express/_helper.py +++ b/flask_express/_helper.py @@ -17,11 +17,11 @@ if t.TYPE_CHECKING: from _typeshed.wsgi import WSGIEnvironment -def async_to_sync(func): +def async_to_sync(func) -> t.Any: return asgiref_async_to_sync(func) -def get_main_ctx_view(func:t.Callable): +def get_main_ctx_view(func:t.Callable) -> t.Any: """ adding the default request, response object with view function """ diff --git a/flask_express/response.py b/flask_express/response.py index 4e0b1c8..c6d2135 100644 --- a/flask_express/response.py +++ b/flask_express/response.py @@ -333,7 +333,7 @@ def mimer(req, res): return self.set('Content-Type', _mimetype) - def attachment(self, file_name:str): + def attachment(self, file_name:str, **kwargs:t.Any): """ send the attachments by using this method. The default attachment folder name is `attachments`. @@ -349,11 +349,25 @@ def attachment(self, file_name:str): def attach(req, res): filename = req.query.filename return res.attachment(file_name) + + ----- version-0.1.3 changes ------ + Now you can set the downloadable name of the attachment. + :for example:: + + from datetime import datetime + + @app.route('/attachments') + def attach(req, res): + filename = req.query.filename + now = datetime.now() + dl_filename = f'{filename.rsplit(".", 1)[0]}_{now.strftime("%Y%m%d-%I%M%S")}.{filename.rsplit(".", 1)[1]}' + return res.attachment(file_name, download_name=dl_filename) """ + kwargs.setdefault('as_attachment', True) return Utils.send_from_directory( current_app.config['ATTACHMENTS_FOLDER'], file_name, - as_attachment=True + **kwargs ), self.status_code def send_file(self, From 88d3d152b10b0c169f9796a9950a230afdac698f Mon Sep 17 00:00:00 2001 From: Aniket Sarkar Date: Thu, 2 Dec 2021 12:46:25 +0530 Subject: [PATCH 3/3] final commit before 1.0.4 --- CHANGELOG.md | 7 ++++-- docs/request.md | 41 +++++++++++++++++++++++++++++- docs/response.md | 13 ++++++++++ examples/demo2/app.py | 6 ++--- flask_express/request.py | 53 ++++++++++++++++++++++++++++++++++++++- flask_express/response.py | 2 +- setup.py | 2 +- tests/test_app_request.py | 14 ++++++++++- 8 files changed, 128 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 875d665..d772c87 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -25,5 +25,8 @@ - Added more test cases. - Changed the License from **GPL** to **MIT**. -## 0.1.4 (Upcoming) -- Added `download_name` features to the `Response.attachment` method to set the downloadable name of the attachment file. \ No newline at end of file +## 0.1.4 +- Added `download_name` features to the `Response.attachment` method to set the downloadable name of the attachment file. +- Added `set_session` and `get_session`, two methods to set and get data from the session object. +- Added `set_sessions` method to add multiple records at a single time to the session object. +- Version update of some dependencies. \ No newline at end of file diff --git a/docs/request.md b/docs/request.md index 434cd32..d08cbdd 100644 --- a/docs/request.md +++ b/docs/request.md @@ -27,4 +27,43 @@ it provides you the args based data. #### **property** `session`: `Type[flask.session.SessionMixin]` -it provides you the default session object of flask globals as a property of `request.Request` class. \ No newline at end of file +it provides you the default session object of flask globals as a property of `request.Request` class. + +**Added in version 1.0.4** + +#### **set_session(key:Any, value:Any) -> Type[flask.session.SessionMixin]** +Set the session object by providing the kay value name. + +**Parameters** `key` – the key name. +**Parameters** `value` – the value for the provided key. + +```python +@app.route('/set-session') +def ss(req, res): + req.set_session('name', 'aniket') + return res.send("OK) +``` + +#### **set_sessions(key_value:Tuple[Any, Any]) -> Type[flask.session.SessionMixin]** +set multiple sessions at a same time by sending the key, value pair in a tuple. + +**Parameters:** `key_value` - Tuple of the key-value pair + +```python +@app.get("/set-sessions") +def sss(req, res): + req.set_sessions(('name_1', 'aniket'), ('name_2', 'sarkar')) + return res.send('OK') +``` + +#### **get_session(key:Any) -> Any** +Get the session value as per the provided key name. + +**Parameters** `key` – the key name to fetch teh mapped value. + +```python +@app.route('/get-session') +def gs(req, res): + req.get_session('name') + return res.send("OK) +``` \ No newline at end of file diff --git a/docs/response.md b/docs/response.md index 31141a8..ef8baa0 100644 --- a/docs/response.md +++ b/docs/response.md @@ -25,6 +25,19 @@ def attach(req, res): return res.attachment(file_name) ``` +**from version 1.0.4 `flask-express` started supporting to set the downloadable name for the attachments.** + +```python +from datetime import datetime + +@app.route('/attachments') +def attach(req, res): + filename = req.query.filename + now = datetime.now() + dl_filename = f'{filename.rsplit(".", 1)[0]}_{now.strftime("%Y%m%d-%I%M%S")}.{filename.rsplit(".", 1)[1]}' + return res.attachment(file_name, download_name=dl_filename) +``` + #### **clear_cookie(key: str, path: str = '/', domain: Optional[str] = None, secure: bool = False, httponly: bool = False, samesite: Optional[str] = None)→ Type[flask_express.response.Response]** Clear a cookie. Fails silently if key doesn’t exist. diff --git a/examples/demo2/app.py b/examples/demo2/app.py index 849a411..bcd93a4 100644 --- a/examples/demo2/app.py +++ b/examples/demo2/app.py @@ -30,7 +30,7 @@ def mrp(req:Request, res:Response): # return res.json(id=1) # print (app.config) # print (app.config['ATTACHMENTS_FOLDER']) - return res.attachment("hello.txt") + return res.attachment("hello.txt", download_name="aniket.txt") @app.route("/check-session") def check_session(req:Request, res:Response): @@ -60,12 +60,12 @@ def redirector(req:Request, res:Response): @app.get("/set-session") def set_session(req:Request, res:Response): - req.session['username'] = 'aniketsarkar' + req.set_session('username', 'marktennyson') return res.send('OK') @app.get("/get-session") def get_session(req:Request, res:Response): - username = req.session.get('username') + username = req.get_session('username') return res.send(dict(username=username)) @app.get("/check-flash") diff --git a/flask_express/request.py b/flask_express/request.py index 78f98fe..5e539cb 100644 --- a/flask_express/request.py +++ b/flask_express/request.py @@ -49,4 +49,55 @@ def session(self) -> t.Type["SessionMixin"]: it provides you the default session object of flask globals as a property of `request.Request` class. """ - return session \ No newline at end of file + return session + + def set_session(self, key:t.Any, value:t.Any) -> "SessionMixin": + """ + set the session object by providing the kay value name. + + added in version 1.0.4 + + for example:: + + @app.route('/set-session') + def ss(req, res): + req.set_session('name', 'aniket') + return res.send("OK) + """ + session[key] = value + + return session + + def set_sessions(self, key_value:t.Tuple[t.Any, t.Any]) -> "SessionMixin": + """ + set multiple sessions at a same time + by sending the key, value pair in a tuple. + + added in version 1.0.4 + + for example:: + + @app.get("/set-sessions") + def sss(req, res): + req.set_sessions(('name_1', 'aniket'), ('name_2', 'sarkar')) + return res.send('OK') + """ + for item in key_value: + self.set_session(item) + + return session + + def get_session(self, key:t.Any) -> t.Optional[t.Any]: + """ + get the session value as per the provided key name. + + added in version 1.0.4 + + for example:: + + @app.route('/get-session') + def gs(req, res): + req.get_session('name') + return res.send("OK) + """ + return session.get(key, None) \ No newline at end of file diff --git a/flask_express/response.py b/flask_express/response.py index c6d2135..2dc1354 100644 --- a/flask_express/response.py +++ b/flask_express/response.py @@ -350,7 +350,7 @@ def attach(req, res): filename = req.query.filename return res.attachment(file_name) - ----- version-0.1.3 changes ------ + ----- version-0.1.4 changes ------ Now you can set the downloadable name of the attachment. :for example:: diff --git a/setup.py b/setup.py index 3fe5932..5a37327 100644 --- a/setup.py +++ b/setup.py @@ -1,7 +1,7 @@ from setuptools import setup, find_packages -VERSION_INFO = (0, 1, 3) +VERSION_INFO = (0, 1, 4) AUTHOR = "Aniket Sarkar" with open("README.md", "r") as f: diff --git a/tests/test_app_request.py b/tests/test_app_request.py index 4e189d1..703324b 100644 --- a/tests/test_app_request.py +++ b/tests/test_app_request.py @@ -28,4 +28,16 @@ def test_queryargs_request(req:"Request", res:"Response"): rv_test_json = client.get('/test-query-request?name=Aniket Sarkar&plannet=Pluto') assert rv_test_json.status_code == 200 - assert rv_test_json.data == b'{"status": 1}' \ No newline at end of file + assert rv_test_json.data == b'{"status": 1}' + +def test_set_get_session_func(app:"FlaskExpress", client:"FlaskClient"): + @app.get("/test-set-get-session-request") + def set_get_session_request(req:"Request", res:"Response"): + req.set_session("username", "expo_9071") + assert req.session.get("username") == "expo_9071" + assert req.get_session("username") == "expo_9071" + return res.json(username=req.get_session("username")) + + rv_test_json = client.get('/test-set-get-session-request') + assert rv_test_json.status_code == 200 + assert rv_test_json.data == b'{"username": "expo_9071"}' \ No newline at end of file