forked from brosner/django-announcements
/
forms.py
40 lines (32 loc) · 1.29 KB
/
forms.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
from django import forms
from django.contrib.auth.models import User
from django.utils.translation import ugettext_lazy as _
try:
from notification import models as notification
except ImportError:
notification = None
from announcements.models import Announcement
class AnnouncementAdminForm(forms.ModelForm):
"""
A custom form for the admin of the Announcement model. Has an extra field
called send_now that when checked will send out the announcement allowing
the user to decide when that happens.
"""
send_now = forms.BooleanField(required=False,
help_text=_("Tick this box to send out this announcement now."))
class Meta:
model = Announcement
exclude = ("creator", "creation_date")
def save(self, commit=True):
"""
Checks the send_now field in the form and when True sends out the
announcement through notification if present.
"""
announcement = super(AnnouncementAdminForm, self).save(commit)
if self.cleaned_data["send_now"]:
if notification:
users = User.objects.all()
notification.send(users, "announcement", {
"announcement": announcement,
}, on_site=False, queue=True)
return announcement