# -*- coding: utf-8 # Copyright 2004-2009 by Vahur Rebas """This is a container for documents.""" import Globals from Globals import Acquisition, Persistent from AccessControl import ClassSecurityInfo from AccessControl import getSecurityManager from Products.BTreeFolder2.BTreeFolder2 import BTreeFolder2 from Acquisition import aq_base, aq_inner, aq_parent, Implicit from zope.interface import implements from OFS.PropertyManager import PropertyManager from interfaces import IDocuments from permissions import * NEW_DOC = """""" class Documents(Persistent,BTreeFolder2,Implicit): """ Container holding documents. """ meta_type = 'Korpus Documents' security = ClassSecurityInfo() security.declareObjectPublic() id = 'Documents' implements(IDocuments) def __init__(self): """ init method """ BTreeFolder2.__init__(self,'Documents') security.declareProtected(perm_list_documents, 'index_html') def index_html(self): """ index html """ return self.documents_index() security.declareProtected(perm_del_documents, 'docs_index_handler') def docs_index_handler(self, REQUEST, del_button='', del_docs=''): """ Documents/ form handler. Deletes documents and marks them ready for marking """ if del_button: for x in del_docs: doc = getattr(self, x, None) if doc is None: continue doc.deleteMe() doc.reindex_object() # return REQUEST.RESPONSE.redirect('index_html') return REQUEST.RESPONSE.redirect('./') security.declareProtected(perm_list_documents, 'getDocuments') def getDocuments(self, REQUEST=None, as_objects = False): """ return a list of document objects """ if REQUEST: is_deleted = REQUEST.get('del', 0) doc_stat = REQUEST.get('stat', -1) texttype = REQUEST.get('texttype', '') sort_on = REQUEST.get('sort_on', '') title = REQUEST.get('title', '') else: is_deleted = 0 doc_stat = -1 texttype = sort_on = title = '' if doc_stat == -1: doc_stat = '' query = { 'is_deleted' : is_deleted, 'document_status' : doc_stat, 'texttype' : texttype, 'getTitle' : title, 'sort_on' : sort_on, } if REQUEST: if REQUEST.get('corpus', None): query['getCorpus'] = REQUEST.get('corpus') try: REQUEST.SESSION.set('stat', doc_stat) REQUEST.SESSION.set('texttype', texttype) REQUEST.SESSION.set('sort_on', sort_on) REQUEST.SESSION.set('title', title) except AttributeError: pass res = self.zcatalog(query, sort_on=sort_on, sort_order='descending') if as_objects: return [ x.getObject() for x in res ] return res def dokKorpused(self): "avalik" return ([x.getCorpus() for x in self.objectValues("Document")]) def evkkKorpused(self): "avalik" return len([x for x in self.objectValues("Document") if x.getCorpus()=="cFOoRQekA"]) def evkkIndeksid(self): "avalik" abi=self.objectValues("Document") i=len(abi)-1 while i>0: x=abi[i] if x.getCorpus()=="cFOoRQekA": x.reindex_object() i=i-1 return "indeksid valmis" security.declareProtected(perm_add_documents, 'newDocument') def newDocument(self, REQUEST): """ source for a new document """ REQUEST.RESPONSE.setHeader('Content-Type', 'text/html') return NEW_DOC def getVisibleText(self): return "" security.declareProtected(perm_edit_document, 'saveDocument') def saveDocument(self, REQUEST): """ save document """ id = self.generateId(prefix='doc_', suffix='_item', rand_ceiling=999999999999) from Document import Document new = Document(id, REQUEST.kupu) self._setObject(id, new) new = getattr(self, id) return REQUEST.RESPONSE.redirect(id) def getStatus(self): return 0 security.declareProtected(perm_add_documents, 'newDocFromParams') def newDocFromParams(self, REQUEST, schema=None, corpus=None): """ new document from parameters """ id = self.generateId(prefix='doc_', suffix='_item', rand_ceiling=999999999999) from Document import Document new = Document(id, NEW_DOC, schema) new.setCorpus(corpus) self._setObject(id, new) new = getattr(self, id) return REQUEST.RESPONSE.redirect(id+'/edit.html') security.declareProtected(perm_manage, 'migr_errors') def migr_errors(self): """ migrate errors """ for x in self.objectValues('Document'): if x.getId() in ['doc_491521501702_item', 'doc_491521501711_item']: continue print x x.convertErrorsNG() return "ok" security.declareProtected(perm_manage, 'fixDocStatus') def fixDocStatus(self): """ if n_of_errors == 0 and doc_status== 1 --> doc_status->0 """ tot = 0 for x in self.objectValues('Document'): if x.getStatus() != 1: continue if x.get_n_of_errors() > 0: continue print x.getId(), x.getStatus(), x.get_n_of_errors(), x.getTitle() tot += 1 x._updateProperty('document_status', 0) x.reindex_object() print "total:", tot return "ok" security.declareProtected(perm_list_documents, 'batch_previous') def batch_previous(self, batch, pcount): """ generate links to next and previous pages """ result = [] c = pcount while batch.previous: batch = batch.previous c = c + 1 result.append([batch.start-1, c]) result.reverse() for x in result: x[1] = len(result)-x[1]+1 if len(result)>5: result = result[:2] + [['...', '...']] + result[-2:] return result security.declareProtected(perm_list_documents, 'batch_next') def batch_next(self, batch, pcount): """ generate links to next and previous pages """ result = [] c = pcount while batch.next: batch = batch.next c = c + 1 result.append([batch.start-1, c]) if len(result) > 5: result = result[:2] + [['...', '...']] + result[-2:] return result security.declareProtected(perm_manage, 'gen_text2') def gen_text2(self, REQUEST): """ asd """ for x in self.objectValues(): x.another_txt() return "ok" security.declareProtected(perm_manage, 'export_all') def export_all(self, REQUEST): """ export all documents to text """ path = "/tmp/korpus/" msg = "exporting\n" for x in self.objectValues(): f = open(path+x.getId()+'.txt', "wb") con = x.getProperty('textdoc') #.replace('\n', '\r\n') try: #f.write(x.getProperty('textdoc').decode('utf-8').encode('windows-1257')) #f.write(x.getProperty('textdoc').decode('utf-8').encode('latin1').replace('\n', '\r\n')) con = con.decode('utf-8').encode('windows-1257') except UnicodeEncodeError: msg += "failed... "+x.getId()+"\n" f.write(con) f.close() return msg def xyz(self): """ asd """ for x in self.objectValues('Document'): x.toText(x.getBody()) return "ok" def arvutus(self, arv): "avalik" return arv*2 Globals.InitializeClass(Documents) # EOF