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 635 2006-10-10 08:23:10Z 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 | import pdb;pdb.set_trace() |
---|
83 | records = self.searchResults({'student' : student_id}) |
---|
84 | if len(records) > 0: |
---|
85 | return -1,"Student with Id %s already booked bed %s" % (student_id,records[0].bed) |
---|
86 | |
---|
87 | records = self.searchResults({'bed_type' : bed_type, "student": ''}) |
---|
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): |
---|
107 | #records = self.searchResults(uid=uid) |
---|
108 | records = self.searchResults({"%s" % self.key : uid}) |
---|
109 | if len(records) > 1: |
---|
110 | # Can not happen, but anyway... |
---|
111 | raise ValueError("More than one record with uid %s" % uid) |
---|
112 | if len(records) == 0: |
---|
113 | return -1 |
---|
114 | record = records[0] |
---|
115 | if record.student == "": |
---|
116 | record_data = {} |
---|
117 | for field in self.schema() + self.indexes(): |
---|
118 | record_data[field] = getattr(record, field) |
---|
119 | # Add the updated data: |
---|
120 | record_data['student'] = student_id |
---|
121 | self.catalog_object(dict2ob(record_data), uid) |
---|
122 | return 1 |
---|
123 | if record.student != student_id: |
---|
124 | return 0 |
---|
125 | if record.student == student_id: |
---|
126 | return 2 |
---|
127 | |
---|
128 | InitializeClass(PinTable) |
---|
129 | |
---|
130 | # BBB: |
---|
131 | AccomodationTable = AccommodationTable |
---|