1 | #-*- mode: python; mode: fold -*- |
---|
2 | # (C) Copyright 2005 AixtraWare <http://aixtraware.de> |
---|
3 | # Author: Joachim Schmitz <js@aixtraware.de> |
---|
4 | # |
---|
5 | # This program is free software; you can redistribute it and/or modify |
---|
6 | # it under the terms of the GNU General Public License version 2 as published |
---|
7 | # by the Free Software Foundation. |
---|
8 | # |
---|
9 | # This program is distributed in the hope that it will be useful, |
---|
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
12 | # GNU General Public License for more details. |
---|
13 | # |
---|
14 | # You should have received a copy of the GNU General Public License |
---|
15 | # along with this program; if not, write to the Free Software |
---|
16 | # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
---|
17 | # 02111-1307, USA. |
---|
18 | # |
---|
19 | # $Id: WAeUPTables.py 1062 2006-12-14 20:16:23Z joachim $ |
---|
20 | |
---|
21 | from zope.interface import implements |
---|
22 | from Globals import InitializeClass |
---|
23 | from Products.ZCatalog.ZCatalog import ZCatalog |
---|
24 | from AccessControl import ClassSecurityInfo |
---|
25 | from Products.CMFCore.permissions import ModifyPortalContent |
---|
26 | |
---|
27 | import DateTime |
---|
28 | import csv,re |
---|
29 | import logging |
---|
30 | import Globals |
---|
31 | p_home = Globals.package_home(globals()) |
---|
32 | i_home = Globals.INSTANCE_HOME |
---|
33 | |
---|
34 | from interfaces import IWAeUPTable |
---|
35 | |
---|
36 | class AttributeHolder(object): |
---|
37 | pass |
---|
38 | |
---|
39 | def dict2ob(dict): |
---|
40 | ob = AttributeHolder() |
---|
41 | for key, value in dict.items(): |
---|
42 | setattr(ob, key, value) |
---|
43 | return ob |
---|
44 | |
---|
45 | |
---|
46 | class WAeUPTable(ZCatalog): |
---|
47 | |
---|
48 | implements(IWAeUPTable) |
---|
49 | security = ClassSecurityInfo() |
---|
50 | |
---|
51 | def addRecord(self, **data): |
---|
52 | # The uid is the same as "bed". |
---|
53 | uid = data[self.key] |
---|
54 | res = self.searchResults({"%s" % self.key : uid}) |
---|
55 | if len(res) > 0: |
---|
56 | raise ValueError("More than one record with uid %s" % uid) |
---|
57 | self.catalog_object(dict2ob(data), uid=uid) |
---|
58 | return uid |
---|
59 | |
---|
60 | def deleteRecord(self, uid): |
---|
61 | #import pdb;pdb.set_trace() |
---|
62 | self.uncatalog_object(uid) |
---|
63 | |
---|
64 | def searchAndSetRecord(self, **data): |
---|
65 | raise NotImplemented |
---|
66 | |
---|
67 | def modifyRecord(self, **data): |
---|
68 | #records = self.searchResults(uid=uid) |
---|
69 | uid = data[self.key] |
---|
70 | records = self.searchResults({"%s" % self.key : uid}) |
---|
71 | if len(records) > 1: |
---|
72 | # Can not happen, but anyway... |
---|
73 | raise ValueError("More than one record with uid %s" % uid) |
---|
74 | if len(records) == 0: |
---|
75 | raise KeyError("No record for uid %s" % uid) |
---|
76 | record = records[0] |
---|
77 | record_data = {} |
---|
78 | for field in self.schema() + self.indexes(): |
---|
79 | record_data[field] = getattr(record, field) |
---|
80 | record_data = {} |
---|
81 | for field in self.schema() + self.indexes(): |
---|
82 | record_data[field] = getattr(record, field) |
---|
83 | # Add the updated data: |
---|
84 | record_data.update(data) |
---|
85 | self.catalog_object(dict2ob(record_data), uid) |
---|
86 | |
---|
87 | def reindexIndex(self, name, REQUEST,pghandler=None): |
---|
88 | if isinstance(name, str): |
---|
89 | name = (name,) |
---|
90 | paths = self._catalog.uids.items() |
---|
91 | i = 0 |
---|
92 | #import pdb;pdb.set_trace() |
---|
93 | for p,rid in paths: |
---|
94 | i += 1 |
---|
95 | metadata = self.getMetadataForRID(rid) |
---|
96 | record_data = {} |
---|
97 | for field in name: |
---|
98 | record_data[field] = metadata.get(field) |
---|
99 | uid = metadata.get(self.key) |
---|
100 | self.catalog_object(dict2ob(record_data), uid, idxs=name, |
---|
101 | update_metadata=0) |
---|
102 | |
---|
103 | security.declareProtected(ModifyPortalContent,"exportAllRecords") |
---|
104 | def exportAllRecords(self): |
---|
105 | "export a WAeUPTable" |
---|
106 | #import pdb;pdb.set_trace() |
---|
107 | fields = [field for field in self.schema()] |
---|
108 | format = ','.join(['"%%(%s)s"' % fn for fn in fields]) |
---|
109 | csv = [] |
---|
110 | csv.append(','.join(['"%s"' % fn for fn in fields])) |
---|
111 | for uid in self._catalog.uids: |
---|
112 | records = self.searchResults({"%s" % self.key : uid}) |
---|
113 | if len(records) > 1: |
---|
114 | # Can not happen, but anyway... |
---|
115 | raise ValueError("More than one record with uid %s" % uid) |
---|
116 | if len(records) == 0: |
---|
117 | raise KeyError("No record for uid %s" % uid) |
---|
118 | rec = records[0] |
---|
119 | csv.append(format % rec) |
---|
120 | current = DateTime.DateTime().strftime("%d-%m-%y_%H_%M_%S") |
---|
121 | open("%s/import/%s-%s.csv" % (i_home,self.getId(),current),"w+").write('\n'.join(csv)) |
---|
122 | |
---|
123 | |
---|
124 | class AccommodationTable(WAeUPTable): |
---|
125 | |
---|
126 | meta_type = 'WAeUP Accommodation Tool' |
---|
127 | name = "accommodation" |
---|
128 | key = "bed" |
---|
129 | def __init__(self): |
---|
130 | WAeUPTable.__init__(self, 'portal_accommodation') |
---|
131 | |
---|
132 | def searchAndReserveBed(self, student_id,bed_type): |
---|
133 | records = self.searchResults({'student' : student_id}) |
---|
134 | if len(records) > 0: |
---|
135 | return -1,"Student with Id %s already booked bed %s" % (student_id,records[0].bed) |
---|
136 | |
---|
137 | records = [r for r in self.searchResults({'bed_type' : bed_type}) if not r.student] |
---|
138 | #import pdb;pdb.set_trace() |
---|
139 | if len(records) == 0: |
---|
140 | return -1,"No bed of this type available" |
---|
141 | rec = records[0] |
---|
142 | self.modifyRecord(bed=rec.bed,student=student_id) |
---|
143 | s_logger = logging.getLogger('hostel_reservation') |
---|
144 | s_logger.info("Student %s reserved bed %s" % (student_id,rec.bed)) |
---|
145 | return 1,rec.bed |
---|
146 | |
---|
147 | |
---|
148 | InitializeClass(AccommodationTable) |
---|
149 | |
---|
150 | class PinTable(WAeUPTable): |
---|
151 | from ZODB.POSException import ConflictError |
---|
152 | meta_type = 'WAeUP Pin Tool' |
---|
153 | name = "pins" |
---|
154 | key = 'pin' |
---|
155 | def __init__(self): |
---|
156 | WAeUPTable.__init__(self, 'portal_pins') |
---|
157 | |
---|
158 | |
---|
159 | def searchAndSetRecord(self, uid, student_id,prefix): |
---|
160 | #records = self.searchResults(uid=uid) |
---|
161 | records = self.searchResults(student = student_id) |
---|
162 | #import pdb;pdb.set_trace() |
---|
163 | if len(records) > 0: |
---|
164 | for r in records: |
---|
165 | if r.pin != uid and r.prefix_batch.startswith(prefix): |
---|
166 | return -2 |
---|
167 | records = self.searchResults({"%s" % self.key : uid}) |
---|
168 | if len(records) > 1: |
---|
169 | # Can not happen, but anyway... |
---|
170 | raise ValueError("More than one record with uid %s" % uid) |
---|
171 | if len(records) == 0: |
---|
172 | return -1 |
---|
173 | record = records[0] |
---|
174 | if record.student == "": |
---|
175 | record_data = {} |
---|
176 | for field in self.schema() + self.indexes(): |
---|
177 | record_data[field] = getattr(record, field) |
---|
178 | # Add the updated data: |
---|
179 | record_data['student'] = student_id |
---|
180 | try: |
---|
181 | self.catalog_object(dict2ob(record_data), uid) |
---|
182 | return 1 |
---|
183 | except ConflictError: |
---|
184 | return 2 |
---|
185 | if record.student.upper() != student_id.upper(): |
---|
186 | return 0 |
---|
187 | if record.student.upper() == student_id.upper(): |
---|
188 | return 2 |
---|
189 | return -3 |
---|
190 | |
---|
191 | InitializeClass(PinTable) |
---|
192 | |
---|
193 | class PumeResultsTable(WAeUPTable): |
---|
194 | |
---|
195 | meta_type = 'WAeUP PumeResults Tool' |
---|
196 | name = "pumeresults" |
---|
197 | key = "jamb_reg_no" |
---|
198 | def __init__(self): |
---|
199 | WAeUPTable.__init__(self, 'portal_pumeresults') |
---|
200 | |
---|
201 | |
---|
202 | InitializeClass(PumeResultsTable) |
---|
203 | |
---|
204 | class StudentsCatalog(WAeUPTable): |
---|
205 | |
---|
206 | meta_type = 'WAeUP Students Catalog' |
---|
207 | name = "students_catalog" |
---|
208 | key = "id" |
---|
209 | def __init__(self): |
---|
210 | WAeUPTable.__init__(self, 'students_catalog') |
---|
211 | |
---|
212 | |
---|
213 | InitializeClass(StudentsCatalog) |
---|
214 | |
---|
215 | # BBB: |
---|
216 | AccomodationTable = AccommodationTable |
---|