Example #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, ))
Example #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, ))
Example #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, ))
Example #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, ))
Example #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, ))
Example #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, ))
Example #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, ))
Example #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), ))
Example #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, ))
Example #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, ))
Example #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)
Example #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)
Example #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)
Example #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, ))
Example #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, ))
Example #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, ))
Example #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, ))
Example #18
0
def update_coalition_autogroups():
    """
    Resets the coalition autogroups.
    """
    for coalition in Coalition.objects():
        scheduler.add_job(update_coalition_autogroup, args=(coalition, ))