source: products/qMemberdataExport/trunk/CMFMemberdataHandler.py @ 2111

Last change on this file since 2111 was 1, checked in by myroslav, 16 years ago

Building directory structure

  • Property svn:eol-style set to native
File size: 3.2 KB
Line 
1from types import UnicodeType
2from Acquisition import aq_inner
3
4from Products.CMFCore.utils import getToolByName
5
6from MemberdataHandlers import registerMemberdataHandler, IMemberdataHandler
7
8class 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        for member in self.getAllMembers():
40            if hasattr(aq_inner(member), 'Schema') and callable(member.Schema):
41                schema = member.getSchema()
42                for field in schema.filterFields():
43                    fname = str(field.getName())
44                    if (fname not in exclude_props) and (include_props and fname in include_props or not include_props):
45                        if field not in self.fieldnames:
46                            self.fieldnames.append(fname)
47        return all_props
48
49    def getAllMembers(self):
50        """
51            Return all members with portal_memberdata.contentValues() folderish method
52        """
53        if not self.compatible: return []
54        return self.tool.contentValues()
55
56    def getMemberProperties(self, member, exclude_props=[], include_props=None):
57        """
58            Return all needed members' properties as dictionary
59            {property name : property value, ...},
60            exclude properties from exclue_props parameter.
61            Get properties from schemas of every member object and collect it.
62        """
63
64        if not self.compatible: return {}
65
66        props = {}
67        if hasattr(aq_inner(member), 'Schema') and callable(member.Schema):
68            schema = member.getSchema()
69            for field in schema.filterFields():
70                fname = str(field.getName())
71                if (fname not in exclude_props) and (include_props and fname in include_props or not include_props):
72                    if not props.has_key(fname):
73                        value = member.getProperty(fname, '')
74                        if type(value) is UnicodeType:
75                            value = value.encode('UTF8')
76                        props[fname] = value
77                    if fname not in self.fieldnames:
78                        self.fieldnames.append(fname)
79
80        return props
81
82registerMemberdataHandler(CMFMemberdataHandler)
Note: See TracBrowser for help on using the repository browser.