[363] | 1 | # (C) Copyright 2005 AixtraWare <http://aixtraware.de> |
---|
| 2 | # Author: Joachim Schmitz <js@aixtraware.de> |
---|
| 3 | # |
---|
| 4 | # This program is free software; you can redistribute it and/or modify |
---|
| 5 | # it under the terms of the GNU General Public License version 2 as published |
---|
| 6 | # by the Free Software Foundation. |
---|
| 7 | # |
---|
| 8 | # This program is distributed in the hope that it will be useful, |
---|
| 9 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 10 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 11 | # GNU General Public License for more details. |
---|
| 12 | # |
---|
| 13 | # You should have received a copy of the GNU General Public License |
---|
| 14 | # along with this program; if not, write to the Free Software |
---|
| 15 | # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
---|
| 16 | # 02111-1307, USA. |
---|
| 17 | # |
---|
| 18 | # $Id: WAeUPTables.py 502 2006-09-11 10:39:59Z joachim $ |
---|
| 19 | |
---|
| 20 | from zope.interface import implements |
---|
| 21 | from Globals import InitializeClass |
---|
| 22 | from Products.ZCatalog.ZCatalog import ZCatalog |
---|
| 23 | |
---|
| 24 | from interfaces import IWAeUPTable |
---|
| 25 | |
---|
| 26 | class AttributeHolder(object): |
---|
| 27 | pass |
---|
| 28 | |
---|
| 29 | def dict2ob(dict): |
---|
| 30 | ob = AttributeHolder() |
---|
| 31 | for key, value in dict.items(): |
---|
| 32 | setattr(ob, key, value) |
---|
| 33 | return ob |
---|
| 34 | |
---|
| 35 | |
---|
| 36 | class WAeUPTable(ZCatalog): |
---|
| 37 | |
---|
| 38 | implements(IWAeUPTable) |
---|
| 39 | |
---|
| 40 | def addRecord(self, **data): |
---|
[502] | 41 | # The uid is the same as "bed". |
---|
| 42 | uid = data[self.key] |
---|
| 43 | res = self.searchResults({"%s" % self.key : uid}) |
---|
| 44 | if len(res) > 0: |
---|
| 45 | raise ValueError("More than one record with uid %s" % uid) |
---|
| 46 | self.catalog_object(dict2ob(data), uid=uid) |
---|
| 47 | return uid |
---|
[363] | 48 | |
---|
| 49 | def deleteRecord(self, uid): |
---|
| 50 | self.uncatalog_object(uid) |
---|
| 51 | |
---|
[502] | 52 | def searchAndSetRecord(self, **data): |
---|
| 53 | raise NotImplemented |
---|
| 54 | |
---|
| 55 | def modifyRecord(self, **data): |
---|
| 56 | #records = self.searchResults(uid=uid) |
---|
| 57 | uid = data[self.key] |
---|
| 58 | records = self.searchResults({"%s" % self.key : uid}) |
---|
[363] | 59 | if len(records) > 1: |
---|
| 60 | # Can not happen, but anyway... |
---|
| 61 | raise ValueError("More than one record with uid %s" % uid) |
---|
| 62 | if len(records) == 0: |
---|
| 63 | raise KeyError("No record for uid %s" % uid) |
---|
| 64 | record = records[0] |
---|
| 65 | record_data = {} |
---|
| 66 | for field in self.schema() + self.indexes(): |
---|
| 67 | record_data[field] = getattr(record, field) |
---|
| 68 | # Add the updated data: |
---|
| 69 | record_data.update(data) |
---|
| 70 | self.catalog_object(dict2ob(record_data), uid) |
---|
| 71 | |
---|
[404] | 72 | class AccommodationTable(WAeUPTable): |
---|
[363] | 73 | |
---|
[404] | 74 | meta_type = 'WAeUP Accommodation Tool' |
---|
[502] | 75 | name = "accommodation" |
---|
| 76 | key = "bed" |
---|
[363] | 77 | def __init__(self): |
---|
[404] | 78 | WAeUPTable.__init__(self, 'portal_accommodation') |
---|
[363] | 79 | |
---|
| 80 | |
---|
[404] | 81 | InitializeClass(AccommodationTable) |
---|
[411] | 82 | |
---|
[440] | 83 | class PinTable(WAeUPTable): |
---|
| 84 | |
---|
| 85 | meta_type = 'WAeUP Pin Tool' |
---|
[502] | 86 | name = "pins" |
---|
| 87 | key = 'pin' |
---|
[440] | 88 | def __init__(self): |
---|
| 89 | WAeUPTable.__init__(self, 'portal_pins') |
---|
| 90 | |
---|
| 91 | |
---|
[502] | 92 | def searchAndSetRecord(self, uid, jamb_reg_no): |
---|
| 93 | #records = self.searchResults(uid=uid) |
---|
| 94 | records = self.searchResults({"%s" % self.key : uid}) |
---|
| 95 | if len(records) > 1: |
---|
| 96 | # Can not happen, but anyway... |
---|
| 97 | raise ValueError("More than one record with uid %s" % uid) |
---|
| 98 | if len(records) == 0: |
---|
| 99 | return -1 |
---|
| 100 | record = records[0] |
---|
| 101 | if record.student == "": |
---|
| 102 | record_data = {} |
---|
| 103 | for field in self.schema() + self.indexes(): |
---|
| 104 | record_data[field] = getattr(record, field) |
---|
| 105 | # Add the updated data: |
---|
| 106 | record_data['student'] = jamb_reg_no |
---|
| 107 | self.catalog_object(dict2ob(record_data), uid) |
---|
| 108 | return 1 |
---|
| 109 | if record.student != jamb_reg_no: |
---|
| 110 | return 0 |
---|
| 111 | if record.student == jamb_reg_no: |
---|
| 112 | return 2 |
---|
[440] | 113 | |
---|
| 114 | InitializeClass(PinTable) |
---|
| 115 | |
---|
[414] | 116 | # BBB: |
---|
| 117 | AccomodationTable = AccommodationTable |
---|