File: __init__.py

package info (click to toggle)
python-flask-seeder 1.2.0-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 292 kB
  • sloc: python: 1,062; makefile: 2
file content (53 lines) | stat: -rwxr-xr-x 1,261 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
""" Flask-Seeder """

from .seeder import Seeder
from .faker import Faker

# pylint: disable=too-few-public-methods
class SeedConfig:
    """ Seed config stored in Flask

    This is intended to be stored as an instance inside a Flask app
    for access to Flask-Seed configuration.
    """
    def __init__(self, db):
        self.db = db


# pylint: disable=too-few-public-methods
class FlaskSeeder:
    """ Flask extension Flask-Seeder

    Usage:
        seeder = FlaskSeeder()
        seeder.init_app(app, db)

    """
    def __init__(self, app=None, db=None):
        """ Initialize FlaskSeeder
        Arguments:
            app: Flask app
            db: SQLAlchemy database object
        """
        self.app = app
        self.db = db

        if app is not None and db is not None:
            self.init_app(app, db)

    def init_app(self, app, db=None):
        """ Initialize app after construction

        Initialize Flask-Seeder and register as an extension with Flask

        Arguments:
            app: Flask app
            db: SQLAlchemy database object

        """
        self.db = db or self.db

        if not hasattr(app, 'extensions'):
            app.extensions = {}

        app.extensions['flask_seeder'] = SeedConfig(self.db)