remove shout id
This commit is contained in:
49
orm/shout.py
49
orm/shout.py
@@ -15,29 +15,29 @@ class ShoutAuthor(Base):
|
||||
__tablename__ = "shout_author"
|
||||
|
||||
id = None
|
||||
shout = Column(ForeignKey('shout.id'), primary_key = True)
|
||||
shout = Column(ForeignKey('shout.slug'), primary_key = True)
|
||||
user = Column(ForeignKey('user.id'), primary_key = True)
|
||||
|
||||
class ShoutViewer(Base):
|
||||
__tablename__ = "shout_viewer"
|
||||
|
||||
id = None
|
||||
shout = Column(ForeignKey('shout.id'), primary_key = True)
|
||||
shout = Column(ForeignKey('shout.slug'), primary_key = True)
|
||||
user = Column(ForeignKey('user.id'), primary_key = True)
|
||||
|
||||
class ShoutTopic(Base):
|
||||
__tablename__ = 'shout_topic'
|
||||
|
||||
id = None
|
||||
shout = Column(ForeignKey('shout.id'), primary_key = True)
|
||||
shout = Column(ForeignKey('shout.slug'), primary_key = True)
|
||||
topic = Column(ForeignKey('topic.slug'), primary_key = True)
|
||||
|
||||
class ShoutRating(Base):
|
||||
__tablename__ = "shout_rating"
|
||||
|
||||
id = None
|
||||
rater_id = Column(ForeignKey('user.id'), primary_key = True)
|
||||
shout_id = Column(ForeignKey('shout.id'), primary_key = True)
|
||||
rater = Column(ForeignKey('user.id'), primary_key = True)
|
||||
shout = Column(ForeignKey('shout.slug'), primary_key = True)
|
||||
ts = Column(DateTime, nullable=False, default = datetime.now, comment="Timestamp")
|
||||
value = Column(Integer)
|
||||
|
||||
@@ -52,16 +52,16 @@ class ShoutRatingStorage:
|
||||
ShoutRatingStorage.ratings = session.query(ShoutRating).all()
|
||||
|
||||
@staticmethod
|
||||
async def get_rating(shout_id):
|
||||
async def get_rating(shout_slug):
|
||||
async with ShoutRatingStorage.lock:
|
||||
shout_ratings = list(filter(lambda x: x.shout_id == shout_id, ShoutRatingStorage.ratings))
|
||||
shout_ratings = list(filter(lambda x: x.shout == shout_slug, ShoutRatingStorage.ratings))
|
||||
return reduce((lambda x, y: x + y.value), shout_ratings, 0)
|
||||
|
||||
@staticmethod
|
||||
async def update_rating(new_rating):
|
||||
async with ShoutRatingStorage.lock:
|
||||
rating = next((x for x in ShoutRatingStorage.ratings \
|
||||
if x.rater_id == new_rating.rater_id and x.shout_id == new_rating.shout_id), None)
|
||||
if x.rater == new_rating.rater and x.shout == new_rating.shout), None)
|
||||
if rating:
|
||||
rating.value = new_rating.value
|
||||
rating.ts = new_rating.ts
|
||||
@@ -73,7 +73,7 @@ class ShoutViewByDay(Base):
|
||||
__tablename__ = "shout_view_by_day"
|
||||
|
||||
id = None
|
||||
shout_id = Column(ForeignKey('shout.id'), primary_key = True)
|
||||
shout = Column(ForeignKey('shout.slug'), primary_key = True)
|
||||
day = Column(DateTime, primary_key = True, default = datetime.now)
|
||||
value = Column(Integer)
|
||||
|
||||
@@ -91,28 +91,28 @@ class ShoutViewStorage:
|
||||
self = ShoutViewStorage
|
||||
self.views = session.query(ShoutViewByDay).all()
|
||||
for view in self.views:
|
||||
shout_id = view.shout_id
|
||||
if not shout_id in self.this_day_views:
|
||||
self.this_day_views[shout_id] = view
|
||||
this_day_view = self.this_day_views[shout_id]
|
||||
shout_slug = view.shout
|
||||
if not shout_slug in self.this_day_views:
|
||||
self.this_day_views[shout_slug] = view
|
||||
this_day_view = self.this_day_views[shout_slug]
|
||||
if this_day_view.day < view.day:
|
||||
self.this_day_views[shout_id] = view
|
||||
self.this_day_views[shout_slug] = view
|
||||
|
||||
@staticmethod
|
||||
async def get_view(shout_id):
|
||||
async def get_view(shout_slug):
|
||||
async with ShoutViewStorage.lock:
|
||||
shout_views = list(filter(lambda x: x.shout_id == shout_id, ShoutViewStorage.views))
|
||||
shout_views = list(filter(lambda x: x.shout == shout_slug, ShoutViewStorage.views))
|
||||
return reduce((lambda x, y: x + y.value), shout_views, 0)
|
||||
|
||||
@staticmethod
|
||||
async def inc_view(shout_id):
|
||||
async def inc_view(shout_slug):
|
||||
self = ShoutViewStorage
|
||||
async with ShoutViewStorage.lock:
|
||||
this_day_view = self.this_day_views.get(shout_id)
|
||||
this_day_view = self.this_day_views.get(shout_slug)
|
||||
day_start = datetime.now().replace(hour = 0, minute = 0, second = 0)
|
||||
if not this_day_view or this_day_view.day < day_start:
|
||||
this_day_view = ShoutViewByDay.create(shout_id = shout_id, value = 1)
|
||||
self.this_day_views[shout_id] = this_day_view
|
||||
this_day_view = ShoutViewByDay.create(shout = shout_slug, value = 1)
|
||||
self.this_day_views[shout_slug] = this_day_view
|
||||
self.views.append(this_day_view)
|
||||
else:
|
||||
this_day_view.value = this_day_view.value + 1
|
||||
@@ -144,15 +144,15 @@ class ShoutViewStorage:
|
||||
class Shout(Base):
|
||||
__tablename__ = 'shout'
|
||||
|
||||
# NOTE: automatic ID here
|
||||
id = None
|
||||
|
||||
slug: str = Column(String, nullable=False, unique=True)
|
||||
slug: str = Column(String, primary_key=True)
|
||||
community: int = Column(Integer, ForeignKey("community.id"), nullable=True, comment="Community")
|
||||
body: str = Column(String, nullable=False, comment="Body")
|
||||
createdAt: str = Column(DateTime, nullable=False, default = datetime.now, comment="Created at")
|
||||
updatedAt: str = Column(DateTime, nullable=True, comment="Updated at")
|
||||
replyTo: int = Column(ForeignKey("shout.id"), nullable=True)
|
||||
versionOf: int = Column(ForeignKey("shout.id"), nullable=True)
|
||||
replyTo: int = Column(ForeignKey("shout.slug"), nullable=True)
|
||||
versionOf: int = Column(ForeignKey("shout.slug"), nullable=True)
|
||||
tags: str = Column(String, nullable=True)
|
||||
publishedBy: bool = Column(ForeignKey("user.id"), nullable=True)
|
||||
publishedAt: str = Column(DateTime, nullable=True)
|
||||
@@ -164,4 +164,3 @@ class Shout(Base):
|
||||
authors = relationship(lambda: User, secondary=ShoutAuthor.__tablename__) # NOTE: multiple authors
|
||||
topics = relationship(lambda: Topic, secondary=ShoutTopic.__tablename__)
|
||||
visibleFor = relationship(lambda: User, secondary=ShoutViewer.__tablename__)
|
||||
old_id: str = Column(String, nullable = True)
|
||||
|
Reference in New Issue
Block a user