Commit cbf4258f by Őry Máté

Merge branch 'feature-profile' into 'master'

Feature: Profile
parents 472b00ed d2263896
......@@ -382,3 +382,7 @@ if get_env_variable('DJANGO_SAML', 'FALSE') == 'TRUE':
'{"mail": ["email"], "sn": ["last_name"], '
'"uid": ["username"], "cn": ["first_name"]}'))
if get_env_variable('DJANGO_SAML_ORG_ID_ATTRIBUTE', False) != False:
SAML_ORG_ID_ATTRIBUTE = get_env_variable(
from logging import getLogger
from django.conf import settings
from django.contrib.auth.models import User
from django.db.models import Model, ForeignKey
from django.contrib.auth.signals import user_logged_in
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)
class Profile(Model):
user = OneToOneField(User)
preferred_language = CharField(verbose_name=_('preferred language'),
default=settings.LANGUAGE_CODE, blank=False)
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)
def create_profile(sender, user, request, **kwargs):
if not
return False
profile, created = Profile.objects.get_or_create(user=user)
return created
if hasattr(settings, 'SAML_ORG_ID_ATTRIBUTE'):
logger.debug("Register save_org_id to djangosaml2 pre_user_save")
from djangosaml2.signals import pre_user_save
def save_org_id(sender, **kwargs):
logger.debug("save_org_id called by %s", sender.username)
attributes = kwargs.pop('attributes')
atr = settings.SAML_ORG_ID_ATTRIBUTE
value = attributes[atr][0]
except Exception as e:
value = None"save_org_id couldn't find attribute. %s", unicode(e))
if is None:
logger.debug("save_org_id saved user %s", unicode(sender))
profile, created = Profile.objects.get_or_create(user=sender)
if created or profile.org_id != value:"org_id of %s added to user %s's profile",
value, sender.username)
profile.org_id = value
logger.debug("org_id of %s already added to user %s's profile",
value, sender.username)
return False
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
limit = user.profile.instance_limit
except Exception as e:
logger.debug('No profile or instance limit: %s', e)
current =
logger.debug('current use: %d, limit: %d', current, limit)
if limit < current:
_('Instance limit (%d) exceeded.') % limit)
if request.is_ajax():
return HttpResponse(json.dumps({'redirect': '/'}),
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