Commit c26b7fc1 by Dudás Ádám

one: UserCloudDetails refactor

parent 8306ae57
...@@ -70,7 +70,7 @@ class UserCloudDetails(models.Model): ...@@ -70,7 +70,7 @@ class UserCloudDetails(models.Model):
try: try:
self.ssh_key.key = pub self.ssh_key.key = pub
except: except AttributeError:
self.ssh_key = SshKey(user=self.user, key=pub) self.ssh_key = SshKey(user=self.user, key=pub)
self.ssh_key.save() self.ssh_key.save()
self.ssh_key_id = self.ssh_key.id self.ssh_key_id = self.ssh_key.id
...@@ -79,25 +79,34 @@ class UserCloudDetails(models.Model): ...@@ -79,25 +79,34 @@ class UserCloudDetails(models.Model):
def reset_smb(self): def reset_smb(self):
"""Generate new Samba password.""" """Generate new Samba password."""
self.smb_password = pwgen() self.smb_password = pwgen()
self.save()
def get_weighted_instance_count(self): def get_weighted_instance_count(self):
states = ['ACTIVE', 'PENDING']
credits = [i.template.instance_type.credit credits = [i.template.instance_type.credit
for i in self.user.instance_set.all() for i in self.user.instance_set.filter(state__in=states)]
if i.state in ('ACTIVE', 'PENDING', )]
return sum(credits) return sum(credits)
def get_instance_pc(self): def get_instance_pc(self):
return 100 * self.get_weighted_instance_count() / self.instance_quota """Get what percent of the user's instance quota is in use."""
inst_quota = self.instance_quota
if inst_quota <= 0:
return 100
else:
return 100 * self.get_weighted_instance_count() / inst_quota
def get_weighted_share_count(self): def get_weighted_share_count(self):
c = 0 credits = [i.template.instance_type.credit * i.instance_limit
for i in Share.objects.filter(owner=self.user).all(): for i in Share.objects.filter(owner=self.user)]
c = c + i.template.instance_type.credit * i.instance_limit return sum(credits)
return c
def get_share_pc(self): def get_share_pc(self):
assert self.share_quota > 0 """Get what percent of the user's share quota is in use."""
return 100 * self.get_weighted_share_count() / self.share_quota share_quota = self.share_quota
if share_quota <= 0:
return 100
else:
return 100 * self.get_weighted_share_count() / share_quota
def set_quota(sender, instance, created, **kwargs): def set_quota(sender, instance, created, **kwargs):
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment