object-oriented and DI

This commit is contained in:
Yax 2021-01-02 12:36:42 +01:00
parent 30428b1af7
commit 6c855e7ead
7 changed files with 110 additions and 100 deletions

View file

@ -2,23 +2,37 @@
# -*- coding: UTF-8 -*-
import json
from playhouse.db_url import connect
from peewee import DatabaseProxy, Model
from playhouse.db_url import connect, SqliteDatabase
from playhouse.shortcuts import model_to_dict
from tinydb import TinyDB
from stacosys.conf import config
def get_db():
return connect(config.get(config.DB_URL))
db = SqliteDatabase(None)
def setup():
from stacosys.model.site import Site
from stacosys.model.comment import Comment
class BaseModel(Model):
class Meta:
database = db
get_db().create_tables([Site, Comment], safe=True)
if config.exists(config.DB_BACKUP_JSON_FILE):
_backup_db(config.DB_BACKUP_JSON_FILE, Comment)
class Database:
def get_db(self):
return db
def setup(self, db_url):
db.init(db_url)
db.connect()
from stacosys.model.site import Site
from stacosys.model.comment import Comment
db.create_tables([Site, Comment], safe=True)
# if config.exists(config.DB_BACKUP_JSON_FILE):
# _backup_db(config.DB_BACKUP_JSON_FILE, Comment)
def _tojson_model(comment):
@ -35,3 +49,4 @@ def _backup_db(db_file, Comment):
for comment in Comment.select():
cc = _tojson_model(comment)
table.insert(cc)

View file

@ -6,49 +6,54 @@ from datetime import datetime
import markdown
import PyRSS2Gen
from stacosys.conf import config
import stacosys.conf.config as config
from stacosys.core.templater import get_template
from stacosys.model.comment import Comment
from stacosys.model.site import Site
def generate_all():
class Rss:
def __init__(self, lang, rss_file, rss_proto):
self._lang = lang
self._rss_file = rss_file
self._rss_proto = rss_proto
for site in Site.select():
generate_site(site.token)
def generate_all(self):
for site in Site.select():
self._generate_site(site.token)
def generate_site(token):
def _generate_site(self, token):
site = Site.select().where(Site.token == token).get()
rss_title = get_template('rss_title_message').render(site=site.name)
md = markdown.Markdown()
site = Site.select().where(Site.token == token).get()
rss_title = get_template(self._lang, "rss_title_message").render(site=site.name)
md = markdown.Markdown()
items = []
for row in (
Comment.select()
.join(Site)
.where(Site.token == token, Comment.published)
.order_by(-Comment.published)
.limit(10)
):
item_link = '%s://%s%s' % (config.get(config.RSS_PROTO), site.url, row.url)
items.append(
PyRSS2Gen.RSSItem(
title='%s - %s://%s%s'
% (config.get(config.RSS_PROTO), row.author_name, site.url, row.url),
link=item_link,
description=md.convert(row.content),
guid=PyRSS2Gen.Guid('%s/%d' % (item_link, row.id)),
pubDate=row.published,
items = []
for row in (
Comment.select()
.join(Site)
.where(Site.token == token, Comment.published)
.order_by(-Comment.published)
.limit(10)
):
item_link = "%s://%s%s" % (self._rss_proto, site.url, row.url)
items.append(
PyRSS2Gen.RSSItem(
title="%s - %s://%s%s"
% (self._rss_proto, row.author_name, site.url, row.url),
link=item_link,
description=md.convert(row.content),
guid=PyRSS2Gen.Guid("%s/%d" % (item_link, row.id)),
pubDate=row.published,
)
)
)
rss = PyRSS2Gen.RSS2(
title=rss_title,
link='%s://%s' % (config.get(config.RSS_PROTO), site.url),
description='Commentaires du site "%s"' % site.name,
lastBuildDate=datetime.now(),
items=items,
)
rss.write_xml(open(config.get(config.RSS_FILE), 'w'), encoding='utf-8')
rss = PyRSS2Gen.RSS2(
title=rss_title,
link="%s://%s" % (self._rss_proto, site.url),
description='Commentaires du site "%s"' % site.name,
lastBuildDate=datetime.now(),
items=items,
)
rss.write_xml(open(self._rss_file, "w"), encoding="utf-8")

View file

@ -8,9 +8,9 @@ from jinja2 import Environment, FileSystemLoader
from stacosys.conf import config
current_path = os.path.dirname(__file__)
template_path = os.path.abspath(os.path.join(current_path, '../templates'))
template_path = os.path.abspath(os.path.join(current_path, "../templates"))
env = Environment(loader=FileSystemLoader(template_path))
def get_template(name):
return env.get_template(config.get(config.LANG) + '/' + name + '.tpl')
def get_template(lang, name):
return env.get_template(lang + "/" + name + ".tpl")

View file

@ -7,28 +7,24 @@ from peewee import TextField
from peewee import DateTimeField
from peewee import ForeignKeyField
from stacosys.model.site import Site
from stacosys.core.database import get_db
from datetime import datetime
from stacosys.core.database import BaseModel
class Comment(Model):
class Comment(BaseModel):
url = CharField()
created = DateTimeField()
notified = DateTimeField(null=True, default=None)
published = DateTimeField(null=True, default=None)
author_name = CharField()
author_site = CharField(default='')
author_gravatar = CharField(default='')
author_site = CharField(default="")
author_gravatar = CharField(default="")
content = TextField()
site = ForeignKeyField(Site, related_name='site')
class Meta:
database = get_db()
site = ForeignKeyField(Site, related_name="site")
def notify_site_admin(self):
self.notified = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
self.notified = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
self.save()
def publish(self):
self.published = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
self.published = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
self.save()

View file

@ -3,14 +3,11 @@
from peewee import Model
from peewee import CharField
from stacosys.core.database import get_db
from stacosys.core.database import BaseModel
class Site(Model):
class Site(BaseModel):
name = CharField(unique=True)
url = CharField()
token = CharField()
admin_email = CharField()
class Meta:
database = get_db()