Beispiel #1
0
def maintenance_job(context, session):
    """Create new maintenance tasks.

    - Check for stickers to ban (via Report)
    - Check for users to be checked
    """
    tasks = []
    # Get all StickerSets with a report and no existing Task
    report_count = func.count(Report.id).label('report_count')
    report_candidates = session.query(StickerSet, report_count) \
        .join(StickerSet.reports) \
        .outerjoin(Task, and_(
            StickerSet.name == Task.sticker_set_name,
            Task.type == Task.REPORT,
        )) \
        .filter(Task.id.is_(None)) \
        .filter(StickerSet.banned.is_(False)) \
        .group_by(StickerSet) \
        .having(report_count >= config['job']['report_count']) \
        .all()

    for (sticker_set, _) in report_candidates:
        task = Task(Task.REPORT, sticker_set=sticker_set)
        tasks.append(task)
        session.add(task)

    # Get all users which tagged more than the configurated amount of stickers since the last user check.
    for is_default_language in [True, False]:
        change_count = func.count(Change.id).label('change_count')
        user_check_candidates = session.query(User, change_count) \
            .join(User.changes) \
            .outerjoin(Change.check_task) \
            .filter(Task.id.is_(None)) \
            .filter(Change.is_default_language.is_(is_default_language)) \
            .group_by(User) \
            .having(change_count >= config['job']['user_check_count']) \
            .all()

        for (user, _) in user_check_candidates:
            task = Task(Task.CHECK_USER_TAGS, user=user)
            task.is_default_language = is_default_language
            session.add(task)

            changes = session.query(Change) \
                .filter(Change.check_task_id.is_(None)) \
                .filter(Change.user_id == user.id) \
                .filter(Change.is_default_language.is_(is_default_language)) \
                .all()

            task.changes_to_check = changes
            session.commit()

        session.commit()
Beispiel #2
0
def maintenance_job(bot, job, session, user):
    """Create new maintenance tasks.

    - Check for stickers to ban (via VoteBan)
    - Check for users to be checked
    """
    tasks = []
    # Get all StickerSets with at least 5 vote bans and no existing Task
    vote_ban_count = func.count(VoteBan.id).label('vote_ban_count')
    vote_ban_candidates = session.query(StickerSet, vote_ban_count) \
        .join(StickerSet.vote_bans) \
        .outerjoin(Task, and_(
            StickerSet.name == Task.sticker_set_name,
            Task.type == Task.VOTE_BAN,
        )) \
        .filter(Task.id.is_(None)) \
        .filter(StickerSet.banned.is_(False)) \
        .group_by(StickerSet) \
        .having(vote_ban_count >= config.VOTE_BAN_COUNT) \
        .all()

    for (sticker_set, _) in vote_ban_candidates:
        task = Task(Task.VOTE_BAN, sticker_set=sticker_set)
        tasks.append(task)
        session.add(task)

    # Get all users which tagged more than the configurated amount of stickers since the last user check.
    for is_default_language in [True, False]:
        change_count = func.count(Change.id).label('change_count')
        user_check_candidates = session.query(User, change_count) \
            .join(User.changes) \
            .outerjoin(Change.check_task) \
            .filter(Task.id.is_(None)) \
            .filter(Change.is_default_language.is_(is_default_language)) \
            .group_by(User) \
            .having(change_count >= config.USER_CHECK_COUNT) \
            .all()

        for (user, _) in user_check_candidates:
            task = Task(Task.CHECK_USER_TAGS, user=user)
            task.is_default_language = is_default_language
            session.add(task)
            session.commit()
            session.query(Change) \
                .filter(Change.check_task_id.is_(None)) \
                .filter(Change.user_id == user.id) \
                .filter(Change.is_default_language.is_(is_default_language)) \
                .update({'check_task_id': task.id}, synchronize_session='fetch')

        session.commit()