annotate gpp/messages/forms.py @ 340:2541d27ca056

Fix #153. Update verbiage on the donations thanks template to meet Paypal guidelines for making it a auto-return page.
author Brian Neal <bgneal@gmail.com>
date Sun, 27 Feb 2011 00:28:20 +0000
parents 88b2b9cb8c1f
children 9df368d9775d
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.")
gremmie@1 42 if self.user.username.lower() == receiver.lower():
gremmie@1 43 raise forms.ValidationError("You can't send a message to yourself.")
gremmie@1 44 return receiver
gremmie@1 45
gremmie@1 46 def save(self, sender, parent_msg=None):
gremmie@1 47 receiver = self.rcvr_user
gremmie@1 48 subject = self.cleaned_data['subject']
gremmie@1 49 message = self.cleaned_data['message']
gremmie@1 50 attach_signature = self.cleaned_data['attach_signature']
gremmie@1 51
gremmie@1 52 new_msg = Message(
gremmie@1 53 sender=sender,
gremmie@1 54 receiver=receiver,
gremmie@1 55 subject=subject,
gremmie@1 56 message=message,
gremmie@1 57 signature_attached=attach_signature,
gremmie@1 58 )
gremmie@1 59 new_msg.save()
gremmie@1 60 if parent_msg is not None:
gremmie@1 61 parent_msg.reply_date = new_msg.send_date
gremmie@1 62 parent_msg.save()
gremmie@1 63
gremmie@1 64 receiver_opts = Options.objects.for_user(receiver)
gremmie@1 65 if receiver_opts.notify_email:
gremmie@1 66 notify_receiver(new_msg)
gremmie@1 67
gremmie@1 68 class Media:
gremmie@1 69 css = {
bgneal@133 70 'all': (settings.GPP_THIRD_PARTY_CSS['markitup'] +
bgneal@133 71 settings.GPP_THIRD_PARTY_CSS['jquery-ui'])
gremmie@1 72 }
bgneal@133 73 js = (settings.GPP_THIRD_PARTY_JS['markitup'] +
bgneal@133 74 settings.GPP_THIRD_PARTY_JS['jquery-ui'])
gremmie@1 75
gremmie@1 76
gremmie@1 77 class OptionsForm(forms.ModelForm):
gremmie@1 78 class Meta:
gremmie@1 79 model = Options
gremmie@1 80
gremmie@1 81
gremmie@1 82 def notify_receiver(new_msg):
gremmie@1 83 """
gremmie@1 84 This function creates the notification email to notify a user of
gremmie@1 85 a new private message.
gremmie@1 86 """
gremmie@1 87 site = Site.objects.get_current()
gremmie@1 88
gremmie@1 89 email_body = render_to_string('messages/notification_email.txt', {
gremmie@1 90 'site': site,
gremmie@1 91 'msg': new_msg,
gremmie@1 92 'options_url': reverse('messages-options'),
gremmie@1 93 })
gremmie@1 94
gremmie@1 95 subject = 'New private message for %s at %s' % (new_msg.receiver.username, site.name)
gremmie@1 96 from_email = settings.GPP_NO_REPLY_EMAIL + '@' + site.domain
gremmie@1 97 send_mail(subject, email_body, from_email, [new_msg.receiver.email])