class ComposeForm(forms.Form): """ A simple default form for private messages. """ recipient = CommaSeparatedUserField(label=_(u"Recipient")) subject = forms.CharField(label=_(u"Subject"), max_length=140) body = forms.CharField(label=_(u"Body"), widget=forms.Textarea(attrs={ 'rows': '12', 'cols': '55' })) attachments = forms.FileField( label=_('Attachments'), widget=forms.ClearableFileInput(attrs={'multiple': True}), required=False) def __init__(self, *args, **kwargs): recipient_filter = kwargs.pop('recipient_filter', None) super(ComposeForm, self).__init__(*args, **kwargs) if recipient_filter is not None: self.fields['recipient']._recipient_filter = recipient_filter def save(self, sender, parent_msg=None, files=[]): recipients = self.cleaned_data['recipient'] subject = self.cleaned_data['subject'] body = self.cleaned_data['body'] message_list = [] for r in recipients: msg = Message( sender=sender, recipient=r, subject=subject, body=body, ) if parent_msg is not None: msg.parent_msg = parent_msg parent_msg.replied_at = timezone.now() parent_msg.save() msg.save() for f in files: attachment = Attachment(file=f, message=msg) attachment.save() message_list.append(msg) if notification: if parent_msg is not None: notification.send([sender], "messages_replied", { 'message': msg, }) notification.send([r], "messages_reply_received", { 'message': msg, }) else: notification.send([sender], "messages_sent", { 'message': msg, }) notification.send([r], "messages_received", { 'message': msg, }) return message_list
class ComposeForm(forms.Form): """ A simple default form for private messages. """ recipient = CommaSeparatedUserField( label=_("Recipient"), widget=forms.TextInput(attrs={'class': 'form-control'})) subject = forms.CharField( label=_("Subject"), max_length=120, widget=forms.TextInput(attrs={'class': 'form-control'})) body = forms.CharField(label=_("Body"), widget=forms.Textarea(attrs={ 'rows': '12', 'cols': '55', 'class': 'form-control' })) def __init__(self, *args, **kwargs): recipient_filter = kwargs.pop('recipient_filter', None) super(ComposeForm, self).__init__(*args, **kwargs) if recipient_filter is not None: self.fields['recipient']._recipient_filter = recipient_filter def save(self, sender, parent_msg=None): recipients = self.cleaned_data['recipient'] subject = self.cleaned_data['subject'] body = self.cleaned_data['body'] message_list = [] for r in recipients: msg = Message( sender=sender, recipient=r, subject=subject, body=body, ) if parent_msg is not None: msg.parent_msg = parent_msg parent_msg.replied_at = datetime.datetime.now() parent_msg.save() msg.save() message_list.append(msg) if notification: if parent_msg is not None: notification.send([sender], "messages_replied", { 'message': msg, }) notification.send([r], "messages_reply_received", { 'message': msg, }) else: notification.send([sender], "messages_sent", { 'message': msg, }) notification.send([r], "messages_received", { 'message': msg, }) return message_list
class ComposeForm(forms.Form): """ A form for creating a new message thread to one or more users. """ recipient = CommaSeparatedUserField(label=_(u"Recipient")) subject = forms.CharField(label=_(u"Subject")) body = forms.CharField(label=_(u"Body"), widget=forms.Textarea(attrs={ 'rows': '12', 'cols': '55' })) def __init__(self, sender, *args, **kwargs): recipient_filter = kwargs.pop('recipient_filter', None) super(ComposeForm, self).__init__(*args, **kwargs) if recipient_filter is not None: self.fields['recipient']._recipient_filter = recipient_filter self.sender = sender def save(self): recipients = self.cleaned_data['recipient'] subject = self.cleaned_data['subject'] body = self.cleaned_data['body'] new_message = Message.objects.create(body=body, sender=self.sender) thread = Thread.objects.create(subject=subject, latest_msg=new_message) thread.all_msgs.add(new_message) thread.save() for recipient in recipients: if recipient != self.sender: Participant.objects.create(thread=thread, user=recipient) (sender_part, created) = Participant.objects.get_or_create(thread=thread, user=self.sender) sender_part.replied_at = sender_part.read_at = datetime.datetime.now() sender_part.save() signals.threaded_message_sent.send(sender=self, message=new_message) return thread
class MessageForm(forms.ModelForm): """ base message form """ recipients = CommaSeparatedUserField(label=_(u"Recipient")) subject = forms.CharField(label=_(u"Subject")) body = forms.CharField(label=_(u"Body"), widget=forms.Textarea(attrs={'rows': '12', 'cols':'55'})) class Meta: model = Message fields = ('recipients', 'subject', 'body',) def __init__(self, sender, *args, **kw): recipient_filter = kw.pop('recipient_filter', None) self.sender = sender super(MessageForm, self).__init__(*args, **kw) if recipient_filter is not None: self.fields['recipients']._recipient_filter = recipient_filter def create_recipient_message(self, recipient, message): return Message( owner = recipient, sender = self.sender, to = recipient.username, recipient = recipient, subject = message.subject, body = message.body, thread = message.thread, sent_at = message.sent_at, ) def get_thread(self, message): return message.thread or uuid.uuid4().hex def save(self, commit=True): recipients = self.cleaned_data['recipients'] instance = super(MessageForm, self).save(commit=False) instance.sender = self.sender instance.owner = self.sender instance.recipient = recipients[0] instance.thread = self.get_thread(instance) instance.unread = False instance.sent_at = datetime.datetime.now() message_list = [] # clone messages in recipients inboxes for r in recipients: if r == self.sender: # skip duplicates continue msg = self.create_recipient_message(r, instance) message_list.append(msg) instance.to = ','.join([r.username for r in recipients]) if commit: instance.save() for msg in message_list: msg.save() if notification: notification.send([msg.recipient], "messages_received", {'message': msg,}) return instance, message_list
class ComposeForm(forms.Form): """ A simple default form for private messages. """ recipient = CommaSeparatedUserField(label=_(u"Recipient"), widget=forms.HiddenInput()) subject = forms.CharField(label=_(u"Subject"), max_length=120) body = forms.CharField(label=_(u"Body"), widget=forms.Textarea(attrs={ 'rows': '12', 'cols': '55' })) #by lxy from django.contrib.auth.models import Group all_group = Group.objects.get(name="all") group_recipient = forms.ModelChoiceField(queryset=Group.objects.all(), label=_(u"group Recipient"), initial=all_group.id, widget=forms.HiddenInput()) def __init__(self, *args, **kwargs): # form = ComposeForm(initial = {'group_recipient': group_recipient }) recipient_filter = kwargs.pop('recipient_filter', None) super(ComposeForm, self).__init__(*args, **kwargs) if recipient_filter is not None: self.fields['recipient']._recipient_filter = recipient_filter def save(self, sender, parent_msg=None): recipients = self.cleaned_data['recipient'] subject = self.cleaned_data['subject'] body = self.cleaned_data['body'] group_recipient = self.cleaned_data['group_recipient'] message_list = [] for r in recipients: msg = Message( sender=sender, recipient=r, subject=subject, body=body, #by lxy group_recipient=group_recipient) if parent_msg is not None: msg.parent_msg = parent_msg parent_msg.replied_at = datetime.datetime.now() parent_msg.save() msg.save() message_list.append(msg) if notification: if parent_msg is not None: notification.send([sender], "messages_replied", { 'message': msg, }) notification.send([r], "messages_reply_received", { 'message': msg, }) else: notification.send([sender], "messages_sent", { 'message': msg, }) notification.send([r], "messages_received", { 'message': msg, }) return message_list