2017-11-11 22:27:42 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
2020-02-14 17:22:25 +00:00
|
|
|
import pytz
|
|
|
|
|
2019-04-17 04:41:55 +00:00
|
|
|
from django.conf import settings
|
2017-11-11 22:27:42 +00:00
|
|
|
from django.contrib.auth.models import User
|
2019-04-17 04:41:55 +00:00
|
|
|
from django.contrib.auth.signals import user_logged_in
|
2017-11-11 22:27:42 +00:00
|
|
|
from django.db import models
|
|
|
|
from django.db.models.signals import post_save
|
|
|
|
from django.dispatch import receiver
|
2020-02-14 17:22:25 +00:00
|
|
|
from django.utils import timezone, translation
|
2019-04-14 21:42:58 +00:00
|
|
|
from django.utils.text import format_lazy
|
2019-04-19 02:55:55 +00:00
|
|
|
from django.utils.translation import gettext_lazy as _
|
2017-11-11 22:27:42 +00:00
|
|
|
|
2017-12-05 15:46:59 +00:00
|
|
|
from rest_framework.authtoken.models import Token
|
|
|
|
|
2017-11-11 22:27:42 +00:00
|
|
|
|
|
|
|
class Settings(models.Model):
|
2017-12-03 21:52:27 +00:00
|
|
|
user = models.OneToOneField(User, on_delete=models.CASCADE)
|
2017-11-11 22:27:42 +00:00
|
|
|
dashboard_refresh_rate = models.DurationField(
|
2019-04-14 04:50:35 +00:00
|
|
|
verbose_name=_('Refresh rate'),
|
2021-07-13 22:49:09 +00:00
|
|
|
help_text=_('If supported by browser, the dashboard will only refresh '
|
|
|
|
'when visible, and also when receiving focus.'),
|
2017-11-11 22:27:42 +00:00
|
|
|
blank=True,
|
|
|
|
null=True,
|
2020-02-14 17:22:25 +00:00
|
|
|
default=timezone.timedelta(minutes=1),
|
2017-11-11 22:27:42 +00:00
|
|
|
choices=[
|
2019-04-14 04:50:35 +00:00
|
|
|
(None, _('disabled')),
|
2020-02-14 17:22:25 +00:00
|
|
|
(timezone.timedelta(minutes=1), _('1 min.')),
|
|
|
|
(timezone.timedelta(minutes=2), _('2 min.')),
|
|
|
|
(timezone.timedelta(minutes=3), _('3 min.')),
|
|
|
|
(timezone.timedelta(minutes=4), _('4 min.')),
|
|
|
|
(timezone.timedelta(minutes=5), _('5 min.')),
|
|
|
|
(timezone.timedelta(minutes=10), _('10 min.')),
|
|
|
|
(timezone.timedelta(minutes=15), _('15 min.')),
|
|
|
|
(timezone.timedelta(minutes=30), _('30 min.')),
|
2017-11-11 22:27:42 +00:00
|
|
|
])
|
2021-05-14 03:28:39 +00:00
|
|
|
dashboard_hide_empty = models.BooleanField(
|
|
|
|
verbose_name=_('Hide Empty Dashboard Cards'),
|
|
|
|
default=False,
|
|
|
|
editable=True
|
|
|
|
)
|
2021-06-19 21:09:05 +00:00
|
|
|
dashboard_hide_age = models.DurationField(
|
|
|
|
verbose_name=_('Hide data older than'),
|
|
|
|
help_text=_('This setting controls which data will be shown '
|
|
|
|
'in the dashboard.'),
|
|
|
|
blank=True,
|
|
|
|
null=True,
|
|
|
|
default=None,
|
|
|
|
choices=[
|
|
|
|
(None, _('show all data')),
|
|
|
|
(timezone.timedelta(days=1), _('1 day')),
|
|
|
|
(timezone.timedelta(days=2), _('2 days')),
|
|
|
|
(timezone.timedelta(days=3), _('3 days')),
|
|
|
|
(timezone.timedelta(weeks=1), _('1 week')),
|
|
|
|
(timezone.timedelta(weeks=4), _('4 weeks')),
|
|
|
|
])
|
2019-04-17 04:41:55 +00:00
|
|
|
language = models.CharField(
|
|
|
|
choices=settings.LANGUAGES,
|
|
|
|
default=settings.LANGUAGE_CODE,
|
|
|
|
max_length=255,
|
|
|
|
verbose_name=_('Language')
|
|
|
|
)
|
2020-02-14 17:22:25 +00:00
|
|
|
timezone = models.CharField(
|
2020-02-14 17:30:17 +00:00
|
|
|
choices=tuple(zip(pytz.common_timezones, pytz.common_timezones)),
|
2020-02-14 17:22:25 +00:00
|
|
|
default=timezone.get_default_timezone_name(),
|
|
|
|
max_length=100,
|
|
|
|
verbose_name=_('Timezone')
|
|
|
|
)
|
2017-11-11 22:27:42 +00:00
|
|
|
|
|
|
|
def __str__(self):
|
2019-04-14 21:42:58 +00:00
|
|
|
return str(format_lazy(_('{user}\'s Settings'), user=self.user))
|
2017-11-11 22:27:42 +00:00
|
|
|
|
2017-12-05 15:46:59 +00:00
|
|
|
def api_key(self, reset=False):
|
|
|
|
"""
|
|
|
|
Get or create an API key for the associated user.
|
|
|
|
:param reset: If True, delete the existing key and create a new one.
|
|
|
|
:return: The user's API key.
|
|
|
|
"""
|
|
|
|
if reset:
|
|
|
|
Token.objects.get(user=self.user).delete()
|
|
|
|
return Token.objects.get_or_create(user=self.user)[0]
|
|
|
|
|
2017-11-11 22:27:42 +00:00
|
|
|
@property
|
|
|
|
def dashboard_refresh_rate_milliseconds(self):
|
|
|
|
"""
|
|
|
|
Convert seconds to milliseconds to be used in a Javascript setInterval
|
|
|
|
function call.
|
|
|
|
:return: the refresh rate in milliseconds or None.
|
|
|
|
"""
|
|
|
|
if self.dashboard_refresh_rate:
|
|
|
|
return self.dashboard_refresh_rate.seconds * 1000
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
|
@receiver(post_save, sender=User)
|
|
|
|
def create_user_settings(sender, instance, created, **kwargs):
|
|
|
|
if created:
|
|
|
|
Settings.objects.create(user=instance)
|
|
|
|
|
|
|
|
|
|
|
|
@receiver(post_save, sender=User)
|
|
|
|
def save_user_settings(sender, instance, **kwargs):
|
|
|
|
instance.settings.save()
|
2019-04-17 04:41:55 +00:00
|
|
|
|
|
|
|
|
|
|
|
@receiver(user_logged_in)
|
|
|
|
def user_logged_in_callback(sender, request, user, **kwargs):
|
2020-02-14 17:22:25 +00:00
|
|
|
if user.settings.language:
|
|
|
|
translation.activate(user.settings.language)
|
2020-02-14 20:35:02 +00:00
|
|
|
# TODO: Change this behavior as session-based language is deprecated.
|
2020-02-14 17:22:25 +00:00
|
|
|
request.session[
|
|
|
|
translation.LANGUAGE_SESSION_KEY] = user.settings.language
|
|
|
|
if user.settings.timezone:
|
|
|
|
timezone.activate(user.settings.timezone)
|
2020-02-14 20:35:02 +00:00
|
|
|
request.session['user_timezone'] = user.settings.timezone
|