draft tinydb persistence
This commit is contained in:
parent
b258194d6f
commit
aa122f3138
6 changed files with 64 additions and 7 deletions
|
|
@ -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")
|
||||
Loading…
Add table
Add a link
Reference in a new issue