1
0
mirror of https://github.com/netbox-community/netbox.git synced 2024-05-10 07:54:54 +00:00

Automatically create UserConfig for users

This commit is contained in:
Jeremy Stretch
2020-04-23 16:36:12 -04:00
parent afa0565a44
commit f3012ed839
3 changed files with 47 additions and 7 deletions

View File

@ -5,6 +5,8 @@ from django.contrib.auth.models import User
from django.contrib.postgres.fields import JSONField
from django.core.validators import MinLengthValidator
from django.db import models
from django.db.models.signals import post_save
from django.dispatch import receiver
from django.utils import timezone
@ -31,23 +33,24 @@ class UserConfig(models.Model):
ordering = ['user']
verbose_name = verbose_name_plural = 'User Preferences'
def get(self, path):
def get(self, path, default=None):
"""
Retrieve a configuration parameter specified by its dotted path. Example:
userconfig.get('foo.bar.baz')
:param path: Dotted path to the configuration key. For example, 'foo.bar' returns self.data['foo']['bar'].
:param default: Default value to return for a nonexistent key (default: None).
"""
d = self.data
keys = path.split('.')
# Iterate down the hierarchy, returning None for any invalid keys
# Iterate down the hierarchy, returning the default value if any invalid key is encountered
for key in keys:
if type(d) is dict:
d = d.get(key)
else:
return None
return default
return d
@ -116,6 +119,15 @@ class UserConfig(models.Model):
self.save()
@receiver(post_save, sender=User)
def create_userconfig(instance, created, **kwargs):
"""
Automatically create a new UserConfig when a new User is created.
"""
if created:
UserConfig(user=instance).save()
class Token(models.Model):
"""
An API token used for user authentication. This extends the stock model to allow each user to have multiple tokens.