Skip to main content

Asset storage for Flask

Project description

# Lagring: asset storage for Flask


## Requirements

- SQLAlchemy
- Flask
- PostgreSQL 9.4+

## Installation

pip install lagring

If you want to use `ImageAsset` class you have to install Pillow as well.

## How to use it

0. Configure your Flask app to have necessary config options:

Parameter | Meaning
-------------------|--------------------------------------------------
ASSET_STORAGE_ROOT | Path to the directory where assets will be stored
ASSET_URL_ROOT | Asset URL invariable part

1. Create storage instance:

```python
from lagring import FlaskLagring

storage = FlaskLagring()
storage.init_app(app)
```

2. Derive your SQLAlchemy model from `storage.Entity` class:

```python
from lagring import Asset

class File(db.Model, storage.Entity):
id = db.Column(db.Integer, primary_key=True)
file = Asset()
```

Note that JSONB field `_assets` will be added to the model (PostgreSQL 9.4+).
You can change the name by overriding `lagring.Entity.asset_data_field` method.

3. Put something to that asset field:

```python
new_file = File()
db.session.add(new_file)
db.session.flush()
new_file.file = '/some/local/path/filename'
db.session.commit()
```

The model instance must have a valid id on asset assignment, so you have to call `flush()`
before that.

4. Then you can use the asset like this:

```python
# get asset URL
url = new_file.file.url
# get asset path
path = new_file.file.abs_path
# delete the asset
del new_file.file
```

5. Besides the basic `Asset` class, there are also `ImageAsset` and `DirectoryAsset` to store
something more specific and have some processing on upload (no docs for that yet, please see
the code).

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

Lagring-0.2.7.1.tar.gz (9.8 kB view hashes)

Uploaded Source

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page