コード例 #1
0
def srp_link_view_requests(request: WSGIRequest,
                           srp_code: str) -> HttpResponse:
    """
    view srp requests for a specific srp code
    :param request:
    :param srp_code:
    """

    logger.info(
        f"View SRP request for SRP code {srp_code} called by {request.user}")

    # check if the provided SRP code is valid
    if AaSrpLink.objects.filter(srp_code=srp_code).exists() is False:
        logger.error(
            f"Unable to locate SRP Fleet using code {srp_code} for user {request.user}"
        )

        messages.error(
            request,
            _(f"Unable to locate SRP code with ID {srp_code}"),
        )

        return redirect("aasrp:dashboard")

    srp_link = AaSrpLink.objects.get(srp_code=srp_code)
    reject_form = AaSrpRequestRejectForm()

    context = {
        "avoid_cdn": avoid_cdn(),
        "srp_link": srp_link,
        "form": reject_form
    }

    return render(request, "aasrp/view_requests.html", context)
コード例 #2
0
def dashboard(request: WSGIRequest,
              show_all_links: bool = False) -> HttpResponse:
    """
    srp dasboard
    :param request:
    :param show_all_links:
    :return:
    """

    # check if the current user has any settings. if not, create the default set
    try:
        user_settings = AaSrpUserSettings.objects.get(user=request.user)
    except AaSrpUserSettings.DoesNotExist:
        # create the default settings in the DB for the current user
        user_settings = AaSrpUserSettings(user=request.user)
        user_settings.save()

    # if this is a POST request we need to process the form data
    if request.method == "POST":
        user_settings_form = AaSrpUserSettingsForm(request.POST,
                                                   instance=user_settings)

        # check whether it's valid:
        if user_settings_form.is_valid():
            # user_settings.user = request.user
            user_settings.disable_notifications = user_settings_form.cleaned_data[
                "disable_notifications"]
            user_settings.save()
    else:
        user_settings_form = AaSrpUserSettingsForm(instance=user_settings)

    logger_message = f"Dashboard with available SRP links called by {request.user}"

    if show_all_links is True:
        if not request.user.has_perm("aasrp.manage_srp"):
            messages.error(
                request,
                _("You do not have the needed permissions to view all SRP links"
                  ),
            )

            return redirect("aasrp:dashboard")

        logger_message = f"Dashboard with all SRP links called by {request.user}"

    logger.info(logger_message)

    context = {
        "avoid_cdn": avoid_cdn(),
        "show_all_links": show_all_links,
        "user_settings_form": user_settings_form,
    }

    return render(request, "aasrp/dashboard.html", context)
コード例 #3
0
def srp_link_edit(request: WSGIRequest, srp_code: str) -> HttpResponse:
    """
    add or edit AAR link
    :param request:
    :param srp_code:
    :return:
    """

    request_user = request.user

    logger.info(
        f"Edit SRP link form for SRP code {srp_code} called by {request_user}")

    # check if the provided SRP code is valid
    if AaSrpLink.objects.filter(srp_code=srp_code).exists() is False:
        logger.error(
            f"Unable to locate SRP Fleet using code {srp_code} for user {request_user}"
        )

        messages.error(
            request,
            _(f"Unable to locate SRP code with ID {srp_code}"),
        )

        return redirect("aasrp:dashboard")

    srp_link = AaSrpLink.objects.get(srp_code=srp_code)

    # if this is a POST request we need to process the form data
    if request.method == "POST":
        # create a form instance and populate it with data
        form = AaSrpLinkUpdateForm(request.POST, instance=srp_link)

        # check whether it's valid:
        if form.is_valid():
            aar_link = form.cleaned_data["aar_link"]

            srp_link.aar_link = aar_link
            srp_link.save()

            messages.success(request, _("AAR link changed"))

            return redirect("aasrp:dashboard")
    else:
        form = AaSrpLinkUpdateForm(instance=srp_link)

    context = {"avoid_cdn": avoid_cdn(), "srp_code": srp_code, "form": form}

    return render(request, "aasrp/link_edit.html", context)
コード例 #4
0
def srp_link_add(request: WSGIRequest) -> HttpResponse:
    """
    add a srp link
    :param request:
    :return:
    """

    logger.info("Add SRP link form called by %s", request.user)

    # if this is a POST request we need to process the form data
    if request.method == "POST":
        # create a form instance and populate it with data from the request
        form = AaSrpLinkForm(request.POST)

        # check whether it's valid:
        if form.is_valid():
            srp_name = form.cleaned_data["srp_name"]
            fleet_time = form.cleaned_data["fleet_time"]
            fleet_doctrine = form.cleaned_data["fleet_doctrine"]
            aar_link = form.cleaned_data["aar_link"]

            srp_link = AaSrpLink(
                srp_name=srp_name,
                fleet_time=fleet_time,
                fleet_doctrine=fleet_doctrine,
                aar_link=aar_link,
                srp_code=get_random_string(length=16),
                fleet_commander=request.user.profile.main_character,
                creator=request.user,
            )
            srp_link.save()

            messages.success(
                request,
                _(f'SRP link "{srp_link.srp_code}" created'),
            )

            return redirect("aasrp:dashboard")

    # if a GET (or any other method) we'll create a blank form
    else:
        form = AaSrpLinkForm()

    context = {"avoid_cdn": avoid_cdn(), "form": form}

    return render(request, "aasrp/link_add.html", context)
コード例 #5
0
def request_srp(request: WSGIRequest, srp_code: str) -> HttpResponse:
    """
    srp request
    :param request:
    :param srp_code:
    """

    request_user = request.user

    logger.info(
        f"SRP request form for SRP code {srp_code} called by {request_user}")

    # check if the provided SRP code is valid
    if AaSrpLink.objects.filter(srp_code=srp_code).exists() is False:
        logger.error(
            f"Unable to locate SRP Fleet using SRP code {srp_code} for user {request_user}"
        )

        messages.error(
            request,
            _(f"Unable to locate SRP Fleet using SRP code {srp_code}"),
        )

        return redirect("aasrp:dashboard")

    srp_link = AaSrpLink.objects.get(srp_code=srp_code)

    # check if the SRP link is still open
    if srp_link.srp_status != AaSrpStatus.ACTIVE:
        messages.error(
            request,
            _("This SRP link is no longer available for SRP requests."))

        return redirect("aasrp:dashboard")

    # if this is a POST request we need to process the form data
    if request.method == "POST":
        # create a form instance and populate it with data from the request
        form = AaSrpRequestForm(request.POST)
        form_is_valid = form.is_valid()

        logger.debug(f"Request type POST contains valid form: {form_is_valid}")

        # check whether it's valid:
        if form.is_valid():
            creator = request.user
            post_time = timezone.now()
            submitted_killmail_link = form.cleaned_data["killboard_link"]

            # parse killmail
            try:
                srp_kill_link = AaSrpManager.get_kill_id(
                    submitted_killmail_link)

                (ship_type_id, ship_value,
                 victim_id) = AaSrpManager.get_kill_data(srp_kill_link)
            except ValueError:
                # invalid killmail
                logger.debug(
                    f"User {request_user} submitted an invalid killmail link "
                    f"({submitted_killmail_link}) or zKillboard server could "
                    f"not be reached")

                messages.error(
                    request,
                    _("Your SRP request Killmail link is invalid. "
                      f"Please make sure you are using {ZKILLBOARD_BASE_URL}"),
                )

                return redirect("aasrp:dashboard")

            if request.user.character_ownerships.filter(
                    character__character_id=str(victim_id)).exists():
                srp_request__character = EveCharacter.objects.get_character_by_id(
                    victim_id)

                (
                    srp_request__ship,
                    created_from_esi,
                ) = EveType.objects.get_or_create_esi(id=ship_type_id)

                srp_request = AaSrpRequest(
                    killboard_link=submitted_killmail_link,
                    creator=creator,
                    srp_link=srp_link,
                    character=srp_request__character,
                    ship_name=srp_request__ship.name,
                    ship=srp_request__ship,
                    loss_amount=ship_value,
                    post_time=post_time,
                    request_code=get_random_string(length=16),
                )
                srp_request.save()

                # add request info to comments
                srp_request_comment = AaSrpRequestComment(
                    comment=form.cleaned_data["additional_info"],
                    srp_request=srp_request,
                    comment_type=AaSrpRequestCommentType.REQUEST_INFO,
                    creator=creator,
                )
                srp_request_comment.save()

                # add insurance information
                insurance_information = AaSrpManager.get_insurance_for_ship_type(
                    ship_type_id=ship_type_id)

                for insurance_level in insurance_information["levels"]:
                    logger.debug(insurance_level)

                    insurance = AaSrpInsurance(
                        srp_request=srp_request,
                        insurance_level=insurance_level["name"],
                        insurance_cost=insurance_level["cost"],
                        insurance_payout=insurance_level["payout"],
                    )
                    insurance.save()

                user_name = request.user
                character_name = srp_request__character
                srp_name = srp_link.srp_name
                srp_code = srp_request.request_code
                logger.info(
                    f"Created SRP request on behalf of user {user_name} "
                    f"(character: {character_name}) for fleet name {srp_name} "
                    f"with SRP code {srp_code}")

                ship = srp_request.ship.name
                messages.success(request,
                                 _(f"Submitted SRP request for your {ship}."))

                # send message to the srp team in their discord channel
                if AASRP_SRP_TEAM_DISCORD_CHANNEL is not None:
                    site_base_url = site_absolute_url()
                    request_code = srp_request.request_code
                    character_name = srp_request__character.character_name
                    ship_type = srp_request__ship.name
                    zkillboard_link = srp_request.killboard_link
                    additional_information = srp_request_comment.comment.replace(
                        "@", "{@}")
                    srp_link = site_base_url + reverse(
                        "aasrp:view_srp_requests", args=[srp_link.srp_code])

                    title = "New SRP Request"
                    message = f"**Request Code:** {request_code}\n"
                    message += f"**Character:** {character_name}\n"
                    message += f"**Ship:** {ship_type}\n"
                    message += f"**zKillboard Link:** {zkillboard_link}\n"
                    message += (
                        f"**Additional Information:**\n{additional_information}\n\n"
                    )
                    message += f"**SRP Code:** {srp_code}\n"
                    message += f"**SRP Link:** {srp_link}\n"

                    send_message_to_discord_channel(
                        channel_id=AASRP_SRP_TEAM_DISCORD_CHANNEL,
                        title=title,
                        message=message,
                    )

                    logger.info(
                        "Sending SRP request notification to the SRP team channel on "
                        "Discord")

                return redirect("aasrp:dashboard")

            messages.error(
                request,
                _(f"Character {victim_id} does not belong to your Auth "
                  f"account. Please add this character as an alt to "
                  f"your main and try again."),
            )

            return redirect("aasrp:dashboard")

    # if a GET (or any other method) we'll create a blank form
    else:
        logger.debug(f"Returning blank SRP request form for {request.user}")

        form = AaSrpRequestForm()

    context = {"avoid_cdn": avoid_cdn(), "srp_code": srp_code, "form": form}

    return render(request, "aasrp/request_srp.html", context)