gremmie@1: """ gremmie@1: Forms for the messages application. gremmie@1: """ gremmie@1: gremmie@1: from django import forms gremmie@1: from django.contrib.auth.models import User gremmie@1: from django.conf import settings gremmie@1: from django.contrib.sites.models import Site gremmie@1: from django.core.urlresolvers import reverse gremmie@1: from django.template.loader import render_to_string gremmie@1: gremmie@1: from core.functions import send_mail gremmie@1: from core.functions import get_full_name gremmie@1: from core.widgets import AutoCompleteUserInput gremmie@1: from messages.models import Message gremmie@1: from messages.models import Options bgneal@436: from messages import MSG_BOX_LIMIT gremmie@1: gremmie@1: bgneal@432: # Maximum size of a private message in characters bgneal@432: MESSAGE_MAX = getattr(settings, 'MESSAGES_MAX_SIZE', 8192) bgneal@432: bgneal@432: gremmie@1: class ComposeForm(forms.Form): bgneal@432: """ bgneal@432: This form is used to compose private messages. bgneal@432: """ bgneal@432: receiver = forms.CharField(label='To', bgneal@432: max_length=30, bgneal@432: widget=AutoCompleteUserInput()) bgneal@432: subject = forms.CharField(max_length=120, widget=forms.TextInput(attrs={'size': 52})) bgneal@432: message = forms.CharField(widget=forms.Textarea(attrs={'class': 'markItUp smileyTarget'})) bgneal@432: attach_signature = forms.BooleanField(label='Attach Signature?', required=False) gremmie@1: bgneal@432: def __init__(self, user, *args, **kwargs): bgneal@432: forms.Form.__init__(self, *args, **kwargs) bgneal@432: self.user = user bgneal@432: options = Options.objects.for_user(user) bgneal@432: self.fields['attach_signature'].initial = options.attach_signature gremmie@1: bgneal@432: def clean_receiver(self): bgneal@432: receiver = self.cleaned_data['receiver'] bgneal@432: try: bgneal@432: self.rcvr_user = User.objects.get(username=receiver) bgneal@432: except User.DoesNotExist: bgneal@432: raise forms.ValidationError("That username does not exist.") bgneal@432: if self.user == self.rcvr_user: bgneal@432: raise forms.ValidationError("You can't send a message to yourself.") bgneal@432: return receiver gremmie@1: bgneal@432: def clean_message(self): bgneal@432: msg = self.cleaned_data['message'] bgneal@432: if len(msg) > MESSAGE_MAX: bgneal@432: raise forms.ValidationError("Your message is too long. Please trim some text.") bgneal@432: return msg gremmie@1: bgneal@436: def clean(self): bgneal@436: # Can we send a message? Is our outbox full? bgneal@436: bgneal@436: count = Message.objects.outbox(self.user).count() bgneal@436: if count >= MSG_BOX_LIMIT: bgneal@436: raise forms.ValidationError("Your outbox is full. Please delete some messages.") bgneal@436: bgneal@436: # Is the receiver's inbox full? bgneal@436: count = Message.objects.inbox(self.rcvr_user).count() bgneal@436: if count >= MSG_BOX_LIMIT: bgneal@436: raise forms.ValidationError( bgneal@436: "Sorry, %s's inbox is full. This message cannot be sent." % self.rcvr_user.username) bgneal@436: bgneal@436: return self.cleaned_data bgneal@436: bgneal@432: def save(self, parent_msg=None): bgneal@432: sender = self.user bgneal@432: receiver = self.rcvr_user bgneal@432: subject = self.cleaned_data['subject'] bgneal@432: message = self.cleaned_data['message'] bgneal@432: attach_signature = self.cleaned_data['attach_signature'] gremmie@1: bgneal@432: new_msg = Message( bgneal@432: sender=sender, bgneal@432: receiver=receiver, bgneal@432: subject=subject, bgneal@432: message=message, bgneal@432: signature_attached=attach_signature, bgneal@432: ) bgneal@432: new_msg.save() bgneal@432: if parent_msg is not None: bgneal@432: parent_msg.reply_date = new_msg.send_date bgneal@432: parent_msg.save() gremmie@1: bgneal@432: receiver_opts = Options.objects.for_user(receiver) bgneal@432: if receiver_opts.notify_email: bgneal@432: notify_receiver(new_msg) bgneal@432: bgneal@432: class Media: bgneal@432: css = { bgneal@432: 'all': (settings.GPP_THIRD_PARTY_CSS['markitup'] + bgneal@432: settings.GPP_THIRD_PARTY_CSS['jquery-ui']) bgneal@432: } bgneal@432: js = (settings.GPP_THIRD_PARTY_JS['markitup'] + bgneal@432: settings.GPP_THIRD_PARTY_JS['jquery-ui']) gremmie@1: gremmie@1: gremmie@1: class OptionsForm(forms.ModelForm): bgneal@432: class Meta: bgneal@432: model = Options gremmie@1: gremmie@1: gremmie@1: def notify_receiver(new_msg): bgneal@432: """ bgneal@432: This function creates the notification email to notify a user of bgneal@432: a new private message. bgneal@432: """ bgneal@432: site = Site.objects.get_current() gremmie@1: bgneal@432: email_body = render_to_string('messages/notification_email.txt', { bgneal@432: 'site': site, bgneal@432: 'msg': new_msg, bgneal@441: 'options_url': reverse('messages-options_tab'), bgneal@432: }) gremmie@1: bgneal@432: subject = 'New private message for %s at %s' % (new_msg.receiver.username, site.name) bgneal@432: from_email = settings.GPP_NO_REPLY_EMAIL + '@' + site.domain bgneal@432: send_mail(subject, email_body, from_email, [new_msg.receiver.email])