# -*- coding: utf-8 # $Id$ # # Copyright 2001, 2002 by IVA Team and contributors # # This file is part of IVA. # # IVA is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # IVA is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with IVA; if not, write to the Free Software # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA """ Subgroups - shared webtop folder for group of people. """ __version__ = "$Revision$"[11:-2] import Globals import WebtopFolder from WebtopItem import WebtopItem from AccessControl import ClassSecurityInfo from common import perm_view, perm_edit, perm_manage, perm_add_lo from input_checks import strip_all from AccessControl.PermissionRole import rolesForPermissionOn class SubgroupManager( WebtopFolder.WebtopFolder, ): """ subgroups manager. creates and controls subgroups """ meta_type = "SubgroupManager" security = ClassSecurityInfo() subgroup_top = 1 def __init__(self): """ construct manager """ self.id = "subgroups" security.declareProtected(perm_view, 'index_html') def index_html(self, REQUEST=None): """ index """ return self.sub_manager_index() def manage_afterAdd(self, item, container): """ manage after add - set permissions """ self.manage_permission(perm_edit, ('Manager','IVAAdmin','Teacher','Student'),0) self.manage_permission('Add FLE LO', ('Owner', 'User'), 1) security.declareProtected(perm_edit, 'subgroup_form_handler') def subgroup_form_handler(self,REQUEST, groupName='', delete='', participants='', eventmanage='', sendmessage='', confirmation='', cancel='', renameGroup= ''): """ redirector - gives control to subgroup """ if groupName: return self.getSubgroupById(groupName).subgroup_form_handler(REQUEST, groupName, delete, participants, eventmanage, sendmessage, renameGroup=renameGroup) security.declareProtected(perm_edit, 'add_subgroup') def add_subgroup(self, grupinimi): """Lisab kursusesise grupi""" sg = Subgroup(self, grupinimi) sg.id = 'sg'+sg.id self._setObject(sg.id, sg) return sg security.declareProtected(perm_edit, 'add_subgroup_handler') def add_subgroup_handler(self, REQUEST=None, subgroup_name='', submit='', cancel=''): """ creating subgroup """ if submit: subgroup_name = strip_all(subgroup_name) if not subgroup_name: return self.message_dialog( message="Please give valid name.", action="workshop_subgroup_add") sgs = self.getSubgroupByName(subgroup_name) if sgs: return self.message_dialog( message="Name taken, please choose different name", action="workshop_subgroup_add") subgroup_id = self.add_subgroup(subgroup_name) elif cancel: pass else: raise 'IVA Error', 'Unknown button' REQUEST.RESPONSE.redirect("index_html") security.declareProtected(perm_view, 'getSubgroups') def getSubgroups(self, REQUEST=None): """ return a list of subgroups """ subs = self.objectValues('Subgroup') if not REQUEST: return subs suund = REQUEST.get('suund', 'weight') if not suund: return subs tmp = [] if suund == 'weight': [tmp.append([x.weight, x]) for x in subs] tmp.sort() tmp.reverse() if suund == 'name': [tmp.append([x.get_name(), x]) for x in subs] tmp.sort() subs = [] [subs.append(x[1]) for x in tmp] return subs security.declareProtected(perm_view, 'getSubgroupById') def getSubgroupById(self, group_id): """ get subgroup by id """ return self.get_child(group_id) def getSubgroupByName(self, group_name): """ get subgroup by name """ for group in self.getSubgroups(): if group.get_name() == group_name: return group return None #security.declareProtected(perm_edit, 'delete_subgroup') security.declarePrivate('delete_subgroup') def delete_subgroup(self, group_id): """ delete subgroup """ # TODO: security check. big problem here obj = getattr(self.aq_inner, group_id) self._delObject(group_id) return 1 security.declareProtected(perm_view, 'user_subgroups') def user_subgroups_by_name(self, user, exclude=''): """ a list of subgroups where user is a member. Can exclude one subgroup - like get all other user subgroups returns a list of subgroup names. """ result = () for group in self.getSubgroups(): if group.get_id() == exclude: continue if group.isSubgroupMember(user): result += (group.get_name(),) return result user_subgroups = user_subgroups_by_name #backwards compatibility def user_subgroups_by_id(self, user, exclude=''): """ a list of subgroups where user is a member. Can exlude one subgroup - like get all other user subgroups returns a list of subgroup ids. """ result = () for group in self.getSubgroups(): if group.get_id() == exclude: continue if group.isSubgroupMember(user): result += (group.get_id(),) return result security.declareProtected(perm_view, 'user_subgroups_printable') def user_subgroups_printable(self, user, exclude=''): """ see above """ result = "" for group in self.user_subgroups(user, exclude): result += group+", " if result: result = "( " + result[:-1] + " )" return result Globals.InitializeClass(SubgroupManager) class Subgroup( WebtopFolder.WebtopFolder, ): """ one subgroup """ meta_type = "Subgroup" security = ClassSecurityInfo() def __init__(self, parent, name): """ construct subgroup """ WebtopItem.__init__(self, parent, name) self.set_icon('folder_gif.gif') self.__id_counter = long(1) def index_html(self, REQUEST=None): """ index page """ self.viewedObject(REQUEST, self.getRelativeURL(self)) return self.wt_index_html() def manage_afterAdd(self, item, container): """ after add """ #XXX: do we need it here? try: WebtopItem.manage_afterAdd(self, item, container) except: pass # kalendrisyndmuysed import Kalender events = Kalender.KalendriSyndmusteKataloog() self._setObject('syndmused', events) # permissions self.manage_permission(perm_edit, ('Manager','Owner','IVAAdmin','Teacher','User'),0) self.manage_permission('Zwiki: Edit pages', ('User',), 1) security.declarePrivate('generate_id') def generate_id(self): """ generate id """ self.__id_counter += 1L return "sg"+str(self.__id_counter) security.declareProtected(perm_view, 'get_list_item_name') def get_list_item_name(self, REQUEST=None): """ Returns the displayable string to be used when the item is shown in a folder contents list.""" return self.get_name() security.declareProtected(perm_view, 'is_subgroup') def is_subgroup(self): """ this is subgroup. return name """ return self.get_name() def is_subgroup_id(self): """ this is subgroup. return id """ return self.get_id() security.declareProtected(perm_edit, 'delete_subgroup_handler') def delete_subgroup_handler(self, REQUEST, groupName='', delete='', confirmation='', cancel=''): """ helps to delete a subgroup """ if delete and confirmation: if groupName: self.delete_subgroup(groupName) else: pass # report problem elif cancel: pass REQUEST.RESPONSE.redirect('../index_html') security.declareProtected(perm_view, 'subgroup_form_handler') def subgroup_form_handler(self,REQUEST,groupName='',delete='',participants='',eventmanage='',sendmessage='',confirmation='',cancel='', renameGroup=''): """ index_html form handler. groupName is actually group id here""" if not self.is_student(REQUEST) and not self.kas_opetaja(REQUEST): return "" if not groupName: return self.message_dialog_error( self, REQUEST, title="Error", message="no group selected", action="index_html") if delete: return self.message_dialog2( title="Confirmation", message="Are you sure you want to delete subgroup? There will be no undo!", option1_name="cancel", option1_value="Cancel", option2_value="Delete", option2_name="confirmation", extra_value_name='delete', extra_values = ('yes',), item_name='groupName', item_value=groupName, handler="%s/delete_subgroup_handler" % self.absolute_url()) return REQUEST.RESPONSE.redirect("index_html") if participants: return REQUEST.RESPONSE.redirect(groupName+"/workshop_subgroup_members") if eventmanage: return REQUEST.RESPONSE.redirect(groupName+"/syndmused/event_list?userLocation=Workshops") if sendmessage: curUser = getattr(self.fle_root().fle_users, str(REQUEST.AUTHENTICATED_USER), None) if curUser is None: return 0 REQUEST.RESPONSE.redirect(curUser.absolute_url()+"/kirjad/createMail?userLocation=Workshops") kellele = '' for x in self.subgroup_members(): if kellele: kellele+=',' kellele+=x REQUEST.SESSION.set('kellele',kellele) # FIX: delete session variable return curUser.kirjad.createMail() if renameGroup: REQUEST.set('item_ids', (self.getId(),)) return self.aq_parent.wt_rename(REQUEST) return security.declareProtected(perm_edit, 'subgroup_members_handler') def subgroup_members_handler(self, REQUEST, add=[], remove=[], change=''): """ Adds or removes persons from subgroup """ if change: if not add and not remove: pass #TODO: report problem - no users selected if add: for user in add: self.add_to_subgroup(user) if remove: for user in remove: self.remove_from_subgroup(user) else: pass # no button clicked REQUEST.RESPONSE.redirect('workshop_subgroup_members') security.declareProtected(perm_view, 'subgroup_members') def subgroup_members(self): """ return a list of subgroup members """ result = [] for u in self.get_local_roles(): if 'User' in u[1] or 'Owner' in u[1]: result.append(u[0]) return result security.declareProtected(perm_view, 'not_subgroup_members') def not_subgroup_members(self): """ return a list of users not in this subgroup """ result = [] course_list = self.get_all_users_id() group_list = self.subgroup_members() for user in course_list: if user not in group_list: result.append(user) return result security.declareProtected(perm_edit, 'add_to_subgroup') def add_to_subgroup(self, user): """ add user to subgroup """ old_roles = self.get_local_roles_for_userid(user) if 'User' not in old_roles and 'Owner' not in old_roles: roles = old_roles + ('User',) self.manage_setLocalRoles(user, roles) return 0 security.declareProtected(perm_edit, 'remove_from_subgroup') def remove_from_subgroup(self, user): """ remove from subgroup """ old_roles = self.get_local_roles_for_userid(user) new_roles = () if 'Owner' in old_roles: # TODO: cannot remove owner from subgroup return 1 for role in old_roles: if role == 'User': continue new_roles += (role,) if new_roles: self.manage_setLocalRoles(user, new_roles) else: self.manage_delLocalRoles((user,)) return 0 security.declareProtected(perm_view, 'isSubgroupMember') def isSubgroupMember(self, user): """ is user a member of subgroup """ roles = self.get_local_roles_for_userid(user) if 'User' in roles or 'Owner' in roles: return 1 return 0 Globals.InitializeClass(Subgroup)