| 1 | # -*- coding: utf-8 -*- |
|---|
| 2 | ## |
|---|
| 3 | ## |
|---|
| 4 | ## This file is part of CDS Indico. |
|---|
| 5 | ## Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007 CERN. |
|---|
| 6 | ## |
|---|
| 7 | ## CDS Indico is free software; you can redistribute it and/or |
|---|
| 8 | ## modify it under the terms of the GNU General Public License as |
|---|
| 9 | ## published by the Free Software Foundation; either version 2 of the |
|---|
| 10 | ## License, or (at your option) any later version. |
|---|
| 11 | ## |
|---|
| 12 | ## CDS Indico is distributed in the hope that it will be useful, but |
|---|
| 13 | ## WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 14 | ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|---|
| 15 | ## General Public License for more details. |
|---|
| 16 | ## |
|---|
| 17 | ## You should have received a copy of the GNU General Public License |
|---|
| 18 | ## along with CDS Indico; if not, write to the Free Software Foundation, Inc., |
|---|
| 19 | ## 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. |
|---|
| 20 | |
|---|
| 21 | from BTrees.OOBTree import OOBTree |
|---|
| 22 | |
|---|
| 23 | from indico.core.index.event import CategoryEventStartDateIndex |
|---|
| 24 | |
|---|
| 25 | from MaKaC.common import DBMgr |
|---|
| 26 | |
|---|
| 27 | |
|---|
| 28 | # TODO: decorator for annoying 'db parameter' |
|---|
| 29 | |
|---|
| 30 | class Catalog(OOBTree): |
|---|
| 31 | _indexMap = { |
|---|
| 32 | 'categ_conf_sd': CategoryEventStartDateIndex |
|---|
| 33 | } |
|---|
| 34 | |
|---|
| 35 | @classmethod |
|---|
| 36 | def initialize(cls, db=None): |
|---|
| 37 | if not db: |
|---|
| 38 | db = DBMgr.getInstance().getDBConnection() |
|---|
| 39 | |
|---|
| 40 | catalog = cls() |
|---|
| 41 | |
|---|
| 42 | for indexName, clazz in cls._indexMap.iteritems(): |
|---|
| 43 | newIdx = clazz() |
|---|
| 44 | catalog[indexName] = newIdx |
|---|
| 45 | |
|---|
| 46 | db.root()['catalog'] = catalog |
|---|
| 47 | |
|---|
| 48 | @classmethod |
|---|
| 49 | def getIdx(cls, indexName, db=None): |
|---|
| 50 | if not db: |
|---|
| 51 | db = DBMgr.getInstance().getDBConnection() |
|---|
| 52 | |
|---|
| 53 | if 'catalog' not in db.root(): |
|---|
| 54 | cls.initialize(db=db) |
|---|
| 55 | |
|---|
| 56 | return db.root()['catalog'].get(indexName) |
|---|
| 57 | |
|---|
| 58 | @classmethod |
|---|
| 59 | def updateDB(cls, dbi=None): |
|---|
| 60 | if not dbi: |
|---|
| 61 | dbi = DBMgr.getInstance() |
|---|
| 62 | db = dbi.getDBConnection() |
|---|
| 63 | |
|---|
| 64 | root = db.root() |
|---|
| 65 | |
|---|
| 66 | if not 'catalog' in root: |
|---|
| 67 | cls.initialize(db=db) |
|---|
| 68 | |
|---|
| 69 | for indexName, clazz in cls._indexMap.iteritems(): |
|---|
| 70 | newIdx = clazz() |
|---|
| 71 | root['catalog'][idxName] = newIdx |
|---|
| 72 | newIdx.initialize(dbi=dbi) |
|---|