# -*- coding: utf-8 # Copyright 2006 by Meelis Mets from Globals import InitializeClass from Products.CMFCore.utils import getToolByName from Products.Archetypes.public import BaseContent, registerType from Permissions import * from AccessControl import ClassSecurityInfo, Unauthorized from config import PROJECT_NAME class BaseFunctions(BaseContent): """ BaseFunctions """ security.declareProtected('View', 'getClasses') def getClasses(self): """ Classes massive """ classes = ['Nobody',] member = self.REQUEST.AUTHENTICATED_USER groupstool = getToolByName(self, 'portal_groups') groups = groupstool.listGroupIds() roles = member.getRolesInContext(self) if 'Teacher' in roles or 'Manager' in roles: classes.append('All users',) for target in groups: target2 = groupstool.getGroupById(target) if 'Manager' in roles: target = str(target) if target not in ['Administrators', 'Reviewers']: classes.append(target) else: for md in target2.getGroupMembers(): if md.id == str(member): classes.append(str(target)) return classes security.declareProtected('View', 'getStudents') def getStudents(self, groupId): """ Students massive """ students = [] member = self.REQUEST.AUTHENTICATED_USER groupstool = getToolByName(self, 'portal_groups') if groupId not in ['Nobody','All users']: group = groupstool.getGroupById(str(groupId)) for md in group.getGroupMembers(): if 'Teacher' not in md.getRolesInContext(self) and self.portal_membership.getHomeFolder(md.id)!=None: students.append(md) return students security.declareProtected('View', 'getAllExhibitions') def getAllExhibitions(self): """returns list on exhibitions""" exhibitionfolder = getattr(self, 'exhibitionfolder') return exhibitionfolder.getFolderContents() security.declareProtected('Manage Exhibitions', 'addToExhibition') def addToExhibition(self,REQUEST): exhibitionfolder = getattr(self, 'exhibitionfolder') selectedExhibitions = REQUEST.get('exhibitions') if hasattr(exhibitionfolder,selectedExhibitions): getattr(exhibitionfolder,selectedExhibitions).addRefsToWhat('refsToExhibition', self) else: for ex in selectedExhibitions: if hasattr(exhibitionfolder,ex): getattr(exhibitionfolder,ex).addRefsToWhat('refsToExhibition', self) security.declareProtected('Manage Exhibitions', 'addRefsToWhat') def addRefsToWhat(self, what, ref, empty=0): """ add reference to what """ field = self.Schema().get(what) prev = field.getRaw(self) new_value = [] from types import ListType, TupleType if type(ref) == ListType or type(ref) == TupleType: new_value = prev if empty: new_value = [] for x in ref: new_value.append(x) else: new_value = prev+[ref,] field.set(self, new_value,) security.declareProtected('Manage Exhibitions', 'delFromExhibition') def delFromExhibition(self, REQUEST): """ delete selected resource """ field = self.Schema().get('refsToExhibition') prev = field.getRaw(self, aslist=True) counter = 1 new = [] for x in prev: if REQUEST.get('exponent_nr_'+str(counter)) != "on": new.append(x) counter = counter + 1 field.set(self, new) security.declareProtected('View', 'getNumberOfAnswers') def getNumberOfAnswers(self): """ Number of answers""" return len(self.getRefsToThisTask()) security.declareProtected('View', 'whoIsTasksCreator') def whoIsTasksCreator(self): """ who is creator?""" if len(self.getRefsToThisTask())>0: return self.getFullName(self.getRefsToThisTask()[0].Creator()) return "-" security.declareProtected('View', 'getFullName') def getFullName(self, username): """ gets members fullname """ if username == None: return '' user_obj = self.portal_membership.getMemberById(username) if not user_obj: return '' f_name = user_obj.getProperty('fullname') if f_name == '': f_name = username return f_name security.declareProtected('View', 'canSeeData') def canSeeData(self): """ member can see data """ member = self.REQUEST.AUTHENTICATED_USER for role in ['Teacher','Manager']: if role in member.getRolesInContext(self): return True return False security.declareProtected('View', 'isOwner') def isOwner(self, member): """ is owner? """ homefolder = self.portal_membership.getHomeFolder(member) if hasattr(homefolder, str(self.Target[0])): object = getattr(homefolder, str(self.Target[0])) if str(object.Creator())==str(member): return True for subfolder in homefolder.objectValues('KFolder'): if hasattr(subfolder, str(self.Target[0])): object = getattr(subfolder, str(self.Target[0])) if str(object.Creator())==str(member): return True return False security.declareProtected('View', 'isCreator') def isCreator(self): """ is creator? has problems with other imported products""" member = self.REQUEST.AUTHENTICATED_USER if str(member)==str(self.Creator()): return True elif 'Manager' in member.getRolesInContext(self): return True else: return False return False security.declareProtected('View', 'getRemoteUrl') def getRemoteUrl(self): return self.absolute_url() security.declareProtected('View', 'cutTitle') def cutTitle(self, title, lenght): """ cut tail of title """ if len(title)>lenght: return title[:lenght]+'...' return title security.declareProtected('View', 'getMyFolders') def getMyFolders(self,member): """ get my folder and all subfolders """ folders = [] homefolder = self.portal_membership.getHomeFolder(member) folders.append(homefolder) subfolders = homefolder.objectValues("KFolder") for subfolder in subfolders: folders.append(subfolder) return folders security.declareProtected('Modify portal content', 'deleteObject') def deleteThisObject(self): """ delete object """ id = self.REQUEST.get('id') if id: self._delObject(id) return self.REQUEST.RESPONSE.redirect(self.absolute_url()) def charReplace(self,string): string=string.replace('ä','\xc3\xa4') string=string.replace('Ä','\xc3\x84') string=string.replace('ö','\xc3\xb6') string=string.replace('Ö','\xc3\xb6') string=string.replace('õ','\xc3\xb5') string=string.replace('Õ','\xC3\x95') string=string.replace('ü','\xc3\xbc') string=string.replace('Ü','\xc3\x9c') string=string.replace('ž','z') string=string.replace('š','s') return string