## ## uniquefield.py ## Login : ## Started on Sat May 28 13:39:15 2011 Uli Fouquet ## $Id$ ## ## Copyright (C) 2011 Uli Fouquet ## This program 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. ## ## This program 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 this program; if not, write to the Free Software ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA ## """ Unique field indexes. """ from grok.index import IndexDefinition from zope.catalog.field import FieldIndex from zope.index.field import FieldIndex as RawFieldIndex from zope.interface import implements from waeup.sirp.index.interfaces import IUniqueFieldIndex, IUnique class RawUniqueFieldIndex(RawFieldIndex): """Unique field index. """ implements(IUnique) def index_doc(self, docid, value): """Index some doc. """ entries = self._fwd_index.get(value, ()) if len(entries) > 0 and docid not in entries: # Accept already existing entries if docid matches # ('reindex' use case). If docid is not contained already, # we have a unique-constraint violation. raise KeyError( 'The value already exists in catalog: %s' % value,) return super(RawUniqueFieldIndex, self).index_doc(docid, value) class UniqueFieldIndex(FieldIndex): implements(IUniqueFieldIndex) class UniqueField(IndexDefinition): """A :class:`grok.Indexes` index that matches against an entire field. Contrary to :class:`grok.index.Field` this index only accepts unique values. """ index_class = UniqueFieldIndex