Example #1
0
def convert_logs_to_dict(log: AFatLog, fatlink_exists: bool = False) -> dict:
    """
    Convert AFatLog to dict
    :param log:
    :type log:
    :param fatlink_exists:
    :type fatlink_exists:
    :return:
    :rtype:
    """

    log_time = log.log_time
    log_time_timestamp = log_time.timestamp()

    # User name
    user_main_character = get_main_character_from_user(user=log.user)

    fatlink_html = _(f"{log.fatlink_hash} (Deleted)")
    if fatlink_exists is True:
        fatlink_link = reverse("afat:fatlinks_details_fatlink",
                               args=[log.fatlink_hash])
        fatlink_html = f'<a href="{fatlink_link}">{log.fatlink_hash}</a>'

    fatlink = {"html": fatlink_html, "hash": log.fatlink_hash}

    summary = {
        "log_time": {
            "time": log_time,
            "timestamp": log_time_timestamp
        },
        "log_event": AFatLog.Event(log.log_event).label,
        "user": user_main_character,
        "fatlink": fatlink,
        "description": log.log_text,
    }

    return summary
Example #2
0
    def test_ajax_get_fatlinks_by_year(self):
        # given
        # self.maxDiff = None
        self.client.force_login(self.user_with_basic_access)

        fatlink_hash = get_hash_on_save()
        fatlink_type_cta = AFatLinkType.objects.create(name="CTA")
        fatlink_created = AFatLink.objects.create(
            fleet="April Fleet 1",
            creator=self.user_with_manage_afat,
            character=self.character_1001,
            hash=fatlink_hash,
            is_esilink=True,
            is_registered_on_esi=True,
            esi_fleet_id=3726458287,
            link_type=fatlink_type_cta,
            afattime="2021-11-05T13:19:49.676Z",
        )

        ClickAFatDuration.objects.create(fleet=fatlink_created, duration=120)

        # when
        fatlink = (AFatLink.objects.select_related_default().
                   annotate_afats_count().get(hash=fatlink_hash))

        url_with_year = reverse(
            "afat:fatlinks_ajax_get_fatlinks_by_year",
            kwargs={"year": 2021},
        )
        result = self.client.get(url_with_year)

        # then
        self.assertEqual(result.status_code, 200)

        creator_main_character = get_main_character_from_user(
            user=fatlink.creator)
        fleet_time = fatlink.afattime
        fleet_time_timestamp = fleet_time.timestamp()
        esi_marker = (
            '<span class="label label-default afat-label afat-label-via-esi '
            'afat-label-active-esi-fleet">via ESI</span>')

        self.assertJSONEqual(
            str(result.content, encoding="utf8"),
            [{
                "pk": fatlink.pk,
                "fleet_name": fatlink.fleet + esi_marker,
                "creator_name": creator_main_character,
                "fleet_type": "CTA",
                "fleet_time": {
                    "time": "2021-11-05T13:19:49.676Z",
                    "timestamp": fleet_time_timestamp,
                },
                "fats_number": 0,
                "hash": fatlink.hash,
                "is_esilink": True,
                "esi_fleet_id": fatlink.esi_fleet_id,
                "is_registered_on_esi": True,
                # "actions": '<a class="btn btn-afat-action btn-info btn-sm" href="/fleet-activity-tracking/fatlink/ncOsHjnjmYZd9k6hI4us8QShRlqJ17/details/"><span class="fas fa-eye"></span></a><a class="btn btn-afat-action btn-danger btn-sm" data-toggle="modal" data-target="#deleteFatLinkModal" data-url="/fleet-activity-tracking/fatlink/ncOsHjnjmYZd9k6hI4us8QShRlqJ17/delete/" data-confirm-text="Delete"data-body-text="<p>Are you sure you want to delete FAT link M2-XFE Keepstar Kill?</p>"><span class="glyphicon glyphicon-trash"></span></a>',
                "actions": "",
                "via_esi": "Yes",
            }],
        )
    def test_helper_convert_logs_to_dict(self):
        # given
        self.client.force_login(self.user_with_manage_afat)
        request = self.factory.get(reverse("afat:dashboard"))
        request.user = self.user_with_manage_afat

        fatlink_hash = get_hash_on_save()
        fatlink_type_cta = AFatLinkType.objects.create(name="CTA")
        fatlink_created = AFatLink.objects.create(
            afattime=timezone.now(),
            fleet="April Fleet 1",
            creator=self.user_with_manage_afat,
            character=self.character_1001,
            hash=fatlink_hash,
            is_esilink=True,
            is_registered_on_esi=True,
            esi_fleet_id="3726458287",
            link_type=fatlink_type_cta,
        )

        duration = ClickAFatDuration.objects.create(fleet=fatlink_created,
                                                    duration=120)

        fleet_type = f" (Fleet Type: {fatlink_created.link_type.name})"

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

        # when
        log = AFatLog.objects.get(fatlink_hash=fatlink_hash)
        log_time = log.log_time
        log_time_timestamp = log_time.timestamp()
        user_main_character = get_main_character_from_user(user=log.user)
        fatlink_link = reverse("afat:fatlinks_details_fatlink",
                               args=[log.fatlink_hash])
        fatlink_html = f'<a href="{fatlink_link}">{log.fatlink_hash}</a>'

        result = convert_logs_to_dict(log=log, fatlink_exists=True)

        # then
        self.assertDictEqual(
            result,
            {
                "log_time": {
                    "time": log_time,
                    "timestamp": log_time_timestamp
                },
                "log_event": AFatLog.Event(log.log_event).label,
                "user": user_main_character,
                "fatlink": {
                    "html": fatlink_html,
                    "hash": log.fatlink_hash
                },
                "description": log.log_text,
            },
        )
    def test_helper_convert_fatlinks_to_dict(self):
        # given
        self.client.force_login(self.user_with_manage_afat)
        request = self.factory.get(reverse("afat:dashboard"))
        request.user = self.user_with_manage_afat

        fatlink_hash_fleet_1 = get_hash_on_save()
        fatlink_1_created = AFatLink.objects.create(
            afattime=timezone.now(),
            fleet="April Fleet 1",
            creator=self.user_with_manage_afat,
            character=self.character_1001,
            hash=fatlink_hash_fleet_1,
            is_esilink=True,
            is_registered_on_esi=True,
            esi_fleet_id="3726458287",
        )
        AFat.objects.create(character=self.character_1101,
                            afatlink=fatlink_1_created,
                            shiptype="Omen")

        fatlink_type_cta = AFatLinkType.objects.create(name="CTA")
        fatlink_hash_fleet_2 = get_hash_on_save()
        fatlink_2_created = AFatLink.objects.create(
            afattime=timezone.now(),
            fleet="April Fleet 2",
            creator=self.user_with_add_fatlink,
            character=self.character_1101,
            hash=fatlink_hash_fleet_2,
            link_type=fatlink_type_cta,
        )
        AFat.objects.create(character=self.character_1001,
                            afatlink=fatlink_2_created,
                            shiptype="Omen")

        # when
        fatlink_1 = (AFatLink.objects.select_related_default().
                     annotate_afats_count().get(hash=fatlink_hash_fleet_1))
        close_esi_tracking_url = reverse("afat:fatlinks_close_esi_fatlink",
                                         args=[fatlink_1.hash])
        edit_url_1 = reverse("afat:fatlinks_details_fatlink",
                             args=[fatlink_1.hash])
        delete_url_1 = reverse("afat:fatlinks_delete_fatlink",
                               args=[fatlink_1.hash])

        fatlink_2 = (AFatLink.objects.select_related_default().
                     annotate_afats_count().get(hash=fatlink_hash_fleet_2))
        edit_url_2 = reverse("afat:fatlinks_details_fatlink",
                             args=[fatlink_2.hash])
        delete_url_2 = reverse("afat:fatlinks_delete_fatlink",
                               args=[fatlink_2.hash])

        result_1 = convert_fatlinks_to_dict(request=request, fatlink=fatlink_1)
        result_2 = convert_fatlinks_to_dict(request=request, fatlink=fatlink_2)

        # then
        fleet_time_1 = fatlink_1.afattime
        fleet_time_timestamp_1 = fleet_time_1.timestamp()
        creator_main_character_1 = get_main_character_from_user(
            user=fatlink_1.creator)
        self.assertDictEqual(
            result_1,
            {
                "pk":
                fatlink_1.pk,
                "fleet_name": ('April Fleet 1<span class="label label-default '
                               "afat-label afat-label-via-esi "
                               'afat-label-active-esi-fleet">via ESI</span>'),
                "creator_name":
                creator_main_character_1,
                "fleet_type":
                "",
                "fleet_time": {
                    "time": fleet_time_1,
                    "timestamp": fleet_time_timestamp_1,
                },
                "fats_number":
                fatlink_1.afats_count,
                "hash":
                fatlink_1.hash,
                "is_esilink":
                True,
                "esi_fleet_id":
                3726458287,
                "is_registered_on_esi":
                True,
                "actions":
                (f'<a class="btn btn-afat-action btn-primary btn-sm" '
                 f'style="margin-left: 0.25rem;" title="Clicking here will '
                 f"stop the automatic tracking through ESI for this fleet "
                 f'and close the associated FAT link." data-toggle="modal" '
                 f'data-target="#cancelEsiFleetModal" '
                 f'data-url="{close_esi_tracking_url}" '
                 f'data-body-text="<p>Are you sure you want to close ESI '
                 f'fleet with ID 3726458287 from Bruce Wayne?</p>" '
                 f'data-confirm-text="Stop Tracking"><i class="fas '
                 f'fa-times"></i></a><a class="btn btn-afat-action btn-info '
                 f'btn-sm" href="{edit_url_1}"><span class="fas '
                 f'fa-eye"></span></a><a class="btn btn-afat-action '
                 f'btn-danger btn-sm" data-toggle="modal" '
                 f'data-target="#deleteFatLinkModal" '
                 f'data-url="{delete_url_1}" '
                 f'data-confirm-text="Delete"data-body-text="<p>Are you '
                 f"sure you want to delete FAT link April Fleet "
                 f'1?</p>"><span class="glyphicon '
                 f'glyphicon-trash"></span></a>'),
                "via_esi":
                "Yes",
            },
        )

        fleet_time_2 = fatlink_2.afattime
        fleet_time_timestamp_2 = fleet_time_2.timestamp()
        creator_main_character_2 = get_main_character_from_user(
            user=fatlink_2.creator)
        self.assertDictEqual(
            result_2,
            {
                "pk":
                fatlink_2.pk,
                "fleet_name":
                "April Fleet 2",
                "creator_name":
                creator_main_character_2,
                "fleet_type":
                "CTA",
                "fleet_time": {
                    "time": fleet_time_2,
                    "timestamp": fleet_time_timestamp_2,
                },
                "fats_number":
                fatlink_2.afats_count,
                "hash":
                fatlink_2.hash,
                "is_esilink":
                False,
                "esi_fleet_id":
                None,
                "is_registered_on_esi":
                False,
                "actions":
                (f'<a class="btn btn-afat-action btn-info btn-sm" '
                 f'href="{edit_url_2}"><span class="fas '
                 f'fa-eye"></span></a><a class="btn btn-afat-action '
                 f'btn-danger btn-sm" data-toggle="modal" '
                 f'data-target="#deleteFatLinkModal" '
                 f'data-url="{delete_url_2}" '
                 f'data-confirm-text="Delete"data-body-text="<p>Are you '
                 f"sure you want to delete FAT link April Fleet "
                 f'2?</p>"><span class="glyphicon '
                 f'glyphicon-trash"></span></a>'),
                "via_esi":
                "No",
            },
        )
Example #5
0
def convert_fatlinks_to_dict(request: WSGIRequest,
                             fatlink: AFatLink,
                             close_esi_redirect: str = None) -> dict:
    """
    Converts an AFatLink object into a dictionary
    :param request:
    :type request:
    :param fatlink:
    :type fatlink:
    :param close_esi_redirect:
    :type close_esi_redirect:
    :return:
    :rtype:
    """

    # Fleet name
    fatlink_fleet = fatlink.fleet if fatlink.fleet is not None else fatlink.hash

    # ESI marker
    via_esi = "No"
    esi_fleet_marker = ""

    # Check for ESI link
    if fatlink.is_esilink:
        via_esi = "Yes"
        esi_fleet_marker_classes = "label label-default afat-label afat-label-via-esi"

        if fatlink.is_registered_on_esi:
            esi_fleet_marker_classes += " afat-label-active-esi-fleet"

        marker_text = _("via ESI")
        esi_fleet_marker += (
            f'<span class="{esi_fleet_marker_classes}">{marker_text}</span>')

    # Fleet type
    fatlink_type = fatlink.link_type.name if fatlink.link_type else ""

    # Creator name
    creator_main_character = get_main_character_from_user(user=fatlink.creator)

    # Fleet time
    fleet_time = fatlink.afattime
    fleet_time_timestamp = fleet_time.timestamp()

    # Action buttons
    actions = ""
    if (fatlink.is_esilink and fatlink.is_registered_on_esi
            and fatlink.creator == request.user):
        button_close_esi_tracking_url = reverse(
            "afat:fatlinks_close_esi_fatlink", args=[fatlink.hash])

        close_esi_redirect_parameter = (f"?next={close_esi_redirect}" if
                                        close_esi_redirect is not None else "")

        button_title = _(
            "Clicking here will stop the automatic tracking through ESI for this fleet and close the associated FAT link."
        )
        modal_body_text = _(
            f"<p>Are you sure you want to close ESI fleet with ID {fatlink.esi_fleet_id} from {fatlink.character.character_name}?</p>"
        )
        modal_confirm_text = _("Stop Tracking")

        actions += (
            '<a class="btn btn-afat-action btn-primary btn-sm" '
            f'style="margin-left: 0.25rem;" title="{button_title}" data-toggle="modal" '
            'data-target="#cancelEsiFleetModal" '
            f'data-url="{button_close_esi_tracking_url}{close_esi_redirect_parameter}" '
            f'data-body-text="{modal_body_text}" '
            f'data-confirm-text="{modal_confirm_text}">'
            '<i class="fas fa-times"></i></a>')

    if request.user.has_perm("afat.manage_afat") or request.user.has_perm(
            "afat.add_fatlink"):
        button_edit_url = reverse("afat:fatlinks_details_fatlink",
                                  args=[fatlink.hash])

        actions += (
            '<a class="btn btn-afat-action btn-info btn-sm" '
            f'href="{button_edit_url}"><span class="fas fa-eye"></span></a>')

    if request.user.has_perm("afat.manage_afat"):
        button_delete_url = reverse("afat:fatlinks_delete_fatlink",
                                    args=[fatlink.hash])
        button_delete_text = _("Delete")
        modal_body_text = _(
            f"<p>Are you sure you want to delete FAT link {fatlink_fleet}?</p>"
        )

        actions += (
            '<a class="btn btn-afat-action btn-danger btn-sm" data-toggle="modal" '
            f'data-target="#deleteFatLinkModal" data-url="{button_delete_url}" '
            f'data-confirm-text="{button_delete_text}"'
            f'data-body-text="{modal_body_text}">'
            '<span class="glyphicon glyphicon-trash">'
            "</span></a>")

    summary = {
        "pk": fatlink.pk,
        "fleet_name": fatlink_fleet + esi_fleet_marker,
        "creator_name": creator_main_character,
        "fleet_type": fatlink_type,
        "fleet_time": {
            "time": fleet_time,
            "timestamp": fleet_time_timestamp
        },
        "fats_number": fatlink.afats_count,
        "hash": fatlink.hash,
        "is_esilink": fatlink.is_esilink,
        "esi_fleet_id": fatlink.esi_fleet_id,
        "is_registered_on_esi": fatlink.is_registered_on_esi,
        "actions": actions,
        "via_esi": via_esi,
    }

    return summary