2020-01-15 12:48:22 +00:00
|
|
|
# Copyright: (c) OpenSpug Organization. https://github.com/openspug/spug
|
|
|
|
# Copyright: (c) <spug.dev@gmail.com>
|
|
|
|
# Released under the MIT License.
|
2020-03-20 07:17:55 +00:00
|
|
|
import django
|
2019-12-06 06:02:22 +00:00
|
|
|
from django.views.generic import View
|
2020-03-20 07:17:55 +00:00
|
|
|
from django.conf import settings
|
2019-12-06 06:02:22 +00:00
|
|
|
from libs import JsonParser, Argument, json_response
|
|
|
|
from apps.setting.utils import AppSetting
|
|
|
|
from apps.setting.models import Setting
|
2020-03-20 07:17:55 +00:00
|
|
|
import platform
|
2020-03-08 13:06:38 +00:00
|
|
|
import ldap
|
2020-05-17 05:16:45 +00:00
|
|
|
import smtplib
|
2019-12-06 06:02:22 +00:00
|
|
|
|
|
|
|
|
|
|
|
class SettingView(View):
|
|
|
|
def get(self, request):
|
2020-05-28 09:15:51 +00:00
|
|
|
data = Setting.objects.all()
|
|
|
|
return json_response(data)
|
2019-12-06 06:02:22 +00:00
|
|
|
|
|
|
|
def post(self, request):
|
|
|
|
form, error = JsonParser(
|
|
|
|
Argument('data', type=list, help='缺少必要的参数')
|
|
|
|
).parse(request.body)
|
|
|
|
if error is None:
|
|
|
|
for item in form.data:
|
|
|
|
AppSetting.set(**item)
|
|
|
|
return json_response(error=error)
|
2020-03-08 13:06:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
def ldap_test(request):
|
|
|
|
form, error = JsonParser(
|
|
|
|
Argument('server'),
|
|
|
|
Argument('port', type=int),
|
|
|
|
Argument('admin_dn'),
|
|
|
|
Argument('password'),
|
|
|
|
).parse(request.body)
|
|
|
|
if error is None:
|
|
|
|
try:
|
|
|
|
con = ldap.initialize("ldap://{0}:{1}".format(form.server, form.port), bytes_mode=False)
|
|
|
|
con.simple_bind_s(form.admin_dn, form.password)
|
|
|
|
return json_response()
|
|
|
|
except Exception as e:
|
|
|
|
error = eval(str(e))
|
|
|
|
return json_response(error=error['desc'])
|
|
|
|
return json_response(error=error)
|
|
|
|
|
|
|
|
|
2020-05-17 05:16:45 +00:00
|
|
|
def email_test(request):
|
|
|
|
form, error = JsonParser(
|
2020-05-18 06:44:31 +00:00
|
|
|
Argument('server', help='请输入邮件服务地址'),
|
|
|
|
Argument('port', type=int, help='请输入邮件服务端口号'),
|
|
|
|
Argument('username', help='请输入邮箱账号'),
|
|
|
|
Argument('password', help='请输入密码/授权码'),
|
2020-05-17 05:16:45 +00:00
|
|
|
).parse(request.body)
|
|
|
|
if error is None:
|
|
|
|
try:
|
2020-05-18 06:44:31 +00:00
|
|
|
if form.port == 465:
|
|
|
|
server = smtplib.SMTP_SSL(form.server, form.port)
|
|
|
|
else:
|
|
|
|
server = smtplib.SMTP(form.server, form.port)
|
2020-05-17 05:16:45 +00:00
|
|
|
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)
|
|
|
|
|
|
|
|
|
2020-03-20 07:17:55 +00:00
|
|
|
def get_about(request):
|
|
|
|
return json_response({
|
|
|
|
'python_version': platform.python_version(),
|
|
|
|
'system_version': platform.platform(),
|
|
|
|
'spug_version': settings.SPUG_VERSION,
|
|
|
|
'django_version': django.get_version()
|
|
|
|
})
|
|
|
|
|
2020-03-08 13:06:38 +00:00
|
|
|
|
|
|
|
|