source: products/CMFPlone_membership_hotfix/trunk/patch_portraits.py @ 2111

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

Building directory structure

  • Property svn:eol-style set to native
File size: 2.9 KB
Line 
1from Products.CMFCore.CMFCorePermissions import SetOwnPassword
2from Products.CMFCore.utils import getToolByName
3from Products.CMFDefault.MembershipTool import MembershipTool as BaseTool
4from Products.CMFPlone import ToolNames
5from OFS.Image import Image
6from AccessControl import ClassSecurityInfo, getSecurityManager
7from Globals import InitializeClass
8from Acquisition import aq_base, aq_parent, aq_inner
9from Products.CMFCore.CMFCorePermissions import View
10from Products.CMFPlone.PloneBaseTool import PloneBaseTool
11
12from utils import scale_image
13from Globals import InitializeClass, DTMLFile
14from ZODB.POSException import ConflictError
15from Products.CMFCore.utils import _checkPermission
16
17from Products.CMFPlone.MembershipTool import MembershipTool
18   
19def changeMemberPortrait(self, portrait, member_id=None):
20    """
21    given a portrait we will modify the users portrait
22    we put this method here because we do not want
23    .personal or portrait in the catalog
24    """
25    if not member_id:
26        member_id = self.getAuthenticatedMember().getUserName()
27
28    if portrait and portrait.filename:
29        scaled, mimetype = scale_image(portrait)
30        portrait = Image(id=member_id, file=scaled, title='')
31        membertool   = getToolByName(self, 'portal_memberdata')
32        membertool._setPortrait(portrait, member_id)
33
34
35
36def getBadMembers(self):
37    """Will search for members with bad images in the portal_memberdata
38    delete their portraits and return their member ids"""
39    memberdata = getToolByName(self, 'portal_memberdata')
40    portraits = getattr(memberdata, 'portraits', None)
41    if portraits is None:
42        return []
43    bad_member_ids = []
44    TXN_THRESHOLD = 50
45    counter = 1
46    for member_id in tuple(portraits.objectIds()):
47        portrait = portraits[member_id]
48        portrait_data = str(portrait.data)
49        if portrait_data == '':
50            continue
51        try:
52            img = PIL.Image.open(StringIO(portrait_data))
53        except ConflictError:
54            pass
55        except:
56            # Anything else we have a bad bad image and we destroy it
57            # and ask questions later.
58            portraits._delObject(member_id)
59            bad_member_ids.append(member_id)
60        if not counter%TXN_THRESHOLD:               
61            get_transaction().commit()
62        counter = counter + 1
63
64    return bad_member_ids
65
66MembershipTool.manage_options = (MembershipTool.manage_options +
67                      ( { 'label' : 'Portraits'
68                     , 'action' : 'manage_portrait_fix'
69                     },))
70MembershipTool.security = ClassSecurityInfo()
71MembershipTool.security.declareProtected('Manage Portal', 'manage_portrait_fix')
72MembershipTool.manage_portrait_fix = DTMLFile('www/portrait_fix', globals())
73MembershipTool.security.declareProtected('Manage Portal', 'getBadMembers')
74MembershipTool.getBadMembers = getBadMembers
75
76InitializeClass(MembershipTool)
Note: See TracBrowser for help on using the repository browser.