2017-11-11 22:27:42 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
from django.contrib.auth.models import User
|
|
|
|
from django.db import models
|
|
|
|
from django.db.models.signals import post_save
|
|
|
|
from django.dispatch import receiver
|
|
|
|
from django.utils.timezone import timedelta
|
2019-04-14 04:50:35 +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'),
|
|
|
|
help_text=_('This setting will only be used when a browser does not '
|
|
|
|
'support refresh on focus.'),
|
2017-11-11 22:27:42 +00:00
|
|
|
blank=True,
|
|
|
|
null=True,
|
|
|
|
default=timedelta(minutes=1),
|
|
|
|
choices=[
|
2019-04-14 04:50:35 +00:00
|
|
|
(None, _('disabled')),
|
|
|
|
(timedelta(minutes=1), _('1 min.')),
|
|
|
|
(timedelta(minutes=2), _('2 min.')),
|
|
|
|
(timedelta(minutes=3), _('3 min.')),
|
|
|
|
(timedelta(minutes=4), _('4 min.')),
|
|
|
|
(timedelta(minutes=5), _('5 min.')),
|
|
|
|
(timedelta(minutes=10), _('10 min.')),
|
|
|
|
(timedelta(minutes=15), _('15 min.')),
|
|
|
|
(timedelta(minutes=30), _('30 min.')),
|
2017-11-11 22:27:42 +00:00
|
|
|
])
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return '{}\'s Settings'.format(self.user)
|
|
|
|
|
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()
|