annotate messages/forms.py @ 985:9b197dbba34b

Fix image_check so it doesn't choke on empty input. Issue #88.
author Brian Neal <bgneal@gmail.com>
date Sun, 25 Oct 2015 13:54:56 -0500
parents 999a71b81111
children 21c592cac71c
rev   line source
gremmie@1 1 """
gremmie@1 2 Forms for the messages application.
bgneal@804 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.widgets import AutoCompleteUserInput
bgneal@814 14 import messages
bgneal@810 15 from messages.models import Flag, Message, Options
gremmie@1 16
gremmie@1 17
bgneal@432 18 # Maximum size of a private message in characters
bgneal@432 19 MESSAGE_MAX = getattr(settings, 'MESSAGES_MAX_SIZE', 8192)
bgneal@432 20
bgneal@432 21
gremmie@1 22 class ComposeForm(forms.Form):
bgneal@432 23 """
bgneal@432 24 This form is used to compose private messages.
bgneal@432 25 """
bgneal@432 26 receiver = forms.CharField(label='To',
bgneal@432 27 max_length=30,
bgneal@432 28 widget=AutoCompleteUserInput())
bgneal@432 29 subject = forms.CharField(max_length=120, widget=forms.TextInput(attrs={'size': 52}))
bgneal@432 30 message = forms.CharField(widget=forms.Textarea(attrs={'class': 'markItUp smileyTarget'}))
bgneal@432 31 attach_signature = forms.BooleanField(label='Attach Signature?', required=False)
bgneal@804 32 parent_id = forms.IntegerField(required=False, widget=forms.HiddenInput)
gremmie@1 33
bgneal@432 34 def __init__(self, user, *args, **kwargs):
bgneal@432 35 forms.Form.__init__(self, *args, **kwargs)
bgneal@432 36 self.user = user
bgneal@432 37 options = Options.objects.for_user(user)
bgneal@432 38 self.fields['attach_signature'].initial = options.attach_signature
gremmie@1 39
bgneal@432 40 def clean_receiver(self):
bgneal@432 41 receiver = self.cleaned_data['receiver']
bgneal@432 42 try:
bgneal@432 43 self.rcvr_user = User.objects.get(username=receiver)
bgneal@432 44 except User.DoesNotExist:
bgneal@432 45 raise forms.ValidationError("That username does not exist.")
bgneal@432 46 if self.user == self.rcvr_user:
bgneal@432 47 raise forms.ValidationError("You can't send a message to yourself.")
bgneal@432 48 return receiver
gremmie@1 49
bgneal@432 50 def clean_message(self):
bgneal@432 51 msg = self.cleaned_data['message']
bgneal@432 52 if len(msg) > MESSAGE_MAX:
bgneal@432 53 raise forms.ValidationError("Your message is too long. Please trim some text.")
bgneal@432 54 return msg
gremmie@1 55
bgneal@436 56 def clean(self):
bgneal@461 57 rcvr = self.cleaned_data.get('receiver')
bgneal@461 58 subject = self.cleaned_data.get('subject')
bgneal@461 59 message = self.cleaned_data.get('message')
bgneal@436 60
bgneal@461 61 if rcvr and subject and message:
bgneal@461 62 # Can we send a message? Is our outbox full?
bgneal@436 63
bgneal@461 64 count = Message.objects.outbox(self.user).count()
bgneal@814 65 if count >= messages.MSG_BOX_LIMIT:
bgneal@461 66 raise forms.ValidationError(
bgneal@461 67 "Your outbox is full. Please delete some messages.")
bgneal@461 68
bgneal@461 69 # Is the receiver's inbox full?
bgneal@461 70 count = Message.objects.inbox(self.rcvr_user).count()
bgneal@814 71 if count >= messages.MSG_BOX_LIMIT:
bgneal@461 72 raise forms.ValidationError(
bgneal@461 73 "Sorry, %s's inbox is full. This message cannot be sent." %
bgneal@461 74 self.rcvr_user.username)
bgneal@436 75
bgneal@436 76 return self.cleaned_data
bgneal@436 77
bgneal@432 78 def save(self, parent_msg=None):
bgneal@432 79 sender = self.user
bgneal@432 80 receiver = self.rcvr_user
bgneal@432 81 subject = self.cleaned_data['subject']
bgneal@432 82 message = self.cleaned_data['message']
bgneal@432 83 attach_signature = self.cleaned_data['attach_signature']
gremmie@1 84
bgneal@432 85 new_msg = Message(
bgneal@432 86 sender=sender,
bgneal@432 87 receiver=receiver,
bgneal@432 88 subject=subject,
bgneal@432 89 message=message,
bgneal@432 90 signature_attached=attach_signature,
bgneal@432 91 )
bgneal@432 92 new_msg.save()
gremmie@1 93
bgneal@804 94 # Update the parent message (if there is one)
bgneal@804 95 parent_id = self.cleaned_data['parent_id']
bgneal@804 96 if parent_id:
bgneal@804 97 try:
bgneal@804 98 parent_msg = Message.objects.get(pk=parent_id)
bgneal@804 99 except Message.DoesNotExist:
bgneal@804 100 parent_msg = None
bgneal@804 101
bgneal@804 102 if parent_msg and parent_msg.receiver == self.user:
bgneal@804 103 parent_msg.reply_date = new_msg.send_date
bgneal@804 104 parent_msg.save()
bgneal@804 105
bgneal@804 106 # Notify recipient
bgneal@432 107 receiver_opts = Options.objects.for_user(receiver)
bgneal@432 108 if receiver_opts.notify_email:
bgneal@432 109 notify_receiver(new_msg)
bgneal@432 110
bgneal@432 111 class Media:
bgneal@432 112 css = {
bgneal@432 113 'all': (settings.GPP_THIRD_PARTY_CSS['markitup'] +
bgneal@432 114 settings.GPP_THIRD_PARTY_CSS['jquery-ui'])
bgneal@432 115 }
bgneal@432 116 js = (settings.GPP_THIRD_PARTY_JS['markitup'] +
bgneal@432 117 settings.GPP_THIRD_PARTY_JS['jquery-ui'])
gremmie@1 118
gremmie@1 119
gremmie@1 120 class OptionsForm(forms.ModelForm):
bgneal@432 121 class Meta:
bgneal@432 122 model = Options
bgneal@741 123 fields = ['attach_signature', 'notify_email']
gremmie@1 124
gremmie@1 125
bgneal@810 126 class ReportForm(forms.ModelForm):
bgneal@810 127 class Meta:
bgneal@810 128 model = Flag
bgneal@810 129 fields = ['comments']
bgneal@810 130 labels = {'comments': ''}
bgneal@810 131 widgets = {
bgneal@810 132 'comments': forms.Textarea(attrs={
bgneal@810 133 'placeholder': 'Enter a comment for the admin (optional).'})
bgneal@810 134 }
bgneal@810 135
bgneal@810 136
gremmie@1 137 def notify_receiver(new_msg):
bgneal@432 138 """
bgneal@432 139 This function creates the notification email to notify a user of
bgneal@432 140 a new private message.
bgneal@432 141 """
bgneal@432 142 site = Site.objects.get_current()
gremmie@1 143
bgneal@432 144 email_body = render_to_string('messages/notification_email.txt', {
bgneal@432 145 'site': site,
bgneal@432 146 'msg': new_msg,
bgneal@808 147 'options_url': reverse('messages-options'),
bgneal@432 148 })
gremmie@1 149
bgneal@432 150 subject = 'New private message for %s at %s' % (new_msg.receiver.username, site.name)
bgneal@432 151 from_email = settings.GPP_NO_REPLY_EMAIL + '@' + site.domain
bgneal@432 152 send_mail(subject, email_body, from_email, [new_msg.receiver.email])