annotate gpp/messages/forms.py @ 430:9df368d9775d

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