Beispiel #1
0
def close_esi_fleet(fatlink: AFatLink, reason: str) -> None:
    """
    Closing ESI fleet
    :param fatlink:
    :param reason:
    """

    logger.info(
        "Closing ESI FAT link with hash {fatlink_hash}. Reason: {reason}".
        format(fatlink_hash=fatlink.hash, reason=reason))

    fatlink.is_registered_on_esi = False
    fatlink.save()
Beispiel #2
0
def close_esi_fleet(fatlink: AFatLink, reason: str) -> None:
    """
    Closing ESI fleet
    :param fatlink:
    :type fatlink:
    :param reason:
    :type reason:
    :return:
    :rtype:
    """

    logger.info(
        f'Closing ESI FAT link with hash "{fatlink.hash}". Reason: {reason}')

    fatlink.is_registered_on_esi = False
    fatlink.save()
Beispiel #3
0
    def _import_from_imicusfat(self) -> None:
        # 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_dellog_count = AFatDelLog.objects.all().count()
            current_afat_links_count = AFatLink.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_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 FAT links
            bfat_fatlinks = BfatFatLink.objects.all()
            for bfat_fatlink in bfat_fatlinks:
                self.stdout.write(
                    "Importing FAT link for fleet '{fleet}' with hash '{fatlink_hash}'."
                    .format(
                        fleet=bfat_fatlink.fleet,
                        fatlink_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()

            # import FATs
            bfat_fats = BfatFat.objects.all()
            for bfat_fat in bfat_fats:
                self.stdout.write(
                    "Importing FATs for FAT link ID '{fatlink_id}'.".format(
                        fatlink_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(
                    "Importing FAT duration with ID '{duration_id}'.".format(
                        duration_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 dellog
            bfat_dellogs = BfatDelLog.objects.all()
            for bfat_dellog in bfat_dellogs:
                self.stdout.write(
                    "Importing FAT dellogwith ID '{dellog_id}'.".format(
                        dellog_id=bfat_dellog.id))

                afat_dellog = AFatDelLog()

                afat_dellog.id = bfat_dellog.id
                afat_dellog.deltype = bfat_dellog.deltype
                afat_dellog.string = bfat_dellog.string
                afat_dellog.remover_id = bfat_dellog.remover_id

                afat_dellog.save()

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

                afat_manualfat = ManualAFat()

                afat_manualfat.id = bfat_manualfat.id
                afat_manualfat.character_id = bfat_manualfat.character_id
                afat_manualfat.creator_id = bfat_manualfat.creator_id
                afat_manualfat.afatlink_id = bfat_manualfat.fatlink_id

                afat_manualfat.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!"
                ))
Beispiel #4
0
def link_create_esi(request: WSGIRequest, token, fatlink_hash: str):
    """
    helper: create ESI link
    :param request:
    :param token:
    :param fatlink_hash:
    :return:
    """

    # 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:link_add")

    # 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 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:link_add")

    # 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:
            logger.info(
                "Closing ESI FAT link with hash {fatlink_hash}. Reason: {reason}".format(
                    fatlink_hash=registered_fleet_to_close["registered_fleet"].hash,
                    reason=(
                        "FC has opened a new fleet with the character {character}"
                    ).format(character=creator_character.character_name),
                )
            )

            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:link_add")

    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
        and request.session["fatlink_form__type"] != -1
    ):
        fatlink.link_type = AFatLinkType.objects.get(
            id=request.session["fatlink_form__type"]
        )

    # save it
    fatlink.save()

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

    # process fleet members
    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

    logger.info(
        "ESI FAT link {fatlink_hash} created by {user}".format(
            fatlink_hash=fatlink_hash, user=request.user
        )
    )

    return redirect("afat:link_edit", fatlink_hash=fatlink_hash)
Beispiel #5
0
def link_create_click(request: WSGIRequest):
    """
    create fatlink helper
    :param request:
    :return:
    """

    if request.method == "POST":
        form = AFatClickFatForm(request.POST)

        if form.is_valid():
            fatlink_hash = get_random_string(length=30)

            link = AFatLink()
            link.fleet = form.cleaned_data["name"]

            if (
                form.cleaned_data["type"] is not None
                and form.cleaned_data["type"] != -1
            ):
                link.link_type = AFatLinkType.objects.get(id=form.cleaned_data["type"])

            link.creator = request.user
            link.hash = fatlink_hash
            link.afattime = timezone.now()
            link.save()

            dur = ClickAFatDuration()
            dur.fleet = AFatLink.objects.get(hash=fatlink_hash)
            dur.duration = form.cleaned_data["duration"]
            dur.save()

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

            logger.info(
                "FAT link {fatlink_hash} with name {name} and a "
                "duration of {duration} minutes was created by {user}".format(
                    fatlink_hash=fatlink_hash,
                    name=form.cleaned_data["name"],
                    duration=form.cleaned_data["duration"],
                    user=request.user,
                )
            )

            return redirect("afat:link_edit", fatlink_hash=fatlink_hash)

        request.session["msg"] = [
            "danger",
            (
                "Something went wrong when attempting to submit your"
                " clickable FAT Link."
            ),
        ]
        return redirect("afat:dashboard")

    request.session["msg"] = [
        "warning",
        (
            'You must fill out the form on the "Add FAT Link" '
            "page to create a clickable FAT Link"
        ),
    ]

    return redirect("afat:dashboard")
Beispiel #6
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!"))
Beispiel #7
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 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 create_clickable_fatlink(request: WSGIRequest, ) -> HttpResponseRedirect:
    """
    create clickable fat link
    :param request:
    :type request:
    :return:
    :rtype:
    """

    if request.method == "POST":
        form = AFatClickFatForm(request.POST)

        if form.is_valid():
            fatlink_hash = get_random_string(length=30)

            fatlink = AFatLink()
            fatlink.fleet = form.cleaned_data["name"]

            if form.cleaned_data["type"] is not None:
                fatlink.link_type = form.cleaned_data["type"]

            fatlink.creator = request.user
            fatlink.hash = fatlink_hash
            fatlink.afattime = timezone.now()
            fatlink.save()

            dur = ClickAFatDuration()
            dur.fleet = AFatLink.objects.get(hash=fatlink_hash)
            dur.duration = form.cleaned_data["duration"]
            dur.save()

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

            # 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'FAT link with name "{form.cleaned_data["name"]}"{fleet_type} and '
                 f'a duration of {form.cleaned_data["duration"]} minutes was created'
                 ),
                fatlink_hash=fatlink.hash,
            )

            logger.info(
                (f'FAT link "{fatlink_hash}" with name '
                 f'"{form.cleaned_data["name"]}"{fleet_type} and a duration '
                 f'of {form.cleaned_data["duration"]} minutes was created '
                 f"by {request.user}"))

            return redirect("afat:fatlinks_details_fatlink",
                            fatlink_hash=fatlink_hash)

        request.session["msg"] = [
            "danger",
            "Something went wrong when attempting to submit yourclickable FAT Link.",
        ]
        return redirect("afat:dashboard")

    request.session["msg"] = [
        "warning",
        ('You must fill out the form on the "Add FAT Link" '
         "page to create a clickable FAT Link"),
    ]

    return redirect("afat:dashboard")
Beispiel #10
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!"))
Beispiel #11
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)
Beispiel #12
0
def create_clickable_fatlink(request: WSGIRequest, ) -> HttpResponseRedirect:
    """
    Create clickable fat link
    :param request:
    :type request:
    :return:
    :rtype:
    """

    if request.method == "POST":
        form = AFatClickFatForm(request.POST)

        if form.is_valid():
            fatlink_hash = get_hash_on_save()

            fatlink = AFatLink()
            fatlink.fleet = form.cleaned_data["name"]

            if form.cleaned_data["type"] is not None:
                fatlink.link_type = form.cleaned_data["type"]

            fatlink.creator = request.user
            fatlink.hash = fatlink_hash
            fatlink.afattime = timezone.now()
            fatlink.save()

            dur = ClickAFatDuration()
            dur.fleet = AFatLink.objects.get(hash=fatlink_hash)
            dur.duration = form.cleaned_data["duration"]
            dur.save()

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

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

            logger.info(
                f'FAT link "{fatlink_hash}" with name '
                f'"{form.cleaned_data["name"]}"{fleet_type} and a duration '
                f'of {form.cleaned_data["duration"]} minutes was created '
                f"by {request.user}")

            messages.success(
                request,
                mark_safe(
                    _("<h4>Success!</h4>"
                      "<p>Clickable FAT Link Created!</p>"
                      "<p>Make sure to give your fleet members the link to "
                      "click so that they get credit for this fleet.</p>")),
            )

            return redirect("afat:fatlinks_details_fatlink",
                            fatlink_hash=fatlink_hash)

        messages.error(
            request,
            mark_safe(
                _("<h4>Error!</h4>"
                  "<p>Something went wrong when attempting "
                  "to submit your clickable FAT Link.</p>")),
        )

        return redirect("afat:fatlinks_add_fatlink")

    messages.warning(
        request,
        mark_safe(
            _("<h4>Warning!</h4>"
              '<p>You must fill out the form on the "Add FAT Link" '
              "page to create a clickable FAT Link</p>")),
    )

    return redirect("afat:fatlinks_add_fatlink")
Beispiel #13
0
    def _import_from_aa_fat(self) -> None:
        # 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 really 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(
                    "Importing FAT link for fleet '{fleet}' with hash '{fatlink_hash}'.".format(
                        fleet=aa_fatlink.fleet, fatlink_hash=aa_fatlink.hash
                    )
                )

                afatlink = AFatLink()

                afatlink.id = aa_fatlink.id
                afatlink.afattime = aa_fatlink.fatdatetime
                afatlink.fleet = aa_fatlink.fleet
                afatlink.hash = aa_fatlink.hash
                afatlink.creator_id = aa_fatlink.creator_id

                afatlink.save()

            aa_fats = Fat.objects.all()
            for aa_fat in aa_fats:
                self.stdout.write(
                    "Importing FATs for FAT link ID '{fatlink_id}'.".format(
                        fatlink_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 INSTALLES_APPS in your local.py!"
                )
            )
    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")
        )
    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")
        )