# -*- coding: utf-8 # -*- Mode: Python; py-indent-offset: 4 -*- """ BaseQuestion Class """ __version__ = "$Revision:33$"[11:-2] import re import datetime from waramulib.interfaces import IWaramuClient import Globals from Globals import Acquisition, Persistent from Acquisition import aq_base, aq_inner, aq_parent, Explicit from AccessControl import ClassSecurityInfo, getSecurityManager from OFS.SimpleItem import SimpleItem from Products.ZCatalog.CatalogPathAwareness import CatalogAware from SOAPpy import SOAPProxy from SOAPpy import Types from SOAPpy import Config import cElementTree from zope.interface import implements from zope.component import getUtility from common import commons from Permissions import * from config import VERSION from interfaces import IBaseQuestion class BaseQuestion(CatalogAware, SimpleItem, Persistent, Explicit, commons): """ """ security = ClassSecurityInfo() security.declareObjectPublic() implements(IBaseQuestion) manage_options = SimpleItem.manage_options def __init__(self): self.titletext = "" self.questiontext = "" self.keywords = {} self.description = {} self.default_catalog = "cat_ques" self._state = "draft" def index_html(self): """ index """ return self.restrictedTraverse('view')() security.declareProtected(perm_edit_question, 'setQuestion') def setQuestion(self, question): self.questiontext = question security.declareProtected(perm_view_question, 'getQuestion') def getQuestion(self, REQUEST=None): """ """ if REQUEST is not None: REQUEST.RESPONSE.setHeader('Content-Type', 'text/html; charset=UTF-8') return self.questiontext security.declareProtected(perm_edit_question, 'setTitle') def setTitle(self, title): self.titletext = title security.declareProtected(perm_edit_question, 'setTitles') def setTitles(self, titles): self.titles = titles security.declareProtected(perm_view_question, 'getTitle') def getTitle(self): """ """ try: all_titles = getattr(self, 'titles', None) if all_titles is not None: if all_titles.has_key(self.getLanguage()): return all_titles.get(self.getLanguage()) except AttributeError: # Fall back to old title pass if self.titletext.strip() == '': return "untitled" return self.titletext security.declareProtected(perm_view_question, 'getTitles') def getTitles(self): titles = getattr(self, 'titles', None) if titles is None: lang = self.getLanguage(); self.titles = {lang: self.titletext} #self.titles = {'et': self.titletext} return self.titles security.declareProtected(perm_edit_question, 'setKeywords') def setKeywords(self, keywords): self.keywords = keywords ktuple = [] for k_lang in keywords: keys = keywords.get(k_lang) keys = keys.split(',') for k in keys: k = k.strip() if str(k) != '': ktuple.append(k) self.k = ktuple security.declareProtected(perm_view_question, 'getKeywords') def getKeywords(self): """ """ return self.keywords security.declareProtected(perm_view_question, 'getKeywordsTuple') def getKeywordsTuple(self): #keywordid massiivina """ """ return getattr(self, 'k', []) security.declareProtected(perm_edit_question, 'setDescription') def setDescription(self, description): self.description = description security.declareProtected(perm_view_question, 'getDescription') def getDescription(self): """ """ return self.description security.declareProtected(perm_edit_question, 'setLanguage') def setLanguage(self, language): self.language = language security.declareProtected(perm_view_question, 'getLanguage') def getLanguage(self): """ """ return getattr(self, 'language', 'et') security.declareProtected(perm_edit_question, 'setDifficulty') def setDifficulty(self, difficulty): self.difficulty = difficulty security.declareProtected(perm_view_question, 'getDifficulty') def getDifficulty(self): """ """ return getattr(self, 'difficulty', 'easy') security.declareProtected(perm_edit_question, 'setCreatingDay') def setCreatingDay(self): self.creatingDay = datetime.datetime.today() security.declareProtected(perm_view_question, 'getCreatingDay') def getCreatingDay(self): """ """ return getattr(self, 'creatingDay', self.getLastChange()) security.declareProtected(perm_edit_question, 'setLastChange') def setLastChange(self): self.lastChange = datetime.datetime.today() security.declareProtected(perm_view_question, 'getLastChange') def getLastChange(self): """ """ if not hasattr(self, 'lastChange'): self.lastChange = datetime.datetime.today() return self.lastChange security.declareProtected(perm_view_question, 'setLastPreviewTime') def setLastPreviewTime(self): self.lastPreview = datetime.datetime.today() security.declareProtected(perm_view_question, 'getLastPreviewTime') def getLastPreviewTime(self): return getattr(self, 'lastPreview', datetime.datetime(2000,01,01)) security.declareProtected(perm_edit_question, 'setUser') def setUser(self, user): self.user = str(user) security.declareProtected(perm_view_question, 'getUser') def getUser(self): """ """ return self.user security.declareProtected(perm_edit_question, 'setLREKeywords') def setLREKeywords(self, val): """ set lre keywords """ if val and isinstance(val, str): val = [val] self.lrekeywords = val security.declareProtected(perm_view_question, 'getLREKeywords') def getLREKeywords(self): """ returns array of lrekeywords """ return getattr(self, 'lrekeywords', []) security.declareProtected(perm_edit_question, 'setLicence') def setLicence(self, val): """ set licence """ self.licence = val security.declareProtected(perm_view_question, 'getLicence') def getLicence(self): return getattr(self, 'licence', 'cc1') security.declareProtected(perm_edit_question, 'setAuthor') def setAuthor(self, val): self.author = val security.declareProtected(perm_view_question, 'getAuthor') def getAuthor(self): return getattr(self, 'author', self.getUser()) security.declareProtected(perm_view_question, 'getAuthorVCard') def getAuthorVCard(self): """ returns author info in a form of vCard """ author = self.getAuthor().decode('utf-8') adata = author.split(" ") fname = adata[0] lname = "" if len(adata) > 1: lname = adata[1] vauthordata = 'BEGIN:VCARD\n' vauthordata += 'VERSION:3.0\n' vauthordata += 'N:%s;%s;;;\n' % (fname, lname) vauthordata += 'FN:%s\n' % (author) #vauthordata += 'ORG:\n' vauthordata += 'END:VCARD' return vauthordata def _setWaramuUid(self, uid): self.waramuUid = uid def getWaramuUid(self): if not hasattr(self, 'waramuUid'): return "" return self.waramuUid def _setHtmlUid(self): """ """ url = self.getRendererServiceURL() namespace = self.getRendererServiceURL() s = SOAPProxy(url) from socket import gaierror try: htmlUid = s._ns(namespace).newQuestion() self.htmlUid = htmlUid except gaierror, timeout: self.htmlUid = '' def getHtmlUid(self): """ """ if not hasattr(self, 'htmlUid'): self._setHtmlUid() if self.htmlUid == "": self._setHtmlUid() return self.htmlUid def _setHtml(self): """ """ renderopts = 31; quesid = [] quesid.append(self.getId()) z = self._makeZip(quesid) zip = Types.base64BinaryType(z) xmlname = str(self.getId())+'.xml' xml = self.xml2(fullURLs=True) xml = xml.encode("utf-8") #from xml.sax.saxutils import escape #xml = escape(xml) import tempfile fd, filename = tempfile.mkstemp() import os os.close(fd) from StringIO import StringIO f = StringIO() f.write(xml) f.seek(0) file = open(filename, 'w') file.write(xml) file.seek(0) file.close() aa = open(filename, 'rb') export_data=aa.read() aa.close() ex = Types.base64BinaryType(export_data) url = self.getRendererServiceURL() namespace = self.getRendererServiceURL() s = SOAPProxy(url) # don't print out SOAP s.config.debug = 0 s.config.dumpSOAPOut = 0 s.config.dumpSOAPIn = 0 # htmlUid = self.getHtmlUid() # if not htmlUid: self._setHtmlUid() htmlUid = self.getHtmlUid() # get question preview try: s._ns(namespace).newContentPackageSessionWithAttachment(xmlname, "r2q2.rendering.xhtml.XHTMLRenderer", renderopts, ex, "", htmlUid) firstStage = s._ns(namespace).firstStage2(htmlUid) except: self._setHtmlUid() htmlUid = self.getHtmlUid() s._ns(namespace).newContentPackageSessionWithAttachment(xmlname, "r2q2.rendering.xhtml.XHTMLRenderer", renderopts, ex, "", htmlUid) firstStage = s._ns(namespace).firstStage2(htmlUid) #print "XML: ", xml #print "HTML: ", firstStage html = firstStage.decode("utf-8") import base64 if html: #val = base64.decodestring(html) val = html html = val.replace('r2q2response.jsp', 'submitAnswer', 1) cm = re.compile('
(.*?)', re.S) head = cm.search(html).group(1) cm = re.compile('