-
Notifications
You must be signed in to change notification settings - Fork 246
/
Copy pathforms.py
56 lines (49 loc) · 2.12 KB
/
forms.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
from django import forms
from django.conf import settings
from django.utils.translation import ugettext_lazy as _
from django.utils import timezone
if "pinax.notifications" in settings.INSTALLED_APPS and getattr(settings, 'DJANGO_MESSAGES_NOTIFY', True):
from pinax.notifications import models as notification
else:
notification = None
from django_messages.models import Message
from django_messages.fields import CommaSeparatedUserField
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'}))
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 = timezone.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