def send_email(request, cfid): cf = get_object_or_404(CommitFest, pk=cfid) if not request.user.is_staff: raise Http404("Only CF managers can do that.") if request.method == 'POST': authoridstring = request.POST['authors'] revieweridstring = request.POST['reviewers'] form = BulkEmailForm(data=request.POST) if form.is_valid(): q = Q() if authoridstring: q = q | Q(patch_author__in=[int(x) for x in authoridstring.split(',')]) if revieweridstring: q = q | Q(patch_reviewer__in=[int(x) for x in revieweridstring.split(',')]) recipients = User.objects.filter(q).distinct() for r in recipients: send_simple_mail(UserWrapper(request.user).email, r.email, form.cleaned_data['subject'], form.cleaned_data['body'], request.user.username) messages.add_message(request, messages.INFO, "Sent email to %s" % r.email) return HttpResponseRedirect('..') else: authoridstring = request.GET.get('authors', None) revieweridstring = request.GET.get('reviewers', None) form = BulkEmailForm(initial={'authors': authoridstring, 'reviewers': revieweridstring}) if authoridstring: authors = list(User.objects.filter(patch_author__in=[int(x) for x in authoridstring.split(',')]).distinct()) else: authors = [] if revieweridstring: reviewers = list(User.objects.filter(patch_reviewer__in=[int(x) for x in revieweridstring.split(',')]).distinct()) else: reviewers = [] if len(authors)==0 and len(reviewers)==0: messages.add_message(request, messages.WARNING, "No recipients specified, cannot send email") return HttpResponseRedirect('..') messages.add_message(request, messages.INFO, "Email will be sent from: %s" % UserWrapper(request.user).email) def _user_and_mail(u): return "%s %s (%s)" % (u.first_name, u.last_name, u.email) if len(authors): messages.add_message(request, messages.INFO, "The email will be sent to the following authors: %s" % ", ".join([_user_and_mail(u) for u in authors])) if len(reviewers): messages.add_message(request, messages.INFO, "The email will be sent to the following reviewers: %s" % ", ".join([_user_and_mail(u) for u in reviewers])) return render_to_response('base_form.html', { 'cf': cf, 'form': form, 'title': 'Send email', 'breadcrumbs': [{'title': cf.title, 'href': '/%s/' % cf.pk},], 'savebutton': 'Send email', }, context_instance=RequestContext(request))
def delete(self): # We can't compare the object, but we should be able to construct something anyway if self.send_notification: subject = "%s id %s has been deleted by %s" % ( self._meta.verbose_name, self.id, get_current_user()) send_simple_mail(settings.NOTIFICATION_FROM, settings.NOTIFICATION_EMAIL, subject, self.full_text_representation()) # Now call our super to actually delete the object super(PgModel, self).delete()
def save_model(self, request, obj, form, change): if change and self.model.send_notification: # We only do processing if something changed, not when adding # a new object. if request.POST.has_key('new_notification') and request.POST['new_notification']: # Need to send off a new notification. We'll also store # it in the database for future reference, of course. if not obj.org.email: # Should not happen because we remove the form field. Thus # a hard exception is ok. raise Exception("Organization does not have an email, canot send notification!") n = ModerationNotification() n.objecttype = obj.__class__.__name__ n.objectid = obj.id n.text = request.POST['new_notification'] n.author = request.user.username n.save() # Now send an email too msgstr = _get_notification_text(request.POST.has_key('remove_after_notify'), obj, request.POST['new_notification']) send_simple_mail(settings.NOTIFICATION_FROM, obj.org.email, "postgresql.org moderation notification", msgstr) # Also generate a mail to the moderators send_simple_mail(settings.NOTIFICATION_FROM, settings.NOTIFICATION_EMAIL, "Moderation comment on %s %s" % (obj.__class__._meta.verbose_name, obj.id), _get_moderator_notification_text(request.POST.has_key('remove_after_notify'), obj, request.POST['new_notification'], request.user.username )) if request.POST.has_key('remove_after_notify'): # Object should not be saved, it should be deleted obj.delete() return # Either no notifications, or done with notifications super(PgwebAdmin, self).save_model(request, obj, form, change)
def PreSaveHandler(self): """If send_notification is set to True, send a default formatted notification mail""" if not self.send_notification: return (subj, cont) = self._get_changes_texts() if not cont: # If any of these come back as None, it means that nothing actually changed, # or that we don't care to send out notifications about it. return cont = self._build_url() + "\n\n" + cont # Build the mail text send_simple_mail(settings.NOTIFICATION_FROM, settings.NOTIFICATION_EMAIL, "%s by %s" % (subj, get_current_user()), cont)