|
2 | 2 | import random |
3 | 3 |
|
4 | 4 | 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 |
6 | 7 | from django.db.models import Count, Q |
7 | 8 | from django.http import Http404, HttpResponse, HttpResponseRedirect |
8 | 9 | from django.shortcuts import get_object_or_404, render |
@@ -70,15 +71,34 @@ def create(request): |
70 | 71 | with reversion.create_revision(): |
71 | 72 | form.save() |
72 | 73 | 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) |
75 | 75 | return HttpResponseRedirect('/quiz/created') |
76 | 76 | else: |
77 | 77 | form = QuestionForm() |
78 | 78 | return render(request, 'quiz/create.html', |
79 | 79 | {'form': form, 'title': 'Create question'}) |
80 | 80 |
|
81 | 81 |
|
| 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 | + |
82 | 102 | def preview_with_key(request, question_id): |
83 | 103 | key = request.GET.get('preview_key') |
84 | 104 | d = {} |
|
0 commit comments