svv/application.py
author Tero Marttila <terom@fixme.fi>
Wed, 22 Dec 2010 21:30:27 +0200
changeset 8 27e37082625e
parent 7 bbac4b0f4320
child 9 0327b83959e9
permissions -rw-r--r--
html: Implement selfclosing and whitespace_sensitive options (for <textarea>s)
6
72c73df76db2 Split wsgi.py into controllers/customers/urls for now; start orders form
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     1
"""
72c73df76db2 Split wsgi.py into controllers/customers/urls for now; start orders form
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     2
    Application runtime state
72c73df76db2 Split wsgi.py into controllers/customers/urls for now; start orders form
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     3
"""
72c73df76db2 Split wsgi.py into controllers/customers/urls for now; start orders form
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     4
72c73df76db2 Split wsgi.py into controllers/customers/urls for now; start orders form
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     5
from sqlalchemy import create_engine
72c73df76db2 Split wsgi.py into controllers/customers/urls for now; start orders form
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     6
72c73df76db2 Split wsgi.py into controllers/customers/urls for now; start orders form
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     7
import logging
72c73df76db2 Split wsgi.py into controllers/customers/urls for now; start orders form
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     8
72c73df76db2 Split wsgi.py into controllers/customers/urls for now; start orders form
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     9
from svv import database as db
72c73df76db2 Split wsgi.py into controllers/customers/urls for now; start orders form
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    10
72c73df76db2 Split wsgi.py into controllers/customers/urls for now; start orders form
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    11
log = logging.getLogger('svv.app')
72c73df76db2 Split wsgi.py into controllers/customers/urls for now; start orders form
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    12
72c73df76db2 Split wsgi.py into controllers/customers/urls for now; start orders form
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    13
class Application (object) :
72c73df76db2 Split wsgi.py into controllers/customers/urls for now; start orders form
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    14
    """
72c73df76db2 Split wsgi.py into controllers/customers/urls for now; start orders form
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    15
        Our core run-time state, which e.g. WSGIAoo acts as a frontend to
72c73df76db2 Split wsgi.py into controllers/customers/urls for now; start orders form
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    16
    """
72c73df76db2 Split wsgi.py into controllers/customers/urls for now; start orders form
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    17
72c73df76db2 Split wsgi.py into controllers/customers/urls for now; start orders form
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    18
    def __init__ (self, db_url) :
72c73df76db2 Split wsgi.py into controllers/customers/urls for now; start orders form
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    19
        """
72c73df76db2 Split wsgi.py into controllers/customers/urls for now; start orders form
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    20
            Initialize app, connecting to database.
72c73df76db2 Split wsgi.py into controllers/customers/urls for now; start orders form
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    21
72c73df76db2 Split wsgi.py into controllers/customers/urls for now; start orders form
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    22
                db_url      - sqlalchemy-style URL to database
72c73df76db2 Split wsgi.py into controllers/customers/urls for now; start orders form
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    23
        """
72c73df76db2 Split wsgi.py into controllers/customers/urls for now; start orders form
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    24
        
72c73df76db2 Split wsgi.py into controllers/customers/urls for now; start orders form
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    25
        if db_url :
72c73df76db2 Split wsgi.py into controllers/customers/urls for now; start orders form
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    26
            # connect
7
bbac4b0f4320 Further improve order form
Tero Marttila <terom@fixme.fi>
parents: 6
diff changeset
    27
            self._engine = engine = create_engine(db_url)
6
72c73df76db2 Split wsgi.py into controllers/customers/urls for now; start orders form
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    28
7
bbac4b0f4320 Further improve order form
Tero Marttila <terom@fixme.fi>
parents: 6
diff changeset
    29
            log.info("Connected to database %s", engine)
6
72c73df76db2 Split wsgi.py into controllers/customers/urls for now; start orders form
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    30
72c73df76db2 Split wsgi.py into controllers/customers/urls for now; start orders form
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    31
        else :
7
bbac4b0f4320 Further improve order form
Tero Marttila <terom@fixme.fi>
parents: 6
diff changeset
    32
            self._engine = None
bbac4b0f4320 Further improve order form
Tero Marttila <terom@fixme.fi>
parents: 6
diff changeset
    33
bbac4b0f4320 Further improve order form
Tero Marttila <terom@fixme.fi>
parents: 6
diff changeset
    34
    @property
bbac4b0f4320 Further improve order form
Tero Marttila <terom@fixme.fi>
parents: 6
diff changeset
    35
    def engine (self) :
bbac4b0f4320 Further improve order form
Tero Marttila <terom@fixme.fi>
parents: 6
diff changeset
    36
        """
bbac4b0f4320 Further improve order form
Tero Marttila <terom@fixme.fi>
parents: 6
diff changeset
    37
            Our sqlalchemy engine providing access to our database
bbac4b0f4320 Further improve order form
Tero Marttila <terom@fixme.fi>
parents: 6
diff changeset
    38
        """
bbac4b0f4320 Further improve order form
Tero Marttila <terom@fixme.fi>
parents: 6
diff changeset
    39
bbac4b0f4320 Further improve order form
Tero Marttila <terom@fixme.fi>
parents: 6
diff changeset
    40
        if not self._engine :
bbac4b0f4320 Further improve order form
Tero Marttila <terom@fixme.fi>
parents: 6
diff changeset
    41
            raise RuntimeError("No database configured")
bbac4b0f4320 Further improve order form
Tero Marttila <terom@fixme.fi>
parents: 6
diff changeset
    42
           
bbac4b0f4320 Further improve order form
Tero Marttila <terom@fixme.fi>
parents: 6
diff changeset
    43
        return self._engine
6
72c73df76db2 Split wsgi.py into controllers/customers/urls for now; start orders form
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    44
72c73df76db2 Split wsgi.py into controllers/customers/urls for now; start orders form
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    45
    def create_tables (self) :
72c73df76db2 Split wsgi.py into controllers/customers/urls for now; start orders form
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    46
        """
72c73df76db2 Split wsgi.py into controllers/customers/urls for now; start orders form
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    47
            Initialize the database if new.
72c73df76db2 Split wsgi.py into controllers/customers/urls for now; start orders form
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    48
        """
72c73df76db2 Split wsgi.py into controllers/customers/urls for now; start orders form
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    49
72c73df76db2 Split wsgi.py into controllers/customers/urls for now; start orders form
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    50
        log.warn("Creating database tables...")
72c73df76db2 Split wsgi.py into controllers/customers/urls for now; start orders form
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    51
72c73df76db2 Split wsgi.py into controllers/customers/urls for now; start orders form
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    52
        db.schema.create_all(self.engine)
72c73df76db2 Split wsgi.py into controllers/customers/urls for now; start orders form
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    53
72c73df76db2 Split wsgi.py into controllers/customers/urls for now; start orders form
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    54
    def get_connection (self) :
72c73df76db2 Split wsgi.py into controllers/customers/urls for now; start orders form
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    55
        """
72c73df76db2 Split wsgi.py into controllers/customers/urls for now; start orders form
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    56
            Return an active sqlalchemy Connection for our database.
72c73df76db2 Split wsgi.py into controllers/customers/urls for now; start orders form
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    57
        """
72c73df76db2 Split wsgi.py into controllers/customers/urls for now; start orders form
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    58
        
72c73df76db2 Split wsgi.py into controllers/customers/urls for now; start orders form
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    59
        return self.engine.connect()
72c73df76db2 Split wsgi.py into controllers/customers/urls for now; start orders form
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    60
7
bbac4b0f4320 Further improve order form
Tero Marttila <terom@fixme.fi>
parents: 6
diff changeset
    61
    def query (self, sql) :
bbac4b0f4320 Further improve order form
Tero Marttila <terom@fixme.fi>
parents: 6
diff changeset
    62
        """
bbac4b0f4320 Further improve order form
Tero Marttila <terom@fixme.fi>
parents: 6
diff changeset
    63
            Execute a simple SQL query, and return results
bbac4b0f4320 Further improve order form
Tero Marttila <terom@fixme.fi>
parents: 6
diff changeset
    64
        """
bbac4b0f4320 Further improve order form
Tero Marttila <terom@fixme.fi>
parents: 6
diff changeset
    65
bbac4b0f4320 Further improve order form
Tero Marttila <terom@fixme.fi>
parents: 6
diff changeset
    66
        return self.engine.execute(sql)
bbac4b0f4320 Further improve order form
Tero Marttila <terom@fixme.fi>
parents: 6
diff changeset
    67