def get_initial(self): initial = super(MessageComposeFormView, self).get_initial() if self.recipients: username_list = [r.strip() for r in self.recipients.split("+")] recipients = [u for u in get_user_model().objects.filter(username__in=username_list)] initial["to"] = recipients return initial
def get_queryset(self): username = self.kwargs['username'] self.recipient = get_object_or_404(get_user_model(), username__iexact=username) queryset = Message.objects.get_conversation_between(self.request.user, self.recipient) self._update_unread_messages(queryset) return queryset
def test_save_msg(self): """ Test valid data """ valid_data = {'to': 'john, jane', 'body': 'Body'} form = ComposeForm(data=valid_data) self.failUnless(form.is_valid()) # Save the form. sender = get_user_model().objects.get(username='******') msg = form.save(sender) # Check if the values are set correctly self.failUnlessEqual(msg.body, valid_data['body']) self.failUnlessEqual(msg.sender, sender) self.failUnless(msg.sent_at) # Check recipients self.failUnlessEqual(msg.recipients.all()[0].username, 'jane') self.failUnlessEqual(msg.recipients.all()[1].username, 'john')
def clean(self, value): super(CommaSeparatedUserField, self).clean(value) names = set(value.split(',')) names_set = set([name.strip() for name in names]) users = list(get_user_model().objects.filter(username__in=names_set)) # Check for unknown names. unknown_names = names_set ^ set([user.username for user in users]) recipient_filter = self._recipient_filter invalid_users = [] if recipient_filter is not None: for r in users: if recipient_filter(r) is False: users.remove(r) invalid_users.append(r.username) if unknown_names or invalid_users: humanized_usernames = ', '.join(list(unknown_names) + invalid_users) raise forms.ValidationError(_("The following usernames are incorrect: %(users)s.") % {'users': humanized_usernames}) return users
from django.test import TestCase from django.core.urlresolvers import reverse from django.utils.text import truncate_words from umessages.models import Message, MessageRecipient, MessageContact from umessages.utils import get_user_model User = get_user_model() class MessageContactTests(TestCase): fixtures = ["users", "messages"] def test_string_formatting(self): """ Test the human representation of a message """ contact = MessageContact.objects.get(pk=1) correct_format = "john and jane" self.failUnlessEqual(contact.__unicode__(), correct_format) def test_opposite_user(self): """ Test if the opposite user is returned """ contact = MessageContact.objects.get(pk=1) john = User.objects.get(pk=1) jane = User.objects.get(pk=2) # Test the opposites self.failUnlessEqual(contact.opposite_user(john), jane) self.failUnlessEqual(contact.opposite_user(jane), john)