Added Jinja template for subject #837

This commit is contained in:
Donald Zou
2025-09-07 22:12:22 +08:00
parent 1319c28f90
commit 2dce79bb85
3 changed files with 30 additions and 18 deletions

View File

@@ -1291,6 +1291,7 @@ def API_Email_Send():
template = Template(body) template = Template(body)
download = p.downloadPeer() download = p.downloadPeer()
body = template.render(peer=p.toJson(), configurationFile=download) body = template.render(peer=p.toJson(), configurationFile=download)
subject = Template(data.get('Subject', '')).render(peer=p.toJson(), configurationFile=download)
if data.get('IncludeAttachment', False): if data.get('IncludeAttachment', False):
u = str(uuid4()) u = str(uuid4())
attachmentName = f'{u}.conf' attachmentName = f'{u}.conf'
@@ -1298,16 +1299,16 @@ def API_Email_Send():
f.write(download['file']) f.write(download['file'])
s, m = EmailSender.send(data.get('Receiver'), data.get('Subject', ''), body, s, m = EmailSender.send(data.get('Receiver'), subject, body,
data.get('IncludeAttachment', False), (attachmentName if download else '')) data.get('IncludeAttachment', False), (attachmentName if download else ''))
return ResponseObject(s, m) return ResponseObject(s, m)
@app.post(f'{APP_PREFIX}/api/email/previewBody') @app.post(f'{APP_PREFIX}/api/email/preview')
def API_Email_PreviewBody(): def API_Email_PreviewBody():
data = request.get_json() data = request.get_json()
subject = data.get('Subject', '')
body = data.get('Body', '') body = data.get('Body', '')
if len(body) == 0:
return ResponseObject(False, "Nothing to preview")
if ("ConfigurationName" not in data.keys() if ("ConfigurationName" not in data.keys()
or "Peer" not in data.keys() or data.get('ConfigurationName') not in WireguardConfigurations.keys()): or "Peer" not in data.keys() or data.get('ConfigurationName') not in WireguardConfigurations.keys()):
return ResponseObject(False, "Please specify configuration and peer") return ResponseObject(False, "Please specify configuration and peer")
@@ -1320,8 +1321,11 @@ def API_Email_PreviewBody():
try: try:
template = Template(body) template = Template(body)
download = p.downloadPeer() download = p.downloadPeer()
body = template.render(peer=p.toJson(), configurationFile=download) # body = template.render(peer=p.toJson(), configurationFile=download)
return ResponseObject(data=body) return ResponseObject(data={
"Body": Template(body).render(peer=p.toJson(), configurationFile=download),
"Subject": Template(subject).render(peer=p.toJson(), configurationFile=download)
})
except Exception as e: except Exception as e:
return ResponseObject(False, message=str(e)) return ResponseObject(False, message=str(e))

View File

@@ -88,7 +88,7 @@ watch(livePreview, () => {
</div> </div>
<div class="col-6" v-if="livePreview"> <div class="col-6" v-if="livePreview">
<PeerShareWithEmailBodyPreview <PeerShareWithEmailBodyPreview
:body="email.Body" :email="email"
:selectedPeer="selectedPeer" :selectedPeer="selectedPeer"
> >
</PeerShareWithEmailBodyPreview> </PeerShareWithEmailBodyPreview>

View File

@@ -2,26 +2,28 @@
import {fetchPost} from "@/utilities/fetch.js"; import {fetchPost} from "@/utilities/fetch.js";
import {ref, watch} from "vue"; import {ref, watch} from "vue";
import LocaleText from "@/components/text/localeText.vue";
const props = defineProps(['body', 'selectedPeer']) const props = defineProps(['email', 'selectedPeer'])
const preview = ref("") const preview = ref("")
const error = ref(false) const error = ref(false)
const errorMsg = ref("") const errorMsg = ref("")
const getPreview = async () => { const getPreview = async () => {
if (props.body){ if (props.email){
error.value = false; error.value = false;
preview.value = ("")
await fetchPost('/api/email/previewBody', { await fetchPost('/api/email/preview', {
Body: props.body, Subject: props.email.Subject,
Body: props.email.Body,
ConfigurationName: props.selectedPeer.configuration.Name, ConfigurationName: props.selectedPeer.configuration.Name,
Peer: props.selectedPeer.id Peer: props.selectedPeer.id
}, (res) => { }, (res) => {
if (res.status){ if (res.status){
preview.value = res.data; preview.value = res.data;
}else{ }else{
preview.value = ("")
errorMsg.value = res.message errorMsg.value = res.message
} }
error.value = !res.status error.value = !res.status
@@ -32,7 +34,7 @@ const getPreview = async () => {
await getPreview(); await getPreview();
let timeout = undefined let timeout = undefined
watch(() => { watch(() => {
return props.body return props.email
}, async () => { }, async () => {
if (timeout === undefined){ if (timeout === undefined){
timeout = setTimeout(async () => { timeout = setTimeout(async () => {
@@ -44,21 +46,27 @@ watch(() => {
await getPreview(); await getPreview();
}, 500) }, 500)
} }
}, {
deep: true
}) })
</script> </script>
<template> <template>
<div class="card rounded-0 border-start-0 border-bottom-0 bg-body-secondary" style="height: 400px; overflow: scroll"> <div class="card rounded-0 border-start-0 border-bottom-0 bg-body-secondary" style="height: 400px; overflow: scroll">
<div class="card-body"> <div class="card-body">
<div class="alert alert-danger rounded-3" v-if="error && body"> <div class="alert alert-danger rounded-3" v-if="error && email.Body">
<i class="bi bi-exclamation-triangle-fill me-2"></i> <i class="bi bi-exclamation-triangle-fill me-2"></i>
<span class="font-monospace"> <span class="font-monospace">
{{errorMsg}} {{errorMsg}}
</span> </span>
</div> </div>
<div v-if="body" <div>
:class="{'opacity-50': error}" <div v-if="preview">
:innerText="preview"></div> <strong><LocaleText t="Subject"></LocaleText>: </strong>{{ preview.Subject }}
</div>
<hr>
<div :class="{'opacity-50': error}" v-bind:innerText="preview.Body"></div>
</div>
</div> </div>
</div> </div>
</template> </template>