def send_system_message(recipient, subject, message_content): """Send a direct message to a user coming from the system user""" from open_connect.connectmessages.models import Thread, Message from open_connect.notifications.models import Notification from open_connect.accounts.models import User # Handle calls where we're passed the user_id instead of a User object. if not isinstance(recipient, User): recipient = User.objects.get(pk=recipient) sysuser = User.objects.get(email=settings.SYSTEM_USER_EMAIL) thread = Thread.objects.create( subject=subject[:100], thread_type='direct', closed=True ) message = Message( thread=thread, sender=sysuser, text=message_content ) message.save(shorten=False) thread.add_user_to_thread(recipient) # Use BeautifulSoup to remove any HTML from the message to make the # plaintext email version of the message notification, created = Notification.objects.get_or_create( recipient_id=recipient.pk, message=message ) if created and recipient.group_notification_period == 'immediate': send_immediate_notification.delay(notification.pk)
def test_non_http_links_not_shortened(self): """Non http/s links shouldn't be shortened.""" thread = mommy.make(Thread) message = Message( text='This is an email: <a href="mailto:[email protected]">lnk</a>', thread=thread, sender=self.create_user()) message.save() self.assertEqual(message.links.count(), 0)
def test_links_in_message_are_not_shortened(self): """If shorten=False, links in message should not be replaced.""" thread = mommy.make(Thread) message = Message( text='This is a <a href="http://www.razzmatazz.local">link</a>', thread=thread, sender=self.create_user()) message.save(shorten=False) self.assertEqual(message.links.count(), 0) self.assertTrue('www.razzmatazz.local' in message.text)
def test_links_in_message_are_shortened(self): """Links in a message should be replaced with short code.""" thread = mommy.make(Thread) message = Message( text='This is a <a href="http://www.razzmatazz.local">link</a>', thread=thread, sender=self.create_user()) message.save() self.assertEqual(message.links.count(), 1) self.assertTrue(message.links.get().short_code in message.text)
def test_non_http_links_not_shortened(self): """Non http/s links shouldn't be shortened.""" thread = mommy.make(Thread) message = Message( text='This is an email: <a href="mailto:[email protected]">lnk</a>', thread=thread, sender=self.create_user() ) message.save() self.assertEqual(message.links.count(), 0)
def test_links_in_message_are_not_shortened(self): """If shorten=False, links in message should not be replaced.""" thread = mommy.make(Thread) message = Message( text='This is a <a href="http://www.razzmatazz.local">link</a>', thread=thread, sender=self.create_user() ) message.save(shorten=False) self.assertEqual(message.links.count(), 0) self.assertTrue('www.razzmatazz.local' in message.text)
def test_links_in_message_are_shortened(self): """Links in a message should be replaced with short code.""" thread = mommy.make(Thread) message = Message( text='This is a <a href="http://www.razzmatazz.local">link</a>', thread=thread, sender=self.create_user() ) message.save() self.assertEqual(message.links.count(), 1) self.assertTrue(message.links.get().short_code in message.text)