models.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. # -*- coding: utf-8 -*-
  2. """
  3. flaskbb.message.models
  4. ~~~~~~~~~~~~~~~~~~~~~~
  5. The models for the conversations and messages are located here.
  6. :copyright: (c) 2014 by the FlaskBB Team.
  7. :license: BSD, see LICENSE for more details.
  8. """
  9. from datetime import datetime
  10. from sqlalchemy_utils import UUIDType
  11. from flaskbb.extensions import db
  12. from flaskbb.utils.database import CRUDMixin
  13. class Conversation(db.Model, CRUDMixin):
  14. __tablename__ = "conversations"
  15. id = db.Column(db.Integer, primary_key=True)
  16. user_id = db.Column(db.Integer, db.ForeignKey("users.id"), nullable=False)
  17. from_user_id = db.Column(db.Integer, db.ForeignKey("users.id"))
  18. to_user_id = db.Column(db.Integer, db.ForeignKey("users.id"))
  19. shared_id = db.Column(UUIDType, nullable=False)
  20. subject = db.Column(db.String(255))
  21. date_created = db.Column(db.DateTime, default=datetime.utcnow())
  22. trash = db.Column(db.Boolean, nullable=False, default=False)
  23. draft = db.Column(db.Boolean, nullable=False, default=False)
  24. unread = db.Column(db.Boolean, nullable=False, default=True)
  25. messages = db.relationship(
  26. "Message", lazy="joined", backref="conversation",
  27. primaryjoin="Message.conversation_id == Conversation.id",
  28. order_by="asc(Message.id)",
  29. cascade="all, delete-orphan"
  30. )
  31. # this is actually the users message box
  32. user = db.relationship("User", lazy="joined", foreign_keys=[user_id])
  33. # the user to whom the conversation is addressed
  34. to_user = db.relationship("User", lazy="joined", foreign_keys=[to_user_id])
  35. # the user who sent the message
  36. from_user = db.relationship("User", lazy="joined",
  37. foreign_keys=[from_user_id])
  38. @property
  39. def first_message(self):
  40. """Returns the first message object."""
  41. return self.messages[0]
  42. @property
  43. def last_message(self):
  44. """Returns the last message object."""
  45. return self.messages[-1]
  46. def save(self, message=None):
  47. """Saves a conversation and returns the saved conversation object.
  48. :param message: If given, it will also save the message for the
  49. conversation. It expects a Message object.
  50. """
  51. if message is not None:
  52. # create the conversation
  53. self.date_created = datetime.utcnow()
  54. db.session.add(self)
  55. db.session.commit()
  56. # create the actual message for the conversation
  57. message.save(self)
  58. return self
  59. db.session.add(self)
  60. db.session.commit()
  61. return self
  62. class Message(db.Model, CRUDMixin):
  63. __tablename__ = "messages"
  64. id = db.Column(db.Integer, primary_key=True)
  65. conversation_id = db.Column(db.Integer, db.ForeignKey("conversations.id"),
  66. nullable=False)
  67. # the user who wrote the message
  68. user_id = db.Column(db.Integer, db.ForeignKey("users.id"), nullable=False)
  69. message = db.Column(db.Text, nullable=False)
  70. date_created = db.Column(db.DateTime, default=datetime.utcnow())
  71. user = db.relationship("User", lazy="joined")
  72. def save(self, conversation=None):
  73. """Saves a private message.
  74. :param conversation: The conversation to which the message
  75. belongs to.
  76. """
  77. if conversation is not None:
  78. self.conversation_id = conversation.id
  79. self.date_created = datetime.utcnow()
  80. db.session.add(self)
  81. db.session.commit()
  82. return self