Mercurial > public > sg101
comparison gpp/messages/forms.py @ 1:dbd703f7d63a
Initial import of sg101 stuff from private repository.
author | gremmie |
---|---|
date | Mon, 06 Apr 2009 02:43:12 +0000 |
parents | |
children | b6263ac72052 |
comparison
equal
deleted
inserted
replaced
0:900ba3c7b765 | 1:dbd703f7d63a |
---|---|
1 """ | |
2 Forms for the messages application. | |
3 """ | |
4 | |
5 from django import forms | |
6 from django.contrib.auth.models import User | |
7 from django.conf import settings | |
8 from django.contrib.sites.models import Site | |
9 from django.core.urlresolvers import reverse | |
10 from django.template.loader import render_to_string | |
11 | |
12 from core.functions import send_mail | |
13 from core.functions import get_full_name | |
14 from core.widgets import AutoCompleteUserInput | |
15 from messages.models import Message | |
16 from messages.models import Options | |
17 | |
18 | |
19 class ComposeForm(forms.Form): | |
20 """ | |
21 This form is used to compose private messages. | |
22 """ | |
23 receiver = forms.CharField(label='To', | |
24 max_length=30, | |
25 widget=AutoCompleteUserInput()) | |
26 subject = forms.CharField(max_length=120, widget=forms.TextInput(attrs={'size': 52})) | |
27 message = forms.CharField(widget=forms.Textarea(attrs={'rows': 20, 'cols': 80})) | |
28 attach_signature = forms.BooleanField(label='Attach Signature?', required=False) | |
29 | |
30 def __init__(self, user, *args, **kwargs): | |
31 forms.Form.__init__(self, *args, **kwargs) | |
32 self.user = user | |
33 options = Options.objects.for_user(user) | |
34 self.fields['attach_signature'].initial = options.attach_signature | |
35 | |
36 def clean_receiver(self): | |
37 receiver = self.cleaned_data['receiver'] | |
38 try: | |
39 self.rcvr_user = User.objects.get(username=receiver) | |
40 except User.DoesNotExist: | |
41 raise forms.ValidationError("That username does not exist.") | |
42 if self.user.username.lower() == receiver.lower(): | |
43 raise forms.ValidationError("You can't send a message to yourself.") | |
44 return receiver | |
45 | |
46 def save(self, sender, parent_msg=None): | |
47 receiver = self.rcvr_user | |
48 subject = self.cleaned_data['subject'] | |
49 message = self.cleaned_data['message'] | |
50 attach_signature = self.cleaned_data['attach_signature'] | |
51 | |
52 new_msg = Message( | |
53 sender=sender, | |
54 receiver=receiver, | |
55 subject=subject, | |
56 message=message, | |
57 signature_attached=attach_signature, | |
58 ) | |
59 new_msg.save() | |
60 if parent_msg is not None: | |
61 parent_msg.reply_date = new_msg.send_date | |
62 parent_msg.save() | |
63 | |
64 receiver_opts = Options.objects.for_user(receiver) | |
65 if receiver_opts.notify_email: | |
66 notify_receiver(new_msg) | |
67 | |
68 class Media: | |
69 css = { | |
70 'all': ('js/markitup/skins/markitup/style.css', | |
71 'js/markitup/sets/markdown/style.css') | |
72 } | |
73 js = ( | |
74 'js/messages/compose.js', | |
75 'js/markitup/jquery.markitup.pack.js', | |
76 'js/markitup/sets/markdown/set.js', | |
77 ) | |
78 | |
79 | |
80 class OptionsForm(forms.ModelForm): | |
81 class Meta: | |
82 model = Options | |
83 | |
84 | |
85 def notify_receiver(new_msg): | |
86 """ | |
87 This function creates the notification email to notify a user of | |
88 a new private message. | |
89 """ | |
90 site = Site.objects.get_current() | |
91 | |
92 email_body = render_to_string('messages/notification_email.txt', { | |
93 'site': site, | |
94 'msg': new_msg, | |
95 'options_url': reverse('messages-options'), | |
96 }) | |
97 | |
98 subject = 'New private message for %s at %s' % (new_msg.receiver.username, site.name) | |
99 from_email = settings.GPP_NO_REPLY_EMAIL + '@' + site.domain | |
100 send_mail(subject, email_body, from_email, [new_msg.receiver.email]) |