Commit cf8ada05 by Bence Dányi

one: share quota checked on template editing

parent 1ba601d9
...@@ -387,6 +387,27 @@ class Template(models.Model): ...@@ -387,6 +387,27 @@ class Template(models.Model):
def get_credits_per_instance(self): def get_credits_per_instance(self):
return self.instance_type.credit return self.instance_type.credit
def get_shares_for(self, user=None):
shares = Share.objects.all().filter(template=self)
if user:
return shares.filter(owner=user)
else:
return shares
def get_share_quota_usage_for(self, user=None):
shares = self.get_shares_for(user)
usage = 0
for share in shares:
usage += share.instance_limit * self.get_credits_per_instance()
return usage
def get_share_quota_usage_for_user_with_type(self, type, user=None):
shares = self.get_shares_for(user)
usage = 0
for share in shares:
usage += share.instance_limit * type.credit
return usage
class Instance(models.Model): class Instance(models.Model):
"""Virtual machine instance.""" """Virtual machine instance."""
name = models.CharField(max_length=100, name = models.CharField(max_length=100,
......
...@@ -147,9 +147,19 @@ class AjaxTemplateEditWizard(View): ...@@ -147,9 +147,19 @@ class AjaxTemplateEditWizard(View):
})) }))
def post(self, request, id, *args, **kwargs): def post(self, request, id, *args, **kwargs):
template = get_object_or_404(Template, id=id) template = get_object_or_404(Template, id=id)
user_details = UserCloudDetails.objects.get(user=request.user)
if template.owner != request.user and not template.public and not request.user.is_superuser: if template.owner != request.user and not template.public and not request.user.is_superuser:
raise PermissionDenied() raise PermissionDenied()
template.instance_type_id = request.POST['size'] instance_type = get_object_or_404(InstanceType, id=request.POST['size'])
current_used_share_quota = user_details.get_weighted_share_count()
current_used_share_quota_without_template = current_used_share_quota - template.get_share_quota_usage_for(request.user)
new_quota_for_current_template = template.get_share_quota_usage_for_user_with_type(instance_type, request.user)
new_used_share_quota = current_used_share_quota_without_template + new_quota_for_current_template
allow_type_modify = True if new_used_share_quota <= user_details.share_quota else False
if not allow_type_modify:
messages.error(request, _('You do not have enough free share quota.'))
return redirect(home)
template.instance_type = instance_type
template.description = request.POST['description'] template.description = request.POST['description']
template.name = request.POST['name'] template.name = request.POST['name']
template.save() template.save()
......
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