def post(self, request): form, error = JsonParser( Argument('username', help='请输入登录名'), Argument('password', help='请输入密码'), ).parse(request.body) if error is None: if User.objects.filter(username=form.username).exists(): return json_response(error=f'已存在登录名为【{form.username}】的用户') form.password_hash = User.make_password(form.pop('password')) # form.created_by = request.user User.objects.create(**form) return json_response(error=error)
def get_alarm(request): form, error = JsonParser( Argument('type', required=False), Argument('name', required=False) ).parse(request.GET, True) if error is None: now = datetime.now() data = {human_date(now - timedelta(days=x + 1)): 0 for x in range(14)} for alarm in Alarm.objects.filter(status='1', created_at__gt=human_date(now - timedelta(days=14)), **form): date = alarm.created_at[:10] if date in data: data[date] += 1 data = [{'date': k, 'value': v} for k, v in data.items()] return json_response(data) return json_response(error=error)
def get_request(request): form, error = JsonParser( Argument('duration', type=list, help='参数错误') ).parse(request.body) if error is None: s_date = form.duration[0] e_date = (parse_time(form.duration[1]) + timedelta(days=1)).strftime('%Y-%m-%d') data = {x.id: {'name': x.name, 'count': 0} for x in App.objects.all()} for req in DeployRequest.objects.filter(created_at__gt=s_date, created_at__lt=e_date): data[req.deploy.app_id]['count'] += 1 data = sorted(data.values(), key=lambda x: x['count'], reverse=True)[:10] return json_response(data) return json_response(error=error)
def delete(self, request): form, error = JsonParser(Argument('id', type=int, help='请指定操作对象')).parse(request.GET) if error is None: user = User.objects.filter(pk=form.id).first() if user: if user.type == 'ldap': return json_response(error='ldap账户无法删除,请使用禁用功能来禁止该账户访问系统') user.role_id = None user.deleted_at = human_datetime() user.deleted_by = request.user user.save() return json_response(error=error)
def patch(self, request): data = dict() form, error = JsonParser( Argument('ip', required=True), Argument('network', required=True), Argument('type', required=True), Argument('mac', required=True), ).parse(request.body, True) if error is None: # 校验mac地址 if is_valid_mac(form.mac): mac = form.mac.upper() ip_detail_info = IpDetailsInfo.objects.filter( ip=form.ip).first() if ip_detail_info: if ip_detail_info.query_mac and mac != ip_detail_info.query_mac: IpDetailsInfo.objects.filter(ip=form.ip).update( ip_type=2, manual_mac=mac, ) else: IpDetailsInfo.objects.filter(ip=form.ip).update( ip_type=1, manual_mac=mac, ) else: IpDetailsInfo.objects.create( ip=form.ip, network=form.network, manual_mac=mac, ip_type=1, ) data["status"] = "success" else: data["status"] = "fail" return json_response(data) return json_response(error=error)
def login(request): form, error = JsonParser( Argument('username', help='请输入用户名'), Argument('password', help='请输入密码'), ).parse(request.body) if error is None: x_real_ip = request.META.get('REMOTE_ADDR', '') user = User.objects.filter(username=form.username).first() if user and not user.is_active: return json_response(error="账户已被系统禁用") else: if user: if user.verify_password(form.password): return handle_user_info(user, x_real_ip) value = cache.get_or_set(form.username, 0, 86400) if value >= 3: if user and user.is_active: user.is_active = False user.save() return json_response(error='账户已被系统禁用') cache.set(form.username, value + 1, 86400) return json_response(error="用户名或密码错误,连续多次错误账户将会被禁用") return json_response(error=error)
def patch(self, request): form, error = JsonParser( Argument('id', type=int, help='请指定操作对象'), Argument('username', required=False), Argument('password', required=False), Argument('nickname', required=False), Argument('role_id', required=False), Argument('is_active', type=bool, required=False), ).parse(request.body, True) if error is None: if form.get('password'): form.token_expired = 0 form.password_hash = User.make_password(form.pop('password')) User.objects.filter(pk=form.pop('id')).update(**form) return json_response(error=error)