# -*- coding: utf-8 # -*- Mode: Python; py-indent-offset: 4 -*- # $Id$ # # Copyright (c) 2006, Vahur Rebas, HTK, TLU # # Permission to use, copy, modify, and distribute this software for any # purpose with or without fee is hereby granted, provided that the above # copyright notice and this permission notice appear in all copies. # # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. """ Container for Tests """ __version__ = "$Revision:33$"[11:-2] from Globals import Acquisition, Persistent, InitializeClass from Acquisition import aq_base, aq_inner, aq_parent, Explicit from AccessControl import ClassSecurityInfo from Products.BTreeFolder2.BTreeFolder2 import BTreeFolder2 from zope.interface import implements from common import commons import test from Permissions import * from interfaces import ITests class Tests(BTreeFolder2, Persistent, Explicit, commons): """ container class for tests """ meta_type = 'Tests' security = ClassSecurityInfo() security.declareObjectPublic() implements(ITests) manage_options = BTreeFolder2.manage_options id = 'tests' def __init__(self): BTreeFolder2.__init__(self, self.id) security.declareProtected(perm_create_test, 'add_new') def add_new(self, REQUEST): """ """ id = self.random_generator('test_') obj = test.Test(id) obj.setUser(REQUEST.get('AUTHENTICATED_USER')) obj.setCreatingDay() self._setObject(id, obj) obj = getattr(self, id) obj.changeTestInfo(REQUEST) return REQUEST.RESPONSE.redirect(id) def getTests(self, user): """ """ from AccessControl import getSecurityManager authenticated_user = getSecurityManager().getUser() role = self.checkRoles(self, authenticated_user, 'Manager') #1-manager 0-somebody else #if REQUEST.get('my_tests', '') == 'on': # user = str(user) # status = ['draft', 'public', 'private'] #else: # user = '' # status = ['draft', 'public'] # if role == 1: # status = ['draft', 'public', 'private'] #query = { # 'getUser': str(user), # 'getStatus': status, # 'getErased': 0, # 'sort_on': 'getLastChange', # 'sort_order': 'descending' #} #t_results = self.cat_tests(query) t_results = self.cat_tests(getUser=str(user), getErased=0, sort_on='getLastChange', sort_order='descending') return t_results security.declareProtected('Manager', 'getAllTests') def getAllTests(self): """ """ return self.cat_tests(getErased=0, sort_on='getLastChange', sort_order='descending') def makeTestZip(self, remove): """ """ import zipfile from StringIO import StringIO import tempfile allques = [] fd, filename = tempfile.mkstemp() import os os.close(fd) zip = zipfile.ZipFile(filename,"w",zipfile.ZIP_DEFLATED) f = StringIO() f.write(self.manifest_beginning()) from cStringIO import StringIO as cStringIO for test in remove: t = getattr(self, test) f.write(t.getTestPackage()) zip.writestr(zipfile.ZipInfo(test + ".xml"), t.getTestXML()) testques = t.getQuestions() for tques in testques: if tques not in allques: allques.append(tques) for ques in allques: x = getattr(self.questions, ques) xml2 = x.xml2(fullURLs=True) xml2 = xml2.encode('utf-8') zip.writestr(zipfile.ZipInfo(ques + ".xml"), xml2) #imageids = x.getImages() #for z in imageids: # picture = getattr(self.images, z) # # bindata = picture.data # streem = cStringIO() # if type(bindata) is type(''): # streem.write(bindata) # else: # while bindata is not None: # streem.write(bindata.data) # bindata = bindata.next # # imageContentType =picture.content_type.split('/') # imagetype = imageContentType[-1] # # streem.seek(0) # zip.writestr(zipfile.ZipInfo(z +"."+imagetype), streem.read()) quesType = x.getType() #if quesType == 'hotspot_type': # zip.writestr(zipfile.ZipInfo(x.getId() +".jpg"), x.drawPic().read()) if quesType == 'Choice_multiple': video_id = x.getVideoId() if video_id != '': video = getattr(self.videos, video_id) zip.writestr(zipfile.ZipInfo(video_id+".mov"), str(video.data)) f.write(x.package()) f.write(' ') f.seek(0) zip.writestr(zipfile.ZipInfo("imsmanifest.xml"), f.read()) zip.close() file = open(filename, 'rb') export_data=file.read() file.close() try: os.remove(filename) except OSError: pass return export_data def deleteTests(self, REQUEST, remove='', export_button=''): """ """ if isinstance(remove, type('')): remove = [remove,] if export_button: if remove == ['']: return REQUEST.RESPONSE.redirect('tests_index.html?message=Choose test to export!') export_data = self.makeTestZip(remove) REQUEST.RESPONSE.setHeader('content-type', 'application/zip') REQUEST.RESPONSE.setHeader('Content-disposition','attachment; filename=test.zip') return export_data if remove != ['']: for t in remove: test = self.getTestById(t) test._setErased() if (test.getWaramuUid() != ""): test._deleteFromWaramu() test.reindex_object() return REQUEST.RESPONSE.redirect('tests_index.html') InitializeClass(Tests) # EOF