comparison messages/forms.py @ 581:ee87ea74d46b

For Django 1.4, rearranged project structure for new manage.py.
author Brian Neal <bgneal@gmail.com>
date Sat, 05 May 2012 17:10:48 -0500
parents gpp/messages/forms.py@4b9970ad0edb
children 9aae4f99f062
comparison
equal deleted inserted replaced
580:c525f3e0b5d0 581:ee87ea74d46b
1 """
2 Forms for the messages application.
3 """
4
5 from django import forms
6 from django.contrib.auth.models import User
7 from django.conf import settings
8 from django.contrib.sites.models import Site
9 from django.core.urlresolvers import reverse
10 from django.template.loader import render_to_string
11
12 from core.functions import send_mail
13 from core.widgets import AutoCompleteUserInput
14 from messages.models import Message
15 from messages.models import Options
16 from messages import MSG_BOX_LIMIT
17
18
19 # Maximum size of a private message in characters
20 MESSAGE_MAX = getattr(settings, 'MESSAGES_MAX_SIZE', 8192)
21
22
23 class ComposeForm(forms.Form):
24 """
25 This form is used to compose private messages.
26 """
27 receiver = forms.CharField(label='To',
28 max_length=30,
29 widget=AutoCompleteUserInput())
30 subject = forms.CharField(max_length=120, widget=forms.TextInput(attrs={'size': 52}))
31 message = forms.CharField(widget=forms.Textarea(attrs={'class': 'markItUp smileyTarget'}))
32 attach_signature = forms.BooleanField(label='Attach Signature?', required=False)
33
34 def __init__(self, user, *args, **kwargs):
35 forms.Form.__init__(self, *args, **kwargs)
36 self.user = user
37 options = Options.objects.for_user(user)
38 self.fields['attach_signature'].initial = options.attach_signature
39
40 def clean_receiver(self):
41 receiver = self.cleaned_data['receiver']
42 try:
43 self.rcvr_user = User.objects.get(username=receiver)
44 except User.DoesNotExist:
45 raise forms.ValidationError("That username does not exist.")
46 if self.user == self.rcvr_user:
47 raise forms.ValidationError("You can't send a message to yourself.")
48 return receiver
49
50 def clean_message(self):
51 msg = self.cleaned_data['message']
52 if len(msg) > MESSAGE_MAX:
53 raise forms.ValidationError("Your message is too long. Please trim some text.")
54 return msg
55
56 def clean(self):
57 rcvr = self.cleaned_data.get('receiver')
58 subject = self.cleaned_data.get('subject')
59 message = self.cleaned_data.get('message')
60
61 if rcvr and subject and message:
62 # Can we send a message? Is our outbox full?
63
64 count = Message.objects.outbox(self.user).count()
65 if count >= MSG_BOX_LIMIT:
66 raise forms.ValidationError(
67 "Your outbox is full. Please delete some messages.")
68
69 # Is the receiver's inbox full?
70 count = Message.objects.inbox(self.rcvr_user).count()
71 if count >= MSG_BOX_LIMIT:
72 raise forms.ValidationError(
73 "Sorry, %s's inbox is full. This message cannot be sent." %
74 self.rcvr_user.username)
75
76 return self.cleaned_data
77
78 def save(self, parent_msg=None):
79 sender = self.user
80 receiver = self.rcvr_user
81 subject = self.cleaned_data['subject']
82 message = self.cleaned_data['message']
83 attach_signature = self.cleaned_data['attach_signature']
84
85 new_msg = Message(
86 sender=sender,
87 receiver=receiver,
88 subject=subject,
89 message=message,
90 signature_attached=attach_signature,
91 )
92 new_msg.save()
93 if parent_msg is not None:
94 parent_msg.reply_date = new_msg.send_date
95 parent_msg.save()
96
97 receiver_opts = Options.objects.for_user(receiver)
98 if receiver_opts.notify_email:
99 notify_receiver(new_msg)
100
101 class Media:
102 css = {
103 'all': (settings.GPP_THIRD_PARTY_CSS['markitup'] +
104 settings.GPP_THIRD_PARTY_CSS['jquery-ui'])
105 }
106 js = (settings.GPP_THIRD_PARTY_JS['markitup'] +
107 settings.GPP_THIRD_PARTY_JS['jquery-ui'])
108
109
110 class OptionsForm(forms.ModelForm):
111 class Meta:
112 model = Options
113
114
115 def notify_receiver(new_msg):
116 """
117 This function creates the notification email to notify a user of
118 a new private message.
119 """
120 site = Site.objects.get_current()
121
122 email_body = render_to_string('messages/notification_email.txt', {
123 'site': site,
124 'msg': new_msg,
125 'options_url': reverse('messages-options_tab'),
126 })
127
128 subject = 'New private message for %s at %s' % (new_msg.receiver.username, site.name)
129 from_email = settings.GPP_NO_REPLY_EMAIL + '@' + site.domain
130 send_mail(subject, email_body, from_email, [new_msg.receiver.email])