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 gremmie@1: gremmie@1: gremmie@1: class ComposeForm(forms.Form): gremmie@1: """ gremmie@1: This form is used to compose private messages. gremmie@1: """ gremmie@1: receiver = forms.CharField(label='To', gremmie@1: max_length=30, gremmie@1: widget=AutoCompleteUserInput()) gremmie@1: subject = forms.CharField(max_length=120, widget=forms.TextInput(attrs={'size': 52})) bgneal@133: message = forms.CharField(widget=forms.Textarea(attrs={'class': 'markItUp smileyTarget'})) gremmie@1: attach_signature = forms.BooleanField(label='Attach Signature?', required=False) gremmie@1: gremmie@1: def __init__(self, user, *args, **kwargs): gremmie@1: forms.Form.__init__(self, *args, **kwargs) gremmie@1: self.user = user gremmie@1: options = Options.objects.for_user(user) gremmie@1: self.fields['attach_signature'].initial = options.attach_signature gremmie@1: gremmie@1: def clean_receiver(self): gremmie@1: receiver = self.cleaned_data['receiver'] gremmie@1: try: gremmie@1: self.rcvr_user = User.objects.get(username=receiver) gremmie@1: except User.DoesNotExist: gremmie@1: raise forms.ValidationError("That username does not exist.") gremmie@1: if self.user.username.lower() == receiver.lower(): gremmie@1: raise forms.ValidationError("You can't send a message to yourself.") gremmie@1: return receiver gremmie@1: gremmie@1: def save(self, sender, parent_msg=None): gremmie@1: receiver = self.rcvr_user gremmie@1: subject = self.cleaned_data['subject'] gremmie@1: message = self.cleaned_data['message'] gremmie@1: attach_signature = self.cleaned_data['attach_signature'] gremmie@1: gremmie@1: new_msg = Message( gremmie@1: sender=sender, gremmie@1: receiver=receiver, gremmie@1: subject=subject, gremmie@1: message=message, gremmie@1: signature_attached=attach_signature, gremmie@1: ) gremmie@1: new_msg.save() gremmie@1: if parent_msg is not None: gremmie@1: parent_msg.reply_date = new_msg.send_date gremmie@1: parent_msg.save() gremmie@1: gremmie@1: receiver_opts = Options.objects.for_user(receiver) gremmie@1: if receiver_opts.notify_email: gremmie@1: notify_receiver(new_msg) gremmie@1: gremmie@1: class Media: gremmie@1: css = { bgneal@133: 'all': (settings.GPP_THIRD_PARTY_CSS['markitup'] + bgneal@133: settings.GPP_THIRD_PARTY_CSS['jquery-ui']) gremmie@1: } bgneal@133: js = (settings.GPP_THIRD_PARTY_JS['markitup'] + bgneal@133: settings.GPP_THIRD_PARTY_JS['jquery-ui']) gremmie@1: gremmie@1: gremmie@1: class OptionsForm(forms.ModelForm): gremmie@1: class Meta: gremmie@1: model = Options gremmie@1: gremmie@1: gremmie@1: def notify_receiver(new_msg): gremmie@1: """ gremmie@1: This function creates the notification email to notify a user of gremmie@1: a new private message. gremmie@1: """ gremmie@1: site = Site.objects.get_current() gremmie@1: gremmie@1: email_body = render_to_string('messages/notification_email.txt', { gremmie@1: 'site': site, gremmie@1: 'msg': new_msg, gremmie@1: 'options_url': reverse('messages-options'), gremmie@1: }) gremmie@1: gremmie@1: subject = 'New private message for %s at %s' % (new_msg.receiver.username, site.name) gremmie@1: from_email = settings.GPP_NO_REPLY_EMAIL + '@' + site.domain gremmie@1: send_mail(subject, email_body, from_email, [new_msg.receiver.email])