[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 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): |
---|
[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): |
---|
[635] | 50 | #import pdb;pdb.set_trace() |
---|
[363] | 51 | self.uncatalog_object(uid) |
---|
| 52 | |
---|
[502] | 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}) |
---|
[363] | 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 | |
---|
[404] | 73 | class AccommodationTable(WAeUPTable): |
---|
[363] | 74 | |
---|
[404] | 75 | meta_type = 'WAeUP Accommodation Tool' |
---|
[502] | 76 | name = "accommodation" |
---|
| 77 | key = "bed" |
---|
[363] | 78 | def __init__(self): |
---|
[404] | 79 | WAeUPTable.__init__(self, 'portal_accommodation') |
---|
[363] | 80 | |
---|
[635] | 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 | |
---|
[673] | 86 | records = [r for r in self.searchResults({'bed_type' : bed_type}) if not r.student] |
---|
[686] | 87 | #import pdb;pdb.set_trace() |
---|
[635] | 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 | |
---|
[363] | 94 | |
---|
[404] | 95 | InitializeClass(AccommodationTable) |
---|
[411] | 96 | |
---|
[440] | 97 | class PinTable(WAeUPTable): |
---|
| 98 | |
---|
| 99 | meta_type = 'WAeUP Pin Tool' |
---|
[502] | 100 | name = "pins" |
---|
| 101 | key = 'pin' |
---|
[440] | 102 | def __init__(self): |
---|
| 103 | WAeUPTable.__init__(self, 'portal_pins') |
---|
| 104 | |
---|
| 105 | |
---|
[710] | 106 | def searchAndSetRecord(self, uid, student_id,prefix): |
---|
[502] | 107 | #records = self.searchResults(uid=uid) |
---|
[710] | 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 |
---|
[502] | 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: |
---|
[635] | 125 | record_data['student'] = student_id |
---|
[502] | 126 | self.catalog_object(dict2ob(record_data), uid) |
---|
| 127 | return 1 |
---|
[635] | 128 | if record.student != student_id: |
---|
[502] | 129 | return 0 |
---|
[635] | 130 | if record.student == student_id: |
---|
[502] | 131 | return 2 |
---|
[440] | 132 | |
---|
| 133 | InitializeClass(PinTable) |
---|
| 134 | |
---|
[414] | 135 | # BBB: |
---|
| 136 | AccomodationTable = AccommodationTable |
---|