Skip to content

Commit 5925aed

Browse files
committed
Also mail editors when a someone contributes a question
1 parent 088980f commit 5925aed

File tree

1 file changed

+23
-3
lines changed

1 file changed

+23
-3
lines changed

quiz/views.py

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
import random
33

44
from django.contrib.admin.views.decorators import staff_member_required
5-
from django.core.mail import mail_admins
5+
from django.contrib.auth.models import Group
6+
from django.core.mail import mail_admins, send_mail
67
from django.db.models import Count, Q
78
from django.http import Http404, HttpResponse, HttpResponseRedirect
89
from django.shortcuts import get_object_or_404, render
@@ -70,15 +71,34 @@ def create(request):
7071
with reversion.create_revision():
7172
form.save()
7273
reversion.set_comment("User contribution")
73-
mail_admins('Someone made a question!', 'https://' +
74-
request.get_host() + '/admin/quiz/question/?state=NEW')
74+
mail_about_new_question(request)
7575
return HttpResponseRedirect('/quiz/created')
7676
else:
7777
form = QuestionForm()
7878
return render(request, 'quiz/create.html',
7979
{'form': form, 'title': 'Create question'})
8080

8181

82+
def mail_about_new_question(request):
83+
subject = 'Someone made a question!'
84+
body = 'https://' + request.get_host() + '/admin/quiz/question/?state=NEW'
85+
mail_admins(subject, body)
86+
87+
try:
88+
editors = Group.objects.get(name='Editors').user_set.all()
89+
except Group.DoesNotExist:
90+
editors = []
91+
92+
editor_emails = [u.email for u in editors if u.email]
93+
if editor_emails:
94+
send_mail(
95+
subject,
96+
body,
97+
None,
98+
editor_emails,
99+
)
100+
101+
82102
def preview_with_key(request, question_id):
83103
key = request.GET.get('preview_key')
84104
d = {}

0 commit comments

Comments
 (0)