Exemple #1
0
def check_app_permission_will_expired():
    start = local_now()
    end = start + timedelta(days=3)

    app_perms = ApplicationPermission.objects.filter(
        date_expired__gte=start, date_expired__lte=end).distinct()

    user_app_mapper = defaultdict(set)
    org_perm_mapper = defaultdict(set)

    for app_perm in app_perms:
        org_perm_mapper[app_perm.org].add(app_perm)

        users = app_perm.get_all_users()
        apps = app_perm.applications.all()
        for u in users:
            user_app_mapper[u].update(apps)

    for user, apps in user_app_mapper.items():
        PermedAppsWillExpireUserMsg(user, apps).publish_async()

    for org, perms in org_perm_mapper.items():
        org_admins = org.admins.all()
        for org_admin in org_admins:
            AppPermsWillExpireForOrgAdminMsg(org_admin, perms,
                                             org).publish_async()
Exemple #2
0
def check_asset_permission_will_expired():
    start = local_now()
    end = start + timedelta(days=3)

    user_asset_mapper = defaultdict(set)
    org_perm_mapper = defaultdict(set)

    asset_perms = AssetPermission.objects.filter(
        date_expired__gte=start, date_expired__lte=end).distinct()

    for asset_perm in asset_perms:
        # 资产授权按照组织分类
        org_perm_mapper[asset_perm.org].add(asset_perm)

        # 计算每个用户即将过期的资产
        users = asset_perm.get_all_users()
        assets = asset_perm.get_all_assets()

        for u in users:
            user_asset_mapper[u].update(assets)

    for user, assets in user_asset_mapper.items():
        PermedAssetsWillExpireUserMsg(user, assets).publish_async()

    for org, perms in org_perm_mapper.items():
        org_admins = org.admins.all()
        for org_admin in org_admins:
            AssetPermsWillExpireForOrgAdminMsg(org_admin, perms,
                                               org).publish_async()
Exemple #3
0
def check_asset_permission_expired():
    """
    这里的任务要足够短,不要影响周期任务
    """
    from settings.models import Setting

    setting_name = 'last_asset_perm_expired_check'

    end = local_now()
    default_start = end - timedelta(days=36000)  # Long long ago in china

    defaults = {'value': dt_formatter(default_start)}
    setting, created = Setting.objects.get_or_create(name=setting_name,
                                                     defaults=defaults)
    if created:
        start = default_start
    else:
        start = dt_parser(setting.value)
    setting.value = dt_formatter(end)
    setting.save()

    asset_perm_ids = AssetPermission.objects.filter(
        date_expired__gte=start,
        date_expired__lte=end).distinct().values_list('id', flat=True)
    asset_perm_ids = list(asset_perm_ids)
    logger.info(f'>>> checking {start} to {end} have {asset_perm_ids} expired')
    UserGrantedTreeRefreshController.add_need_refresh_by_asset_perm_ids_cross_orgs(
        asset_perm_ids)
Exemple #4
0
    def get_dingtalk_msg(self) -> dict:
        # 钉钉相同的消息一天只能发一次,所以给所有消息添加基于时间的序号,使他们不相同
        message = self.text_msg['message']
        time = local_now().strftime('%Y-%m-%d %H:%M:%S')
        suffix = '\n{}: {}'.format(_('Time'), time)

        return {
            'subject': self.text_msg['subject'],
            'message': message + suffix
        }
Exemple #5
0
def contains_time_period(time_periods):
    """
    time_periods: [{"id": 1, "value": "00:00~07:30、10:00~13:00"}, {"id": 2, "value": "00:00~00:00"}]
    """
    if not time_periods:
        return False

    current_time = local_now().strftime('%H:%M')
    today_time_period = next(filter(lambda x: str(x['id']) == local_now().strftime("%w"), time_periods))
    today_time_period = today_time_period['value']
    if not today_time_period:
        return False

    for time in today_time_period.split('、'):
        start, end = time.split('~')
        end = "24:00" if end == "00:00" else end
        if start <= current_time <= end:
            return True
    return False
Exemple #6
0
    def mark_msgs_as_read(cls, user_id, msg_ids):
        site_msg_users = SiteMessageUsers.objects.filter(
            user_id=user_id, sitemessage_id__in=msg_ids, has_read=False)

        for site_msg_user in site_msg_users:
            site_msg_user.has_read = True
            site_msg_user.read_at = local_now()

        SiteMessageUsers.objects.bulk_update(site_msg_users,
                                             fields=('has_read', 'read_at'))
Exemple #7
0
def check_asset_permission_will_expired():
    start = local_now()
    end = start + timedelta(days=3)

    user_asset_remain_day_mapper = defaultdict(dict)
    org_perm_remain_day_mapper = defaultdict(dict)

    asset_perms = AssetPermission.objects.filter(
        date_expired__gte=start, date_expired__lte=end).distinct()

    for asset_perm in asset_perms:
        date_expired = dt_parser(asset_perm.date_expired)
        remain_days = (end - date_expired).days

        org = asset_perm.org
        # 资产授权按照组织分类
        if org in org_perm_remain_day_mapper[remain_days]:
            org_perm_remain_day_mapper[remain_days][org].add(asset_perm)
        else:
            org_perm_remain_day_mapper[remain_days][org] = {
                asset_perm,
            }

        # 计算每个用户即将过期的资产
        users = asset_perm.get_all_users()
        assets = asset_perm.get_all_assets()

        for u in users:
            if u in user_asset_remain_day_mapper[remain_days]:
                user_asset_remain_day_mapper[remain_days][u].update(assets)
            else:
                user_asset_remain_day_mapper[remain_days][u] = set(assets)

    for day_count, user_asset_mapper in user_asset_remain_day_mapper.items():
        for user, assets in user_asset_mapper.items():
            PermedAssetsWillExpireUserMsg(user, assets,
                                          day_count).publish_async()

    for day_count, org_perm_mapper in org_perm_remain_day_mapper.items():
        for org, perms in org_perm_mapper.items():
            org_admins = org.admins.all()
            for org_admin in org_admins:
                AssetPermsWillExpireForOrgAdminMsg(org_admin, perms, org,
                                                   day_count).publish_async()
Exemple #8
0
def check_app_permission_will_expired():
    start = local_now()
    end = start + timedelta(days=3)

    app_perms = ApplicationPermission.objects.filter(
        date_expired__gte=start, date_expired__lte=end).distinct()

    user_app_remain_day_mapper = defaultdict(dict)
    org_perm_remain_day_mapper = defaultdict(dict)

    for app_perm in app_perms:
        date_expired = dt_parser(app_perm.date_expired)
        remain_days = (end - date_expired).days

        org = app_perm.org
        if org in org_perm_remain_day_mapper[remain_days]:
            org_perm_remain_day_mapper[remain_days][org].add(app_perm)
        else:
            org_perm_remain_day_mapper[remain_days][org] = {
                app_perm,
            }

        users = app_perm.get_all_users()
        apps = app_perm.applications.all()
        for u in users:
            if u in user_app_remain_day_mapper[remain_days]:
                user_app_remain_day_mapper[remain_days][u].update(apps)
            else:
                user_app_remain_day_mapper[remain_days][u] = set(apps)

    for day_count, user_app_mapper in user_app_remain_day_mapper.items():
        for user, apps in user_app_mapper.items():
            PermedAppsWillExpireUserMsg(user, apps, day_count).publish_async()

    for day_count, org_perm_mapper in org_perm_remain_day_mapper.items():
        for org, perms in org_perm_mapper.items():
            org_admins = org.admins.all()
            for org_admin in org_admins:
                AppPermsWillExpireForOrgAdminMsg(org_admin, perms, org,
                                                 day_count).publish_async()
Exemple #9
0
def create_or_update_celery_periodic_tasks(tasks):
    """
    :param tasks: {
        'add-every-monday-morning': {
            'task': 'tasks.add' # A registered celery task,
            'interval': 30,
            'crontab': "30 7 * * *",
            'args': (16, 16),
            'kwargs': {},
            'enabled': False,
            'description': ''
        },
    }
    :return:
    """
    # Todo: check task valid, task and callback must be a celery task
    for name, detail in tasks.items():
        interval = None
        crontab = None
        last_run_at = None

        try:
            IntervalSchedule.objects.all().count()
        except (ProgrammingError, OperationalError):
            return None

        if isinstance(detail.get("interval"), int):
            kwargs = dict(
                every=detail['interval'],
                period=IntervalSchedule.SECONDS,
            )
            # 不能使用 get_or_create,因为可能会有多个
            interval = IntervalSchedule.objects.filter(**kwargs).first()
            if interval is None:
                interval = IntervalSchedule.objects.create(**kwargs)
            last_run_at = local_now()
        elif isinstance(detail.get("crontab"), str):
            try:
                minute, hour, day, month, week = detail["crontab"].split()
            except ValueError:
                logger.error("crontab is not valid")
                return
            kwargs = dict(
                minute=minute, hour=hour, day_of_week=week,
                day_of_month=day, month_of_year=month, timezone=get_current_timezone()
            )
            crontab = CrontabSchedule.objects.filter(**kwargs).first()
            if crontab is None:
                crontab = CrontabSchedule.objects.create(**kwargs)
        else:
            logger.error("Schedule is not valid")
            return

        defaults = dict(
            interval=interval,
            crontab=crontab,
            name=name,
            task=detail['task'],
            enabled=detail.get('enabled', True),
            args=json.dumps(detail.get('args', [])),
            kwargs=json.dumps(detail.get('kwargs', {})),
            description=detail.get('description') or '',
            last_run_at=last_run_at,
        )
        task = PeriodicTask.objects.update_or_create(
            defaults=defaults, name=name,
        )
        PeriodicTasks.update_changed()
        return task