annotate gpp/messages/forms.py @ 6:b6263ac72052

Use DRY principle to manage third party javascript libraries. Created script_tags template tags to generate the correct link and script tags for 3rd party libraries. The settings.py file is the only place where the full path name is specified.
author Brian Neal <bgneal@gmail.com>
date Sat, 11 Apr 2009 22:50:56 +0000
parents dbd703f7d63a
children c515b7401078
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 """
gremmie@1 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}))
gremmie@1 27 message = forms.CharField(widget=forms.Textarea(attrs={'rows': 20, 'cols': 80}))
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@6 70 'all': settings.GPP_THIRD_PARTY_CSS['markitup'],
gremmie@1 71 }
bgneal@6 72 js = ('js/messages/compose.js', ) + settings.GPP_THIRD_PARTY_JS['markitup']
gremmie@1 73
gremmie@1 74
gremmie@1 75 class OptionsForm(forms.ModelForm):
gremmie@1 76 class Meta:
gremmie@1 77 model = Options
gremmie@1 78
gremmie@1 79
gremmie@1 80 def notify_receiver(new_msg):
gremmie@1 81 """
gremmie@1 82 This function creates the notification email to notify a user of
gremmie@1 83 a new private message.
gremmie@1 84 """
gremmie@1 85 site = Site.objects.get_current()
gremmie@1 86
gremmie@1 87 email_body = render_to_string('messages/notification_email.txt', {
gremmie@1 88 'site': site,
gremmie@1 89 'msg': new_msg,
gremmie@1 90 'options_url': reverse('messages-options'),
gremmie@1 91 })
gremmie@1 92
gremmie@1 93 subject = 'New private message for %s at %s' % (new_msg.receiver.username, site.name)
gremmie@1 94 from_email = settings.GPP_NO_REPLY_EMAIL + '@' + site.domain
gremmie@1 95 send_mail(subject, email_body, from_email, [new_msg.receiver.email])