models.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. class Conversation(db.Model):
  13. __tablename__ = "conversations"
  14. id = db.Column(db.Integer, primary_key=True)
  15. user_id = db.Column(db.Integer, db.ForeignKey("users.id"), nullable=False)
  16. from_user_id = db.Column(db.Integer, db.ForeignKey("users.id"))
  17. to_user_id = db.Column(db.Integer, db.ForeignKey("users.id"))
  18. shared_id = db.Column(UUIDType, nullable=False)
  19. subject = db.Column(db.String(255))
  20. date_created = db.Column(db.DateTime, default=datetime.utcnow())
  21. trash = db.Column(db.Boolean, nullable=False, default=False)
  22. draft = db.Column(db.Boolean, nullable=False, default=False)
  23. unread = db.Column(db.Boolean, nullable=False, default=True)
  24. messages = db.relationship(
  25. "Message", lazy="joined", backref="conversation",
  26. primaryjoin="Message.conversation_id == Conversation.id",
  27. cascade="all, delete-orphan"
  28. )
  29. # this is actually the users message box
  30. user = db.relationship("User", lazy="joined", foreign_keys=[user_id])
  31. # the user to whom the conversation is addressed
  32. to_user = db.relationship("User", lazy="joined", foreign_keys=[to_user_id])
  33. # the user who sent the message
  34. from_user = db.relationship("User", lazy="joined",
  35. foreign_keys=[from_user_id])
  36. @property
  37. def first_message(self):
  38. """Returns the first message object."""
  39. return self.messages[0]
  40. @property
  41. def last_message(self):
  42. """Returns the last message object."""
  43. return self.messages[-1]
  44. def save(self, message=None):
  45. """Saves a conversation and returns the saved conversation object.
  46. :param message: If given, it will also save the message for the
  47. conversation. It expects a Message object.
  48. """
  49. if message is not None:
  50. # create the conversation
  51. self.date_created = datetime.utcnow()
  52. db.session.add(self)
  53. db.session.commit()
  54. # create the actual message for the conversation
  55. message.save(self)
  56. return self
  57. db.session.add(self)
  58. db.session.commit()
  59. return self
  60. def delete(self):
  61. """Deletes a private message"""
  62. db.session.delete(self)
  63. db.session.commit()
  64. return self
  65. class Message(db.Model):
  66. __tablename__ = "messages"
  67. id = db.Column(db.Integer, primary_key=True)
  68. conversation_id = db.Column(db.Integer, db.ForeignKey("conversations.id"),
  69. nullable=False)
  70. # the user who wrote the message
  71. user_id = db.Column(db.Integer, db.ForeignKey("users.id"), nullable=False)
  72. message = db.Column(db.Text, nullable=False)
  73. date_created = db.Column(db.DateTime, default=datetime.utcnow())
  74. user = db.relationship("User", lazy="joined")
  75. def save(self, conversation=None):
  76. """Saves a private message.
  77. :param conversation: The conversation to which the message
  78. belongs to.
  79. """
  80. if conversation is not None:
  81. self.conversation_id = conversation.id
  82. self.date_created = datetime.utcnow()
  83. db.session.add(self)
  84. db.session.commit()
  85. return self
  86. def delete(self):
  87. """Deletes a private message"""
  88. db.session.delete(self)
  89. db.session.commit()
  90. return self