コード例 #1
0
ファイル: jobs.py プロジェクト: drmhdh/sticker-finder
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.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.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()
コード例 #2
0
ファイル: sticker_set.py プロジェクト: drmhdh/sticker-finder
    def get_or_create(session, name, chat, user):
        """Get or create a new sticker set."""
        name = name.lower()
        sticker_set = session.query(StickerSet).get(name)
        if not sticker_set:
            # Create a task for adding a sticker.
            # This task will be processed by a job, since adding a sticker can take quite a while
            sticker_set = StickerSet(name, None)
            sticker_set.is_default_language = user.is_default_language
            task = Task(Task.SCAN_SET,
                        sticker_set=sticker_set,
                        chat=chat,
                        user=user)
            session.add(sticker_set)
            session.add(task)
            # Error handling: Retry in case somebody sent to stickers at the same time
            try:
                session.commit()
            except IntegrityError as e:
                session.rollback()
                sticker_set = session.query(StickerSet).get(name)
                if sticker_set is None:
                    raise e

        return sticker_set
コード例 #3
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 international 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.international.is_(international)).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.international = international
            session.add(task)

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

            task.changes_to_check = changes
            session.commit()

        session.commit()