annotate gpp/messages/forms.py @ 453:532b9c6f2482

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