annotate messages/forms.py @ 1095:74d7e56a9cfb

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