From beeb2442ad1acddfaf095aacfa98c6d855cf5c73 Mon Sep 17 00:00:00 2001 From: yumaojun <719118794@qq.com> Date: Thu, 7 Jan 2016 15:21:39 +0800 Subject: [PATCH] =?UTF-8?q?=E5=9B=9E=E6=94=B6sudo=E7=94=A8=E6=88=B7?= =?UTF-8?q?=EF=BC=8C=20=E6=B7=BB=E5=8A=A0sudo=E5=88=AB=E5=90=8D=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0=E8=A7=84=E5=88=99=E6=A3=80=E6=9F=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- jperm/views.py | 64 ++++++++++++++++++++++++++++---------------------- 1 file changed, 36 insertions(+), 28 deletions(-) diff --git a/jperm/views.py b/jperm/views.py index 26d6c4468..5e7707394 100644 --- a/jperm/views.py +++ b/jperm/views.py @@ -608,25 +608,28 @@ def perm_sudo_add(request): """ # 渲染数据 header_title, path1, path2 = "Sudo命令", "别名管理", "添加别名" + try: + if request.method == "POST": + # 获取参数: name, comment + name = request.POST.get("sudo_name").strip().upper() + comment = request.POST.get("sudo_comment").strip() + commands = request.POST.get("sudo_commands").strip() - if request.method == "POST": - # 获取参数: name, comment - name = request.POST.get("sudo_name").strip().upper() - comment = request.POST.get("sudo_comment").strip() - commands = request.POST.get("sudo_commands").strip() + if not name or not commands: + raise ServerError(u"sudo name 和 commands是必填项!") - pattern = re.compile(r'[\n,\r]') - commands = ', '.join(list_drop_str(pattern.split(commands), u'')) - logger.debug(u'添加sudo %s: %s' % (name, commands)) - - if get_object(PermSudo, name=name): - error = 'Sudo别名 %s已经存在' % name - else: - sudo = PermSudo(name=name.strip(), comment=comment, commands=commands) - sudo.save() - msg = u"添加Sudo命令别名: %s" % name - # 渲染数据 + pattern = re.compile(r'[\n,\r]') + commands = ', '.join(list_drop_str(pattern.split(commands), u'')) + logger.debug(u'添加sudo %s: %s' % (name, commands)) + if get_object(PermSudo, name=name): + error = 'Sudo别名 %s已经存在' % name + else: + sudo = PermSudo(name=name.strip(), comment=comment, commands=commands) + sudo.save() + msg = u"添加Sudo命令别名: %s" % name + except ServerError, e: + error = e return my_render('jperm/perm_sudo_add.html', locals(), request) @@ -643,22 +646,27 @@ def perm_sudo_edit(request): sudo_id = request.GET.get("id") sudo = PermSudo.objects.get(id=sudo_id) - if request.method == "POST": - name = request.POST.get("sudo_name").upper() - commands = request.POST.get("sudo_commands") - comment = request.POST.get("sudo_comment") + try: + if request.method == "POST": + name = request.POST.get("sudo_name").upper() + commands = request.POST.get("sudo_commands") + comment = request.POST.get("sudo_comment") - pattern = re.compile(r'[\n,\r]') - commands = ', '.join(list_drop_str(pattern.split(commands), u'')).strip() - logger.debug(u'添加sudo %s: %s' % (name, commands)) + if not name or not commands: + raise ServerError(u"sudo name 和 commands是必填项!") - sudo.name = name.strip() - sudo.commands = commands - sudo.comment = comment - sudo.save() + pattern = re.compile(r'[\n,\r]') + commands = ', '.join(list_drop_str(pattern.split(commands), u'')).strip() + logger.debug(u'添加sudo %s: %s' % (name, commands)) - msg = u"更新命令别名: %s" % name + sudo.name = name.strip() + sudo.commands = commands + sudo.comment = comment + sudo.save() + msg = u"更新命令别名: %s" % name + except ServerError, e: + error = e return my_render('jperm/perm_sudo_edit.html', locals(), request)