Fixed sanic_peewee error and added a simple interface to query and create
This commit is contained in:
parent
6ecf2a6eb2
commit
6ea43d8e6d
|
@ -10,8 +10,9 @@ from sanic.response import json
|
||||||
|
|
||||||
## peewee_async related imports
|
## peewee_async related imports
|
||||||
import peewee
|
import peewee
|
||||||
from peewee_async import Manager, PostgresqlDatabase
|
from peewee import Model, BaseModel
|
||||||
|
from peewee_async import Manager, PostgresqlDatabase, execute
|
||||||
|
from functools import partial
|
||||||
# we instantiate a custom loop so we can pass it to our db manager
|
# we instantiate a custom loop so we can pass it to our db manager
|
||||||
|
|
||||||
## from peewee_async docs:
|
## from peewee_async docs:
|
||||||
|
@ -19,42 +20,77 @@ from peewee_async import Manager, PostgresqlDatabase
|
||||||
# with manager! It’s all automatic. But you can run Manager.connect() or
|
# with manager! It’s all automatic. But you can run Manager.connect() or
|
||||||
# Manager.close() when you need it.
|
# Manager.close() when you need it.
|
||||||
|
|
||||||
|
class AsyncManager(Manager):
|
||||||
|
"""Inherit the peewee_async manager with our own object
|
||||||
|
configuration
|
||||||
|
|
||||||
# let's create a simple key value store:
|
database.allow_sync = False
|
||||||
class KeyValue(peewee.Model):
|
"""
|
||||||
key = peewee.CharField(max_length=40, unique=True)
|
|
||||||
text = peewee.TextField(default='')
|
def __init__(self, _model_class, *args, **kwargs):
|
||||||
|
super(AsyncManager, self).__init__(*args, **kwargs)
|
||||||
|
self._model_class = _model_class
|
||||||
|
self.database.allow_sync = False
|
||||||
|
|
||||||
|
def _do_fill(self, method, *args, **kwargs):
|
||||||
|
_class_method = getattr(super(AsyncManager, self), method)
|
||||||
|
pf = partial(_class_method, self._model_class)
|
||||||
|
return pf(*args, **kwargs)
|
||||||
|
|
||||||
|
def new(self, *args, **kwargs):
|
||||||
|
return self._do_fill('create', *args, **kwargs)
|
||||||
|
|
||||||
|
def get(self, *args, **kwargs):
|
||||||
|
return self._do_fill('get', *args, **kwargs)
|
||||||
|
|
||||||
|
def execute(self, query):
|
||||||
|
return execute(query)
|
||||||
|
|
||||||
|
|
||||||
|
def _get_meta_db_class(db):
|
||||||
|
"""creating a declartive class model for db"""
|
||||||
|
class _BlockedMeta(BaseModel):
|
||||||
|
def __new__(cls, name, bases, attrs):
|
||||||
|
_instance = super(_BlockedMeta, cls).__new__(cls, name, bases, attrs)
|
||||||
|
_instance.objects = AsyncManager(_instance, db)
|
||||||
|
return _instance
|
||||||
|
|
||||||
|
class _Base(Model, metaclass=_BlockedMeta):
|
||||||
|
|
||||||
|
def to_dict(self):
|
||||||
|
return self._data
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
database = database
|
database=db
|
||||||
|
return _Base
|
||||||
# create table synchronously
|
|
||||||
KeyValue.create_table(True)
|
|
||||||
|
|
||||||
# OPTIONAL: close synchronous connection
|
|
||||||
database.close()
|
|
||||||
|
|
||||||
# OPTIONAL: disable any future syncronous calls
|
|
||||||
objects.database.allow_sync = False # this will raise AssertionError on ANY sync call
|
|
||||||
|
|
||||||
|
|
||||||
app = Sanic('peewee_example')
|
def declarative_base(*args, **kwargs):
|
||||||
|
"""Returns a new Modeled Class after inheriting meta and Model classes"""
|
||||||
|
db = PostgresqlDatabase(*args, **kwargs)
|
||||||
|
return _get_meta_db_class(db)
|
||||||
|
|
||||||
@app.listener('before_server_start')
|
|
||||||
def setup(app, loop):
|
AsyncBaseModel = declarative_base(database='test',
|
||||||
database = PostgresqlDatabase(database='test',
|
|
||||||
host='127.0.0.1',
|
host='127.0.0.1',
|
||||||
user='postgres',
|
user='postgres',
|
||||||
password='mysecretpassword')
|
password='mysecretpassword')
|
||||||
|
|
||||||
objects = Manager(database, loop=loop)
|
# let's create a simple key value store:
|
||||||
|
class KeyValue(AsyncBaseModel):
|
||||||
|
key = peewee.CharField(max_length=40, unique=True)
|
||||||
|
text = peewee.TextField(default='')
|
||||||
|
|
||||||
|
|
||||||
|
app = Sanic('peewee_example')
|
||||||
|
|
||||||
|
|
||||||
@app.route('/post/<key>/<value>')
|
@app.route('/post/<key>/<value>')
|
||||||
async def post(request, key, value):
|
async def post(request, key, value):
|
||||||
"""
|
"""
|
||||||
Save get parameters to database
|
Save get parameters to database
|
||||||
"""
|
"""
|
||||||
obj = await objects.create(KeyValue, key=key, text=value)
|
obj = await KeyValue.objects.create(key=key, text=value)
|
||||||
return json({'object_id': obj.id})
|
return json({'object_id': obj.id})
|
||||||
|
|
||||||
|
|
||||||
|
@ -63,7 +99,7 @@ async def get(request):
|
||||||
"""
|
"""
|
||||||
Load all objects from database
|
Load all objects from database
|
||||||
"""
|
"""
|
||||||
all_objects = await objects.execute(KeyValue.select())
|
all_objects = await KeyValue.objects.execute(KeyValue.select())
|
||||||
serialized_obj = []
|
serialized_obj = []
|
||||||
for obj in all_objects:
|
for obj in all_objects:
|
||||||
serialized_obj.append({
|
serialized_obj.append({
|
||||||
|
|
Loading…
Reference in New Issue
Block a user