Exemplo n.º 1
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"
            )
        )
Exemplo n.º 2
0
def create_esi_fatlink_callback(request: WSGIRequest, token,
                                fatlink_hash: str) -> HttpResponseRedirect:
    """
    helper: create ESI link (callback, used when coming back from character selection)
    :param request:
    :type request:
    :param token:
    :type token:
    :param fatlink_hash:
    :type fatlink_hash:
    :return:
    :rtype:
    """

    # check if there is a fleet
    try:
        required_scopes = ["esi-fleets.read_fleet.v1"]
        esi_token = Token.get_token(token.character_id, required_scopes)

        fleet_from_esi = esi.client.Fleets.get_characters_character_id_fleet(
            character_id=token.character_id,
            token=esi_token.valid_access_token()).result()
    except Exception:
        # not in a fleet
        request.session["msg"] = [
            "warning",
            "To use the ESI function, you neeed to be in fleet and you need to be "
            "the fleet boss! You can create a clickable FAT link and share it, "
            "if you like.",
        ]

        # return to "Add FAT Link" view
        return redirect("afat:fatlinks_add_fatlink")

    # check if this character already has a fleet
    creator_character = EveCharacter.objects.get(
        character_id=token.character_id)
    registered_fleets_for_creator = AFatLink.objects.filter(
        is_esilink=True,
        is_registered_on_esi=True,
        character__character_name=creator_character.character_name,
    )

    fleet_already_registered = False
    character_has_registered_fleets = False
    registered_fleets_to_close = list()

    if registered_fleets_for_creator.count() > 0:
        character_has_registered_fleets = True

        for registered_fleet in registered_fleets_for_creator:
            if registered_fleet.esi_fleet_id == fleet_from_esi["fleet_id"]:
                # Character already has a fleet
                fleet_already_registered = True
            else:
                registered_fleets_to_close.append(
                    {"registered_fleet": registered_fleet})

    # if the FC already has a fleet and it is the same as already registered,
    # just throw a warning
    if fleet_already_registered is True:
        request.session["msg"] = [
            "warning",
            "Fleet with ID {fleet_id} for your character {character_name} "
            "has already been registered and pilots joining this "
            "fleet are automatically tracked.".format(
                fleet_id=fleet_from_esi["fleet_id"],
                character_name=creator_character.character_name,
            ),
        ]

        # return to "Add FAT Link" view
        return redirect("afat:fatlinks_add_fatlink")

    # if it's a new fleet, remove all former registered fleets if there are any
    if (character_has_registered_fleets is True
            and fleet_already_registered is False
            and len(registered_fleets_to_close) > 0):
        for registered_fleet_to_close in registered_fleets_to_close:
            reason = (f"FC has opened a new fleet with the "
                      f"character {creator_character.character_name}")

            logger.info(
                (f"Closing ESI FAT link with hash "
                 f'"{registered_fleet_to_close["registered_fleet"].hash}". '
                 f"Reason: {reason}"))

            registered_fleet_to_close[
                "registered_fleet"].is_registered_on_esi = False
            registered_fleet_to_close["registered_fleet"].save()

    # check if we deal with the fleet boss here
    try:
        esi_fleet_member = esi.client.Fleets.get_fleets_fleet_id_members(
            fleet_id=fleet_from_esi["fleet_id"],
            token=esi_token.valid_access_token(),
        ).result()
    except Exception:
        request.session["msg"] = [
            "warning",
            "Not Fleet Boss! Only the fleet boss can utilize the ESI function. "
            "You can create a clickable FAT link and share it, if you like.",
        ]

        # return to "Add FAT Link" view
        return redirect("afat:fatlinks_add_fatlink")

    creator_character = EveCharacter.objects.get(
        character_id=token.character_id)

    # create the fatlink
    fatlink = AFatLink(
        afattime=timezone.now(),
        fleet=request.session["fatlink_form__name"],
        creator=request.user,
        character=creator_character,
        hash=fatlink_hash,
        is_esilink=True,
        is_registered_on_esi=True,
        esi_fleet_id=fleet_from_esi["fleet_id"],
    )

    # add fleet type if there is any
    if request.session["fatlink_form__type"] is not None:
        fatlink.link_type_id = request.session["fatlink_form__type"]

    # save it
    fatlink.save()

    # 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'ESI FAT link with name "{request.session["fatlink_form__name"]}"'
            f"{fleet_type} was created by {request.user}"),
        fatlink_hash=fatlink.hash,
    )

    logger.info((f'ESI FAT link "{fatlink_hash}" with name '
                 f'"{request.session["fatlink_form__name"]}"{fleet_type} '
                 f"was created by {request.user}"))

    # clear session
    del request.session["fatlink_form__name"]
    del request.session["fatlink_form__type"]

    # process fleet members in the background
    process_fats.delay(data_list=esi_fleet_member,
                       data_source="esi",
                       fatlink_hash=fatlink_hash)

    request.session["{fatlink_hash}-creation-code".format(
        fatlink_hash=fatlink_hash)] = 200

    return redirect("afat:fatlinks_details_fatlink", fatlink_hash=fatlink_hash)
    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.º 4
0
def create_esi_fatlink_callback(request: WSGIRequest, token,
                                fatlink_hash: str) -> HttpResponseRedirect:
    """
    Helper :: create ESI link (callback, used when coming back from character selection)
    :param request:
    :type request:
    :param token:
    :type token:
    :param fatlink_hash:
    :type fatlink_hash:
    :return:
    :rtype:
    """

    # Check if there is a fleet
    try:
        required_scopes = ["esi-fleets.read_fleet.v1"]
        esi_token = Token.get_token(token.character_id, required_scopes)

        fleet_from_esi = esi.client.Fleets.get_characters_character_id_fleet(
            character_id=token.character_id,
            token=esi_token.valid_access_token()).result()
    except Exception:
        # Not in a fleet
        messages.warning(
            request,
            mark_safe(
                _("<h4>Warning!</h4>"
                  "<p>To use the ESI function, you need to be in fleet and you need "
                  "to be the fleet boss! You can create a clickable FAT link and "
                  "share it, if you like.</p>")),
        )

        # Return to "Add FAT Link" view
        return redirect("afat:fatlinks_add_fatlink")

    # check if this character already has a fleet
    creator_character = EveCharacter.objects.get(
        character_id=token.character_id)
    registered_fleets_for_creator = AFatLink.objects.select_related_default(
    ).filter(
        is_esilink=True,
        is_registered_on_esi=True,
        character__character_name=creator_character.character_name,
    )

    fleet_already_registered = False
    character_has_registered_fleets = False
    registered_fleets_to_close = []

    if registered_fleets_for_creator.count() > 0:
        character_has_registered_fleets = True

        for registered_fleet in registered_fleets_for_creator:
            if registered_fleet.esi_fleet_id == fleet_from_esi["fleet_id"]:
                # Character already has a fleet
                fleet_already_registered = True
            else:
                registered_fleets_to_close.append(
                    {"registered_fleet": registered_fleet})

    # If the FC already has a fleet, and it is the same as already registered,
    # just throw a warning
    if fleet_already_registered is True:
        messages.warning(
            request,
            mark_safe(
                _(f'<h4>Warning!</h4><p>Fleet with ID "{fleet_from_esi["fleet_id"]}" for your character {creator_character.character_name} has already been registered and pilots joining this fleet are automatically tracked.</p>'
                  )),
        )

        # Return to "Add FAT Link" view
        return redirect("afat:fatlinks_add_fatlink")

    # If it's a new fleet, remove all former registered fleets, if there are any
    if (character_has_registered_fleets is True
            and fleet_already_registered is False
            and len(registered_fleets_to_close) > 0):
        for registered_fleet_to_close in registered_fleets_to_close:
            reason = (f"FC has opened a new fleet with the "
                      f"character {creator_character.character_name}")

            logger.info(
                f"Closing ESI FAT link with hash "
                f'"{registered_fleet_to_close["registered_fleet"].hash}". '
                f"Reason: {reason}")

            registered_fleet_to_close[
                "registered_fleet"].is_registered_on_esi = False
            registered_fleet_to_close["registered_fleet"].save()

    # Check if we deal with the fleet boss here
    try:
        esi_fleet_member = esi.client.Fleets.get_fleets_fleet_id_members(
            fleet_id=fleet_from_esi["fleet_id"],
            token=esi_token.valid_access_token(),
        ).result()
    except Exception:
        messages.warning(
            request,
            mark_safe(
                _("<h4>Warning!</h4>"
                  "<p>Not Fleet Boss! Only the fleet boss can utilize the ESI "
                  "function. You can create a clickable FAT link and share it, "
                  "if you like.</p>")),
        )

        # Return to "Add FAT Link" view
        return redirect("afat:fatlinks_add_fatlink")

    creator_character = EveCharacter.objects.get(
        character_id=token.character_id)

    # Create the fat link
    fatlink = AFatLink(
        afattime=timezone.now(),
        fleet=request.session["fatlink_form__name"],
        creator=request.user,
        character=creator_character,
        hash=fatlink_hash,
        is_esilink=True,
        is_registered_on_esi=True,
        esi_fleet_id=fleet_from_esi["fleet_id"],
    )

    # Add fleet type, if there is any
    if request.session["fatlink_form__type"] is not None:
        fatlink.link_type_id = request.session["fatlink_form__type"]

    # Save it
    fatlink.save()

    # Writing DB log
    fleet_type = ""
    if fatlink.link_type:
        fleet_type = f"(Fleet Type: {fatlink.link_type.name})"

    write_log(
        request=request,
        log_event=AFatLog.Event.CREATE_FATLINK,
        log_text=(
            f'ESI FAT link with name "{request.session["fatlink_form__name"]}" '
            f"{fleet_type} was created by {request.user}"),
        fatlink_hash=fatlink.hash,
    )

    logger.info(f'ESI FAT link "{fatlink_hash}" with name '
                f'"{request.session["fatlink_form__name"]}"{fleet_type} '
                f"was created by {request.user}")

    # Clear session
    del request.session["fatlink_form__name"]
    del request.session["fatlink_form__type"]

    # Process fleet members in background
    process_fats.delay(data_list=esi_fleet_member,
                       data_source="esi",
                       fatlink_hash=fatlink_hash)

    messages.success(
        request,
        mark_safe(
            _("<h4>Success!</h4>"
              "<p>FAT Link Created!</p>"
              "<p>FATs have been queued, they may take a few mins to show up.</p>"
              "<p>Pilots who join later will be automatically added until you "
              "close or leave the fleet in-game.</p>")),
    )

    return redirect("afat:fatlinks_details_fatlink", fatlink_hash=fatlink_hash)
    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")
        )