Mercurial > public > sg101
comparison gpp/messages/forms.py @ 432:ca744075e7b7
For #211; limit private message sizes to 8K.
author | Brian Neal <bgneal@gmail.com> |
---|---|
date | Fri, 06 May 2011 01:41:21 +0000 |
parents | 9df368d9775d |
children | 241c80ff16c5 |
comparison
equal
deleted
inserted
replaced
431:0d91176cf9b3 | 432:ca744075e7b7 |
---|---|
14 from core.widgets import AutoCompleteUserInput | 14 from core.widgets import AutoCompleteUserInput |
15 from messages.models import Message | 15 from messages.models import Message |
16 from messages.models import Options | 16 from messages.models import Options |
17 | 17 |
18 | 18 |
19 # Maximum size of a private message in characters | |
20 MESSAGE_MAX = getattr(settings, 'MESSAGES_MAX_SIZE', 8192) | |
21 | |
22 | |
19 class ComposeForm(forms.Form): | 23 class ComposeForm(forms.Form): |
20 """ | 24 """ |
21 This form is used to compose private messages. | 25 This form is used to compose private messages. |
22 """ | 26 """ |
23 receiver = forms.CharField(label='To', | 27 receiver = forms.CharField(label='To', |
24 max_length=30, | 28 max_length=30, |
25 widget=AutoCompleteUserInput()) | 29 widget=AutoCompleteUserInput()) |
26 subject = forms.CharField(max_length=120, widget=forms.TextInput(attrs={'size': 52})) | 30 subject = forms.CharField(max_length=120, widget=forms.TextInput(attrs={'size': 52})) |
27 message = forms.CharField(widget=forms.Textarea(attrs={'class': 'markItUp smileyTarget'})) | 31 message = forms.CharField(widget=forms.Textarea(attrs={'class': 'markItUp smileyTarget'})) |
28 attach_signature = forms.BooleanField(label='Attach Signature?', required=False) | 32 attach_signature = forms.BooleanField(label='Attach Signature?', required=False) |
29 | 33 |
30 def __init__(self, user, *args, **kwargs): | 34 def __init__(self, user, *args, **kwargs): |
31 forms.Form.__init__(self, *args, **kwargs) | 35 forms.Form.__init__(self, *args, **kwargs) |
32 self.user = user | 36 self.user = user |
33 options = Options.objects.for_user(user) | 37 options = Options.objects.for_user(user) |
34 self.fields['attach_signature'].initial = options.attach_signature | 38 self.fields['attach_signature'].initial = options.attach_signature |
35 | 39 |
36 def clean_receiver(self): | 40 def clean_receiver(self): |
37 receiver = self.cleaned_data['receiver'] | 41 receiver = self.cleaned_data['receiver'] |
38 try: | 42 try: |
39 self.rcvr_user = User.objects.get(username=receiver) | 43 self.rcvr_user = User.objects.get(username=receiver) |
40 except User.DoesNotExist: | 44 except User.DoesNotExist: |
41 raise forms.ValidationError("That username does not exist.") | 45 raise forms.ValidationError("That username does not exist.") |
42 if self.user == self.rcvr_user: | 46 if self.user == self.rcvr_user: |
43 raise forms.ValidationError("You can't send a message to yourself.") | 47 raise forms.ValidationError("You can't send a message to yourself.") |
44 return receiver | 48 return receiver |
45 | 49 |
46 def save(self, parent_msg=None): | 50 def clean_message(self): |
47 sender = self.user | 51 msg = self.cleaned_data['message'] |
48 receiver = self.rcvr_user | 52 if len(msg) > MESSAGE_MAX: |
49 subject = self.cleaned_data['subject'] | 53 raise forms.ValidationError("Your message is too long. Please trim some text.") |
50 message = self.cleaned_data['message'] | 54 return msg |
51 attach_signature = self.cleaned_data['attach_signature'] | |
52 | 55 |
53 new_msg = Message( | 56 def save(self, parent_msg=None): |
54 sender=sender, | 57 sender = self.user |
55 receiver=receiver, | 58 receiver = self.rcvr_user |
56 subject=subject, | 59 subject = self.cleaned_data['subject'] |
57 message=message, | 60 message = self.cleaned_data['message'] |
58 signature_attached=attach_signature, | 61 attach_signature = self.cleaned_data['attach_signature'] |
59 ) | |
60 new_msg.save() | |
61 if parent_msg is not None: | |
62 parent_msg.reply_date = new_msg.send_date | |
63 parent_msg.save() | |
64 | 62 |
65 receiver_opts = Options.objects.for_user(receiver) | 63 new_msg = Message( |
66 if receiver_opts.notify_email: | 64 sender=sender, |
67 notify_receiver(new_msg) | 65 receiver=receiver, |
66 subject=subject, | |
67 message=message, | |
68 signature_attached=attach_signature, | |
69 ) | |
70 new_msg.save() | |
71 if parent_msg is not None: | |
72 parent_msg.reply_date = new_msg.send_date | |
73 parent_msg.save() | |
68 | 74 |
69 class Media: | 75 receiver_opts = Options.objects.for_user(receiver) |
70 css = { | 76 if receiver_opts.notify_email: |
71 'all': (settings.GPP_THIRD_PARTY_CSS['markitup'] + | 77 notify_receiver(new_msg) |
72 settings.GPP_THIRD_PARTY_CSS['jquery-ui']) | 78 |
73 } | 79 class Media: |
74 js = (settings.GPP_THIRD_PARTY_JS['markitup'] + | 80 css = { |
75 settings.GPP_THIRD_PARTY_JS['jquery-ui']) | 81 'all': (settings.GPP_THIRD_PARTY_CSS['markitup'] + |
82 settings.GPP_THIRD_PARTY_CSS['jquery-ui']) | |
83 } | |
84 js = (settings.GPP_THIRD_PARTY_JS['markitup'] + | |
85 settings.GPP_THIRD_PARTY_JS['jquery-ui']) | |
76 | 86 |
77 | 87 |
78 class OptionsForm(forms.ModelForm): | 88 class OptionsForm(forms.ModelForm): |
79 class Meta: | 89 class Meta: |
80 model = Options | 90 model = Options |
81 | 91 |
82 | 92 |
83 def notify_receiver(new_msg): | 93 def notify_receiver(new_msg): |
84 """ | 94 """ |
85 This function creates the notification email to notify a user of | 95 This function creates the notification email to notify a user of |
86 a new private message. | 96 a new private message. |
87 """ | 97 """ |
88 site = Site.objects.get_current() | 98 site = Site.objects.get_current() |
89 | 99 |
90 email_body = render_to_string('messages/notification_email.txt', { | 100 email_body = render_to_string('messages/notification_email.txt', { |
91 'site': site, | 101 'site': site, |
92 'msg': new_msg, | 102 'msg': new_msg, |
93 'options_url': reverse('messages-options'), | 103 'options_url': reverse('messages-options'), |
94 }) | 104 }) |
95 | 105 |
96 subject = 'New private message for %s at %s' % (new_msg.receiver.username, site.name) | 106 subject = 'New private message for %s at %s' % (new_msg.receiver.username, site.name) |
97 from_email = settings.GPP_NO_REPLY_EMAIL + '@' + site.domain | 107 from_email = settings.GPP_NO_REPLY_EMAIL + '@' + site.domain |
98 send_mail(subject, email_body, from_email, [new_msg.receiver.email]) | 108 send_mail(subject, email_body, from_email, [new_msg.receiver.email]) |