Skip to content

Commit

Permalink
Merge pull request #64 from marktennyson/development
Browse files Browse the repository at this point in the history
Development
  • Loading branch information
marktennyson authored Dec 2, 2021
2 parents 39a795b + e2c3e63 commit 312c4f5
Show file tree
Hide file tree
Showing 10 changed files with 147 additions and 14 deletions.
8 changes: 6 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@
- Added more test cases.
- Changed the License from **GPL** to **MIT**.

## 0.1.4 [Upcoming]
## 0.1.4
- Added CODE_OF_CONDUCT.md file.
- Added CONTRIBUTING.md file.
- Added CONTRIBUTING.md file.
- 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.
2 changes: 1 addition & 1 deletion docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<img src="https://raw.githubusercontent.com/marktennyson/flask-express/main/logos/flask-express-logo.png">

[![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

Expand Down
41 changes: 40 additions & 1 deletion docs/request.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
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)
```
13 changes: 13 additions & 0 deletions docs/response.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
6 changes: 3 additions & 3 deletions examples/demo2/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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")
Expand Down
4 changes: 2 additions & 2 deletions flask_express/_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
"""
Expand Down
53 changes: 52 additions & 1 deletion flask_express/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
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)
18 changes: 16 additions & 2 deletions flask_express/response.py
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand All @@ -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.4 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,
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
14 changes: 13 additions & 1 deletion tests/test_app_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}'
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"}'

0 comments on commit 312c4f5

Please sign in to comment.