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_text_cleaner(self): """Test _text_cleaner().""" text = """ <html>This is HTML Yes it is </html>""" message = Message(text=dedent(text)) result = message._text_cleaner() self.assertEqual(result, 'This is HTML Yes it is')
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)
def test_snippet_beginning_nonletter(self): """Test a long snippet that starts and ends with a non-letter""" message = Message(clean_text=u"!I already know what this will be!!!!!") self.assertEqual( message.snippet, 'I already know what...' )
def test_snippet_long_unicode(self): """Test a unicode-filled string replaces the unicode character""" message = Message(clean_text=u"This sentence — pauses a bit") self.assertEqual( message.snippet, 'This sentence -- paus...' )
def test_get_form_sender_id_is_authenticated_user(self): """Sender id should be set to the currently authenticated user.""" view = views.MessageCreateView() view.request = self.request view.object = Message() form = view.get_form(views.MessageCreateView.form_class) self.assertEqual(form.instance.sender_id, self.request.user.pk)
def test_snippet_long_strip_end(self): """Test long unicode-filled snippets ending with a non-letter""" # Without stripping the non-character end this would end with ' --' message = Message(clean_text=u"This was a longer — sentence") self.assertEqual( message.snippet, 'This was a longer...' )
def test_snippet_short(self): """Short messages w/em dash should return all ASCII stripped text""" # Em dashes are turned into double dashes, stripped from end message = Message(clean_text=u'short — test— ') self.assertEqual( message.snippet, 'short -- test' )
def test_long_snippet(self): """Long Snippet should return first 140 characters of clean_text.""" message = Message(clean_text=''.join('x' for _ in range(0, 200))) self.assertEqual( message.long_snippet, 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' 'xxxxxxxx')