diff --git a/config.ini b/config.ini index 894d434..8d7b7e2 100755 --- a/config.ini +++ b/config.ini @@ -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 diff --git a/stacosys/conf/config.py b/stacosys/conf/config.py index 712a77f..a4742d3 100644 --- a/stacosys/conf/config.py +++ b/stacosys/conf/config.py @@ -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() diff --git a/stacosys/core/database.py b/stacosys/core/database.py index a50165b..5be8287 100644 --- a/stacosys/core/database.py +++ b/stacosys/core/database.py @@ -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) diff --git a/stacosys/core/persistence.py b/stacosys/core/persistence.py new file mode 100644 index 0000000..32351dd --- /dev/null +++ b/stacosys/core/persistence.py @@ -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") diff --git a/tests/test_persistence.py b/tests/test_persistence.py new file mode 100644 index 0000000..d4dd92d --- /dev/null +++ b/tests/test_persistence.py @@ -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 diff --git a/tests/test_stacosys.py b/tests/test_stacosys.py index f0d2e64..124b4fa 100644 --- a/tests/test_stacosys.py +++ b/tests/test_stacosys.py @@ -2,4 +2,4 @@ from stacosys import __version__ def test_version(): - assert __version__ == '0.1.0' + assert __version__ == '1.1.0'