Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ADD] Optional Doctrine Dropdown to FAT Creation Forms #363

Merged
merged 9 commits into from
Nov 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 26 additions & 1 deletion afat/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,18 @@
Admin pages configuration
"""

# Third Party
from solo.admin import SingletonModelAdmin

# Django
from django.contrib import admin, messages
from django.db.models import Count
from django.utils.translation import gettext_lazy as _
from django.utils.translation import ngettext

# Alliance Auth AFAT
from afat.models import Fat, FatLink, FleetType, Log
from afat.forms import DoctrineAdminForm, SettingAdminForm
from afat.models import Doctrine, Fat, FatLink, FleetType, Log, Setting


def custom_filter(title):
Expand Down Expand Up @@ -287,3 +291,24 @@ class AFatLogAdmin(admin.ModelAdmin):
"user__profile__main_character__character_name",
"user__username",
)


@admin.register(Setting)
class SettingAdmin(SingletonModelAdmin):
"""
Setting Admin
"""

form = SettingAdminForm


@admin.register(Doctrine)
class DoctrineAdmin(admin.ModelAdmin):
"""
Doctrine Admin
"""

form = DoctrineAdminForm

# Display all fields in the admin page
list_display = [field.name for field in Doctrine._meta.get_fields()]
32 changes: 32 additions & 0 deletions afat/app_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import unidecode

# Django
from django.apps import apps
from django.utils.text import slugify
from django.utils.translation import gettext_lazy as _

Expand Down Expand Up @@ -37,3 +38,34 @@
)

AFAT_BASE_URL = slugify(unidecode.unidecode(AFAT_APP_NAME), allow_unicode=True)


def fittings_installed() -> bool:
"""
Check if the Fittings module is installed

:return:
:rtype:
"""

return apps.is_installed(app_name="fittings")


def use_fittings_module_for_doctrines() -> bool:
"""
Check if the Fittings module is used for doctrines

:return:
:rtype:
"""

# Alliance Auth AFAT
from afat.models import ( # pylint: disable=import-outside-toplevel, cyclic-import
Setting,
)

return (
fittings_installed() is True
and Setting.get_setting(Setting.Field.USE_DOCTRINES_FROM_FITTINGS_MODULE)
is True
)
54 changes: 51 additions & 3 deletions afat/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

# Alliance Auth AFAT
from afat.app_settings import AFAT_DEFAULT_FATLINK_EXPIRY_TIME
from afat.models import FleetType
from afat.models import Doctrine, FleetType, Setting


def get_mandatory_form_label_text(text):
Expand Down Expand Up @@ -48,7 +48,16 @@ class AFatEsiFatForm(forms.Form):
required=False,
label=_("Fleet type (optional)"),
queryset=FleetType.objects.filter(is_enabled=True),
# empty_label=_("Please select a fleet type"),
)
doctrine_esi = forms.CharField(
required=False,
label=_("Doctrine (optional)"),
widget=forms.TextInput(
attrs={
"data-datalist": "afat-fleet-doctrine-list",
"data-full-width": "true",
}
),
)


Expand Down Expand Up @@ -90,7 +99,16 @@ class AFatClickFatForm(forms.Form):
required=False,
label=_("Fleet type (optional)"),
queryset=FleetType.objects.filter(is_enabled=True),
# empty_label=_("Please select a fleet type"),
)
doctrine = forms.CharField(
required=False,
label=_("Doctrine (optional)"),
widget=forms.TextInput(
attrs={
"data-datalist": "afat-fleet-doctrine-list",
"data-full-width": "true",
}
),
)
duration = forms.IntegerField(
required=True,
Expand All @@ -112,3 +130,33 @@ class FatLinkEditForm(forms.Form):
label=get_mandatory_form_label_text(text=_("Fleet name")),
max_length=255,
)


class SettingAdminForm(forms.ModelForm):
"""
Form definitions for the FleetType form
"""

class Meta: # pylint: disable=too-few-public-methods
"""
Meta
"""

model = Setting

fields = "__all__"


class DoctrineAdminForm(forms.ModelForm):
"""
Form definitions for the Doctrine form
"""

class Meta: # pylint: disable=too-few-public-methods
"""
Meta
"""

model = Doctrine

fields = "__all__"
48 changes: 48 additions & 0 deletions afat/helper/fatlinks.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@
Helper functions for fat links view
"""

# Django
from django.db.models import Prefetch

# Alliance Auth
from allianceauth.authentication.admin import User

# Alliance Auth AFAT
from afat.app_settings import use_fittings_module_for_doctrines
from afat.models import FatLink


Expand Down Expand Up @@ -39,3 +43,47 @@
"has_open_esi_fleets": has_open_esi_fleets,
"open_esi_fleets_list": open_esi_fleets_list,
}


def get_doctrines() -> list[dict[int, str]]:
"""
Get all enabled doctrines

:return:
:rtype:
"""

if use_fittings_module_for_doctrines() is True:
# Third Party
from fittings.models import ( # pylint: disable=import-outside-toplevel

Check warning on line 58 in afat/helper/fatlinks.py

View check run for this annotation

Codecov / codecov/patch

afat/helper/fatlinks.py#L58

Added line #L58 was not covered by tests
Doctrine,
Fitting,
)

cls = Doctrine.objects

Check warning on line 63 in afat/helper/fatlinks.py

View check run for this annotation

Codecov / codecov/patch

afat/helper/fatlinks.py#L63

Added line #L63 was not covered by tests

doctrines = (

Check warning on line 65 in afat/helper/fatlinks.py

View check run for this annotation

Codecov / codecov/patch

afat/helper/fatlinks.py#L65

Added line #L65 was not covered by tests
cls.prefetch_related("category")
.prefetch_related(
Prefetch(
lookup="fittings",
queryset=Fitting.objects.select_related("ship_type"),
)
)
.union(
cls.prefetch_related("category").prefetch_related(
Prefetch(
lookup="fittings",
queryset=Fitting.objects.select_related("ship_type"),
)
)
)
.order_by("name")
)
else:
# Alliance Auth AFAT
from afat.models import Doctrine # pylint: disable=import-outside-toplevel

doctrines = Doctrine.objects.filter(is_enabled=True).distinct().order_by("name")

return doctrines
14 changes: 7 additions & 7 deletions afat/helper/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,7 @@ def convert_fatlinks_to_dict( # pylint: disable=too-many-locals
actions += (
'<a class="btn btn-danger btn-sm" data-bs-toggle="modal" '
f'data-bs-target="#deleteFatLinkModal" data-url="{button_delete_url}" '
f'data-confirm-text="{button_delete_text}"'
f'data-body-text="{modal_body_text}">'
f'data-confirm-text="{button_delete_text}" data-body-text="{modal_body_text}">'
'<i class="fa-solid fa-trash-can fa-fw"></i></a>'
)

Expand All @@ -138,6 +137,7 @@ def convert_fatlinks_to_dict( # pylint: disable=too-many-locals
"fleet_name": fatlink_fleet + esi_fleet_marker,
"creator_name": creator_main_character,
"fleet_type": fatlink_type,
"doctrine": fatlink.doctrine,
"fleet_time": {"time": fleet_time, "timestamp": fleet_time_timestamp},
"fats_number": fatlink.fats_count,
"hash": fatlink.hash,
Expand All @@ -161,9 +161,6 @@ def convert_fats_to_dict(request: WSGIRequest, fat: Fat) -> dict:
:rtype:
"""

# Fleet type
fleet_type = fat.fatlink.link_type.name if fat.fatlink.link_type is not None else ""

# ESI marker
via_esi = "No"
esi_fleet_marker = ""
Expand Down Expand Up @@ -196,7 +193,7 @@ def convert_fats_to_dict(request: WSGIRequest, fat: Fat) -> dict:
'data-bs-toggle="modal" '
'data-bs-target="#deleteFatModal" '
f'data-url="{button_delete_fat}" '
f'data-confirm-text="{button_delete_text}"'
f'data-confirm-text="{button_delete_text}" '
f'data-body-text="{modal_body_text}">'
'<i class="fa-solid fa-eye"></i>'
"</a>"
Expand All @@ -213,8 +210,11 @@ def convert_fats_to_dict(request: WSGIRequest, fat: Fat) -> dict:
"ship_type": fat.shiptype,
"character_name": fat.character.character_name,
"fleet_name": fleet_name + esi_fleet_marker,
"doctrine": fat.fatlink.doctrine,
"fleet_time": {"time": fleet_time, "timestamp": fleet_time_timestamp},
"fleet_type": fleet_type,
"fleet_type": (
fat.fatlink.link_type.name if fat.fatlink.link_type is not None else ""
),
"via_esi": via_esi,
"actions": actions,
}
Expand Down
98 changes: 98 additions & 0 deletions afat/migrations/0024_doctrines_and_settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
# Generated by Django 4.2.16 on 2024-11-08 16:42

# Django
from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("afat", "0023_the_big_rename"),
]

operations = [
migrations.CreateModel(
name="Doctrine",
fields=[
(
"id",
models.AutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
(
"name",
models.CharField(
help_text="Short name to identify this doctrine",
max_length=255,
unique=True,
verbose_name="Name",
),
),
(
"notes",
models.TextField(
blank=True,
default="",
help_text="You can add notes about this doctrine here if you want. (optional)",
verbose_name="Notes",
),
),
(
"is_enabled",
models.BooleanField(
db_index=True,
default=True,
help_text="Whether this doctrine is enabled or not.",
verbose_name="Is enabled",
),
),
],
options={
"verbose_name": "Doctrine",
"verbose_name_plural": "Doctrines",
"default_permissions": (),
},
),
migrations.CreateModel(
name="Setting",
fields=[
(
"id",
models.AutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
(
"use_doctrines_from_fittings_module",
models.BooleanField(
db_index=True,
default=False,
help_text="Whether to use the doctrines from the Fittings modules in the doctrine dropdown. Note: The fittings module needs to be installed for this.",
verbose_name="Use Doctrines from Fittings module",
),
),
],
options={
"verbose_name": "Setting",
"verbose_name_plural": "Settings",
"default_permissions": (),
},
),
migrations.AddField(
model_name="fatlink",
name="doctrine",
field=models.CharField(
blank=True,
default="",
help_text="The FAT link doctrine",
max_length=254,
),
),
]
Loading