def _migrate_manual_fat_log(self) -> None:
        """
        Start the migration
        :return:
        :rtype:
        """

        manual_fat_logs = ManualAFat.objects.all()

        if manual_fat_logs.count() > 0:
            for manual_log in manual_fat_logs:
                if manual_log.created_at is not None:
                    afat_log = AFatLog()

                    afat_log.user_id = manual_log.creator_id
                    afat_log.log_time = manual_log.created_at
                    afat_log.log_event = AFatLog.Event.MANUAL_FAT
                    afat_log.log_text = (
                        f"Pilot {manual_log.character} manually added. "
                        f"(Migrated from old Manual FAT log)")
                    afat_log.fatlink_hash = manual_log.afatlink.hash
                    afat_log.save()

                manual_log.delete()

        self.stdout.write(self.style.SUCCESS("Migration complete!"))
Example #2
0
def write_log(request: WSGIRequest, log_event: str, fatlink_hash: str,
              log_text: str):
    """
    Write the log
    :param request:
    :type request:
    :param log_event:
    :type log_event:
    :param fatlink_hash:
    :type fatlink_hash:
    :param log_text:
    :type log_text:
    :return:
    :rtype:
    """

    # Alliance Auth AFAT
    from afat.models import AFatLog

    afat_log = AFatLog()
    afat_log.user = request.user
    afat_log.log_event = log_event
    afat_log.log_text = log_text
    afat_log.fatlink_hash = fatlink_hash
    afat_log.save()
Example #3
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 #4
0
    def _import_from_aa_fat(self) -> None:
        """
        Start the import
        :return:
        :rtype:
        """

        # Check if AA FAT is active
        if aa_fat_installed():
            self.stdout.write(
                self.style.SUCCESS(
                    "Alliance Auth FAT module is active, let's go!"))

            # First, we check if the target tables are empty ...
            current_afat_links_count = AFatLink.objects.all().count()
            current_afat_count = AFat.objects.all().count()

            if current_afat_count > 0 or current_afat_links_count > 0:
                self.stdout.write(
                    self.style.WARNING(
                        "You already have FAT data with the AFAT module. "
                        "Import cannot be continued."))

                return

            aa_fatlinks = Fatlink.objects.all()
            for aa_fatlink in aa_fatlinks:
                self.stdout.write(
                    f"Importing FAT link for fleet '{aa_fatlink.fleet}' with hash "
                    f"'{aa_fatlink.hash}'.")

                afatlink = AFatLink()

                afatlink.id = aa_fatlink.id
                afatlink.afattime = aa_fatlink.fatdatetime
                afatlink.fleet = (aa_fatlink.fleet if aa_fatlink.fleet
                                  is not None else aa_fatlink.hash)
                afatlink.hash = aa_fatlink.hash
                afatlink.creator_id = aa_fatlink.creator_id

                afatlink.save()

                # Write to log table
                log_text = f"FAT link {aa_fatlink.hash} with name {aa_fatlink.fleet} was created by {aa_fatlink.creator}"

                afatlog = AFatLog()
                afatlog.log_time = aa_fatlink.fatdatetime
                afatlog.log_event = AFatLog.Event.CREATE_FATLINK
                afatlog.log_text = log_text
                afatlog.user_id = aa_fatlink.creator_id
                afatlog.save()

            aa_fats = Fat.objects.all()
            for aa_fat in aa_fats:
                self.stdout.write(
                    f"Importing FATs for FAT link ID '{aa_fat.id}'.")

                afat = AFat()

                afat.id = aa_fat.id
                afat.system = aa_fat.system
                afat.shiptype = aa_fat.shiptype
                afat.character_id = aa_fat.character_id
                afat.afatlink_id = aa_fat.fatlink_id

                afat.save()

            self.stdout.write(
                self.style.SUCCESS(
                    "Import complete! "
                    "You can now deactivate the Alliance Auth FAT "
                    "module in your local.py"))
        else:
            self.stdout.write(
                self.style.WARNING("Alliance Auth FAT module is not active. "
                                   "Please make sure you have it in your "
                                   "INSTALLED_APPS in your local.py!"))
    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,
            },
        )
Example #6
0
    def _import_from_imicusfat(self) -> None:
        """
        Start the import
        :return:
        :rtype:
        """

        # First, we check if the target tables are empty ...
        current_afat_count = AFat.objects.all().count()
        current_afat_links_count = AFatLink.objects.all().count()
        current_afat_linktype_count = AFatLinkType.objects.all().count()
        current_afat_clickduration_count = ClickAFatDuration.objects.all().count()

        if (
            current_afat_count > 0
            or current_afat_links_count > 0
            or current_afat_linktype_count > 0
            or current_afat_clickduration_count > 0
        ):
            self.stdout.write(
                self.style.WARNING(
                    "You already have FAT data with the aFAT module. "
                    "Import cannot be continued."
                )
            )

            return

        # Before we do anything, remove all "deleted" FATlinks and FATs
        IFatLink.all_objects.filter(deleted_at__isnull=False).hard_delete()
        IFat.all_objects.filter(deleted_at__isnull=False).hard_delete()

        # Import fat link type
        imicusfat_fleettypes = IFatLinkType.objects.all()
        for imicusfat_fleettype in imicusfat_fleettypes:
            self.stdout.write(f"Importing fleet type '{imicusfat_fleettype.name}'.")

            afat_fleettype = AFatLinkType()

            afat_fleettype.id = imicusfat_fleettype.id
            afat_fleettype.name = imicusfat_fleettype.name
            afat_fleettype.is_enabled = imicusfat_fleettype.is_enabled

            afat_fleettype.save()

        # Import FAT links
        imicusfat_fatlinks = IFatLink.objects.all()
        for imicusfat_fatlink in imicusfat_fatlinks:
            fleet = imicusfat_fatlink.fleet
            fatlink_hash = imicusfat_fatlink.hash
            fatlink_name = imicusfat_fatlink.fleet
            fatlink_creator = imicusfat_fatlink.creator

            self.stdout.write(
                f"Importing FAT link for fleet '{fleet}' with hash '{fatlink_hash}'."
            )

            afatlink = AFatLink()

            afatlink.id = imicusfat_fatlink.id
            afatlink.afattime = imicusfat_fatlink.ifattime
            afatlink.fleet = (
                imicusfat_fatlink.fleet
                if imicusfat_fatlink.fleet is not None
                else fatlink_hash
            )
            afatlink.hash = fatlink_hash
            afatlink.creator_id = imicusfat_fatlink.creator_id
            afatlink.link_type_id = imicusfat_fatlink.link_type_id
            afatlink.is_esilink = imicusfat_fatlink.is_esilink

            afatlink.save()

            # Write to log table
            if imicusfat_fatlink.is_esilink:
                log_text = (
                    f"ESI FAT link {fatlink_hash} with name {fatlink_name} "
                    f"was created by {fatlink_creator}"
                )
            else:
                try:
                    fleet_duration = ClickIFatDuration.objects.get(
                        fleet_id=imicusfat_fatlink.id
                    )

                    log_text = (
                        f"FAT link {fatlink_hash} with name {fatlink_name} and a "
                        f"duration of {fleet_duration.duration} minutes was created "
                        f"by {fatlink_creator}"
                    )
                except ClickIFatDuration.DoesNotExist:
                    log_text = (
                        f"FAT link {fatlink_hash} with name {fatlink_name} "
                        f"was created by {fatlink_creator}"
                    )

            afatlog = AFatLog()
            afatlog.log_time = imicusfat_fatlink.ifattime
            afatlog.log_event = AFatLog.Event.CREATE_FATLINK
            afatlog.log_text = log_text
            afatlog.user_id = imicusfat_fatlink.creator_id
            afatlog.save()

        # Import FATs
        imicustaf_fats = IFat.objects.all()
        for imicusfat_fat in imicustaf_fats:
            self.stdout.write(f"Importing FATs for FAT link ID '{imicusfat_fat.id}'.")

            afat = AFat()

            afat.id = imicusfat_fat.id
            afat.system = imicusfat_fat.system
            afat.shiptype = imicusfat_fat.shiptype
            afat.character_id = imicusfat_fat.character_id
            afat.afatlink_id = imicusfat_fat.ifatlink_id

            afat.save()

        # Import click FAT durations
        imicusfat_clickfatdurations = ClickIFatDuration.objects.all()
        for imicusfat_clickfatduration in imicusfat_clickfatdurations:
            self.stdout.write(
                f"Importing FAT duration with ID '{imicusfat_clickfatduration.id}'."
            )

            afat_clickfatduration = ClickAFatDuration()

            afat_clickfatduration.id = imicusfat_clickfatduration.id
            afat_clickfatduration.duration = imicusfat_clickfatduration.duration
            afat_clickfatduration.fleet_id = imicusfat_clickfatduration.fleet_id

            afat_clickfatduration.save()

        # Import manual fat to log table
        imicusfat_manualfats = ManualIFat.objects.all()
        for imicusfat_manualfat in imicusfat_manualfats:
            self.stdout.write(
                f"Importing manual FAT with ID '{imicusfat_manualfat.id}'."
            )

            fatlink = IFatLink.objects.get(manualifat=imicusfat_manualfat)
            pilot_name = imicusfat_manualfat.character.character_name
            log_text = (
                f"Pilot {pilot_name} was manually added to "
                f'FAT link with hash "{fatlink.hash}"'
            )

            if imicusfat_manualfat.created_at is not None:
                afatlog = AFatLog()
                afatlog.log_time = imicusfat_manualfat.created_at
                afatlog.log_event = AFatLog.Event.MANUAL_FAT
                afatlog.log_text = log_text
                afatlog.user_id = imicusfat_manualfat.creator_id
                afatlog.save()

        self.stdout.write(
            self.style.SUCCESS(
                "Import complete! "
                "You can now deactivate the ImicusFAT module in your local.py"
            )
        )
Example #7
0
    def _import_from_imicusfat(self) -> None:
        """
        start the import
        :return:
        :rtype:
        """

        # check if AA FAT is active
        if bfat_installed():
            self.stdout.write(
                self.style.SUCCESS("ImicusFAT module is active, let's go!"))

            # first we check if the target tables are really empty ...
            current_afat_count = AFat.objects.all().count()
            current_afat_links_count = AFatLink.objects.all().count()
            current_afat_clickduration_count = ClickAFatDuration.objects.all(
            ).count()

            if (current_afat_count > 0 or current_afat_links_count > 0
                    or current_afat_clickduration_count > 0):
                self.stdout.write(
                    self.style.WARNING(
                        "You already have FAT data with the AFAT module. "
                        "Import cannot be continued."))

                return

            # import FAT links
            bfat_fatlinks = BfatFatLink.objects.all()
            for bfat_fatlink in bfat_fatlinks:
                self.stdout.write(
                    f'Importing FAT link for fleet "{bfat_fatlink.fleet}" '
                    f'with hash "{bfat_fatlink.hash}".')

                afatlink = AFatLink()

                afatlink.id = bfat_fatlink.id
                afatlink.afattime = bfat_fatlink.fattime
                afatlink.fleet = bfat_fatlink.fleet
                afatlink.hash = bfat_fatlink.hash
                afatlink.creator_id = bfat_fatlink.creator_id

                afatlink.save()

                # write to log table
                try:
                    fleet_duration = BfatClickFatDuration.objects.get(
                        fleet_id=bfat_fatlink.id)

                    log_text = (
                        f'FAT link "{bfat_fatlink.hash}" with name '
                        f'"{bfat_fatlink.fleet}" and a duration of '
                        f"{fleet_duration.duration} minutes was created "
                        f"by {bfat_fatlink.creator}")
                except BfatClickFatDuration.DoesNotExist:
                    log_text = (
                        f'FAT link "{bfat_fatlink.hash}" with name '
                        f'"{bfat_fatlink.fleet}" was created by {bfat_fatlink.creator}'
                    )

                if bfat_fatlink.fattime is not None:
                    afatlog = AFatLog()
                    afatlog.log_time = bfat_fatlink.fattime
                    afatlog.log_event = AFatLogEvent.CREATE_FATLINK
                    afatlog.log_text = log_text
                    afatlog.user_id = bfat_fatlink.creator_id
                    afatlog.save()

            # import FATs
            bfat_fats = BfatFat.objects.all()
            for bfat_fat in bfat_fats:
                self.stdout.write(
                    f"Importing FATs for FAT link ID {bfat_fat.id}.")

                afat = AFat()

                afat.id = bfat_fat.id
                afat.system = bfat_fat.system
                afat.shiptype = bfat_fat.shiptype
                afat.character_id = bfat_fat.character_id
                afat.afatlink_id = bfat_fat.fatlink_id

                afat.save()

            # import click FAT durations
            bfat_clickfatdurations = BfatClickFatDuration.objects.all()
            for bfat_clickfatduration in bfat_clickfatdurations:
                self.stdout.write(
                    f"Importing FAT duration with ID {bfat_clickfatduration.id}."
                )

                afat_clickfatduration = ClickAFatDuration()

                afat_clickfatduration.id = bfat_clickfatduration.id
                afat_clickfatduration.duration = bfat_clickfatduration.duration
                afat_clickfatduration.fleet_id = bfat_clickfatduration.fleet_id

                afat_clickfatduration.save()

            # import manual fat
            bfat_manualfats = BfatManualFat.objects.all()
            for bfat_manualfat in bfat_manualfats:
                self.stdout.write(
                    f"Importing manual FAT with ID {bfat_manualfat.id}.")

                fatlink = BfatFatLink.objects.get(manualfat=bfat_manualfat)
                log_text = (
                    f"Pilot {bfat_manualfat.character.character_name} was manually "
                    f'added to FAT link with hash "{fatlink.hash}"')

                afatlog = AFatLog()
                afatlog.log_time = bfat_manualfat.created_at
                afatlog.log_event = AFatLogEvent.MANUAL_FAT
                afatlog.log_text = log_text
                afatlog.user_id = bfat_manualfat.creator_id
                afatlog.save()

            self.stdout.write(
                self.style.SUCCESS(
                    "Import complete! "
                    "You can now deactivate the bFAT module in your local.py"))
        else:
            self.stdout.write(
                self.style.WARNING("bFAT module is not active. "
                                   "Please make sure you have it in your "
                                   "INSTALLED_APPS in your local.py!"))
    def _import_from_imicusfat(self) -> None:
        """
        start the import
        :return:
        :rtype:
        """

        # first we check if the target tables are really empty ...
        current_afat_count = AFat.objects.all().count()
        current_afat_links_count = AFatLink.objects.all().count()
        current_afat_linktype_count = AFatLinkType.objects.all().count()
        current_afat_clickduration_count = ClickAFatDuration.objects.all(
        ).count()

        if (current_afat_count > 0 or current_afat_links_count > 0
                or current_afat_linktype_count > 0
                or current_afat_clickduration_count > 0):
            self.stdout.write(
                self.style.WARNING(
                    "You already have FAT data with the aFAT module. "
                    "Import cannot be continued."))

            return

        # import fatlinktype
        imicusfat_fleettypes = IFatLinkType.objects.all()
        for imicusfat_fleettype in imicusfat_fleettypes:
            self.stdout.write("Importing fleet type '{fleet_type}'.".format(
                fleet_type=imicusfat_fleettype.name))

            afat_fleettype = AFatLinkType()

            afat_fleettype.id = imicusfat_fleettype.id
            afat_fleettype.name = imicusfat_fleettype.name
            afat_fleettype.is_enabled = imicusfat_fleettype.is_enabled

            afat_fleettype.save()

        # import FAT links
        imicusfat_fatlinks = IFatLink.objects.all()
        for imicusfat_fatlink in imicusfat_fatlinks:
            self.stdout.write("Importing FAT link for fleet '{fleet}' with "
                              "hash '{fatlink_hash}'.".format(
                                  fleet=imicusfat_fatlink.fleet,
                                  fatlink_hash=imicusfat_fatlink.hash,
                              ))

            afatlink = AFatLink()

            afatlink.id = imicusfat_fatlink.id
            afatlink.afattime = imicusfat_fatlink.ifattime
            afatlink.fleet = imicusfat_fatlink.fleet
            afatlink.hash = imicusfat_fatlink.hash
            afatlink.creator_id = imicusfat_fatlink.creator_id
            afatlink.link_type_id = imicusfat_fatlink.link_type_id
            afatlink.is_esilink = imicusfat_fatlink.is_esilink

            afatlink.save()

            # write to log table
            if imicusfat_fatlink.is_esilink:
                log_text = (
                    "ESI FAT link {fatlink_hash} with name {name} was created by {user}"
                ).format(
                    fatlink_hash=imicusfat_fatlink.hash,
                    name=imicusfat_fatlink.fleet,
                    user=imicusfat_fatlink.creator,
                )
            else:
                try:
                    fleet_duration = ClickIFatDuration.objects.get(
                        fleet_id=imicusfat_fatlink.id)

                    log_text = (
                        "FAT link {fatlink_hash} with name {name} and a "
                        "duration of {duration} minutes was created by {user}"
                    ).format(
                        fatlink_hash=imicusfat_fatlink.hash,
                        name=imicusfat_fatlink.fleet,
                        duration=fleet_duration.duration,
                        user=imicusfat_fatlink.creator,
                    )
                except ClickIFatDuration.DoesNotExist:
                    log_text = (
                        "FAT link {fatlink_hash} with name {name} was created by {user}"
                    ).format(
                        fatlink_hash=imicusfat_fatlink.hash,
                        name=imicusfat_fatlink.fleet,
                        user=imicusfat_fatlink.creator,
                    )

            afatlog = AFatLog()
            afatlog.log_time = imicusfat_fatlink.ifattime
            afatlog.log_event = AFatLogEvent.CREATE_FATLINK
            afatlog.log_text = log_text
            afatlog.user_id = imicusfat_fatlink.creator_id
            afatlog.save()

        # import FATs
        imicustaf_fats = IFat.objects.all()
        for imicusfat_fat in imicustaf_fats:
            self.stdout.write(
                "Importing FATs for FAT link ID '{fatlink_id}'.".format(
                    fatlink_id=imicusfat_fat.id))

            afat = AFat()

            afat.id = imicusfat_fat.id
            afat.system = imicusfat_fat.system
            afat.shiptype = imicusfat_fat.shiptype
            afat.character_id = imicusfat_fat.character_id
            afat.afatlink_id = imicusfat_fat.ifatlink_id

            afat.save()

        # import click FAT durations
        imicusfat_clickfatdurations = ClickIFatDuration.objects.all()
        for imicusfat_clickfatduration in imicusfat_clickfatdurations:
            self.stdout.write(
                "Importing FAT duration with ID '{duration_id}'.".format(
                    duration_id=imicusfat_clickfatduration.id))

            afat_clickfatduration = ClickAFatDuration()

            afat_clickfatduration.id = imicusfat_clickfatduration.id
            afat_clickfatduration.duration = imicusfat_clickfatduration.duration
            afat_clickfatduration.fleet_id = imicusfat_clickfatduration.fleet_id

            afat_clickfatduration.save()

        # import manual fat to log table
        imicusfat_manualfats = ManualIFat.objects.all()
        for imicusfat_manualfat in imicusfat_manualfats:
            self.stdout.write(
                "Importing manual FAT with ID '{manualfat_id}'.".format(
                    manualfat_id=imicusfat_manualfat.id))

            fatlink = IFatLink.objects.get(manualifat=imicusfat_manualfat)
            log_text = (
                "Pilot {pilot_name} was manually added to "
                'FAT link with hash "{fatlink_hash}"').format(
                    pilot_name=imicusfat_manualfat.character.character_name,
                    fatlink_hash=fatlink.hash,
                )

            if imicusfat_manualfat.created_at is not None:
                afatlog = AFatLog()
                afatlog.log_time = imicusfat_manualfat.created_at
                afatlog.log_event = AFatLogEvent.MANUAL_FAT
                afatlog.log_text = log_text
                afatlog.user_id = imicusfat_manualfat.creator_id
                afatlog.save()

        self.stdout.write(
            self.style.SUCCESS(
                "Import complete! "
                "You can now deactivate the ImicusFAT module in your local.py")
        )