Пример #1
0
def link_create_esi(request: WSGIRequest, token, fatlink_hash: str):
    """
    helper: create ESI link
    :param request:
    :param token:
    :param fatlink_hash:
    :return:
    """

    # Check if there is a fleet
    try:
        required_scopes = ["esi-fleets.read_fleet.v1"]
        esi_token = Token.get_token(token.character_id, required_scopes)

        fleet_from_esi = esi.client.Fleets.get_characters_character_id_fleet(
            character_id=token.character_id, token=esi_token.valid_access_token()
        ).result()
    except Exception:
        # Not in a fleet
        request.session["msg"] = [
            "warning",
            "To use the ESI function, you neeed to be in fleet and you need to be "
            "the fleet boss! You can create a clickable FAT link and share it, "
            "if you like.",
        ]

        # return to "Add FAT Link" view
        return redirect("afat:link_add")

    # Check if this character already has a fleet
    creator_character = EveCharacter.objects.get(character_id=token.character_id)
    registered_fleets_for_creator = AFatLink.objects.filter(
        is_esilink=True,
        is_registered_on_esi=True,
        character__character_name=creator_character.character_name,
    )

    fleet_already_registered = False
    character_has_registered_fleets = False
    registered_fleets_to_close = list()

    if registered_fleets_for_creator.count() > 0:
        character_has_registered_fleets = True

        for registered_fleet in registered_fleets_for_creator:
            if registered_fleet.esi_fleet_id == fleet_from_esi["fleet_id"]:
                # Character already has a fleet
                fleet_already_registered = True
            else:
                registered_fleets_to_close.append(
                    {"registered_fleet": registered_fleet}
                )

    if fleet_already_registered is True:
        request.session["msg"] = [
            "warning",
            "Fleet with ID {fleet_id} for your character {character_name} "
            "has already been registered and pilots joining this "
            "fleet are automatically tracked.".format(
                fleet_id=fleet_from_esi["fleet_id"],
                character_name=creator_character.character_name,
            ),
        ]

        # return to "Add FAT Link" view
        return redirect("afat:link_add")

    # remove all former registered fleets if there are any
    if (
        character_has_registered_fleets is True
        and fleet_already_registered is False
        and len(registered_fleets_to_close) > 0
    ):
        for registered_fleet_to_close in registered_fleets_to_close:
            logger.info(
                "Closing ESI FAT link with hash {fatlink_hash}. Reason: {reason}".format(
                    fatlink_hash=registered_fleet_to_close["registered_fleet"].hash,
                    reason=(
                        "FC has opened a new fleet with the character {character}"
                    ).format(character=creator_character.character_name),
                )
            )

            registered_fleet_to_close["registered_fleet"].is_registered_on_esi = False
            registered_fleet_to_close["registered_fleet"].save()

    # Check if we deal with the fleet boss here
    try:
        esi_fleet_member = esi.client.Fleets.get_fleets_fleet_id_members(
            fleet_id=fleet_from_esi["fleet_id"],
            token=esi_token.valid_access_token(),
        ).result()
    except Exception:
        request.session["msg"] = [
            "warning",
            "Not Fleet Boss! Only the fleet boss can utilize the ESI function. "
            "You can create a clickable FAT link and share it, if you like.",
        ]

        # return to "Add FAT Link" view
        return redirect("afat:link_add")

    creator_character = EveCharacter.objects.get(character_id=token.character_id)

    # create the fatlink
    fatlink = AFatLink(
        afattime=timezone.now(),
        fleet=request.session["fatlink_form__name"],
        creator=request.user,
        character=creator_character,
        hash=fatlink_hash,
        is_esilink=True,
        is_registered_on_esi=True,
        esi_fleet_id=fleet_from_esi["fleet_id"],
    )

    # add fleet type if there is any
    if (
        request.session["fatlink_form__type"] is not None
        and request.session["fatlink_form__type"] != -1
    ):
        fatlink.link_type = AFatLinkType.objects.get(
            id=request.session["fatlink_form__type"]
        )

    # save it
    fatlink.save()

    # clear session
    del request.session["fatlink_form__name"]
    del request.session["fatlink_form__type"]

    # process fleet members
    process_fats.delay(
        data_list=esi_fleet_member, data_source="esi", fatlink_hash=fatlink_hash
    )

    request.session[
        "{fatlink_hash}-creation-code".format(fatlink_hash=fatlink_hash)
    ] = 200

    logger.info(
        "ESI FAT link {fatlink_hash} created by {user}".format(
            fatlink_hash=fatlink_hash, user=request.user
        )
    )

    return redirect("afat:link_edit", fatlink_hash=fatlink_hash)
Пример #2
0
def create_clickable_fatlink(request: WSGIRequest, ) -> HttpResponseRedirect:
    """
    create clickable fat link
    :param request:
    :type request:
    :return:
    :rtype:
    """

    if request.method == "POST":
        form = AFatClickFatForm(request.POST)

        if form.is_valid():
            fatlink_hash = get_random_string(length=30)

            fatlink = AFatLink()
            fatlink.fleet = form.cleaned_data["name"]

            if form.cleaned_data["type"] is not None:
                fatlink.link_type = form.cleaned_data["type"]

            fatlink.creator = request.user
            fatlink.hash = fatlink_hash
            fatlink.afattime = timezone.now()
            fatlink.save()

            dur = ClickAFatDuration()
            dur.fleet = AFatLink.objects.get(hash=fatlink_hash)
            dur.duration = form.cleaned_data["duration"]
            dur.save()

            request.session["{fatlink_hash}-creation-code".format(
                fatlink_hash=fatlink_hash)] = 201

            # writing DB log
            fleet_type = ""
            if fatlink.link_type:
                fleet_type = f" (Fleet Type: {fatlink.link_type.name})"

            write_log(
                request=request,
                log_event=AFatLogEvent.CREATE_FATLINK,
                log_text=
                (f'FAT link with name "{form.cleaned_data["name"]}"{fleet_type} and '
                 f'a duration of {form.cleaned_data["duration"]} minutes was created'
                 ),
                fatlink_hash=fatlink.hash,
            )

            logger.info(
                (f'FAT link "{fatlink_hash}" with name '
                 f'"{form.cleaned_data["name"]}"{fleet_type} and a duration '
                 f'of {form.cleaned_data["duration"]} minutes was created '
                 f"by {request.user}"))

            return redirect("afat:fatlinks_details_fatlink",
                            fatlink_hash=fatlink_hash)

        request.session["msg"] = [
            "danger",
            "Something went wrong when attempting to submit yourclickable FAT Link.",
        ]
        return redirect("afat:dashboard")

    request.session["msg"] = [
        "warning",
        ('You must fill out the form on the "Add FAT Link" '
         "page to create a clickable FAT Link"),
    ]

    return redirect("afat:dashboard")
Пример #3
0
def link_create_click(request: WSGIRequest):
    """
    create fatlink helper
    :param request:
    :return:
    """

    if request.method == "POST":
        form = AFatClickFatForm(request.POST)

        if form.is_valid():
            fatlink_hash = get_random_string(length=30)

            link = AFatLink()
            link.fleet = form.cleaned_data["name"]

            if (
                form.cleaned_data["type"] is not None
                and form.cleaned_data["type"] != -1
            ):
                link.link_type = AFatLinkType.objects.get(id=form.cleaned_data["type"])

            link.creator = request.user
            link.hash = fatlink_hash
            link.afattime = timezone.now()
            link.save()

            dur = ClickAFatDuration()
            dur.fleet = AFatLink.objects.get(hash=fatlink_hash)
            dur.duration = form.cleaned_data["duration"]
            dur.save()

            request.session[
                "{fatlink_hash}-creation-code".format(fatlink_hash=fatlink_hash)
            ] = 202

            logger.info(
                "FAT link {fatlink_hash} with name {name} and a "
                "duration of {duration} minutes was created by {user}".format(
                    fatlink_hash=fatlink_hash,
                    name=form.cleaned_data["name"],
                    duration=form.cleaned_data["duration"],
                    user=request.user,
                )
            )

            return redirect("afat:link_edit", fatlink_hash=fatlink_hash)

        request.session["msg"] = [
            "danger",
            (
                "Something went wrong when attempting to submit your"
                " clickable FAT Link."
            ),
        ]
        return redirect("afat:dashboard")

    request.session["msg"] = [
        "warning",
        (
            'You must fill out the form on the "Add FAT Link" '
            "page to create a clickable FAT Link"
        ),
    ]

    return redirect("afat:dashboard")
Пример #4
0
def create_clickable_fatlink(request: WSGIRequest, ) -> HttpResponseRedirect:
    """
    Create clickable fat link
    :param request:
    :type request:
    :return:
    :rtype:
    """

    if request.method == "POST":
        form = AFatClickFatForm(request.POST)

        if form.is_valid():
            fatlink_hash = get_hash_on_save()

            fatlink = AFatLink()
            fatlink.fleet = form.cleaned_data["name"]

            if form.cleaned_data["type"] is not None:
                fatlink.link_type = form.cleaned_data["type"]

            fatlink.creator = request.user
            fatlink.hash = fatlink_hash
            fatlink.afattime = timezone.now()
            fatlink.save()

            dur = ClickAFatDuration()
            dur.fleet = AFatLink.objects.get(hash=fatlink_hash)
            dur.duration = form.cleaned_data["duration"]
            dur.save()

            # Writing DB log
            fleet_type = (f" (Fleet Type: {fatlink.link_type.name})"
                          if fatlink.link_type else "")

            write_log(
                request=request,
                log_event=AFatLog.Event.CREATE_FATLINK,
                log_text=
                (f'FAT link with name "{form.cleaned_data["name"]}"{fleet_type} and '
                 f'a duration of {form.cleaned_data["duration"]} minutes was created'
                 ),
                fatlink_hash=fatlink.hash,
            )

            logger.info(
                f'FAT link "{fatlink_hash}" with name '
                f'"{form.cleaned_data["name"]}"{fleet_type} and a duration '
                f'of {form.cleaned_data["duration"]} minutes was created '
                f"by {request.user}")

            messages.success(
                request,
                mark_safe(
                    _("<h4>Success!</h4>"
                      "<p>Clickable FAT Link Created!</p>"
                      "<p>Make sure to give your fleet members the link to "
                      "click so that they get credit for this fleet.</p>")),
            )

            return redirect("afat:fatlinks_details_fatlink",
                            fatlink_hash=fatlink_hash)

        messages.error(
            request,
            mark_safe(
                _("<h4>Error!</h4>"
                  "<p>Something went wrong when attempting "
                  "to submit your clickable FAT Link.</p>")),
        )

        return redirect("afat:fatlinks_add_fatlink")

    messages.warning(
        request,
        mark_safe(
            _("<h4>Warning!</h4>"
              '<p>You must fill out the form on the "Add FAT Link" '
              "page to create a clickable FAT Link</p>")),
    )

    return redirect("afat:fatlinks_add_fatlink")