def change_platform_name(self, request, *args, **kwargs): platform_info = PlatformInfoModel.get_instance() admin_name = request.data.get('admin_name') admin_name = admin_name if admin_name else platform_info.admin_name console_name = request.data.get('console_name') console_name = console_name if console_name else platform_info.console_name PlatformInfoModel.set_platform_names(admin_name, console_name)
def post(self, request, *args, **kwargs): platform_info = PlatformInfoModel.get_instance() platform_names = { "admin_name": platform_info.admin_name, "console_name": platform_info.console_name } return Response(platform_names)
def __init__(self, request, dict_=None, processors=None, *args, **kwargs): platform_info = PlatformInfoModel.get_instance() platform_names = { "admin_name": platform_info.admin_name, "console_name": platform_info.console_name } common_context = {"platform_names": platform_names} if dict_: dict_.update(common_context) dict_.update(settings.__dict__) super(RequestContext, self).__init__(request, dict_, processors, *args, **kwargs)
def get_all_quota(payload): zone = payload["zone"] owner = payload["owner"] global_quotas = GlobalQuota.mget_by_zone(zone_name=zone) result = {} for global_quota in global_quotas: quota_type = global_quota.quota_type quota = QuotaModel.get_quota( q_type=quota_type, owner=owner, zone=zone ) used = quota.used if quota else 0 user_quota_switch = PlatformInfoModel.get_instance().user_quota_switch # why not create record for this user ? if not quota: QuotaModel.create(quota_type=quota_type, capacity=global_quota.capacity, zone=zone, owner=owner) if user_quota_switch: capacity = quota.capacity if quota else global_quota.capacity else: quotas = QuotaModel.get_quota_list_by_quota_type(quota_type) used_all = 0 for _quota in quotas: used_all += _quota.used capacity = settings.TOTAL_RESOURCES_MAP.get(quota_type, 0) - used_all + used result[quota_type] = { 'used': used, 'capacity': capacity, } # 检查有没有北京分区的数据,没有则添加以适应admin部分 # todo:后期去除 bj_zone = "bj" bj_global_quotas = GlobalQuota.mget_by_zone(zone_name=bj_zone) for bj_global_quota in bj_global_quotas: quota_type = bj_global_quota.quota_type quota = QuotaModel.get_quota( q_type=quota_type, owner=owner, zone=bj_zone ) if not quota: QuotaModel.create(quota_type=quota_type, capacity=bj_global_quota.capacity, zone=bj_zone, owner=owner) return console_response(0, "succ", len(result), result)
def get_quota(payload): """ 获取配额信息: 如果用户本身的配额记录为空得话,使用全局的配额信息 """ zone = payload["zone"] owner = payload["owner"] quota_type = payload["quota_type"] user_quota = QuotaModel.get_quota(zone=zone, q_type=quota_type, owner=owner) used = user_quota.used if user_quota else 0 global_quota = GlobalQuota.get_by_zone_and_type( zone_name=zone, quota_type=quota_type ) if not global_quota: return console_response( QuotaErrorCode.QUOTA_NOT_FOUND, _(u"全局配额记录不存在") ) # why not create record for this user ? if not user_quota: QuotaModel.create(quota_type=quota_type, capacity=global_quota.capacity, zone=zone, owner=owner) user_quota_switch = PlatformInfoModel.get_instance().user_quota_switch if user_quota_switch: capacity = user_quota.capacity if user_quota else global_quota.capacity else: quotas = QuotaModel.get_quota_list_by_quota_type(quota_type) used_all = 0 for _quota in quotas: used_all += _quota.used capacity = settings.TOTAL_RESOURCES_MAP.get(quota_type, 0) - used_all + used ret_data = {"capacity": capacity, "used": used, "quota_type": quota_type} return console_response(0, "succ", len(ret_data), ret_data)
def _add_license(self, payload, **kwargs): license_key = PlatformInfoModel.get_instance().license_key if not license_key: logger.error("license_key is None") payload.update({"license_key": license_key}) return func(self, payload, **kwargs)
def get_user_quota(self, request, *args, **kwargs): switch = PlatformInfoModel.get_user_quota() return JsonResponse(data={'switch': switch})
def set_user_quota(self, request, *args, **kwargs): switch = request.data.get("switch") == "true" PlatformInfoModel.set_user_quota(switch=switch)