X-Git-Url: https://scm.cri.mines-paristech.fr/git/Plinn.git/blobdiff_plain/7e74f1754d15f96bd33bae06b855c48c87db2498..a8ce11cea15f572925793b181a06fdd5b9b4127a:/utils.py diff --git a/utils.py b/utils.py index 470b49a..de68f22 100755 --- a/utils.py +++ b/utils.py @@ -27,6 +27,8 @@ import re from types import StringType from random import randrange from Acquisition import aq_base +from quopri import encodestring +from zope.globalrequest import getRequest from AccessControl.PermissionRole import rolesForPermissionOn from AccessControl import ModuleSecurityInfo from AccessControl import getSecurityManager @@ -92,12 +94,12 @@ def formatFullName(memberName, memberGivenName, memberId, nameBefore=1) : memberFullName = u'' if memberName and memberGivenName : if nameBefore : - memberFullName = memberName.upper() + ' ' + capitalizeCompoundGivenName(memberGivenName) + memberFullName = memberName.capitalize() + ' ' + capitalizeCompoundGivenName(memberGivenName) else : - memberFullName = capitalizeCompoundGivenName(memberGivenName) + ' ' + memberName.upper() + memberFullName = capitalizeCompoundGivenName(memberGivenName) + ' ' + memberName.capitalize() elif memberName and not memberGivenName : - memberFullName = memberName.upper() + memberFullName = memberName.capitalize() elif not memberName and memberGivenName : memberFullName = capitalizeCompoundGivenName(memberGivenName) @@ -163,7 +165,11 @@ def _checkMemberPermission(userid, permission, obj, StringType = type('')): return 0 def getCPInfo(self) : - try: cp = _cb_decode(self.REQUEST['__cp']) + if self.REQUEST.RESPONSE.cookies.has_key('__cp') : + cp = self.REQUEST.RESPONSE.cookies['__cp']['value'] + else : + cp = self.REQUEST.get('__cp') + try: cp = _cb_decode(cp) except: return None return cp @@ -194,7 +200,7 @@ security.declarePublic('Message') Message = MessageFactory('plinn') security.declarePublic('translate') -def translate(message, context): +def translate(message, context=None): """ Translate i18n message. """ if isinstance(message, Exception): @@ -202,7 +208,11 @@ def translate(message, context): message = message[0] except (TypeError, IndexError): pass - return i18ntranslate(message, domain='plinn', context=context.REQUEST) + if not context : + request = getRequest() + else : + request = context.REQUEST + return i18ntranslate(message, domain='plinn', context=request) security.declarePublic('desacc') desacc = Utf8Utils.desacc @@ -220,7 +230,7 @@ def getPreferredLanguages(context): security.declarePublic('getBestTranslationLanguage') def getBestTranslationLanguage(langs, context): """ returns best translation language according - availables languages (param langs) + to available languages (param langs) and user preferences (retrieves by context) """ request = getattr(context, 'REQUEST', None) @@ -251,6 +261,36 @@ def getAdapterByInterface(ob, dotted_name, default=_marker) : else : return default +security.declarePublic('encodeQuopriEmail') +def encodeQuopriEmail(name, email) : + qpName = encodestring(name).replace('=\n', '') + return '''"=?utf-8?q?%s?=" <%s>''' % (qpName, email) + +security.declarePublic('encodeMailHeader') +def encodeMailHeader(content) : + s = encodestring(content).replace('=\n', '') + s = s.replace('_', '=5F') + s = s.replace(' ', '_') + + lines = [] + STEP = 50 + start = 0 + stop = STEP + part = s[start:stop] + lines.append(part) + + while len(part) == STEP: + start = start + STEP + stop = stop + STEP + part = s[start:stop] + lines.append(part) + + lines = [' =?utf-8?Q?%s?=' % part for part in lines] + s = '\n'.join(lines) + s = s.strip() + return s + + def _sudo(func, userid=None) : """ execute func or any callable object @@ -279,4 +319,20 @@ def _sudo(func, userid=None) : raise e return ret - \ No newline at end of file + +security.declarePublic('searchContentsWithLocalRolesForAuthenticatedUser') +def searchContentsWithLocalRolesForAuthenticatedUser(**kw): + mtool = getUtilityByInterfaceName('Products.CMFCore.interfaces.IMembershipTool') + ctool = getUtilityByInterfaceName('Products.CMFCore.interfaces.ICatalogTool') + member = mtool.getAuthenticatedMember() + userid = member.getId() + userAndGroups = ['user:%s' % userid] + + getGroups = getattr(member, 'getGroups', None) + if getGroups is not None : + for group in getGroups(): + userAndGroups.append('user:'+group) + + kw[ 'allowedRolesAndUsers' ] = userAndGroups + + return ctool.unrestrictedSearchResults(**kw)