annotate gpp/messages/forms.py @ 435:b2f02766cc72

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