Commit 59ba9b54 by Őry Máté

one: share quota

parent 00bb4f06
......@@ -79,6 +79,14 @@ class UserCloudDetails(models.Model):
return c
def get_instance_pc(self):
return 100*self.get_weighted_instance_count()/self.instance_quota
def get_weighted_share_count(self):
c = 0
for i in Share.objects.filter(owner=self.user).all():
c = c + i.template.instance_type.credit * i.instance_limit
return c
def get_share_pc(self):
return 100*self.get_weighted_share_count()/self.share_quota
def set_quota(sender, instance, created, **kwargs):
if not StoreApi.userexist(instance.user.username):
......@@ -168,6 +176,7 @@ class Share(models.Model):
help_text=_('Maximal count of instances launchable for this share.'))
per_user_limit = models.IntegerField(verbose_name=_('per user limit'),
help_text=_('Maximal count of instances launchable by a single user.'))
owner = models.ForeignKey(User, null=True, blank=True)
def get_running_or_stopped(self):
return Instance.objects.all().exclude(state='DONE').filter(share=self).count()
......
{% load i18n %}
{% load l10n %}
{% get_current_language as LANGUAGE_CODE %}
<div class="contentblock" id="template">
<h2>{% trans "Templates" %}</h2>
......@@ -131,9 +132,9 @@
<li class="wm small">
<div class="summary">
<div class="quota">
<div class="used" style="background-color: rgba(0,255,0,0.2); width: 19%"></div>
<div class="used" style="background-color: rgba(0,255,0,0.2); width: {{userdetails.get_share_pc|unlocalize}}%"></div>
</div>
<div class="name">{% blocktrans with used=19 all=100 %}Share quota: {{used}}/{{all}}{% endblocktrans %}</div>
<div class="name">{% blocktrans with used=userdetails.get_weighted_share_count all=userdetails.share_quota %}Share quota: {{used}}/{{all}}{% endblocktrans %}</div>
<div class="clear"></div>
</div>
</li>
......
......@@ -65,7 +65,7 @@
<li class="wm small">
<div class="summary">
<div class="quota">
<div class="used" style="background-color: rgba(0,255,0,0.2); width: {{userdetails.get_instance_pc}}%"></div>
<div class="used" style="background-color: rgba(0,255,0,0.2); width: {{userdetails.get_instance_pc|unlocalize}}%"></div>
</div>
<div class="name">{% blocktrans with used=userdetails.get_weighted_instance_count all=userdetails.instance_quota %}Personal quota: {{used}}/{{all}}{% endblocktrans %}</div>
<div class="clear"></div>
......
......@@ -137,6 +137,9 @@ ajax_template_wizard = login_required(AjaxTemplateWizard.as_view())
class AjaxShareWizard(View):
def get(self, request, id, gid=None, *args, **kwargs):
det = UserCloudDetails.objects.get(user=request.user)
if det.get_weighted_share_count() >= det.share_quota:
return HttpResponse(unicode(_('You do not have any free share quota.')))
types = TYPES_L
types[0]['default'] = True
for i, t in enumerate(types):
......@@ -146,13 +149,14 @@ class AjaxShareWizard(View):
if gid:
gid = get_object_or_404(Group, id=gid)
return render_to_response('new-share.html', RequestContext(request,{
return render_to_response('new-share.html', RequestContext(request, {
'base': get_object_or_404(Template, id=id),
'groups': request.user.person_set.all()[0].owned_groups.all(),
'types': types,
'group': gid,
}))
def post(self, request, id, gid=None, *args, **kwargs):
det = UserCloudDetails.objects.get(user=request.user)
base = get_object_or_404(Template, id=id)
if base.owner != request.user and not base.public and not request.user.is_superuser:
raise PermissionDenied()
......@@ -168,10 +172,12 @@ class AjaxShareWizard(View):
if not stype in TYPES.keys():
raise PermissionDenied()
il = request.POST['instance_limit']
# TODO check quota
if det.get_weighted_share_count() + int(il)*base.instance_type.credit > det.share_quota:
messages.error(request, _('You do not have enough free share quota.'))
return redirect('/')
s = Share.objects.create(name=request.POST['name'], description=request.POST['description'],
type=stype, instance_limit=il, per_user_limit=request.POST['per_user_limit'],
group=group, template=base)
group=group, template=base, owner=request.user)
messages.success(request, _('Successfully shared %s.') % base)
return redirect(group)
ajax_share_wizard = login_required(AjaxShareWizard.as_view())
......
......@@ -172,6 +172,7 @@ def group_show(request, gid):
'mytemplates': mytemplates,
'publictemplates': publictemplates,
'noshare': noshare,
'userdetails': UserCloudDetails.objects.get(user=request.user),
}))
@login_required
......
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