combo/combo/profile/models.py

67 lines
2.5 KiB
Python

# combo - content management system
# Copyright (C) 2014-2017 Entr'ouvert
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from collections import OrderedDict
import copy
from django.conf import settings
from django.db import models
from django.utils.dateparse import parse_date
from django.utils.translation import ugettext_lazy as _
from combo.data.models import JsonCellBase
from combo.data.library import register_cell_class
class Profile(models.Model):
user = models.OneToOneField(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
initial_login_view_timestamp = models.DateTimeField(null=True)
@register_cell_class
class ProfileCell(JsonCellBase):
template_name = 'combo/profile.html'
first_data_key = 'profile'
cache_duration = None
class Meta:
verbose_name = _('Profile')
@property
def url(self):
idp = list(settings.KNOWN_SERVICES.get('authentic').values())[0]
return '{%% load combo %%}%sapi/users/{{ concerned_user|name_id }}/' % idp.get('url')
def is_visible(self, user=None):
if not user or user.is_anonymous:
return False
return super(ProfileCell, self).is_visible(user)
def get_cell_extra_context(self, context):
extra_context = super(ProfileCell, self).get_cell_extra_context(context)
extra_context['profile_fields'] = OrderedDict()
if extra_context.get('profile') is not None:
for attribute in settings.USER_PROFILE_CONFIG.get('fields'):
extra_context['profile_fields'][attribute['name']] = copy.copy(attribute)
value = extra_context['profile'].get(attribute['name'])
if value:
if attribute['kind'] in ('birthdate', 'date'):
value = parse_date(value)
extra_context['profile_fields'][attribute['name']]['value'] = value
else:
extra_context['error'] = 'unknown user'
return extra_context