示例#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
    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,
            },
        )