| 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 710 2006-10-16 22:30:16Z 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): |
|---|
| 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 |
|---|
| 48 | |
|---|
| 49 | def deleteRecord(self, uid): |
|---|
| 50 | #import pdb;pdb.set_trace() |
|---|
| 51 | self.uncatalog_object(uid) |
|---|
| 52 | |
|---|
| 53 | def searchAndSetRecord(self, **data): |
|---|
| 54 | raise NotImplemented |
|---|
| 55 | |
|---|
| 56 | def modifyRecord(self, **data): |
|---|
| 57 | #records = self.searchResults(uid=uid) |
|---|
| 58 | uid = data[self.key] |
|---|
| 59 | records = self.searchResults({"%s" % self.key : uid}) |
|---|
| 60 | if len(records) > 1: |
|---|
| 61 | # Can not happen, but anyway... |
|---|
| 62 | raise ValueError("More than one record with uid %s" % uid) |
|---|
| 63 | if len(records) == 0: |
|---|
| 64 | raise KeyError("No record for uid %s" % uid) |
|---|
| 65 | record = records[0] |
|---|
| 66 | record_data = {} |
|---|
| 67 | for field in self.schema() + self.indexes(): |
|---|
| 68 | record_data[field] = getattr(record, field) |
|---|
| 69 | # Add the updated data: |
|---|
| 70 | record_data.update(data) |
|---|
| 71 | self.catalog_object(dict2ob(record_data), uid) |
|---|
| 72 | |
|---|
| 73 | class AccommodationTable(WAeUPTable): |
|---|
| 74 | |
|---|
| 75 | meta_type = 'WAeUP Accommodation Tool' |
|---|
| 76 | name = "accommodation" |
|---|
| 77 | key = "bed" |
|---|
| 78 | def __init__(self): |
|---|
| 79 | WAeUPTable.__init__(self, 'portal_accommodation') |
|---|
| 80 | |
|---|
| 81 | def searchAndReserveBed(self, student_id,bed_type): |
|---|
| 82 | records = self.searchResults({'student' : student_id}) |
|---|
| 83 | if len(records) > 0: |
|---|
| 84 | return -1,"Student with Id %s already booked bed %s" % (student_id,records[0].bed) |
|---|
| 85 | |
|---|
| 86 | records = [r for r in self.searchResults({'bed_type' : bed_type}) if not r.student] |
|---|
| 87 | #import pdb;pdb.set_trace() |
|---|
| 88 | if len(records) == 0: |
|---|
| 89 | return -1,"no bed of this type available" |
|---|
| 90 | rec = records[0] |
|---|
| 91 | self.modifyRecord(bed=rec.bed,student=student_id) |
|---|
| 92 | return 1,rec.bed |
|---|
| 93 | |
|---|
| 94 | |
|---|
| 95 | InitializeClass(AccommodationTable) |
|---|
| 96 | |
|---|
| 97 | class PinTable(WAeUPTable): |
|---|
| 98 | |
|---|
| 99 | meta_type = 'WAeUP Pin Tool' |
|---|
| 100 | name = "pins" |
|---|
| 101 | key = 'pin' |
|---|
| 102 | def __init__(self): |
|---|
| 103 | WAeUPTable.__init__(self, 'portal_pins') |
|---|
| 104 | |
|---|
| 105 | |
|---|
| 106 | def searchAndSetRecord(self, uid, student_id,prefix): |
|---|
| 107 | #records = self.searchResults(uid=uid) |
|---|
| 108 | records = self.searchResults(student = student_id) |
|---|
| 109 | if len(records) > 0: |
|---|
| 110 | for r in records: |
|---|
| 111 | if r.pin != uid and r.prefix_batch.startswith(prefix): |
|---|
| 112 | return -2 |
|---|
| 113 | records = self.searchResults({"%s" % self.key : uid}) |
|---|
| 114 | if len(records) > 1: |
|---|
| 115 | # Can not happen, but anyway... |
|---|
| 116 | raise ValueError("More than one record with uid %s" % uid) |
|---|
| 117 | if len(records) == 0: |
|---|
| 118 | return -1 |
|---|
| 119 | record = records[0] |
|---|
| 120 | if record.student == "": |
|---|
| 121 | record_data = {} |
|---|
| 122 | for field in self.schema() + self.indexes(): |
|---|
| 123 | record_data[field] = getattr(record, field) |
|---|
| 124 | # Add the updated data: |
|---|
| 125 | record_data['student'] = student_id |
|---|
| 126 | self.catalog_object(dict2ob(record_data), uid) |
|---|
| 127 | return 1 |
|---|
| 128 | if record.student != student_id: |
|---|
| 129 | return 0 |
|---|
| 130 | if record.student == student_id: |
|---|
| 131 | return 2 |
|---|
| 132 | |
|---|
| 133 | InitializeClass(PinTable) |
|---|
| 134 | |
|---|
| 135 | # BBB: |
|---|
| 136 | AccomodationTable = AccommodationTable |
|---|