You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
103 lines
3.5 KiB
103 lines
3.5 KiB
import datetime
|
|
from django import forms
|
|
from django.conf import settings
|
|
from django.utils.translation import ugettext_lazy as _
|
|
|
|
if "notification" in settings.INSTALLED_APPS:
|
|
from notification import models as notification
|
|
else:
|
|
notification = None
|
|
|
|
from django_messages.models import Message
|
|
from django_messages.fields import CommaSeparatedUserField
|
|
from accounts.models import User
|
|
|
|
class ReplyForm(forms.Form):
|
|
recipient = forms.CharField(widget=forms.HiddenInput(), required=False)
|
|
subject = forms.CharField(label=_(u"Subject"), max_length=120, required=False)
|
|
body = forms.CharField(label=_(u"Body"),
|
|
widget=forms.Textarea(attrs={'rows': '12', 'cols':'55'}))
|
|
|
|
def save(self, sender, parent_msg=None):
|
|
subject = self.cleaned_data['subject']
|
|
body = self.cleaned_data['body']
|
|
recipient = self.cleaned_data['recipient']
|
|
msg = Message(
|
|
sender = sender,
|
|
recipient = recipient,
|
|
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()
|
|
|
|
return msg
|
|
|
|
def clean_recipient(self):
|
|
recipient = self.cleaned_data.get('recipient')
|
|
user = User.objects.get(id=recipient)
|
|
return user
|
|
|
|
class SendForm(forms.Form):
|
|
body = forms.CharField(label=_(u"Body"),
|
|
widget=forms.Textarea(attrs={'rows': '12', 'cols':'55'}))
|
|
|
|
def save(self, sender, recipient):
|
|
body = self.cleaned_data['body']
|
|
msg = Message(
|
|
sender = sender,
|
|
recipient = recipient,
|
|
body = body,
|
|
)
|
|
msg.save()
|
|
|
|
return msg
|
|
|
|
|
|
class ComposeForm(forms.Form):
|
|
"""
|
|
A simple default form for private messages.
|
|
"""
|
|
recipient = CommaSeparatedUserField(label=_(u"Recipient"))
|
|
subject = forms.CharField(label=_(u"Subject"), max_length=120, required=False)
|
|
body = forms.CharField(label=_(u"Body"),
|
|
widget=forms.Textarea(attrs={'rows': '12', 'cols':'55'}))
|
|
|
|
|
|
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 |