# -*- 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 """Contains class Jamming, which has a number of JamSessions.""" __version__ = '$Revision$'[11:-2] import time import OFS, Globals from Globals import Persistent from AccessControl import ClassSecurityInfo from Cruft import Cruft from JamSessionLinear import JamSessionLinear from JamSessionTree import JamSessionTree from JamSessionGraph import JamSessionGraph from TraversableWrapper import Traversable from common import intersect_bool, make_action, translate from common import perm_view, perm_edit, perm_manage, perm_add_lo from input_checks import is_valid_title from zope.component import getUtility from interfaces import IStatistics """A container for JamSessions. The only reason we really need this class is for implementing Jamming tab by using own fle_html_header here.""" class Jamming( Traversable, Cruft, # for find_URL_of_fle_root() Persistent, OFS.Folder.Folder, ): """Jamming, contained within Course, represents jamming for one course.""" meta_type = 'Jamming' security = ClassSecurityInfo() security.declareObjectPublic() def __init__(self, id_): """Constructor of the Jamming.""" self.id = id_ self.title = 'Jamming, container for JamSessions' # This is for group folder path listings - show path up to jamming. self.toplevel = 1 security.declareProtected(perm_view, 'index_html') def index_html(self,REQUEST): """ index html """ return REQUEST.RESPONSE.redirect('jamming_index_html') security.declareProtected(perm_view, 'get_bg_colour_name') def get_bg_colour_name(self): """...""" return 'bl' def get_name(self): """Get course name.""" return "jamming" security.declareProtected(perm_view, 'get_n_jam_sessions') def get_n_jam_sessions(self): """Return number of JamSessions in (the jamming of) this course.""" return len(self.get_children('JamSession')) security.declareProtected(perm_view, 'get_n_artefacts') def get_n_artefacts(self): """Return total number of JamArtefact in this jamming.""" n = 0 for js in self.get_children('JamSession'): n += js.get_n_artefacts() return n security.declareProtected(perm_view, 'get_n_unread_artefacts') def get_n_unread_artefacts(self, REQUEST): """Return total number of unread JamArtefact in this jamming.""" n = 0 for js in self.get_children('JamSession'): n += js.get_n_unread_artefacts(REQUEST) return n security.declareProtected(perm_edit, 'delete_jam_sessions_handler') def delete_jam_sessions_handler( self, REQUEST, jam_session_id_list): """Form handler for deleting JamSessions.""" return self.message_dialog2( self, REQUEST, title = 'Confirmation', message = translate(self, 'Are you sure you want to delete the following jam sessions:', target=self.giveLanguage(REQUEST)) + ' ' + \ ', '.join([self.get_child(js_id).get_name() for \ js_id in jam_session_id_list]), handler = 'delete_jam_sessions_confirmation_form_handler', extra_value_name = 'jam_session_id_list', extra_values = jam_session_id_list, option1_value = 'Cancel', option1_name = 'cancel', option2_value = 'Ok', option2_name = 'delete' ) security.declareProtected(perm_edit, 'delete_jam_sessions_confirmation_form_handler') def delete_jam_sessions_confirmation_form_handler( self, REQUEST, jam_session_id_list, cancel='', # submit buttons delete='', ): """Delete jam sessions.""" if delete: for js_id in jam_session_id_list: self._delObject(js_id) elif cancel: pass else: raise 'FLE Error', 'Unknown button' REQUEST.RESPONSE.redirect('index_html') security.declareProtected(perm_add_lo, 'form_handler') def form_handler( self, my_name = '', type = None, # 'linear', 'tree', or 'graph' description = None, artefact_name = None, artefact_upload = None, annotation = '', submit = '', # submit buttons cancel = '', # REQUEST = None, ): """Form handler for creating new JamSession.""" if submit: if type == 'graph' and not self.has_PIL(): # Users are not able to select graph type from UI if PIL is # not installed so this never happens unless input is hacked. raise 'FLE Error', 'PIL not installed' error_fields = [] my_name = my_name.strip() if not is_valid_title(my_name): error_fields.append(translate(self,'Title of jam session:',target=self.giveLanguage(REQUEST))) if not description: error_fields.append(translate(self,'Description of jam session:',target=self.giveLanguage(REQUEST))) if type not in ('linear', 'tree', 'graph'): error_fields.append(translate(self,'Type of jam session:',target=self.giveLanguage(REQUEST))) if not artefact_upload or len(artefact_upload.filename)==0: error_fields.append(translate(self,'Upload the starting artefact:',target=self.giveLanguage(REQUEST))) if not is_valid_title(artefact_name): error_fields.append(translate(self,'Name of the starting artefact:',target=self.giveLanguage(REQUEST))) import re tmp = error_fields error_fields = [] for x in tmp: error_fields.append(re.sub(':','',x)) if len(error_fields) > 0: msg = translate(self,'Invalid fields',target=self.giveLanguage(REQUEST)) + ": '" + \ "' , '".join(error_fields) + "'" return self.message_dialog_error( self, REQUEST, title= 'Invalid input', message=msg, action=apply( make_action, ['jam_add_session_form'] + [(x, eval(x)) for x in ('my_name', 'description', 'type', 'artefact_name', 'annotation')])) data = artefact_upload.read() try: content_type = artefact_upload.headers['content-type'] except KeyError: content_type = '' id_ = 'js' + self.parent().parent().generate_id() jam_session_class = {'linear': JamSessionLinear, 'tree': JamSessionTree, 'graph': JamSessionGraph}[type] self._setObject(id_, jam_session_class( id_, my_name, description, artefact_name, # starting artefact data, # content_type, # )) if REQUEST: s = getUtility(IStatistics) s._updateStat(REQUEST, 'postedArtefacts') s._updateStat(REQUEST, 'modified_jamming') ja = self._getOb(id_).get_children('JamArtefact')[0] uname = repr(REQUEST.AUTHENTICATED_USER) ja.update_reader(uname) if annotation: ja.add_annotation(uname, time.time(), annotation) elif cancel: pass else: raise 'FLE Error', 'Unknown button' # Should never happen. if REQUEST: REQUEST.RESPONSE.redirect('index_html') else: # import code return self.get_child(id_) Globals.default__class_init__(Jamming) # EOF