Expose basic way to send test email

most of the mechanics of sending email could already be tested by
sending a "preview" email of any type, or e.g. via Feedback.  but it
seemed like the Configure Email Settings page should have a dedicated
way to test sending
This commit is contained in:
Lance Edgar 2023-05-19 17:16:19 -05:00
parent 05bb3849a2
commit de13e48aa5
2 changed files with 84 additions and 16 deletions

View file

@ -26,30 +26,72 @@
</div>
% if request.has_perm('errors.bogus'):
<h3 class="block is-size-3">Testing</h3>
<div class="block" style="padding-left: 2rem;">
<h3 class="block is-size-3">Testing</h3>
<div class="block" style="padding-left: 2rem;">
<b-field grouped>
<p class="control">
You can raise a "bogus" error to test if/how it generates email:
</p>
<b-field grouped>
<b-field horizontal label="Recipient">
<b-input v-model="testRecipient"></b-input>
</b-field>
<b-button type="is-primary"
@click="sendTest()"
:disabled="sendingTest">
{{ sendingTest ? "Working, please wait..." : "Send Test Email" }}
</b-button>
</b-field>
<div class="level">
<div class="level-left">
<div class="level-item">
<p>You can raise a "bogus" error to test if/how that generates email:</p>
</div>
<div class="level-item">
<b-button type="is-primary"
% if request.has_perm('errors.bogus'):
@click="raiseBogusError()"
:disabled="raisingBogusError">
:disabled="raisingBogusError"
% else:
disabled
title="your permissions do not allow this"
% endif
>
% if request.has_perm('errors.bogus'):
{{ raisingBogusError ? "Working, please wait..." : "Raise Bogus Error" }}
% else:
Raise Bogus Error
% endif
</b-button>
</b-field>
</div>
</div>
</h3>
% endif
</div>
</div>
</%def>
<%def name="modify_this_page_vars()">
${parent.modify_this_page_vars()}
% if request.has_perm('errors.bogus'):
<script type="text/javascript">
<script type="text/javascript">
ThisPageData.testRecipient = ${json.dumps(request.user.email_address)|n}
ThisPageData.sendingTest = false
ThisPage.methods.sendTest = function() {
this.sendingTest = true
let url = '${url('emailprofiles.send_test')}'
let params = {recipient: this.testRecipient}
this.simplePOST(url, params, response => {
this.$buefy.toast.open({
message: "Test email was sent!",
type: 'is-success',
duration: 4000, // 4 seconds
})
this.sendingTest = false
}, response => {
this.sendingTest = false
})
}
% if request.has_perm('errors.bogus'):
ThisPageData.raisingBogusError = false
@ -74,8 +116,8 @@
})
}
</script>
% endif
% endif
</script>
</%def>

View file

@ -297,6 +297,22 @@ class EmailSettingView(MasterView):
'true' if data['hidden'] else 'false')
return {'ok': True}
def send_test(self):
"""
AJAX view for sending a test email.
"""
data = self.request.json_body
recip = data.get('recipient')
if not recip:
return {'error': "Must specify recipient"}
app = self.get_rattail_app()
app.send_email('hello', to=[recip], cc=None, bcc=None,
default_subject="Hello world")
return {'ok': True}
@classmethod
def defaults(cls, config):
cls._email_defaults(config)
@ -318,6 +334,16 @@ class EmailSettingView(MasterView):
permission='{}.configure'.format(permission_prefix),
renderer='json')
# send test
config.add_route('{}.send_test'.format(route_prefix),
'{}/send-test'.format(url_prefix),
request_method='POST')
config.add_view(cls, attr='send_test',
route_name='{}.send_test'.format(route_prefix),
permission='{}.configure'.format(permission_prefix),
renderer='json')
# TODO: deprecate / remove this
ProfilesView = EmailSettingView