Skip to content

Commit

Permalink
U 系统设置 - 报警设置 - 自定义邮件服务添加在线测试
Browse files Browse the repository at this point in the history
  • Loading branch information
zyupo committed May 17, 2020
1 parent 6da1371 commit 83098fb
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 4 deletions.
1 change: 1 addition & 0 deletions spug_api/apps/setting/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@
urlpatterns = [
url(r'^$', SettingView.as_view()),
url(r'^ldap_test/$', ldap_test),
url(r'^email_test/$', email_test),
url(r'^about/$', get_about)
]
19 changes: 19 additions & 0 deletions spug_api/apps/setting/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from apps.setting.models import Setting
import platform
import ldap
import smtplib


class SettingView(View):
Expand Down Expand Up @@ -44,6 +45,24 @@ def ldap_test(request):
return json_response(error=error)


def email_test(request):
form, error = JsonParser(
Argument('server'),
Argument('port', type=int),
Argument('username'),
Argument('password'),
).parse(request.body)
if error is None:
try:
server = smtplib.SMTP_SSL(form.server, form.port)
server.login(form.username, form.password)
return json_response()
except Exception as e:
error = e.smtp_error.decode('utf-8')
return json_response(error=error)
return json_response(error=error)


def get_about(request):
return json_response({
'python_version': platform.python_version(),
Expand Down
24 changes: 20 additions & 4 deletions spug_web/src/pages/system/setting/AlarmSetting.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,22 @@ class AlarmSetting extends React.Component {
this.setting = JSON.parse(lds.get(store.settings, 'mail_service.value', "{}"));
this.state = {
mode: this.setting['server'] === undefined ? '1' : '2',
spug_key: lds.get(store.settings, 'spug_key.value', "")
spug_key: lds.get(store.settings, 'spug_key.value', ""),
mail_test_loading: false,
}
}

handleEmailTest = () => {
this.props.form.validateFields((error, data) => {
if (!error) {
this.setState({mail_test_loading: true});
http.post('/api/setting/email_test/', data).then(()=> {
message.success('邮件服务连接成功')
}).finally(()=> this.setState({mail_test_loading: false}))
}
})
};

_doSubmit = (formData) => {
store.loading = true;
http.post('/api/setting/', {data: formData})
Expand Down Expand Up @@ -126,11 +138,15 @@ class AlarmSetting extends React.Component {
</Form.Item>
</div>
</Form.Item>
<div>
<Button
type="danger" loading={this.state.mail_test_loading}
style={{ display: mode === '1' ? 'none' : 'inline-block' ,marginRight: 10}}
onClick={this.handleEmailTest}>测试邮件服务</Button>
<Button
type="primary"
loading={store.loading}
style={{marginTop: 20}}
type="primary" loading={store.loading} style={{ marginTop: 20}}
onClick={this.handleSubmit}>保存设置</Button>
</div>
</Form>
</React.Fragment>
)
Expand Down

0 comments on commit 83098fb

Please sign in to comment.