Commit e1db1dc1 by Őry Máté

dashboard: Temporary user quota

Closes #44
parent 94d421a9
......@@ -2,14 +2,16 @@ from logging import getLogger
from django.conf import settings
from django.contrib.auth.models import User
from django.db.models import Model, ForeignKey, OneToOneField, CharField
from django.db.models import (
Model, ForeignKey, OneToOneField, CharField, IntegerField
)
from django.utils.translation import ugettext_lazy as _
from vm.models import Instance
logger = getLogger(__name__)
class Favourite(Model):
instance = ForeignKey(Instance)
user = ForeignKey(User)
......@@ -24,6 +26,7 @@ class Profile(Model):
org_id = CharField( # may be populated from eduPersonOrgId field
unique=True, blank=True, null=True, max_length=64,
help_text=_('Unique identifier of the person, e.g. a student number.'))
instance_limit = IntegerField(default=5)
if hasattr(settings, 'SAML_ORG_ID_ATTRIBUTE'):
......@@ -51,7 +54,6 @@ if hasattr(settings, 'SAML_ORG_ID_ATTRIBUTE'):
value, sender.username)
return False
pre_user_save.connect(save_org_id, weak=False)
else:
logger.debug("Do not register save_org_id to djangosaml2 pre_user_save")
......@@ -683,7 +683,6 @@ class VmCreate(LoginRequiredMixin, TemplateView):
})
return self.render_to_response(context)
# TODO handle not ajax posts
def post(self, request, *args, **kwargs):
form = self.form_class(request.POST)
if not form.is_valid():
......@@ -691,6 +690,22 @@ class VmCreate(LoginRequiredMixin, TemplateView):
post = form.cleaned_data
user = request.user
try:
limit = user.profile.instance_limit
except Exception as e:
logger.debug('No profile or instance limit: %s', e)
else:
current = Instance.active.filter(owner=user).count()
logger.debug('current use: %d, limit: %d', current, limit)
if limit < current:
messages.error(request,
_('Instance limit (%d) exceeded.') % limit)
if request.is_ajax():
return HttpResponse(json.dumps({'redirect': '/'}),
content_type="application/json")
else:
return redirect('/')
template = post['template']
if not template.has_level(request.user, 'user'):
raise PermissionDenied()
......
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