diff --git a/docker-compose.yml b/docker-compose.yml index ac99b2d20..4748cdb50 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -27,8 +27,7 @@ services: postgres: image: postgres:12.3 - # command: ["postgres", "-c", "log_statement=all"] - # uncomment this if u wanna output all SQL queries to the console + command: ["postgres", "-c", "log_statement=all"] volumes: - "./schema.sql:/docker-entrypoint-initdb.d/00-schema.sql" - "./seed-db.sql:/docker-entrypoint-initdb.d/10-seed-db.sql" diff --git a/files/classes/comment.py b/files/classes/comment.py index 913c1b231..bb51e86be 100644 --- a/files/classes/comment.py +++ b/files/classes/comment.py @@ -91,15 +91,12 @@ class Comment(Base): @property @lazy def options(self): - li = [x for x in self.child_comments if x.author_id == AUTOPOLLER_ID] - return sorted(li, key=lambda x: x.id) - + return self.child_comments.filter_by(author_id=AUTOPOLLER_ID).order_by(Comment.id).all() @property @lazy def choices(self): - li = [x for x in self.child_comments if x.author_id == AUTOCHOICE_ID] - return sorted(li, key=lambda x: x.id) + return self.child_comments.filter_by(author_id=AUTOCHOICE_ID).order_by(Comment.id).all() def total_poll_voted(self, v): @@ -221,15 +218,17 @@ class Comment(Base): def replies(self): if self.replies2 != None: return [x for x in self.replies2 if not x.author.shadowbanned] if not self.parent_submission: - return sorted((x for x in self.child_comments if x.author and not x.author.shadowbanned), key=lambda x: x.created_utc) - return sorted((x for x in self.child_comments if x.author and not x.author.shadowbanned and x.author_id not in (AUTOPOLLER_ID, AUTOBETTER_ID, AUTOCHOICE_ID)), key=lambda x: x.realupvotes, reverse=True) + return self.child_comments.join(User, User.id == Comment.author_id).filter(User.shadowbanned == None).order_by(Comment.id).all() + return self.child_comments.join(User, User.id == Comment.author_id).filter(User.shadowbanned == None, Comment.author_id.notin_((AUTOPOLLER_ID, AUTOBETTER_ID, AUTOCHOICE_ID))).order_by(Comment.realupvotes.desc()).all() + @property def replies3(self): if self.replies2 != None: return self.replies2 if not self.parent_submission: - return sorted(self.child_comments, key=lambda x: x.created_utc) - return sorted((x for x in self.child_comments if x.author_id not in (AUTOPOLLER_ID, AUTOBETTER_ID, AUTOCHOICE_ID)), key=lambda x: x.realupvotes, reverse=True) + return self.child_comments.order_by(Comment.id).all() + return self.child_comments.filter(Comment.author_id.notin_((AUTOPOLLER_ID, AUTOBETTER_ID, AUTOCHOICE_ID))).order_by(Comment.realupvotes.desc()).all() + @property def replies2(self): diff --git a/files/classes/flags.py b/files/classes/flags.py index 6df97b9f2..570108f51 100644 --- a/files/classes/flags.py +++ b/files/classes/flags.py @@ -21,7 +21,7 @@ class Flag(Base): super().__init__(*args, **kwargs) def __repr__(self): - return f"" + return f"" @property @lazy @@ -54,7 +54,7 @@ class CommentFlag(Base): super().__init__(*args, **kwargs) def __repr__(self): - return f"" + return f"" @property @lazy diff --git a/files/classes/sub.py b/files/classes/sub.py index 16ef72622..bd61cd59c 100644 --- a/files/classes/sub.py +++ b/files/classes/sub.py @@ -20,7 +20,7 @@ class Sub(Base): bannerurl = Column(String) css = Column(String) - blocks = relationship("SubBlock", lazy="dynamic", primaryjoin="SubBlock.sub==Sub.name", viewonly=True) + blocks = relationship("SubBlock", primaryjoin="SubBlock.sub==Sub.name", viewonly=True) def __repr__(self): @@ -46,4 +46,4 @@ class Sub(Base): @property @lazy def block_num(self): - return self.blocks.count() \ No newline at end of file + return len(self.blocks) \ No newline at end of file diff --git a/files/classes/user.py b/files/classes/user.py index f1ba947ea..a513c06a0 100644 --- a/files/classes/user.py +++ b/files/classes/user.py @@ -188,7 +188,7 @@ class User(Base): @property @lazy def is_cakeday(self): - if time.time() - self.created_utc > 365 * 86400: + if self.created_utc and time.time() - self.created_utc > 365 * 86400: if not self.has_badge(134): new_badge = Badge(badge_id=134, user_id=self.id) g.db.add(new_badge) @@ -602,7 +602,7 @@ class User(Base): @property @lazy def applications(self): - return g.db.query(OauthApp).filter_by(author_id=self.id).order_by(OauthApp.id) + return g.db.query(OauthApp).filter_by(author_id=self.id).order_by(OauthApp.id).all() @property @lazy diff --git a/files/helpers/lazy.py b/files/helpers/lazy.py index e91cf2b63..94739c095 100644 --- a/files/helpers/lazy.py +++ b/files/helpers/lazy.py @@ -1,5 +1,4 @@ -# Prevents certain properties from having to be recomputed each time they -# are referenced +# Prevents certain properties from having to be recomputed each time they are referenced def lazy(f): @@ -8,11 +7,15 @@ def lazy(f): o = args[0] - if "_lazy" not in o.__dict__: o.__dict__["_lazy"] = {} + if "_lazy" not in o.__dict__: + o.__dict__["_lazy"] = {} - if f.__name__ not in o.__dict__["_lazy"]: o.__dict__["_lazy"][f.__name__] = f(*args, **kwargs) + name = f.__name__ + str(args) + str(kwargs), + + if name not in o.__dict__["_lazy"]: + o.__dict__["_lazy"][name] = f(*args, **kwargs) - return o.__dict__["_lazy"][f.__name__] + return o.__dict__["_lazy"][name] wrapper.__name__ = f.__name__ return wrapper