From 56a1467d3c13cab6bc590633ca8022c32b3e1c89 Mon Sep 17 00:00:00 2001 From: tonyrewin Date: Sat, 19 Nov 2022 15:02:45 +0300 Subject: [PATCH] some orm fixes --- orm/collab.py | 4 +++- orm/shout.py | 20 +++++++++++--------- orm/viewed.py | 3 +-- services/zine/topics.py | 5 +++-- 4 files changed, 18 insertions(+), 14 deletions(-) diff --git a/orm/collab.py b/orm/collab.py index 4b205134..3e3a5839 100644 --- a/orm/collab.py +++ b/orm/collab.py @@ -1,8 +1,9 @@ from datetime import datetime from sqlalchemy import Boolean, Column, String, ForeignKey, DateTime - +from sqlalchemy.orm import relationship from base.orm import Base +from orm.user import User class CollabAuthor(Base): @@ -21,5 +22,6 @@ class Collab(Base): title = Column(String, nullable=True, comment="Title") body = Column(String, nullable=True, comment="Body") pic = Column(String, nullable=True, comment="Picture") + authors = relationship(lambda: User, secondary=CollabAuthor.__tablename__) createdAt = Column(DateTime, default=datetime.now, comment="Created At") createdBy = Column(ForeignKey("user.id"), comment="Created By") diff --git a/orm/shout.py b/orm/shout.py index 324b2ba1..f65b1268 100644 --- a/orm/shout.py +++ b/orm/shout.py @@ -67,12 +67,14 @@ class Shout(Base): @staticmethod def init_table(): with local_session() as session: - entry = { - "slug": "genesis-block", - "body": "", - "title": "Ничего", - "lang": "ru" - } - s = Shout.create(**entry) - session.add(s) - session.commit() + s = session.query(Shout).first() + if not s: + entry = { + "slug": "genesis-block", + "body": "", + "title": "Ничего", + "lang": "ru" + } + s = Shout.create(**entry) + session.add(s) + session.commit() diff --git a/orm/viewed.py b/orm/viewed.py index f37963f3..6d089234 100644 --- a/orm/viewed.py +++ b/orm/viewed.py @@ -7,7 +7,7 @@ class ViewedEntry(Base): __tablename__ = "viewed" viewer = Column(ForeignKey("user.slug"), default='anonymous') - shout = Column(ForeignKey("shout.slug")) + shout = Column(ForeignKey("shout.slug"), default="genesis-block") amount = Column(Integer, default=1) createdAt = Column( DateTime, nullable=False, default=datetime.now, comment="Created at" @@ -17,7 +17,6 @@ class ViewedEntry(Base): def init_table(): with local_session() as session: entry = { - "shout": "genesis-block", "amount": 0 } viewed = ViewedEntry.create(**entry) diff --git a/services/zine/topics.py b/services/zine/topics.py index 97773414..ee95dfd2 100644 --- a/services/zine/topics.py +++ b/services/zine/topics.py @@ -12,7 +12,8 @@ class TopicStorage: topics = session.query(Topic) self.topics = dict([(topic.slug, topic) for topic in topics]) for tpc in self.topics.values(): - self.load_parents(tpc) + # self.load_parents(tpc) + pass print("[zine.topics] %d precached" % len(self.topics.keys())) @@ -64,4 +65,4 @@ class TopicStorage: self = TopicStorage async with self.lock: self.topics[topic.slug] = topic - self.load_parents(topic) + # self.load_parents(topic)