def create(request): """ 创建用户 :param request: :return: """ if request.method == "GET": groups = UserGroup.objects.all() return render_to_response("user/user_create.html", locals(), context_instance=RequestContext(request)) elif request.method == 'POST': response = {"success": False, "error": "", "id": None} try: # 获取参数 json_str = request.POST.get('json', None) if not json_str: response['error'] = '必要参数为空!' return HttpResponse(json.dumps(response), mimetype="application/json") json_obj = json.loads(json_str) username = json_obj.get('username', None) password = json_obj.get('password', None) group_str = json_obj.get('group', None) telphone = json_obj.get('telphone', None) email = json_obj.get('email', None) is_staff = json_obj.get('is_staff', None) is_active = json_obj.get('is_active', None) if not username or not password: response['error'] = '必要参数为空!' return HttpResponse(json.dumps(response), mimetype="application/json") # UserGroup group = UserGroup.objects(key=group_str) if not group: response['error'] = '非法用户组!' return HttpResponse(json.dumps(response), mimetype="application/json") group = group[0] # User if User.objects(username=username): response['error'] = '用户名已存在!' return HttpResponse(json.dumps(response), mimetype="application/json") user_obj = User.create_user(username, password, group.key, telphone=telphone, is_staff=is_staff, is_active=is_active, is_superuser=True if group.key == USER_GROUP_ADMIN else False, email=email if email else None) response["id"] = str(user_obj.id) response["success"] = True response["error"] = "执行成功!" return HttpResponse(json.dumps(response), mimetype="application/json") except Exception, e: response["error"] = "系统异常![%s]" % str(e) logger.error(response["error"] + getTraceBack()) return HttpResponse(json.dumps(response), mimetype="application/json")
# user表 if len(User.objects()) == 0 : User.create_user('admin', '123', is_staff=True, is_active=True, is_superuser=True) logger.info('创建admin用户成功!') else: logger.info('已有用户,无需创建admin') except Exception as e: logger.error('程序异常 --> [%s]' % str(e)) # 初始化用户组对象 try: for item_dict in USER_GROUP_LIST: key = item_dict['k'] value = item_dict['v'] if len(UserGroup.objects(key=key)) == 0: group = UserGroup() group.key = key group.value = value group.save() logger.info('初始化用户组成功!') except Exception as e: logger.error('初始化用户组异常![%s]' % e.message) # 初始化菜单对象 try: for menu_item in MENU_LIST: main_menu = menu_item['main'] if not Menu.objects(type=MENU_MAIN, p_key=main_menu[2], c_key=main_menu[3]): Menu.gen_new_menu(main_menu[0], main_menu[1], main_menu[2], main_menu[3], main_menu[4]) logger.info('生成主菜单:[%s][%s][%s][%s][%s]' % (main_menu[0], main_menu[1], main_menu[2], main_menu[3], main_menu[4]))
is_staff=True, is_active=True, is_superuser=True) logger.info('创建admin用户成功!') else: logger.info('已有用户,无需创建admin') except Exception as e: logger.error('程序异常 --> [%s]' % str(e)) # 初始化用户组对象 try: for item_dict in USER_GROUP_LIST: key = item_dict['k'] value = item_dict['v'] if len(UserGroup.objects(key=key)) == 0: group = UserGroup() group.key = key group.value = value group.save() logger.info('初始化用户组成功!') except Exception as e: logger.error('初始化用户组异常![%s]' % e.message) # 初始化菜单对象 try: for menu_item in MENU_LIST: main_menu = menu_item['main'] if not Menu.objects( type=MENU_MAIN, p_key=main_menu[2], c_key=main_menu[3]): Menu.gen_new_menu(main_menu[0], main_menu[1], main_menu[2],
def edit(request): """ 编辑用户 :param request: :return: """ if request.method == "GET": try: # 标记编辑 is_edit = True id = request.GET.get("id", None) if id is None: error = '必要参数为空!' logger.error(error) return render_to_response('item/temp.html', locals(), context_instance=RequestContext(request)) user_obj = User.objects.get(pk=id) groups = UserGroup.objects.all() return render_to_response("user/user_create.html", locals(), context_instance=RequestContext(request)) except Exception as e: error = '编辑用户异常![%s]' % str(e) logger.error(error + getTraceBack()) return render_to_response('item/temp.html', locals(), context_instance=RequestContext(request)) elif request.method == 'POST': response = {"success": False, "error": "", "id": None} try: # 获取参数 json_str = request.POST.get('json', None) if not json_str: response['error'] = '必要参数为空!' return HttpResponse(json.dumps(response), mimetype="application/json") json_obj = json.loads(json_str) user_id = json_obj.get('user_id', None) password = json_obj.get('password', None) group_str = json_obj.get('group', None) email = json_obj.get('email', None) is_staff = json_obj.get('is_staff', None) is_active = json_obj.get('is_active', None) if not user_id or not password: response['error'] = '必要参数为空!' return HttpResponse(json.dumps(response), mimetype="application/json") # UserGroup group = UserGroup.objects(key=group_str) if not group: response['error'] = '非法用户组!' return HttpResponse(json.dumps(response), mimetype="application/json") group = group[0] # User user = User.objects(pk=user_id) if not user: response['error'] = '用户未找到!' return HttpResponse(json.dumps(response), mimetype="application/json") user = user[0] user.password = make_password(password) user.group = group.key user.email = email if email else None user.is_staff = is_staff user.is_active = is_active user.is_superuser = True if group.key == USER_GROUP_ADMIN else False user.save() response["success"] = True response["error"] = "执行成功!" return HttpResponse(json.dumps(response), mimetype="application/json") except Exception, e: response["error"] = "系统异常![%s]" % str(e) logger.error(response["error"] + getTraceBack()) return HttpResponse(json.dumps(response), mimetype="application/json")
def get_group_value(key): return UserGroup.objects(key=key)[0].value