mirror of
https://gitlab.com/tildes/tildes.git
synced 2026-04-17 06:48:36 +02:00
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.
This commit is contained in:
@@ -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")
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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(
|
||||
|
||||
@@ -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)
|
||||
|
||||
|
||||
@@ -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():
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user