Skip to content

yhttp/yhttp-pony

Repository files navigation

yhttp-pony

PyPI Build Coverage Status

Pony ORM extension for yhttp.

Install

sudo apt install python3-dev libpq-dev postgresql  # Postgresql
pip install yhttp-pony

Usage

This is how to use the extension.

from yhttp import Appliation, json
from yhttp.ext import pony as ponyext 
from pony.orm import db_session as dbsession, PrimaryKey, Required


app = Application()
app.settings.merge('''
db:
  url: postgres://postgres:postgres@localhost/foo
''')
db = ponyext.install(app)


class Foo(db.Entity):
    id = PrimaryKey(int, auto=True)
    title = Required(str)


@app.route()
@json
@dbsession
def get(req):
    return {f.id:f.title for f in Foo.select()}


app.ready()

Command line interface

There is some command line interfaces which will be automatically added to your application when you call ponyext.install(app).

myapp db create
myapp db drop

Extending db sub-command

import easycli
from yhttp.ext.pony import initialize, deinitialize

from mypackage import app  # yhttp application


class InsertMockup(easycli.SubCommand):
    __command__ = 'insert-mockup-data'

    def __call__(self, args):
        initialize(app.db, app.settings.db)

        ...

        deinitialize(app.db)

...

db = install(app, cliarguments=[InsertMockup])

Use it as:

myapp db insert-mockup-data

Running tests

echo "ALTER USER postgres PASSWORD 'postgres'" | sudo -u postgres psql
make test
make cover