def _import_from_imicusfat(self) -> None:
        # first we check if the target tables are really empty ...
        current_afat_count = AFat.objects.all().count()
        current_afat_dellog_count = AFatDelLog.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()
        current_afat_manualfat_count = ManualAFat.objects.all().count()

        if (current_afat_count > 0 or current_afat_dellog_count > 0
                or current_afat_links_count > 0
                or current_afat_linktype_count > 0
                or current_afat_clickduration_count > 0
                or current_afat_manualfat_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.deleted_at = imicusfat_fleettype.deleted_at
            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.deleted_at = imicusfat_fatlink.deleted_at
            afatlink.link_type_id = imicusfat_fatlink.link_type_id
            afatlink.is_esilink = imicusfat_fatlink.is_esilink

            afatlink.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.deleted_at = imicusfat_fat.deleted_at

            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 dellog
        imicusfat_dellogs = IFatDelLog.objects.all()
        for imicusfat_dellog in imicusfat_dellogs:
            self.stdout.write(
                "Importing FAT dellogwith ID '{dellog_id}'.".format(
                    dellog_id=imicusfat_dellog.id))

            afat_dellog = AFatDelLog()

            afat_dellog.id = imicusfat_dellog.id
            afat_dellog.deltype = imicusfat_dellog.deltype
            afat_dellog.string = imicusfat_dellog.string
            afat_dellog.remover_id = imicusfat_dellog.remover_id

            afat_dellog.save()

        # import manual fat
        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))

            afat_manualfat = ManualAFat()

            afat_manualfat.id = imicusfat_manualfat.id
            afat_manualfat.character_id = imicusfat_manualfat.character_id
            afat_manualfat.creator_id = imicusfat_manualfat.creator_id
            afat_manualfat.afatlink_id = imicusfat_manualfat.ifatlink_id
            afat_manualfat.created_at = imicusfat_manualfat.created_at

            afat_manualfat.save()

        self.stdout.write(
            self.style.SUCCESS(
                "Import complete! "
                "You can now deactivate the ImicusFAT module in your local.py")
        )
Exemplo n.º 2
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"
            )
        )
    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")
        )