from OFS.SimpleItem import SimpleItem from OFS.SimpleItem import Item from OFS.PropertyManager import PropertyManager from Products.CMFCore.utils import UniqueObject from Globals import InitializeClass from AccessControl import ClassSecurityInfo, Unauthorized from Products.CMFCore.utils import getToolByName from ZODB.PersistentMapping import PersistentMapping from Acquisition import aq_base from config import MANAGE_PORTAL from plone.intelligenttext.transforms import convertWebIntelligentPlainTextToHtml from DateTime.DateTime import DateTime class MemoriesTool(PropertyManager, SimpleItem, UniqueObject): """ Simple helper tool """ id = 'memories_tool' meta_type = 'MemoriesTool' security = ClassSecurityInfo() plone_tool = 1 toolicon = 'skins/Memories/memories_tool.gif' __allow_access_to_unprotected_subobjects__ = 1 security.declarePublic('getMemberById') def getMemberById(self,id): mtool = getToolByName(self, 'portal_membership') member = mtool.getMemberById(id) return member security.declarePublic('getMemberInfo') def getMemberInfo(self,memberId=None): """ Duplicated the method of MembershipTool, also returns our own properties """ mtool = getToolByName(self, 'portal_membership') if not memberId: member = mtool.getAuthenticatedMember() else: member = mtool.getMemberById(memberId) if member is None: return None memberinfo = { 'fullname' : member.getProperty('fullname'), 'description' : member.getProperty('description'), 'location' : member.getProperty('location'), 'language' : member.getProperty('language'), 'home_page' : member.getProperty('home_page'), 'history_interest' : member.getProperty('history_interest'), 'last_login_time' : member.getProperty('last_login_time'), 'gender' : member.getProperty('gender'), 'username' : member.getUserName(), } return memberinfo def getAllAnnouncements(self): """ Returns all published announcements """ pc = getToolByName(self, 'portal_catalog') announcements = pc({'portal_type':'Announcement','review_state':'published','sort_on':'created','sort_order':'descending'}) return announcements def getAllMemoriesByMember(self,memberId): """ Returns all public Memories created by member """ pc = getToolByName(self, 'portal_catalog') memories = pc({'portal_type':'GeoLocation','Creator':memberId,'sort_on':'created','sort_order':'descending'}) return memories def getNumberOfMemoriesByMember(self, memberId): """ Returns number of published memories """ pc = getToolByName(self, 'portal_catalog') memories = pc({'portal_type':'GeoLocation','review_state':'published','Creator':memberId,'sort_on':'created','sort_order':'descending'}) return len(memories) def getNumberOfMemberAnnouncements(self, memberId): """ Returns number of published announcements """ pc = getToolByName(self, 'portal_catalog') announcements = pc({'portal_type':'Announcement','review_state':'published','Creator':memberId,'sort_on':'created','sort_order':'descending'}) return len(announcements) def getAllMemoriesForCategory(self, category=None): """ Returns all Memories for some some category """ if not category: return [] pc = getToolByName(self, 'portal_catalog') currentmonth = "%s/%s/01 00:00:00" % (DateTime().year(), DateTime().month()) print "currentmonth", currentmonth memories = pc({'portal_type':'GeoLocation','Subject':category,'review_state':'published','sort_on':'created','sort_order':'descending'}, created={'query': currentmonth, 'range':'min'}) return memories security.declarePublic('convertPlainTextToHtml') def convertPlainTextToHtml(self, text): return convertWebIntelligentPlainTextToHtml(text) security.declarePublic('getLatestUserLogin') def getLatestUserLogin(self): # get all users # get users info mtool = getToolByName(self, 'portal_membership') all_members = [] # XXX This might be very expensive # XXX Not sure if we do need all the infor provided by getMemberInfo members = mtool.listMembers() for member in members: all_members.append((member.getProperty('last_login_time'),member.getUserName())) all_members.sort() returned = all_members[:10] returned.reverse() return [self.getMemberInfo(m[1]) for m in returned] def getMostActiveUsers(self): """ Returns the list of 10 users with most memories """ mtool = getToolByName(self, 'portal_membership') pc = getToolByName(self, 'portal_catalog') memberids = mtool.listMemberIds() all_member_data = [] for memberid in memberids: memories = pc({'portal_type':'GeoLocation','review_state':'published','Creator':memberid}) result = len(memories) all_member_data.append([int(result),memberid]) all_member_data.sort(key=lambda x:x[0], reverse=True) return all_member_data[:10] def getLatestMemories(self): """ Returns 10 latest memories """ pc = getToolByName(self, 'portal_catalog') memories = pc({'portal_type':'GeoLocation','review_state':'published','sort_on':'created','sort_order':'descending'}) return memories[:20] def getMostCommentedMemories(self): """ Returns 20 most commented memories """ pc = getToolByName(self, 'portal_catalog') memories = pc({'portal_type':'GeoLocation','review_state':'published','sort_on':'getComments','sort_order':'descending'}) return memories[:20] def getCommentsNumber(self, obj): """ Uses catalog to return the number of comments """ # Uses catalog objects only pc = getToolByName(self, 'portal_catalog') replies = pc({'portal_type':'Discussion Item','path':obj.getPath()}) return len(replies) def getSelectablePeriods(self): now_year = DateTime().year() all_years = [] y = 1800 # XXX Need to make generation of the final year automatic for x in range(1800, 2011, 10): if x != 1800: all_years.append('%s - %s' % (str(y+1), str(x))) y = x all_years.reverse() return all_years security.declarePublic('doTheReindexing') def doTheReindexing(self, object): # reindex the object for anybody object.reindexObject() security.declarePublic('shortenedTitle') def shortenedTitle(self, title, max_length): if len(title)<=max_length: return title while len(title)>max_length: title = title[:title.rfind(' ')] return title+"..." def givePlainTextCharactersNumber(self, text): """ Converts html to plaintext and returns the length """ import re plain_result = re.sub('<.*?>', '', text) return len(plain_result) InitializeClass(MemoriesTool)