From b095a77da57cac895d47c3fdf8d65981206898bf Mon Sep 17 00:00:00 2001 From: "Ing. Jan KRCMAR" Date: Tue, 18 Jul 2017 15:04:56 +0200 Subject: [PATCH] configure_user() moved from class MyRemoteUserBackend to class UserAttributes, as static factory. call configure_user() while adding user from form --- accounts/backends.py | 19 +------------------ accounts/models.py | 29 +++++++++++++++++++++++++++++ accounts/views.py | 1 + 3 files changed, 31 insertions(+), 18 deletions(-) diff --git a/accounts/backends.py b/accounts/backends.py index 3fb208b..e66b94a 100644 --- a/accounts/backends.py +++ b/accounts/backends.py @@ -5,26 +5,9 @@ from instances.models import Instance class MyRemoteUserBackend(RemoteUserBackend): #create_unknown_user = True - default_instances = [ 'debian8-template' ] - - def create_missing_userattributes(self, user): - try: - userattributes = user.userattributes - except UserAttributes.DoesNotExist: - userattributes = UserAttributes(user=user) - userattributes.save() - - def add_default_instances(self, user): - existing_instances = UserInstance.objects.filter(user=user) - if not existing_instances: - for instance_name in self.default_instances: - instance = Instance.objects.get(name=instance_name) - user_instance = UserInstance(user=user, instance=instance) - user_instance.save() def configure_user(self, user): #user.is_superuser = True - self.create_missing_userattributes(user) - self.add_default_instances(user) + UserAttributes.configure_user(user) return user diff --git a/accounts/models.py b/accounts/models.py index 3c8b00c..0ddf450 100644 --- a/accounts/models.py +++ b/accounts/models.py @@ -30,5 +30,34 @@ class UserAttributes(models.Model): max_memory = models.IntegerField(default=2048) max_disk_size = models.IntegerField(default=20) + default_instances = [ + 'debian8-template', + 'debian8-template-nocf', + 'debian9-template', + 'debian9-template-nocf', + ] + + @staticmethod + def create_missing_userattributes(user): + try: + userattributes = user.userattributes + except UserAttributes.DoesNotExist: + userattributes = UserAttributes(user=user) + userattributes.save() + + @staticmethod + def add_default_instances(user): + existing_instances = UserInstance.objects.filter(user=user) + if not existing_instances: + for instance_name in UserAttributes.default_instances: + instance = Instance.objects.get(name=instance_name) + user_instance = UserInstance(user=user, instance=instance) + user_instance.save() + + @staticmethod + def configure_user(user): + UserAttributes.create_missing_userattributes(user) + UserAttributes.add_default_instances(user) + def __unicode__(self): return self.user.username diff --git a/accounts/views.py b/accounts/views.py index 4b87b22..6e9fd52 100644 --- a/accounts/views.py +++ b/accounts/views.py @@ -88,6 +88,7 @@ def accounts(request): if not error_messages: new_user = User.objects.create_user(data['name'], None, data['password']) new_user.save() + UserAttributes.configure_user(new_user) return HttpResponseRedirect(request.get_full_path()) if 'edit' in request.POST: user_id = request.POST.get('user_id', '')