Exemplo n.º 1
0
def process_line(line, type_, fatlink_hash):
    """
    process_line
    processing every single character on its own
    :param line:
    :param type_:
    :param fatlink_hash:
    :return:
    """

    link = AFatLink.objects.get(hash=fatlink_hash)

    if type_ == "comp":
        character = get_or_create_char(name=line[0].strip(" "))
        system = line[1].strip(" (Docked)")
        shiptype = line[2]

        if character is not None:
            AFat(
                afatlink_id=link.pk,
                character=character,
                system=system,
                shiptype=shiptype,
            ).save()
    else:
        character = get_or_create_char(name=line.strip(" "))

        if character is not None:
            AFat(afatlink_id=link.pk, character=character).save()
Exemplo n.º 2
0
def process_character(char, fatlink_hash):
    """
    process_character
    :param char:
    :type char:
    :param fatlink_hash:
    :type fatlink_hash:
    :return:
    :rtype:
    """

    link = AFatLink.objects.get(hash=fatlink_hash)
    char_id = char["character_id"]
    character = get_or_create_character(character_id=char_id)

    # only process if the character is not already registered for this FAT
    if AFat.objects.filter(character=character, afatlink_id=link.pk).exists() is False:
        solar_system_id = char["solar_system_id"]
        ship_type_id = char["ship_type_id"]

        solar_system = esi.client.Universe.get_universe_systems_system_id(
            system_id=solar_system_id
        ).result()
        ship = esi.client.Universe.get_universe_types_type_id(
            type_id=ship_type_id
        ).result()

        solar_system_name = solar_system["name"]
        ship_name = ship["name"]

        logger.info(
            "New Pilot: Adding {character_name} in {system_name} flying a {ship_name} "
            'to FAT link "{fatlink_hash}"'.format(
                character_name=character,
                system_name=solar_system_name,
                ship_name=ship_name,
                fatlink_hash=fatlink_hash,
            )
        )

        AFat(
            afatlink_id=link.pk,
            character=character,
            system=solar_system_name,
            shiptype=ship_name,
        ).save()
Exemplo n.º 3
0
def process_character(character_id: int, solar_system_id: int,
                      ship_type_id: int, fatlink_hash: str):
    """
    Process character
    :param character_id:
    :param solar_system_id:
    :param ship_type_id:
    :param fatlink_hash:
    :return:
    """

    # Only process if the character is not already registered for this FAT
    if (AFat.objects.filter(character__character_id=character_id,
                            afatlink__hash__exact=fatlink_hash).exists() is
            False):
        character = get_or_create_character(character_id=character_id)
        link = AFatLink.objects.get(hash=fatlink_hash)

        solar_system = esi.client.Universe.get_universe_systems_system_id(
            system_id=solar_system_id).result()
        ship = esi.client.Universe.get_universe_types_type_id(
            type_id=ship_type_id).result()

        solar_system_name = solar_system["name"]
        ship_name = ship["name"]

        logger.info(
            f"New Pilot: Adding {character} in {solar_system_name} flying "
            f'a {ship_name} to FAT link "{fatlink_hash}"')

        AFat(
            afatlink_id=link.pk,
            character=character,
            system=solar_system_name,
            shiptype=ship_name,
        ).save()
Exemplo n.º 4
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!"
                ))
Exemplo n.º 5
0
def link_edit(request: WSGIRequest, fatlink_hash: str = None) -> HttpResponse:
    """
    edit fatlink view
    :param request:
    :param fatlink_hash:
    :return:
    """

    if fatlink_hash is None:
        request.session["msg"] = ["warning", "No FAT Link hash provided."]

        return redirect("afat:dashboard")

    try:
        link = AFatLink.objects.get(hash=fatlink_hash)
    except AFatLink.DoesNotExist:
        request.session["msg"] = ["warning", "The hash provided is not valid."]

        return redirect("afat:dashboard")

    if request.method == "POST":
        fatlink_edit_form = FatLinkEditForm(request.POST)
        manual_fat_form = AFatManualFatForm(request.POST)

        if fatlink_edit_form.is_valid():
            link.fleet = fatlink_edit_form.cleaned_data["fleet"]
            link.save()
            request.session[
                "{fatlink_hash}-task-code".format(fatlink_hash=fatlink_hash)
            ] = 1
        elif manual_fat_form.is_valid():
            character_name = manual_fat_form.cleaned_data["character"]
            system = manual_fat_form.cleaned_data["system"]
            shiptype = manual_fat_form.cleaned_data["shiptype"]
            creator = request.user
            character = get_or_create_char(name=character_name)
            created_at = timezone.now()

            if character is not None:
                AFat(
                    afatlink_id=link.pk,
                    character=character,
                    system=system,
                    shiptype=shiptype,
                ).save()

                ManualAFat(
                    afatlink_id=link.pk,
                    creator=creator,
                    character=character,
                    created_at=created_at,
                ).save()

                request.session[
                    "{fatlink_hash}-task-code".format(fatlink_hash=fatlink_hash)
                ] = 3
            else:
                request.session[
                    "{fatlink_hash}-task-code".format(fatlink_hash=fatlink_hash)
                ] = 4
        else:
            request.session[
                "{fatlink_hash}-task-code".format(fatlink_hash=fatlink_hash)
            ] = 0

    msg_code = None
    message = None

    if "msg" in request.session:
        msg_code = 999
        message = request.session.pop("msg")
    elif (
        "{fatlink_hash}-creation-code".format(fatlink_hash=fatlink_hash)
        in request.session
    ):
        msg_code = request.session.pop(
            "{fatlink_hash}-creation-code".format(fatlink_hash=fatlink_hash)
        )
    elif (
        "{fatlink_hash}-task-code".format(fatlink_hash=fatlink_hash) in request.session
    ):
        msg_code = request.session.pop(
            "{fatlink_hash}-task-code".format(fatlink_hash=fatlink_hash)
        )

    # Flatlist / Raw Data Tab (deactivated as of 2020-12-26)
    # fats = AFat.objects.filter(afatlink=link)
    # flatlist = None
    # if len(fats) > 0:
    #     flatlist = []
    #
    #     for fat in fats:
    #         fatinfo = [fat.character.character_name, str(fat.system), str(fat.shiptype)]
    #         flatlist.append("\t".join(fatinfo))
    #
    #     flatlist = "\r\n".join(flatlist)

    # let's see if the link is still valid or has expired already
    link_ongoing = True
    try:
        dur = ClickAFatDuration.objects.get(fleet=link)
        now = timezone.now() - timedelta(minutes=dur.duration)

        if now >= link.afattime:
            # link expired
            link_ongoing = False
    except ClickAFatDuration.DoesNotExist:
        # ESI lnk
        link_ongoing = False

    context = {
        "form": AFatLinkForm,
        "msg_code": msg_code,
        "message": message,
        "link": link,
        # "flatlist": flatlist,
        "link_ongoing": link_ongoing,
    }

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

    return render(request, "afat/fleet_edit.html", context)
Exemplo n.º 6
0
def click_link(request: WSGIRequest, token, fatlink_hash: str = None):
    """
    click fatlink helper
    :param request:
    :param token:
    :param fatlink_hash:
    :return:
    """

    if fatlink_hash is None:
        request.session["msg"] = ["warning", "No FAT link hash provided."]

        return redirect("afat:dashboard")

    try:
        try:
            fleet = AFatLink.objects.get(hash=fatlink_hash)
        except AFatLink.DoesNotExist:
            request.session["msg"] = ["warning", "The hash provided is not valid."]

            return redirect("afat:dashboard")

        dur = ClickAFatDuration.objects.get(fleet=fleet)
        now = timezone.now() - timedelta(minutes=dur.duration)

        if now >= fleet.afattime:
            request.session["msg"] = [
                "warning",
                (
                    "Sorry, that FAT Link is expired. If you were on that fleet, "
                    "contact your FC about having your FAT manually added."
                ),
            ]

            return redirect("afat:dashboard")

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

        try:
            required_scopes = [
                "esi-location.read_location.v1",
                "esi-location.read_online.v1",
                "esi-location.read_ship_type.v1",
            ]
            esi_token = Token.get_token(token.character_id, required_scopes)

            # check if character is online
            character_online = esi.client.Location.get_characters_character_id_online(
                character_id=token.character_id, token=esi_token.valid_access_token()
            ).result()

            if character_online["online"] is True:
                # character location
                location = esi.client.Location.get_characters_character_id_location(
                    character_id=token.character_id,
                    token=esi_token.valid_access_token(),
                ).result()

                # current ship
                ship = esi.client.Location.get_characters_character_id_ship(
                    character_id=token.character_id,
                    token=esi_token.valid_access_token(),
                ).result()

                # system information
                system = esi.client.Universe.get_universe_systems_system_id(
                    system_id=location["solar_system_id"]
                ).result()["name"]

                ship_name = provider.get_itemtype(ship["ship_type_id"]).name

                try:
                    fat = AFat(
                        afatlink=fleet,
                        character=character,
                        system=system,
                        shiptype=ship_name,
                    )
                    fat.save()

                    if fleet.fleet is not None:
                        name = fleet.fleet
                    else:
                        name = fleet.hash

                    request.session["msg"] = [
                        "success",
                        (
                            "FAT registered for {character_name} "
                            "at {fleet_name}".format(
                                character_name=character.character_name, fleet_name=name
                            )
                        ),
                    ]

                    logger.info(
                        "Fleetparticipation for fleet {fleet_name} "
                        "registered for pilot {character_name}".format(
                            fleet_name=name, character_name=character.character_name
                        )
                    )

                    return redirect("afat:dashboard")
                except Exception:
                    request.session["msg"] = [
                        "warning",
                        (
                            "A FAT already exists for the selected character "
                            "({character_name}) and fleet combination.".format(
                                character_name=character.character_name
                            )
                        ),
                    ]

                    return redirect("afat:dashboard")
            else:
                request.session["msg"] = [
                    "warning",
                    (
                        "Cannot register the fleet participation for {character_name}. "
                        "The character needs to be online.".format(
                            character_name=character.character_name
                        )
                    ),
                ]

                return redirect("afat:dashboard")
        except Exception:
            request.session["msg"] = [
                "warning",
                (
                    "There was an issue with the token for {character_name}. "
                    "Please try again.".format(character_name=character.character_name)
                ),
            ]

            return redirect("afat:dashboard")
    except Exception:
        request.session["msg"] = [
            "warning",
            "The hash provided is not for a clickable FAT Link.",
        ]

        return redirect("afat:dashboard")
Exemplo n.º 7
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!"))
Exemplo n.º 8
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.º 9
0
def details_fatlink(request: WSGIRequest,
                    fatlink_hash: str = None) -> HttpResponse:
    """
    fatlink view
    :param request:
    :type request:
    :param fatlink_hash:
    :type fatlink_hash:
    :return:
    :rtype:
    """

    if fatlink_hash is None:
        request.session["msg"] = ["warning", "No FAT Link hash provided."]

        return redirect("afat:dashboard")

    try:
        link = AFatLink.objects.get(hash=fatlink_hash)
    except AFatLink.DoesNotExist:
        request.session["msg"] = ["warning", "The hash provided is not valid."]

        return redirect("afat:dashboard")

    if request.method == "POST":
        fatlink_edit_form = FatLinkEditForm(request.POST)
        manual_fat_form = AFatManualFatForm(request.POST)

        if fatlink_edit_form.is_valid():
            link.fleet = fatlink_edit_form.cleaned_data["fleet"]
            link.save()

            # writing DB log
            write_log(
                request=request,
                log_event=AFatLogEvent.CHANGE_FATLINK,
                log_text=(
                    'FAT link changed. Fleet name was set to "{fleet_name}"'
                ).format(fleet_name=link.fleet),
                fatlink_hash=link.hash,
            )

            logger.info(
                ('FAT link with hash "{fatlink_hash}" changed. '
                 'Fleet name was set to "{fleet_name}" by {user}').format(
                     fatlink_hash=link.hash,
                     fleet_name=link.fleet,
                     user=request.user))

            request.session["{fatlink_hash}-task-code".format(
                fatlink_hash=fatlink_hash)] = 1
        elif manual_fat_form.is_valid():
            character_name = manual_fat_form.cleaned_data["character"]
            system = manual_fat_form.cleaned_data["system"]
            shiptype = manual_fat_form.cleaned_data["shiptype"]
            character = get_or_create_character(name=character_name)

            if character is not None:
                AFat(
                    afatlink_id=link.pk,
                    character=character,
                    system=system,
                    shiptype=shiptype,
                ).save()

                request.session["{fatlink_hash}-task-code".format(
                    fatlink_hash=fatlink_hash)] = 3

                # writing DB log
                write_log(
                    request=request,
                    log_event=AFatLogEvent.MANUAL_FAT,
                    log_text=
                    ("Pilot {pilot_name} flying a {ship_type} was manually added"
                     ).format(
                         pilot_name=character.character_name,
                         ship_type=shiptype,
                     ),
                    fatlink_hash=link.hash,
                )

                logger.info((
                    "Pilot {pilot_name} flying a {ship_type} was manually added to "
                    'FAT link with hash "{fatlink_hash}" by {user}').format(
                        fatlink_hash=link.hash,
                        pilot_name=character.character_name,
                        ship_type=shiptype,
                        user=request.user,
                    ))
            else:
                request.session["{fatlink_hash}-task-code".format(
                    fatlink_hash=fatlink_hash)] = 4
        else:
            request.session["{fatlink_hash}-task-code".format(
                fatlink_hash=fatlink_hash)] = 2

    logger.info(
        'FAT link "{fatlink_hash}" details view called by {user}'.format(
            fatlink_hash=fatlink_hash, user=request.user))

    msg_code = None
    message = None

    if "msg" in request.session:
        msg_code = 0
        message = request.session.pop("msg")
    elif ("{fatlink_hash}-creation-code".format(fatlink_hash=fatlink_hash)
          in request.session):
        msg_code = request.session.pop(
            "{fatlink_hash}-creation-code".format(fatlink_hash=fatlink_hash))
    elif ("{fatlink_hash}-task-code".format(fatlink_hash=fatlink_hash)
          in request.session):
        msg_code = request.session.pop(
            "{fatlink_hash}-task-code".format(fatlink_hash=fatlink_hash))

    # let's see if the link is still valid or has expired already and can be re-opened
    # and FATs can be manually added
    # (only possible for 24 hours after creating the FAT link)
    link_ongoing = True
    link_can_be_reopened = False
    link_expires = None
    manual_fat_can_be_added = False

    # time dependant settings
    try:
        dur = ClickAFatDuration.objects.get(fleet=link)
        link_expires = link.afattime + timedelta(minutes=dur.duration)
        now = timezone.now()

        if link_expires <= now:
            # link expired
            link_ongoing = False

            if (link.reopened is False
                    and get_time_delta(link_expires, now, "minutes") <
                    AFAT_DEFAULT_FATLINK_REOPEN_GRACE_TIME):
                link_can_be_reopened = True

        # manual fat still possible?
        # only possible if the FAT link has not been re-opened
        # and has been created within the last 24 hours
        if link.reopened is False and get_time_delta(link.afattime, now,
                                                     "hours") < 24:
            manual_fat_can_be_added = True
    except ClickAFatDuration.DoesNotExist:
        # ESI link
        link_ongoing = False

    is_clickable_link = False
    if link.is_esilink is False:
        is_clickable_link = True

    context = {
        "msg_code": str(msg_code),
        "message": message,
        "link": link,
        "is_esi_link": link.is_esilink,
        "is_clickable_link": is_clickable_link,
        "link_expires": link_expires,
        "link_ongoing": link_ongoing,
        "link_can_be_reopened": link_can_be_reopened,
        "manual_fat_can_be_added": manual_fat_can_be_added,
        "reopen_grace_time": AFAT_DEFAULT_FATLINK_REOPEN_GRACE_TIME,
        "reopen_duration": AFAT_DEFAULT_FATLINK_REOPEN_DURATION,
    }

    return render(request, "afat/fatlinks_details_fatlink.html", context)
Exemplo n.º 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!"))
Exemplo n.º 11
0
def details_fatlink(request: WSGIRequest, fatlink_hash: str) -> HttpResponse:
    """
    Fat link view
    :param request:
    :type request:
    :param fatlink_hash:
    :type fatlink_hash:
    :return:
    :rtype:
    """

    try:
        link = AFatLink.objects.select_related_default().get(hash=fatlink_hash)
    except AFatLink.DoesNotExist:
        messages.warning(
            request,
            mark_safe(
                _("<h4>Warning!</h4><p>The hash provided is not valid.</p>")),
        )

        return redirect("afat:dashboard")

    if request.method == "POST":
        fatlink_edit_form = FatLinkEditForm(request.POST)
        manual_fat_form = AFatManualFatForm(request.POST)

        if fatlink_edit_form.is_valid():
            link.fleet = fatlink_edit_form.cleaned_data["fleet"]
            link.save()

            # Writing DB log
            write_log(
                request=request,
                log_event=AFatLog.Event.CHANGE_FATLINK,
                log_text=
                f'FAT link changed. Fleet name was set to "{link.fleet}"',
                fatlink_hash=link.hash,
            )

            logger.info(
                f'FAT link with hash "{link.hash}" changed. '
                f'Fleet name was set to "{link.fleet}" by {request.user}')

            messages.success(
                request,
                mark_safe(
                    _("<h4>Success!</h4><p>Fleet name successfully changed.</p>"
                      )),
            )
        elif manual_fat_form.is_valid():
            character_name = manual_fat_form.cleaned_data["character"]
            system = manual_fat_form.cleaned_data["system"]
            shiptype = manual_fat_form.cleaned_data["shiptype"]
            character = get_or_create_character(name=character_name)

            if character is not None:
                AFat(
                    afatlink_id=link.pk,
                    character=character,
                    system=system,
                    shiptype=shiptype,
                ).save()

                messages.success(
                    request,
                    mark_safe(
                        _("<h4>Success!</h4><p>Manual FAT processed.</p>")),
                )

                # Writing DB log
                write_log(
                    request=request,
                    log_event=AFatLog.Event.MANUAL_FAT,
                    log_text=(f"Pilot {character.character_name} "
                              f"flying a {shiptype} was manually added"),
                    fatlink_hash=link.hash,
                )

                logger.info(
                    f"Pilot {character.character_name} flying a {shiptype} was"
                    f" manually added to FAT link with "
                    f'hash "{link.hash}" by {request.user}')
            else:
                messages.error(
                    request,
                    mark_safe(
                        _("<h4>Oh No!</h4>"
                          "<p>Manual FAT processing failed! "
                          "The character name you entered was not found.</p>")
                    ),
                )
        else:
            messages.error(
                request,
                mark_safe(_("<h4>Oh No!</h4><p>Something went wrong!</p>")),
            )

    logger.info(
        f'FAT link "{fatlink_hash}" details view called by {request.user}')

    # Let's see if the link is still valid or has expired already and can be re-opened
    # and FATs can be manually added
    # (only possible for 24 hours after creating the FAT link)
    link_ongoing = True
    link_can_be_reopened = False
    link_expires = None
    manual_fat_can_be_added = False

    # Time dependant settings
    try:
        dur = ClickAFatDuration.objects.get(fleet=link)
    except ClickAFatDuration.DoesNotExist:
        # ESI link
        link_ongoing = False
    else:
        link_expires = link.afattime + timedelta(minutes=dur.duration)
        now = timezone.now()

        if link_expires <= now:
            # Link expired
            link_ongoing = False

            if (link.reopened is False
                    and get_time_delta(link_expires, now, "minutes") <
                    AFAT_DEFAULT_FATLINK_REOPEN_GRACE_TIME):
                link_can_be_reopened = True

        # Manual fat still possible?
        # Only possible if the FAT link has not been re-opened
        # and has been created within the last 24 hours
        if link.reopened is False and get_time_delta(link.afattime, now,
                                                     "hours") < 24:
            manual_fat_can_be_added = True

    is_clickable_link = False
    if link.is_esilink is False:
        is_clickable_link = True

    if link.is_esilink and link.is_registered_on_esi:
        link_ongoing = True

    context = {
        "link": link,
        "is_esi_link": link.is_esilink,
        "is_clickable_link": is_clickable_link,
        "link_expires": link_expires,
        "link_ongoing": link_ongoing,
        "link_can_be_reopened": link_can_be_reopened,
        "manual_fat_can_be_added": manual_fat_can_be_added,
        "reopen_grace_time": AFAT_DEFAULT_FATLINK_REOPEN_GRACE_TIME,
        "reopen_duration": AFAT_DEFAULT_FATLINK_REOPEN_DURATION,
    }

    return render(request, "afat/view/fatlinks/fatlinks_details_fatlink.html",
                  context)
Exemplo n.º 12
0
def add_fat(request: WSGIRequest,
            token,
            fatlink_hash: str = None) -> HttpResponseRedirect:
    """
    Click fat link helper
    :param request:
    :type request:
    :param token:
    :type token:
    :param fatlink_hash:
    :type fatlink_hash:
    :return:
    :rtype:
    """

    if fatlink_hash is None:
        messages.warning(
            request,
            mark_safe(_("<h4>Warning!</h4><p>No FAT link hash provided.</p>")),
        )

        return redirect("afat:dashboard")

    try:
        fleet = AFatLink.objects.get(hash=fatlink_hash, is_esilink=False)
    except AFatLink.DoesNotExist:
        messages.warning(
            request,
            mark_safe(
                _("<h4>Warning!</h4><p>The hash provided is not valid.</p>")),
        )

        return redirect("afat:dashboard")

    dur = ClickAFatDuration.objects.get(fleet=fleet)
    now = timezone.now() - timedelta(minutes=dur.duration)

    if now >= fleet.afattime:
        messages.warning(
            request,
            mark_safe(
                _("<h4>Warning!</h4>"
                  "<p>Sorry, that FAT Link is expired. "
                  "If you were on that fleet, contact your FC about "
                  "having your FAT manually added.</p>")),
        )

        return redirect("afat:dashboard")

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

    try:
        required_scopes = [
            "esi-location.read_location.v1",
            "esi-location.read_online.v1",
            "esi-location.read_ship_type.v1",
        ]
        esi_token = Token.get_token(token.character_id, required_scopes)
    except Exception:
        messages.warning(
            request,
            mark_safe(
                _(f"<h4>Warning!</h4><p>There was an issue with the ESI token for {character.character_name}. Please try again.</p>"
                  )),
        )

        return redirect("afat:dashboard")

    # Check if character is online
    character_online = esi.client.Location.get_characters_character_id_online(
        character_id=token.character_id,
        token=esi_token.valid_access_token()).result()

    if character_online["online"] is True:
        # Character location
        location = esi.client.Location.get_characters_character_id_location(
            character_id=token.character_id,
            token=esi_token.valid_access_token(),
        ).result()

        # Current ship
        ship = esi.client.Location.get_characters_character_id_ship(
            character_id=token.character_id,
            token=esi_token.valid_access_token(),
        ).result()

        # System information
        system = esi.client.Universe.get_universe_systems_system_id(
            system_id=location["solar_system_id"]).result()["name"]

        ship_name = provider.get_itemtype(ship["ship_type_id"]).name

        try:
            AFat(afatlink=fleet,
                 character=character,
                 system=system,
                 shiptype=ship_name).save()
        except IntegrityError:
            messages.warning(
                request,
                mark_safe(
                    _(f"<h4>Warning!</h4><p>A FAT already exists for the selected character ({character.character_name}) and fleet combination.</p>"
                      )),
            )
        else:
            if fleet.fleet is not None:
                fleet_name = fleet.fleet
            else:
                fleet_name = fleet.hash

            messages.success(
                request,
                mark_safe(
                    _(f"<h4>Success!</h4><p>FAT registered for {character.character_name} at {fleet_name}</p>"
                      )),
            )

            logger.info(
                f'Participation for fleet "{fleet_name}" registered for '
                f"pilot {character.character_name}")
    else:
        messages.warning(
            request,
            mark_safe(
                _(f"<h4>Warning!</h4><p>Cannot register the fleet participation for {character.character_name}. The character needs to be online.</p>"
                  )),
        )

    return redirect("afat:dashboard")
Exemplo n.º 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")
        )