Advertisement
Guest User

Untitled

a guest
Feb 1st, 2016
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.07 KB | None | 0 0
  1. class SMSMessage(Base):
  2. """Inbound/outbound SMS message."""
  3. __tablename__ = "sms_message"
  4.  
  5. id = Column(Integer, autoincrement=True, primary_key=True)
  6.  
  7. #: When this review was created
  8. created_at = Column(UTCDateTime, default=now)
  9.  
  10. # Who is the customer we are having a dialog with
  11. customer_id = Column(ForeignKey("users.id"))
  12. customer = relationship(User, primaryjoin=customer_id == User.id, backref="sms_messages")
  13.  
  14. sms_message_type = Column(Enum('incoming', 'outgoing', name="sms_message_type"), nullable=False)
  15.  
  16. message = Column(String(1600), nullable=True, default=None)
  17.  
  18. # Attachents are handled on file-system, not on database, but we record a count here so we can read them
  19. attachment_count = Column(Integer, nullable=False, default=0)
  20.  
  21. # Any Twilio location data from celltower/phone number
  22. location_data = sqlalchemy.Column(JSONB, default={}, nullable=True)
  23.  
  24. @classmethod
  25. def handle_incoming(cls, phone_number, message, attachments:typing.List=None, registry=None, basic_auth_username=None, basic_auth_password=None):
  26. """
  27. :param phone_number:
  28. :param message:
  29. :param attachments: List of media URLs
  30. :return:
  31. """
  32. user, created = User.get_or_create_sms_user(phone_number)
  33. if not user.id:
  34. DBSession.flush()
  35.  
  36. assert user.id
  37. sms = SMSMessage(customer=user, sms_message_type="incoming", message=message)
  38. DBSession.add(sms)
  39.  
  40. if attachments:
  41. DBSession.flush()
  42. sms.attachment_count = len(attachments)
  43. for a in attachments:
  44. try:
  45. UserMedia.fetch_from_url(registry, a, user=user, sms=sms, basic_auth_username=basic_auth_username, basic_auth_password=basic_auth_password)
  46. except Exception as e:
  47. logger.error("Could not get attachment %s", a)
  48. logger.exception(e)
  49.  
  50. return user, sms, created
  51.  
  52. def get_attachments(self):
  53. return self.attached_medias
  54.  
  55.  
  56. Index('sms_message_customer_index', SMSMessage.customer_id)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement