Skip to content

Commit

Permalink
Merge pull request #366 from ppfeufer/migrate-settings-to-db
Browse files Browse the repository at this point in the history
  • Loading branch information
ppfeufer authored Nov 8, 2024
2 parents 30cb4f6 + 40c98c7 commit 490e087
Show file tree
Hide file tree
Showing 11 changed files with 176 additions and 74 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,14 @@ Section Order:
- Optional doctrine field to FAT link creation forms
- Integrity hashes to CSS and JavaScript

### Changed

- Settings from `local.py` moved to DB

### Removed

- `AFAT_APP_NAME` setting to ensure the app name can be properly localized

## \[3.3.0\] - 2024-11-06

### Added
Expand Down
18 changes: 9 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -189,15 +189,15 @@ python myauth/manage.py afat_import_from_allianceauth_fat

## Settings<a name="settings"></a>

To customize the module, the following settings are available.

| Name | Description | Default Value | Value Type |
| :------------------------------------- | :--------------------------------------------------------------- | :---------------------- | :--------- |
| AFAT_APP_NAME | Custom application name, in case you'd like a different name | Fleet Activity Tracking | string |
| AFAT_DEFAULT_FATLINK_EXPIRY_TIME | Default expiry time for clickable FAT links in Minutes | 60 | int |
| AFAT_DEFAULT_FATLINK_REOPEN_GRACE_TIME | Time in minutes a FAT link can be re-opened after it has expired | 60 | int |
| AFAT_DEFAULT_FATLINK_REOPEN_DURATION | Time in minutes a FAT link is re-opened | 60 | int |
| AFAT_DEFAULT_LOG_DURATION | Time in days before log entries are being removed from the DB | 60 | int |
To customize the module, the following settings can be managed in your admin backend:

| Setting | Description | Default Value |
| :--------------------------------- | :-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | :------------ |
| Use doctrines from fittings module | Whether to use the doctrines from the Fittings modules in the doctrine dropdown or from AFATs own doctrine list. (Note: The [fittings module](https://gitlab.com/colcrunch/fittings) needs to be installed for this.) | No |
| Default FAT link expiry time | Default expiry time for clickable FAT links in Minutes | 60 |
| Default FAT link reopen grace time | Time in minutes a FAT link can be re-opened after it has expired | 60 |
| Default FAT link reopen duration | Time in minutes a FAT link is re-opened | 60 |
| Default log duration | Time in days before log entries are being removed from the DB | 60 |

## Permissions<a name="permissions"></a>

Expand Down
34 changes: 3 additions & 31 deletions afat/app_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,42 +2,14 @@
Our app setting
"""

# Third Party
import unidecode

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

# Alliance Auth (External Libs)
from app_utils.app_settings import clean_setting

# Set default expiry time in minutes
AFAT_DEFAULT_FATLINK_EXPIRY_TIME = clean_setting(
name="AFAT_DEFAULT_FATLINK_EXPIRY_TIME", default_value=60
)

# Set the default time in minutes a FAT lnk can be re-opened after it is expired
AFAT_DEFAULT_FATLINK_REOPEN_GRACE_TIME = clean_setting(
name="AFAT_DEFAULT_FATLINK_REOPEN_GRACE_TIME", default_value=60
)

# Set the default time in minutes a FAT link is re-opened for
AFAT_DEFAULT_FATLINK_REOPEN_DURATION = clean_setting(
name="AFAT_DEFAULT_FATLINK_REOPEN_DURATION", default_value=60
)

AFAT_DEFAULT_LOG_DURATION = clean_setting(
name="AFAT_DEFAULT_LOG_DURATION", default_value=60
)

# Name of this app, as shown in the Auth sidebar and page titles
AFAT_APP_NAME = clean_setting(
name="AFAT_APP_NAME", default_value=_("Fleet Activity Tracking"), required_type=str
)
# Alliance Auth AFAT
from afat import __title__

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


def fittings_installed() -> bool:
Expand Down
6 changes: 3 additions & 3 deletions afat/auth_hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
from allianceauth.services.hooks import MenuItemHook, UrlHook

# Alliance Auth AFAT
from afat import urls
from afat.app_settings import AFAT_APP_NAME, AFAT_BASE_URL
from afat import __title__, urls
from afat.app_settings import AFAT_BASE_URL


class AaAfatMenuItem(MenuItemHook): # pylint: disable=too-few-public-methods
Expand All @@ -20,7 +20,7 @@ def __init__(self):
# Setup menu entry for sidebar
MenuItemHook.__init__(
self,
text=AFAT_APP_NAME,
text=__title__,
classes="fa-solid fa-space-shuttle",
url_name="afat:dashboard",
navactive=["afat:"],
Expand Down
19 changes: 14 additions & 5 deletions afat/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
from django.utils.translation import gettext_lazy as _

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


Expand All @@ -35,7 +34,7 @@ def get_mandatory_form_label_text(text):
class AFatEsiFatForm(forms.Form):
"""
Fat link form
Used to create ESI fatlinks
Used to create ESI FAT links
"""

name_esi = forms.CharField(
Expand Down Expand Up @@ -63,7 +62,7 @@ class AFatEsiFatForm(forms.Form):

class AFatManualFatForm(forms.Form):
"""
Manual fat form
Manual FAT form
"""

character = forms.CharField(
Expand All @@ -86,7 +85,7 @@ class AFatManualFatForm(forms.Form):
class AFatClickFatForm(forms.Form):
"""
Fat link form
Used to create clickable fatlinks
Used to create clickable FAT links
"""

name = forms.CharField(
Expand Down Expand Up @@ -114,10 +113,20 @@ class AFatClickFatForm(forms.Form):
required=True,
label=get_mandatory_form_label_text(text=_("FAT link expiry time in minutes")),
min_value=1,
initial=AFAT_DEFAULT_FATLINK_EXPIRY_TIME,
initial=0,
widget=forms.TextInput(attrs={"placeholder": _("Expiry time in minutes")}),
)

def __init__(self, *args, **kwargs):
self.request = kwargs.pop("request", None)
super().__init__(*args, **kwargs)

# This is a hack to set the initial value of the duration field,
# which comes from the settings in the database.
self.fields["duration"].initial = Setting.get_setting(
Setting.Field.DEFAULT_FATLINK_EXPIRY_TIME
)


class FatLinkEditForm(forms.Form):
"""
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# Generated by Django 4.2.16 on 2024-11-08 19:45

# Django
from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("afat", "0024_doctrines_and_settings"),
]

operations = [
migrations.AddField(
model_name="setting",
name="default_fatlink_expiry_time",
field=models.PositiveIntegerField(
default=60,
help_text="Default expiry time for clickable FAT links in minutes. (Default: 60 minutes)",
verbose_name="Default FAT link expiry time",
),
),
migrations.AddField(
model_name="setting",
name="default_fatlink_reopen_duration",
field=models.PositiveIntegerField(
default=60,
help_text="Default time in minutes a FAT link is re-opened for. (Default: 60 minutes)",
verbose_name="Default FAT link reopen duration",
),
),
migrations.AddField(
model_name="setting",
name="default_fatlink_reopen_grace_time",
field=models.PositiveIntegerField(
default=60,
help_text="Default time in minutes a FAT link can be re-opened after it is expired. (Default: 60 minutes)",
verbose_name="Default FAT link reopen grace time",
),
),
migrations.AddField(
model_name="setting",
name="default_log_duration",
field=models.PositiveIntegerField(
default=60,
help_text="Default time in days a log entry is kept. (Default: 60 days)",
verbose_name="Default log duration",
),
),
migrations.AlterField(
model_name="setting",
name="use_doctrines_from_fittings_module",
field=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",
),
),
]
45 changes: 44 additions & 1 deletion afat/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -440,10 +440,53 @@ class Field(models.TextChoices):
Choices for Setting.Field
"""

DEFAULT_FATLINK_EXPIRY_TIME = "default_fatlink_expiry_time", _(
"Default FAT link expiry time"
)
DEFAULT_FATLINK_REOPEN_DURATION = "default_fatlink_reopen_duration", _(
"Default FAT link reopen duration"
)
DEFAULT_FATLINK_REOPEN_GRACE_TIME = "default_fatlink_reopen_grace_time", _(
"Default FAT link reopen grace time"
)
DEFAULT_LOG_DURATION = "default_log_duration", _("Default log duration")
USE_DOCTRINES_FROM_FITTINGS_MODULE = "use_doctrines_from_fittings_module", _(
"Use Doctrines from Fittings module"
"Use doctrines from fittings module"
)

default_fatlink_expiry_time = models.PositiveIntegerField(
default=60,
help_text=_(
"Default expiry time for clickable FAT links in minutes. "
"(Default: 60 minutes)"
),
verbose_name=Field.DEFAULT_FATLINK_EXPIRY_TIME.label, # pylint: disable=no-member
)

default_fatlink_reopen_grace_time = models.PositiveIntegerField(
default=60,
help_text=_(
"Default time in minutes a FAT link can be re-opened after it is expired. "
"(Default: 60 minutes)"
),
verbose_name=Field.DEFAULT_FATLINK_REOPEN_GRACE_TIME.label, # pylint: disable=no-member
)

default_fatlink_reopen_duration = models.PositiveIntegerField(
default=60,
help_text=_(
"Default time in minutes a FAT link is re-opened for. "
"(Default: 60 minutes)"
),
verbose_name=Field.DEFAULT_FATLINK_REOPEN_DURATION.label, # pylint: disable=no-member
)

default_log_duration = models.PositiveIntegerField(
default=60,
help_text=_("Default time in days a log entry is kept. (Default: 60 days)"),
verbose_name=Field.DEFAULT_LOG_DURATION.label, # pylint: disable=no-member
)

use_doctrines_from_fittings_module = models.BooleanField(
default=False,
db_index=True,
Expand Down
10 changes: 6 additions & 4 deletions afat/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@

# Alliance Auth AFAT
from afat import __title__
from afat.app_settings import AFAT_DEFAULT_LOG_DURATION
from afat.models import Fat, FatLink, Log
from afat.models import Fat, FatLink, Log, Setting
from afat.providers import esi
from afat.utils import get_or_create_character

Expand Down Expand Up @@ -308,8 +307,11 @@ def logrotate():
:rtype:
"""

logger.info(msg=f"Cleaning up logs older than {AFAT_DEFAULT_LOG_DURATION} days")
logger.info(
msg=f"Cleaning up logs older than {Setting.get_setting(Setting.Field.DEFAULT_LOG_DURATION)} days"
)

Log.objects.filter(
log_time__lte=timezone.now() - timedelta(days=AFAT_DEFAULT_LOG_DURATION)
log_time__lte=timezone.now()
- timedelta(days=Setting.get_setting(Setting.Field.DEFAULT_LOG_DURATION))
).delete()
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
</p>

<p>
<button class="btn btn-default btn-sm"
<button class="btn btn-secondary btn-sm"
data-bs-toggle="modal"
data-bs-target="#reopenFatLinkModal"
data-url="{% url 'afat:fatlinks_reopen_fatlink' link.hash %}"
Expand Down
Loading

0 comments on commit 490e087

Please sign in to comment.