1
0
Fork 0
mirror of https://github.com/retspen/webvirtcloud synced 2024-12-24 15:15:22 +00:00

configure_user() moved from class MyRemoteUserBackend to class UserAttributes, as static factory.

call configure_user() while adding user from form
This commit is contained in:
Ing. Jan KRCMAR 2017-07-18 15:04:56 +02:00
parent f2e1273f85
commit b095a77da5
3 changed files with 31 additions and 18 deletions

View file

@ -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

View file

@ -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

View file

@ -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', '')