draft tinydb persistence
This commit is contained in:
parent
b258194d6f
commit
aa122f3138
6 changed files with 64 additions and 7 deletions
|
@ -4,6 +4,13 @@
|
|||
lang = fr
|
||||
db_url = sqlite:///db.sqlite
|
||||
newcomment_polling = 60
|
||||
db_file = db.json
|
||||
|
||||
[site]
|
||||
name = "My blog"
|
||||
url = http://blog.mydomain.com
|
||||
token = aabbccddeeffgghhiijjkkllmm
|
||||
admin_email = admin@mydomain.com
|
||||
|
||||
[http]
|
||||
host = 127.0.0.1
|
||||
|
|
|
@ -7,6 +7,7 @@ import profig
|
|||
FLASK_APP = 'flask.app'
|
||||
|
||||
DB_URL = 'main.db_url'
|
||||
DB_FILE = 'main.db_file'
|
||||
LANG = 'main.lang'
|
||||
COMMENT_POLLING = 'main.newcomment_polling'
|
||||
|
||||
|
@ -29,6 +30,10 @@ SMTP_PORT = 'smtp.port'
|
|||
SMTP_LOGIN = 'smtp.login'
|
||||
SMTP_PASSWORD = 'smtp.password'
|
||||
|
||||
SITE_NAME = 'site.name'
|
||||
SITE_URL = 'site.url'
|
||||
SITE_TOKEN = 'site.token'
|
||||
SITE_ADMIN_EMAIL = 'site.admin_email'
|
||||
|
||||
# variable
|
||||
params = dict()
|
||||
|
|
|
@ -16,23 +16,26 @@ def setup():
|
|||
|
||||
get_db().create_tables([Site, Comment], safe=True)
|
||||
|
||||
|
||||
from playhouse.shortcuts import model_to_dict
|
||||
import json
|
||||
|
||||
|
||||
def tojson_model(comment):
|
||||
dcomment = model_to_dict(comment)
|
||||
del dcomment['site']
|
||||
del dcomment["site"]
|
||||
tcomment = json.dumps(dcomment, indent=4, sort_keys=True, default=str)
|
||||
return json.loads(tcomment)
|
||||
return json.loads(tcomment)
|
||||
|
||||
def tojson_models(models):
|
||||
print(json.dumps(list(models.dicts()), indent=4, sort_keys=True, default=str))
|
||||
|
||||
def dump_db():
|
||||
from tinydb import TinyDB, Query
|
||||
from stacosys.model.comment import Comment
|
||||
db = TinyDB('db.json')
|
||||
|
||||
db = TinyDB("db.json", sort_keys=True, indent=4, separators=(",", ": "))
|
||||
db.drop_tables()
|
||||
table = db.table("comments")
|
||||
for comment in Comment.select():
|
||||
cc = tojson_model(comment)
|
||||
print(cc)
|
||||
db.insert(cc)
|
||||
table.insert(cc)
|
||||
|
|
22
stacosys/core/persistence.py
Normal file
22
stacosys/core/persistence.py
Normal file
|
@ -0,0 +1,22 @@
|
|||
#!/usr/bin/python
|
||||
# -*- coding: UTF-8 -*-
|
||||
|
||||
from tinydb import TinyDB
|
||||
from tinydb.storages import MemoryStorage
|
||||
|
||||
from stacosys.conf import config
|
||||
|
||||
|
||||
class Persistence:
|
||||
def __init__(self):
|
||||
db_file = config.get(config.DB_FILE)
|
||||
if db_file:
|
||||
self.db = TinyDB(db_file, sort_keys=True, indent=4, separators=(",", ": "))
|
||||
else:
|
||||
self.db = TinyDB(storage=MemoryStorage)
|
||||
|
||||
def get_db(self):
|
||||
return self.db
|
||||
|
||||
def get_table_comments(self):
|
||||
return self.db.table("comments")
|
20
tests/test_persistence.py
Normal file
20
tests/test_persistence.py
Normal file
|
@ -0,0 +1,20 @@
|
|||
#!/usr/bin/python
|
||||
# -*- coding: UTF-8 -*-
|
||||
|
||||
import pytest
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def persistence():
|
||||
from stacosys.conf import config
|
||||
|
||||
config.params = {"main.db_file": None}
|
||||
from stacosys.core import persistence
|
||||
|
||||
return persistence.Persistence()
|
||||
|
||||
|
||||
def test_init_persistence(persistence):
|
||||
assert persistence is not None
|
||||
assert persistence.get_db() is not None
|
||||
assert persistence.get_table_comments() is not None
|
|
@ -2,4 +2,4 @@ from stacosys import __version__
|
|||
|
||||
|
||||
def test_version():
|
||||
assert __version__ == '0.1.0'
|
||||
assert __version__ == '1.1.0'
|
||||
|
|
Loading…
Add table
Reference in a new issue