Add mechanism for site wide settings

The two settings properties introduced here are not yet used.
This commit is contained in:
Christopher C. Wells 2022-08-14 16:38:59 -07:00 committed by Christopher C. Wells
parent 7b7f17fde6
commit 613f53a4da
8 changed files with 119 additions and 1 deletions

View File

@ -6,7 +6,22 @@ from django.contrib.auth.forms import PasswordChangeForm, UserCreationForm
from django.contrib.auth.models import Group
from django.utils.translation import gettext_lazy as _
from .models import Settings
from core.widgets import TimeInput
from .models import Settings, SiteSettings
class SiteSettingsForm(forms.ModelForm):
class Meta:
model = SiteSettings
fields = [
"nap_start_min",
"nap_start_max",
]
widgets = {
"nap_start_min": TimeInput(),
"nap_start_max": TimeInput(),
}
class BabyBuddyUserForm(forms.ModelForm):

View File

@ -0,0 +1,45 @@
# Generated by Django 4.0.7 on 2022-08-14 23:28
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
("babybuddy", "0023_alter_settings_timezone"),
]
operations = [
migrations.CreateModel(
name="SiteSettings",
fields=[
(
"id",
models.AutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
(
"nap_start_min",
models.TimeField(
blank=True,
help_text="The minimum default time that a sleep entry is consider a nap.If set the nap property will be preselected if the starttime is within the bounds.",
null=True,
verbose_name="Default minimum nap start time",
),
),
(
"nap_start_max",
models.TimeField(
blank=True,
help_text="The maximum default time that a sleep entry is consider a nap.If set the nap property will be preselected if the starttime is within the bounds.",
null=True,
verbose_name="Default maximum nap start time",
),
),
],
),
]

View File

@ -93,6 +93,31 @@ class Settings(models.Model):
return self.dashboard_refresh_rate.seconds * 1000
return None
class SiteSettings(models.Model):
nap_start_min = models.TimeField(
verbose_name=_("Default minimum nap start time"),
help_text=_(
"The minimum default time that a sleep entry is consider a nap."
"If set the nap property will be preselected if the start"
"time is within the bounds."
),
blank=True,
null=True,
)
nap_start_max = models.TimeField(
verbose_name=_("Default maximum nap start time"),
help_text=_(
"The maximum default time that a sleep entry is consider a nap."
"If set the nap property will be preselected if the start"
"time is within the bounds."
),
blank=True,
null=True,
)
def __str__(self):
return _("Site Settings")
@receiver(post_save, sender=get_user_model())
def create_user_settings(sender, instance, created, **kwargs):

View File

@ -336,6 +336,8 @@
<h6 class="dropdown-header">{% trans "Site" %}</h6>
<a href="{% url 'api:api-root' %}" class="dropdown-item">{% trans "API Browser" %}</a>
{% if request.user.is_staff %}
<a href="{% url 'babybuddy:site-settings-update' %}"
class="dropdown-item">{% trans "Settings" %}</a>
<a href="{% url 'babybuddy:user-list' %}"
class="dropdown-item">{% trans "Users" %}</a>
<a href="{% url 'admin:index' %}"

View File

@ -0,0 +1,14 @@
{% extends 'babybuddy/page.html' %}
{% load i18n %}
{% block title %}{% trans "Site Settings" %}{% endblock %}
{% block breadcrumbs %}
<li class="breadcrumb-item">{% trans "Site" %}</li>
<li class="breadcrumb-item active">{% trans "Settings" %}</li>
{% endblock %}
{% block content %}
<h1>{% trans "Site Settings" %}</h1>
{% include 'babybuddy/form.html' %}
{% endblock %}

View File

@ -35,6 +35,7 @@ app_patterns = [
name="password_reset_complete",
),
path("", views.RootRouter.as_view(), name="root-router"),
path("settings/", views.SiteSettingsUpdate.as_view(), name="site-settings-update"),
path("welcome/", views.Welcome.as_view(), name="welcome"),
path("users/", views.UserList.as_view(), name="user-list"),
path("users/add/", views.UserAdd.as_view(), name="user-add"),

View File

@ -37,6 +37,7 @@ from django_filters.views import FilterView
from babybuddy import forms
from babybuddy.mixins import LoginRequiredMixin, PermissionRequiredMixin, StaffOnlyMixin
from babybuddy.models import SiteSettings
def csrf_failure(request, reason=""):
@ -95,6 +96,17 @@ class LogoutView(LogoutViewBase):
pass
class SiteSettingsUpdate(StaffOnlyMixin, SuccessMessageMixin, UpdateView):
model = SiteSettings
template_name = "babybuddy/site_settings_form.html"
form_class = forms.SiteSettingsForm
success_url = reverse_lazy("babybuddy:site-settings-update")
success_message = gettext_lazy("Site settings updated.")
def get_object(self, queryset=None):
return SiteSettings.objects.get_or_create(pk=1)[0]
class UserList(StaffOnlyMixin, BabyBuddyFilterView):
model = get_user_model()
template_name = "babybuddy/user_list.html"

View File

@ -119,3 +119,7 @@ class DateTimeInput(DateTimeBaseInput):
class DateInput(DateTimeBaseInput):
input_type = "date"
class TimeInput(DateTimeBaseInput):
input_type = "time"