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")
Example #2
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")
Example #3
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")