430 |
430 |
def get_context_data(self, **kwargs):
|
431 |
431 |
context = super(ProfileView, self).get_context_data(**kwargs)
|
432 |
432 |
request = self.request
|
433 |
|
auth_level = int(request.GET.get('auth_level', 0))
|
434 |
|
frontends = utils.get_backends('AUTH_FRONTENDS', required_auth_level=auth_level)
|
|
433 |
filter_by_auth_level = int(request.GET.get('auth_level', 0))
|
|
434 |
frontends = utils.get_backends('AUTH_FRONTENDS', filter_by_auth_level)
|
435 |
435 |
|
436 |
436 |
if request.method == "POST":
|
437 |
437 |
for frontend in frontends:
|
... | ... | |
441 |
441 |
if request.session.test_cookie_worked():
|
442 |
442 |
request.session.delete_test_cookie()
|
443 |
443 |
return frontend.post(request, form, None, '/profile')
|
|
444 |
|
|
445 |
if not filter_by_auth_level:
|
|
446 |
self.get_user_attributes_context(context)
|
|
447 |
|
|
448 |
self.get_credential_management_context(context, frontends)
|
|
449 |
hooks.call_hooks('modify_context_data', self, context)
|
|
450 |
return context
|
|
451 |
|
|
452 |
def get_user_attributes_context(self, context):
|
444 |
453 |
# User attributes management
|
|
454 |
user = self.request.user
|
445 |
455 |
profile = []
|
446 |
456 |
field_names = app_settings.A2_PROFILE_FIELDS
|
447 |
457 |
if not field_names:
|
448 |
458 |
field_names = list(app_settings.A2_REGISTRATION_FIELDS)
|
449 |
|
for field_name in getattr(request.user, 'USER_PROFILE', []):
|
|
459 |
for field_name in getattr(user, 'USER_PROFILE', []):
|
450 |
460 |
if field_name not in field_names:
|
451 |
461 |
field_names.append(field_name)
|
452 |
462 |
qs = models.Attribute.objects.filter(Q(user_editable=True) | Q(user_visible=True))
|
... | ... | |
471 |
481 |
if not attribute.user_visible:
|
472 |
482 |
continue
|
473 |
483 |
html_value = attribute.get_kind().get('html_value', lambda a, b: b)
|
474 |
|
qs = models.AttributeValue.objects.with_owner(request.user)
|
|
484 |
qs = models.AttributeValue.objects.with_owner(user)
|
475 |
485 |
qs = qs.filter(attribute=attribute)
|
476 |
486 |
qs = qs.select_related()
|
477 |
487 |
value = [at_value.to_python() for at_value in qs]
|
... | ... | |
482 |
492 |
else:
|
483 |
493 |
# fallback to model attributes
|
484 |
494 |
try:
|
485 |
|
field = request.user._meta.get_field(field_name)
|
|
495 |
field = user._meta.get_field(field_name)
|
486 |
496 |
except FieldDoesNotExist:
|
487 |
497 |
continue
|
488 |
498 |
if not title:
|
489 |
499 |
title = field.verbose_name
|
490 |
|
value = getattr(self.request.user, field_name, None)
|
|
500 |
value = getattr(user, field_name, None)
|
491 |
501 |
attribute = models.Attribute(name=field_name, label=title)
|
492 |
502 |
|
493 |
503 |
raw_value = None
|
... | ... | |
501 |
511 |
if value or app_settings.A2_PROFILE_DISPLAY_EMPTY_FIELDS:
|
502 |
512 |
profile.append((title, value))
|
503 |
513 |
attributes.append({'attribute': attribute, 'values': raw_value})
|
|
514 |
context.update({
|
|
515 |
'profile': profile,
|
|
516 |
'attributes': attributes,
|
|
517 |
'allow_account_deletion': app_settings.A2_REGISTRATION_CAN_DELETE_ACCOUNT,
|
|
518 |
'allow_profile_edit': EditProfile.can_edit_profile(),
|
|
519 |
'allow_email_change': app_settings.A2_PROFILE_CAN_CHANGE_EMAIL,
|
|
520 |
# TODO: deprecated should be removed when publik-base-theme is updated
|
|
521 |
'allow_password_change': utils.user_can_change_password(request=self.request),
|
|
522 |
})
|
504 |
523 |
|
505 |
|
# Credentials management
|
506 |
|
parameters = {'request': request,
|
|
524 |
def get_credential_management_context(self, context, frontends):
|
|
525 |
parameters = {'request': self.request,
|
507 |
526 |
'context': context}
|
508 |
527 |
profiles = [utils.get_authenticator_method(frontend, 'profile', parameters) for frontend in frontends]
|
509 |
528 |
# Old frontends data structure for templates
|
... | ... | |
517 |
536 |
if app_settings.A2_PROFILE_CAN_MANAGE_FEDERATION:
|
518 |
537 |
for idp_backend in idp_backends:
|
519 |
538 |
if hasattr(idp_backend, 'federation_management'):
|
520 |
|
federation_management.extend(idp_backend.federation_management(request))
|
|
539 |
federation_management.extend(idp_backend.federation_management(self.request))
|
521 |
540 |
context.update({
|
522 |
541 |
'frontends_block': blocks,
|
523 |
542 |
'frontends_block_by_id': blocks_by_id,
|
524 |
|
'profile': profile,
|
525 |
|
'attributes': attributes,
|
526 |
|
'allow_account_deletion': app_settings.A2_REGISTRATION_CAN_DELETE_ACCOUNT,
|
527 |
|
'allow_profile_edit': EditProfile.can_edit_profile(),
|
528 |
|
'allow_email_change': app_settings.A2_PROFILE_CAN_CHANGE_EMAIL,
|
529 |
|
# TODO: deprecated should be removed when publik-base-theme is updated
|
530 |
|
'allow_password_change': utils.user_can_change_password(request=request),
|
531 |
543 |
'federation_management': federation_management,
|
532 |
|
'auth_level': auth_level,
|
533 |
544 |
})
|
534 |
|
hooks.call_hooks('modify_context_data', self, context)
|
535 |
|
return context
|
536 |
545 |
|
537 |
546 |
profile = login_required(ProfileView.as_view())
|
538 |
547 |
|
539 |
|
-
|