Skip to content

Commit

Permalink
[CHANGE] Move settings from local.py to DB
Browse files Browse the repository at this point in the history
  • Loading branch information
ppfeufer committed Nov 8, 2024
1 parent 30cb4f6 commit 5bf4bfe
Show file tree
Hide file tree
Showing 10 changed files with 168 additions and 74 deletions.
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
43 changes: 26 additions & 17 deletions afat/views/fatlinks.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,6 @@

# Alliance Auth AFAT
from afat import __title__
from afat.app_settings import (
AFAT_DEFAULT_FATLINK_EXPIRY_TIME,
AFAT_DEFAULT_FATLINK_REOPEN_DURATION,
AFAT_DEFAULT_FATLINK_REOPEN_GRACE_TIME,
)
from afat.forms import (
AFatClickFatForm,
AFatEsiFatForm,
Expand All @@ -45,7 +40,15 @@
from afat.helper.fatlinks import get_doctrines, get_esi_fleet_information_by_user
from afat.helper.time import get_time_delta
from afat.helper.views import convert_fatlinks_to_dict, convert_fats_to_dict
from afat.models import Duration, Fat, FatLink, FleetType, Log, get_hash_on_save
from afat.models import (
Duration,
Fat,
FatLink,
FleetType,
Log,
Setting,
get_hash_on_save,
)
from afat.providers import esi
from afat.tasks import process_fats
from afat.utils import get_or_create_character, write_log
Expand Down Expand Up @@ -138,7 +141,9 @@ def add_fatlink(request: WSGIRequest) -> HttpResponse:

context = {
"link_types_configured": link_types_configured,
"default_expiry_time": AFAT_DEFAULT_FATLINK_EXPIRY_TIME,
"default_expiry_time": Setting.get_setting(
Setting.Field.DEFAULT_FATLINK_EXPIRY_TIME
),
"esi_fleet": get_esi_fleet_information_by_user(request.user),
"esi_fatlink_form": AFatEsiFatForm(),
"manual_fatlink_form": AFatClickFatForm(),
Expand Down Expand Up @@ -856,11 +861,9 @@ def details_fatlink( # pylint: disable=too-many-statements too-many-branches to
# Link expired
link_ongoing = False

if (
link.reopened is False
and get_time_delta(then=link_expires, now=now, interval="minutes")
< AFAT_DEFAULT_FATLINK_REOPEN_GRACE_TIME
):
if link.reopened is False and get_time_delta(
then=link_expires, now=now, interval="minutes"
) < Setting.get_setting(Setting.Field.DEFAULT_FATLINK_REOPEN_GRACE_TIME):
link_can_be_reopened = True

# Manual fat still possible?
Expand All @@ -887,8 +890,12 @@ def details_fatlink( # pylint: disable=too-many-statements too-many-branches to
"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,
"reopen_grace_time": Setting.get_setting(
Setting.Field.DEFAULT_FATLINK_REOPEN_GRACE_TIME
),
"reopen_duration": Setting.get_setting(
Setting.Field.DEFAULT_FATLINK_REOPEN_DURATION
),
}

return render(
Expand Down Expand Up @@ -1146,7 +1153,9 @@ def reopen_fatlink(request: WSGIRequest, fatlink_hash: str) -> HttpResponseRedir
time_difference_in_minutes = get_time_delta(
then=created_at, now=now, interval="minutes"
)
new_duration = time_difference_in_minutes + AFAT_DEFAULT_FATLINK_REOPEN_DURATION
new_duration = time_difference_in_minutes + Setting.get_setting(
Setting.Field.DEFAULT_FATLINK_REOPEN_DURATION
)

fatlink_duration.duration = new_duration
fatlink_duration.save()
Expand All @@ -1160,7 +1169,7 @@ def reopen_fatlink(request: WSGIRequest, fatlink_hash: str) -> HttpResponseRedir
log_event=Log.Event.REOPEN_FATLINK,
log_text=(
"FAT link re-opened for a duration of "
f"{AFAT_DEFAULT_FATLINK_REOPEN_DURATION} minutes"
f"{Setting.get_setting(Setting.Field.DEFAULT_FATLINK_REOPEN_DURATION)} minutes"
),
fatlink_hash=fatlink_duration.fleet.hash,
)
Expand All @@ -1169,7 +1178,7 @@ def reopen_fatlink(request: WSGIRequest, fatlink_hash: str) -> HttpResponseRedir
msg=(
f'FAT link with hash "{fatlink_hash}" '
f"re-opened by {request.user} for a "
f"duration of {AFAT_DEFAULT_FATLINK_REOPEN_DURATION} minutes"
f"duration of {Setting.get_setting(Setting.Field.DEFAULT_FATLINK_REOPEN_DURATION)} minutes"
)
)

Expand Down
Loading

0 comments on commit 5bf4bfe

Please sign in to comment.