# -*- coding: utf-8 # Copyright 2009 by Vahur Rebas import Globals from OFS.Folder import Folder from OFS.SimpleItem import SimpleItem from AccessControl import ClassSecurityInfo from OFS.PropertyManager import PropertyManager from zope.interface import implements from zope.component import getUtility from interfaces import ICorpusManager, ICorpus, IDocuments from permissions import perm_manage from utils import generateUniq class CorpusManager(PropertyManager, Folder): """ Corpus manager. """ meta_type = 'CorpusManager' security = ClassSecurityInfo() security.declareObjectPublic() implements(ICorpusManager) id = 'corpora' manage_options = Folder.manage_options+PropertyManager.manage_options security.declareProtected(perm_manage, 'new_corpus_handler') def new_corpus_handler(self, REQUEST, cname='', cdesc='', cancel=None, create=None): """ create a new corpus """ if create and cname: cid = generateUniq(7, prefix='c') c = Corpus(cid) c.setTitle(cname) c.setDescription(cdesc) self._setObject(cid, c) return REQUEST.RESPONSE.redirect(self.absolute_url() + '/') security.declareProtected(perm_manage, 'corpus_list_handler') def corpus_list_handler(self, REQUEST, del_corpus_button=None, corpus_id=None): """ handles buttons on corpora index page """ if del_corpus_button and corpus_id: self._delObject(corpus_id) return REQUEST.RESPONSE.redirect(self.absolute_url() + '/') def getCorpuses(self): """ returns a list of corpora""" return [c for c in self.objectValues('Corpus') if not c.isDeleted() ] Globals.InitializeClass(CorpusManager) class Corpus(PropertyManager, SimpleItem): """ Corpus object """ meta_type = 'Corpus' security = ClassSecurityInfo() security.declareObjectPublic() implements(ICorpus) def __init__(self, _id): """ init, provide id """ self.id = _id self.title = '' self.desc = '' self._deleted = False def setTitle(self, t): self.title = t def setDescription(self, d): self.desc = d def getTitle(self): return self.title def getDescription(self): return self.desc def isDeleted(self): return getattr(self, '_deleted', False) def get_n_of_documents(self): """ return a number of documents assigned to this corpus """ dm = getUtility(IDocuments).__of__(self.aq_parent) return len(dm.getDocuments({'corpus': self.getId()})) Globals.InitializeClass(Corpus) # EOF