From 44d8aab4f16668a8ae69885843282e710c4cb0dc Mon Sep 17 00:00:00 2001 From: Deimos Date: Mon, 7 Oct 2019 17:38:57 -0600 Subject: [PATCH] Models: set objects instead of directly using ids This was always inconsistent - I've been setting objects in some models, and ids in other ones. Using ids can cause various errors, especially in cases where one of the objects involved hasn't been committed to the database yet and so hasn't been assigned its id. --- tildes/tildes/models/comment/comment.py | 8 ++------ tildes/tildes/models/comment/comment_label.py | 4 ++-- .../tildes/models/comment/comment_notification.py | 6 +++--- tildes/tildes/models/message/message.py | 13 ++++++++----- tildes/tildes/models/topic/topic.py | 4 ++-- tildes/tildes/models/user/user_invite_code.py | 5 ++++- 6 files changed, 21 insertions(+), 19 deletions(-) diff --git a/tildes/tildes/models/comment/comment.py b/tildes/tildes/models/comment/comment.py index d305fa4..872a0e3 100644 --- a/tildes/tildes/models/comment/comment.py +++ b/tildes/tildes/models/comment/comment.py @@ -131,13 +131,9 @@ class Comment(DatabaseModel): ): """Create a new comment.""" self.topic = topic - self.user_id = author.user_id - if parent_comment: - self.parent_comment_id = parent_comment.comment_id - else: - self.parent_comment_id = None - + self.user = author self.markdown = markdown + self.parent_comment = parent_comment incr_counter("comments") diff --git a/tildes/tildes/models/comment/comment_label.py b/tildes/tildes/models/comment/comment_label.py index 35697df..9170cc3 100644 --- a/tildes/tildes/models/comment/comment_label.py +++ b/tildes/tildes/models/comment/comment_label.py @@ -58,8 +58,8 @@ class CommentLabel(DatabaseModel): reason: Optional[str] = None, ): """Add a new label to a comment.""" - self.comment_id = comment.comment_id - self.user_id = user.user_id + self.comment = comment + self.user = user self.label = label self.weight = weight self.reason = reason diff --git a/tildes/tildes/models/comment/comment_notification.py b/tildes/tildes/models/comment/comment_notification.py index 1c1473f..68c34de 100644 --- a/tildes/tildes/models/comment/comment_notification.py +++ b/tildes/tildes/models/comment/comment_notification.py @@ -99,15 +99,15 @@ class CommentNotification(DatabaseModel): for user in users_to_mention: # prevent the user from mentioning themselves - if comment.user_id == user.user_id: + if comment.user == user: continue if parent_comment: # prevent comment replies from mentioning that comment's poster - if parent_comment.user_id == user.user_id: + if parent_comment.user == user: continue # prevent top-level comments from mentioning the thread creator - elif comment.topic.user_id == user.user_id: + elif comment.topic.user == user: continue mention_notification = cls( diff --git a/tildes/tildes/models/message/message.py b/tildes/tildes/models/message/message.py index f8c374e..50f4da6 100644 --- a/tildes/tildes/models/message/message.py +++ b/tildes/tildes/models/message/message.py @@ -111,9 +111,9 @@ class MessageConversation(DatabaseModel): def __init__(self, sender: User, recipient: User, subject: str, markdown: str): """Create a new message conversation between two users.""" - self.sender_id = sender.user_id - self.recipient_id = recipient.user_id - self.unread_user_ids = [self.recipient_id] + self.sender = sender + self.recipient = recipient + self.unread_user_ids = [self.recipient.user_id] self.subject = subject self.markdown = markdown self.rendered_html = convert_markdown_to_safe_html(markdown) @@ -231,12 +231,15 @@ class MessageReply(DatabaseModel): markdown: str = deferred(Column(Text, nullable=False)) rendered_html: str = Column(Text, nullable=False) + conversation: MessageConversation = relationship( + "MessageConversation", innerjoin=True + ) sender: User = relationship("User", lazy=False, innerjoin=True) def __init__(self, conversation: MessageConversation, sender: User, markdown: str): """Add a new reply to a message conversation.""" - self.conversation_id = conversation.conversation_id - self.sender_id = sender.user_id + self.conversation = conversation + self.sender = sender self.markdown = markdown self.rendered_html = convert_markdown_to_safe_html(markdown) diff --git a/tildes/tildes/models/topic/topic.py b/tildes/tildes/models/topic/topic.py index 5af94b6..0931093 100644 --- a/tildes/tildes/models/topic/topic.py +++ b/tildes/tildes/models/topic/topic.py @@ -196,8 +196,8 @@ class Topic(DatabaseModel): def _create_base_topic(cls, group: Group, author: User, title: str) -> "Topic": """Create the "base" for a new topic.""" new_topic = cls() - new_topic.group_id = group.group_id - new_topic.user_id = author.user_id + new_topic.group = group + new_topic.user = author # if the title is all caps, convert to title case if title.isupper(): diff --git a/tildes/tildes/models/user/user_invite_code.py b/tildes/tildes/models/user/user_invite_code.py index 187a512..ca5133e 100644 --- a/tildes/tildes/models/user/user_invite_code.py +++ b/tildes/tildes/models/user/user_invite_code.py @@ -8,6 +8,7 @@ import string from datetime import datetime from sqlalchemy import CheckConstraint, Column, ForeignKey, Integer, Text, TIMESTAMP +from sqlalchemy.orm import relationship from sqlalchemy.sql.expression import text from tildes.lib.string import separate_string @@ -39,6 +40,8 @@ class UserInviteCode(DatabaseModel): ) invitee_id: int = Column(Integer, ForeignKey("users.user_id")) + user: User = relationship("User", innerjoin=True, foreign_keys=[user_id]) + def __str__(self) -> str: """Format the code into a more easily readable version.""" return separate_string(self.code, "-", 5) @@ -49,7 +52,7 @@ class UserInviteCode(DatabaseModel): Note that uniqueness is not confirmed here, so there is the potential to create duplicate codes (which will fail to commit to the database). """ - self.user_id = user.user_id + self.user = user code_chars = random.choices(self.ALPHABET, k=self.LENGTH) self.code = "".join(code_chars)