Commit e1db1dc1 by Őry Máté

dashboard: Temporary user quota

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