示例#1
0
def index_users_skillpoints():
    """
    Measures all users skillpoints
    """
    for usr in User.objects():
        if has_esi_scope(usr, EsiScope.ESI_SKILLS_READ_SKILLS_V1):
            scheduler.add_job(index_user_skillpoints, args=(usr, ))
示例#2
0
def on_coalition_post_save(_sender: Any, **kwargs):
    """
    Whenever a coalition is saved in the database.
    """
    if kwargs.get("created", False):
        coalition: Coalition = kwargs["document"]
        scheduler.add_job(update_coalition_autogroup, args=(coalition, ))
示例#3
0
def index_users_wallets():
    """
    Indexes user wallet balance
    """
    for usr in User.objects():
        if has_esi_scope(usr, EsiScope.ESI_WALLET_READ_CHARACTER_WALLET_V1):
            scheduler.add_job(index_user_wallets, args=(usr, ))
示例#4
0
def index_users_mails():
    """
    Index all user emails.
    """
    for usr in User.objects():
        if has_esi_scope(usr, EsiScope.ESI_MAIL_READ_MAIL_V1):
            scheduler.add_job(index_user_mails, args=(usr, ))
示例#5
0
def ensure_corporations_members():
    """
    Iterates through all corporations (in the database) and makes sure their
    members exist in the database. See
    :meth:`sni.user.jobs.ensure_corporation_members`.
    """
    for corporation in Corporation.objects(corporation_id__gte=2000000):
        scheduler.add_job(ensure_corporation_members, args=(corporation, ))
示例#6
0
def update_alliance_autogroups():
    """
    Resets all the alliance autogroup. Instead of querying the ESI, it queries
    the database for all user in the corporations in that alliance, assuming
    the user and corporation records are up-to-date.
    """
    for alliance in Alliance.objects():
        scheduler.add_job(update_alliance_autogroup, args=(alliance, ))
示例#7
0
def ensure_alliances_members():
    """
    Iterates through all alliances (in the database) and makes sure their
    member corporations exist in the database. See
    :meth:`sni.user.jobs.ensure_alliance_members`.
    """
    for alliance in Alliance.objects:
        scheduler.add_job(ensure_alliance_members, args=(alliance, ))
示例#8
0
def ensure_corporation_members(corporation: Corporation):
    """
    Ensure that all members of a corporation exist in the database.
    """
    logging.debug("Ensuring members of corporation %s",
                  corporation.corporation_name)
    scope = EsiScope.ESI_CORPORATIONS_READ_CORPORATION_MEMBERSHIP_V1
    result = EsiRefreshToken.objects.aggregate([
        {
            "$lookup": {
                "as": "owner_data",
                "foreignField": "_id",
                "from": "user",
                "localField": "owner",
            },
        },
        {
            "$match": {
                "owner_data.corporation": corporation.pk,
                "scopes": scope.value,
                "valid": True,
            }
        },
        {
            "$project": {
                "owner_data.character_id": 1
            }
        },
    ])

    response = None
    for document in result:
        try:
            character_id = document["owner_data"][0]["character_id"]
            esi_access_token = get_access_token(character_id, scope)
            response = esi_get(
                f"latest/corporations/{corporation.corporation_id}/members/",
                token=esi_access_token.access_token,
            )
        except Exception:
            response = None
        else:
            break

    if response is None:
        logging.debug(
            ("Could not list members of corporation %s (%d): "
             'no refresh token with scope "%s"'),
            corporation.corporation_name,
            corporation.corporation_id,
            scope.value,
        )
        return

    for character_id in response.data:
        scheduler.add_job(ensure_user, args=(int(character_id), ))
示例#9
0
def index_users_location():
    """
    Indexes all user's location
    """
    for usr in User.objects():
        if (has_esi_scope(usr, EsiScope.ESI_LOCATION_READ_LOCATION_V1)
                and has_esi_scope(usr, EsiScope.ESI_LOCATION_READ_ONLINE_V1)
                and has_esi_scope(usr,
                                  EsiScope.ESI_LOCATION_READ_SHIP_TYPE_V1)):
            scheduler.add_job(index_user_location, args=(usr, ))
示例#10
0
def on_user_post_save(_sender: Any, **kwargs):
    """
    Whenever a user is saved in the database.
    """
    if not kwargs.get("created", False):
        usr: User = kwargs["document"]
        if usr.character_id == 0:
            return
        # scheduler.add_job(update_user_from_esi, args=(usr, ))
        scheduler.add_job(update_user_autogroup, args=(usr, ))
示例#11
0
def start_discord_bot() -> None:
    """
    Starts the discord bot (or rather, schedules it to be started), and
    corrects the Discord command and event handlers.
    """
    from sni.scheduler import scheduler
    from sni.discord.bot import start_bot
    import sni.discord.commands
    import sni.discord.events

    scheduler.add_job(start_bot)
示例#12
0
def post_alliance(
        alliance_id: int,
        tkn: Token = Depends(from_authotization_header_nondyn),
):
    """
    Manually fetches an alliance from the ESI. Requires a clearance level of
    8 or more.
    """
    assert_has_clearance(tkn.owner, "sni.fetch_alliance")
    alliance = ensure_alliance(alliance_id)
    scheduler.add_job(ensure_alliance_members, args=(alliance, ))
    return GetAllianceOut.from_record(alliance)
示例#13
0
def post_job(
        callable_name: str,
        tkn: Token = Depends(from_authotization_header_nondyn),
):
    """
    Submits a job to the scheduler. Requires a clearance level of 10.
    """
    assert_has_clearance(tkn.owner, "sni.system.submit_job")
    try:
        function = object_from_name(callable_name)
        job = scheduler.add_job(function)
        return GetJobOut.from_job(job)
    except AttributeError:
        raise HTTPException(status_code=status.HTTP_404_NOT_FOUND)
示例#14
0
def update_alliances_from_esi():
    """
    Updates the alliances properties from the ESI.
    """
    for alliance in Alliance.objects:
        scheduler.add_job(update_alliance_from_esi, args=(alliance, ))
示例#15
0
def update_users_from_esi():
    """
    Iterated through all users and updates their field from ESI.
    """
    for usr in User.objects(character_id__gt=0, clearance_level__gte=0):
        scheduler.add_job(update_user_from_esi, args=(usr, ))
示例#16
0
def update_corporations_from_esi():
    """
    Updates corporations properties. (yes)
    """
    for corporation in Corporation.objects(corporation_id__gte=2000000):
        scheduler.add_job(update_corporation_from_esi, args=(corporation, ))
示例#17
0
def update_corporation_autogroups():
    """
    Resets the corporations autogroups.
    """
    for corporation in Corporation.objects(corporation_id__gte=2000000):
        scheduler.add_job(update_corporation_autogroup, args=(corporation, ))
示例#18
0
def update_coalition_autogroups():
    """
    Resets the coalition autogroups.
    """
    for coalition in Coalition.objects():
        scheduler.add_job(update_coalition_autogroup, args=(coalition, ))