[1] | 1 | from types import UnicodeType |
---|
| 2 | from Acquisition import aq_inner |
---|
| 3 | |
---|
| 4 | from Products.CMFCore.utils import getToolByName |
---|
| 5 | |
---|
| 6 | from MemberdataHandlers import registerMemberdataHandler, IMemberdataHandler |
---|
| 7 | |
---|
| 8 | class CMFMemberdataHandler: |
---|
| 9 | """ |
---|
| 10 | Manage portal_memberdata after migration it to CMFMembers' portal_memberdata tool |
---|
| 11 | """ |
---|
| 12 | |
---|
| 13 | __implements__ = IMemberdataHandler |
---|
| 14 | |
---|
| 15 | def __init__(self, context): |
---|
| 16 | self.context = context |
---|
| 17 | self.tool = getToolByName(self.context, 'portal_memberdata') |
---|
| 18 | self.fieldnames = [] |
---|
| 19 | self.compatible = 'BaseBTreeFolder' in [klass.__name__ for klass in self.tool.__class__.__bases__] |
---|
| 20 | self.is_compatible() |
---|
| 21 | |
---|
| 22 | def is_compatible(self): |
---|
| 23 | """ |
---|
| 24 | Take portal_memberdata tool and analyse possibility of working |
---|
| 25 | with it. |
---|
| 26 | If handler does not know how to work with given portal_memberdata, |
---|
| 27 | it return 'False' on init, otherwise - 'True'. |
---|
| 28 | As condition used inheritance from 'BaseBTreeFolder' class. |
---|
| 29 | """ |
---|
| 30 | if not self.compatible: |
---|
| 31 | raise |
---|
| 32 | return self.compatible |
---|
| 33 | |
---|
| 34 | def listAllMemberProperties(self, exclude_props=[], include_props=None): |
---|
| 35 | """ |
---|
| 36 | Collect and return all distinct shema fields from all portal cmf members |
---|
| 37 | """ |
---|
| 38 | if not self.is_compatible(): return [] |
---|
| 39 | all_props = [] |
---|
| 40 | for member in self.getAllMembers(): |
---|
| 41 | if hasattr(aq_inner(member), 'Schema') and callable(member.Schema): |
---|
| 42 | schema = member.getSchema() |
---|
| 43 | fields = [f.getName() for f in schema.filterFields() |
---|
| 44 | if (f.getName() not in exclude_props) and (include_props and f.getName() in include_props or not include_props)] |
---|
| 45 | for field in fields: |
---|
| 46 | if field not in all_props: |
---|
| 47 | all_props.append(str(field)) |
---|
| 48 | return all_props |
---|
| 49 | |
---|
| 50 | def getAllMembers(self): |
---|
| 51 | """ |
---|
| 52 | Return all members with portal_memberdata.contentValues() folderish method |
---|
| 53 | """ |
---|
| 54 | if not self.compatible: return [] |
---|
| 55 | return self.tool.contentValues() |
---|
| 56 | |
---|
| 57 | def getMemberProperties(self, member, exclude_props=[], include_props=None): |
---|
| 58 | """ |
---|
| 59 | Return all needed members' properties as dictionary |
---|
| 60 | {property name : property value, ...}, |
---|
| 61 | exclude properties from exclue_props parameter. |
---|
| 62 | Get properties from schemas of every member object and collect it. |
---|
| 63 | """ |
---|
| 64 | if not self.compatible: return {} |
---|
| 65 | if hasattr(aq_inner(member), 'Schema') and callable(member.Schema): |
---|
| 66 | schema = member.getSchema() |
---|
| 67 | fields = [f.getName() for f in schema.filterFields() |
---|
| 68 | if (f.getName() not in exclude_props) and (include_props and f.getName() in include_props or not include_props)] |
---|
| 69 | for field in fields: |
---|
| 70 | if field not in self.fieldnames: |
---|
| 71 | self.fieldnames.append(str(field)) |
---|
| 72 | props = {} |
---|
| 73 | for field in self.fieldnames: |
---|
| 74 | value = member.getProperty(str(field), '') |
---|
| 75 | if type(value) is UnicodeType: |
---|
| 76 | value = value.encode('UTF8') |
---|
| 77 | props[field] = value |
---|
| 78 | return props |
---|
| 79 | |
---|
| 80 | registerMemberdataHandler(CMFMemberdataHandler) |
---|