Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve Profile Handling #40

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/collective/sidebar/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,18 @@
from collective.sidebar.patches import apply_portrait_patch
from zope.i18nmessageid import MessageFactory

import zope.deferredimport


# Patch Profile-Scale
apply_portrait_patch()

# Translation Domain
_ = MessageFactory('collective.sidebar')

# Deferred Imports
zope.deferredimport.defineFrom(
'collective.sidebar.directives',
'user_avatar',
'AVATAR_KEY',
)
63 changes: 60 additions & 3 deletions src/collective/sidebar/browser/sidebar.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# -*- coding: utf-8 -*-

from collective.sidebar import _
from collective.sidebar.directives import AVATAR_KEY
from collective.sidebar.utils import crop
from collective.sidebar.utils import get_icon
from collective.sidebar.utils import get_user
Expand All @@ -17,6 +18,7 @@
from Products.Five.browser.pagetemplatefile import ViewPageTemplateFile
from zope.component import getMultiAdapter
from zope.component import queryMultiAdapter
from zope.dottedname.resolve import resolve

import pkg_resources

Expand Down Expand Up @@ -56,15 +58,70 @@ def get_static_links(self):
return sidebar_links

def get_user_data(self):
"""
Returns member data for the currently logged-in user.
Can also return custom member object data when toggling the,
memberareaCreationFlag option in the ZMI.
Note:
Custom avatar images are only used when marking a field with
the user_avatar directive in the Content-Type schema.
"""
# Set Defaults
user = get_user()
portal = api.portal.get()
portal_url = self.get_portal_url()
mtool = api.portal.get_tool('portal_membership')
dxtool = api.portal.get_tool('portal_types')
# Get Default Portrait
portrait = mtool.getPersonalPortrait(id=user[1])
# Set Default User-Variables
user_info = mtool.getMemberInfo(user[1])
portal_url = self.get_portal_url()
user_avatar = portrait.absolute_url()
user_url = portal_url + '/@@personal-information'
# When Custom Member-Creation is Active
if mtool.memberareaCreationFlag:
# Get Dexterity Type
user_dx = dxtool.get(mtool.memberarea_type)
custom_schema = user_dx.schema
if custom_schema:
# Get Dexterity-Schema Interface
user_dx_iface = resolve(custom_schema)
# Get Marked-Fields from Metadata
avatar_fields = user_dx_iface.queryTaggedValue(AVATAR_KEY)
avatar_field = None
if avatar_fields:
avatar_field = avatar_fields[0][1]
if avatar_field:
# Get User-View and Fetch Image
users = api.content.find(
portal_type=mtool.memberarea_type,
context=portal.get(mtool.membersfolder_id),
id=user[1],
)
if users:
images_view = api.content.get_view(
'images',
users[0].getObject(),
self.request,
)
scale = images_view.scale(
avatar_field,
width=256,
height=256,
direction='down',
)
user_avatar = scale.url
# Set User-Profile URL
user_url = '{0}/{1}/{2}'.format(
portal_url,
mtool.membersfolder_id,
user_info.get('username', ''),
)
# Concatenate the Data Together
data = {
'user_info': user_info,
'portrait': portrait.absolute_url(),
'user_url': portal_url + '/@@personal-information',
'user_avatar': user_avatar,
'user_url': user_url,
}
return data

Expand Down
4 changes: 2 additions & 2 deletions src/collective/sidebar/browser/templates/sidebar.pt
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@
<div class="menu-profile"
tal:define="data view/get_user_data;
user_info data/user_info;
portrait data/portrait;
user_avatar data/user_avatar;
user_url data/user_url">

<a href="${user_url}">

<div class="profile-image" style="background-image: url('${portrait}');"></div>
<div class="profile-image" style="background-image: url('${user_avatar}');"></div>

<div class="profile-info">
<div class="profile-name" tal:content="python:user_info['fullname'] or user_info['username']"></div>
Expand Down
32 changes: 32 additions & 0 deletions src/collective/sidebar/directives.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# -*- coding: utf-8 -*-

from plone.supermodel.directives import MetadataListDirective
from zope.interface import Interface
from zope.interface.interfaces import IInterface


AVATAR_KEY = u'collective.sidebar.avatar'


class user_avatar(MetadataListDirective):
"""
Directive used to mark a field as the profile image provider.
"""

key = AVATAR_KEY
value = True

def factory(self, *args):
"""
The user_avatar directive accepts as arguments one or more
fieldnames (string) of fields which should be used.
"""
if not args:
raise TypeError(
'The user_avatar directive expects at least one argument.'
)
form_interface = Interface
if IInterface.providedBy(args[0]):
form_interface = args[0]
args = args[1:]
return [(form_interface, field, self.value) for field in args]
21 changes: 21 additions & 0 deletions src/collective/sidebar/utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# -*- coding: utf-8 -*-

from collective.sidebar.directives import AVATAR_KEY
from plone import api
from zope import schema


def crop(text, max_char_length):
Expand Down Expand Up @@ -29,6 +31,25 @@ def get_user():
return user, user_id, user_dir


def user_avatar(iface, field_name):
"""
Mark a field in the existing interface as the avatar image provider.
"""
if schema.getFields(iface).get(field_name) is None:
dottedname = '.'.join((iface.__module__, iface.__name__))
raise AttributeError(
'{0} has no field "{1}"'.format(
dottedname,
field_name
)
)
store = iface.queryTaggedValue(AVATAR_KEY)
if store is None:
store = []
store.append((iface, field_name, 'true'))
iface.setTaggedValue(AVATAR_KEY, store)


def get_icon(icon):
"""
Returns a css class for the icon. Reads the icon font from the registry.
Expand Down