[966] | 1 | #-*- mode: python; mode: fold -*- |
---|
[363] | 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 1596 2007-03-19 21:45:44Z joachim $ |
---|
| 20 | |
---|
| 21 | from zope.interface import implements |
---|
| 22 | from Globals import InitializeClass |
---|
| 23 | from Products.ZCatalog.ZCatalog import ZCatalog |
---|
[780] | 24 | from AccessControl import ClassSecurityInfo |
---|
| 25 | from Products.CMFCore.permissions import ModifyPortalContent |
---|
[363] | 26 | |
---|
[780] | 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 | |
---|
[363] | 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 | |
---|
[834] | 45 | |
---|
[1146] | 46 | class WAeUPTable(ZCatalog): ###( |
---|
[834] | 47 | |
---|
[363] | 48 | implements(IWAeUPTable) |
---|
[780] | 49 | security = ClassSecurityInfo() |
---|
[834] | 50 | |
---|
[363] | 51 | def addRecord(self, **data): |
---|
[502] | 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 |
---|
[834] | 59 | |
---|
[363] | 60 | def deleteRecord(self, uid): |
---|
[635] | 61 | #import pdb;pdb.set_trace() |
---|
[363] | 62 | self.uncatalog_object(uid) |
---|
[834] | 63 | |
---|
[502] | 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}) |
---|
[363] | 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) |
---|
[1062] | 80 | record_data = {} |
---|
| 81 | for field in self.schema() + self.indexes(): |
---|
| 82 | record_data[field] = getattr(record, field) |
---|
[363] | 83 | # Add the updated data: |
---|
| 84 | record_data.update(data) |
---|
| 85 | self.catalog_object(dict2ob(record_data), uid) |
---|
| 86 | |
---|
[1062] | 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) |
---|
[1082] | 102 | |
---|
[780] | 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)) |
---|
[834] | 122 | |
---|
[1146] | 123 | ###) |
---|
[834] | 124 | |
---|
[1146] | 125 | class AccommodationTable(WAeUPTable): ###( |
---|
[834] | 126 | |
---|
[404] | 127 | meta_type = 'WAeUP Accommodation Tool' |
---|
[502] | 128 | name = "accommodation" |
---|
| 129 | key = "bed" |
---|
[363] | 130 | def __init__(self): |
---|
[404] | 131 | WAeUPTable.__init__(self, 'portal_accommodation') |
---|
[363] | 132 | |
---|
[635] | 133 | def searchAndReserveBed(self, student_id,bed_type): |
---|
| 134 | records = self.searchResults({'student' : student_id}) |
---|
| 135 | if len(records) > 0: |
---|
[1293] | 136 | return -1,"Student with Id %s already booked bed %s." % (student_id,records[0].bed) |
---|
[834] | 137 | |
---|
[673] | 138 | records = [r for r in self.searchResults({'bed_type' : bed_type}) if not r.student] |
---|
[686] | 139 | #import pdb;pdb.set_trace() |
---|
[635] | 140 | if len(records) == 0: |
---|
[1306] | 141 | return -2,"No bed available" |
---|
[635] | 142 | rec = records[0] |
---|
| 143 | self.modifyRecord(bed=rec.bed,student=student_id) |
---|
[1596] | 144 | s_logger = logging.getLogger('WAeUPTables.AccommodationTable.searchAndReserveBed') |
---|
| 145 | s_logger.info('%s reserved bed %s' % (student_id,rec.bed)) |
---|
[635] | 146 | return 1,rec.bed |
---|
[363] | 147 | |
---|
[834] | 148 | |
---|
[404] | 149 | InitializeClass(AccommodationTable) |
---|
[411] | 150 | |
---|
[1146] | 151 | ###) |
---|
| 152 | |
---|
| 153 | class PinTable(WAeUPTable): ###( |
---|
[1030] | 154 | from ZODB.POSException import ConflictError |
---|
[440] | 155 | meta_type = 'WAeUP Pin Tool' |
---|
[502] | 156 | name = "pins" |
---|
| 157 | key = 'pin' |
---|
[440] | 158 | def __init__(self): |
---|
| 159 | WAeUPTable.__init__(self, 'portal_pins') |
---|
[1082] | 160 | |
---|
| 161 | |
---|
[710] | 162 | def searchAndSetRecord(self, uid, student_id,prefix): |
---|
[502] | 163 | #records = self.searchResults(uid=uid) |
---|
[710] | 164 | records = self.searchResults(student = student_id) |
---|
[990] | 165 | #import pdb;pdb.set_trace() |
---|
[710] | 166 | if len(records) > 0: |
---|
| 167 | for r in records: |
---|
[834] | 168 | if r.pin != uid and r.prefix_batch.startswith(prefix): |
---|
[710] | 169 | return -2 |
---|
[502] | 170 | records = self.searchResults({"%s" % self.key : uid}) |
---|
| 171 | if len(records) > 1: |
---|
| 172 | # Can not happen, but anyway... |
---|
| 173 | raise ValueError("More than one record with uid %s" % uid) |
---|
| 174 | if len(records) == 0: |
---|
| 175 | return -1 |
---|
| 176 | record = records[0] |
---|
| 177 | if record.student == "": |
---|
| 178 | record_data = {} |
---|
| 179 | for field in self.schema() + self.indexes(): |
---|
| 180 | record_data[field] = getattr(record, field) |
---|
| 181 | # Add the updated data: |
---|
[635] | 182 | record_data['student'] = student_id |
---|
[1030] | 183 | try: |
---|
| 184 | self.catalog_object(dict2ob(record_data), uid) |
---|
| 185 | return 1 |
---|
| 186 | except ConflictError: |
---|
| 187 | return 2 |
---|
[990] | 188 | if record.student.upper() != student_id.upper(): |
---|
[502] | 189 | return 0 |
---|
[997] | 190 | if record.student.upper() == student_id.upper(): |
---|
[502] | 191 | return 2 |
---|
[997] | 192 | return -3 |
---|
[440] | 193 | |
---|
| 194 | InitializeClass(PinTable) |
---|
| 195 | |
---|
[1146] | 196 | ###) |
---|
[966] | 197 | |
---|
[1146] | 198 | class PumeResultsTable(WAeUPTable): ###( |
---|
| 199 | |
---|
[966] | 200 | meta_type = 'WAeUP PumeResults Tool' |
---|
| 201 | name = "pumeresults" |
---|
| 202 | key = "jamb_reg_no" |
---|
| 203 | def __init__(self): |
---|
| 204 | WAeUPTable.__init__(self, 'portal_pumeresults') |
---|
| 205 | |
---|
| 206 | |
---|
| 207 | InitializeClass(PumeResultsTable) |
---|
| 208 | |
---|
[1146] | 209 | ###) |
---|
[971] | 210 | |
---|
[1146] | 211 | class StudentsCatalog(WAeUPTable): ###( |
---|
[1558] | 212 | security = ClassSecurityInfo() |
---|
[1146] | 213 | |
---|
[971] | 214 | meta_type = 'WAeUP Students Catalog' |
---|
| 215 | name = "students_catalog" |
---|
| 216 | key = "id" |
---|
[1558] | 217 | interesting_types = ('Student', |
---|
| 218 | 'StudentApplication', |
---|
| 219 | 'StudentCLearance', |
---|
| 220 | 'StudentPersonal', |
---|
| 221 | 'StudentStudyCourse', |
---|
| 222 | ) |
---|
[971] | 223 | def __init__(self): |
---|
| 224 | WAeUPTable.__init__(self, 'students_catalog') |
---|
| 225 | |
---|
[1596] | 226 | |
---|
| 227 | |
---|
[1558] | 228 | security.declarePrivate('notify_event_listener') |
---|
| 229 | def notify_event_listener(self,event_type,object,infos): |
---|
| 230 | "listen for events" |
---|
| 231 | pt = object.portal_type |
---|
| 232 | mt = object.meta_type |
---|
| 233 | students_catalog = self.students_catalog |
---|
| 234 | #if pt not in self.interesting_types: |
---|
| 235 | # return |
---|
| 236 | #print "%(pt)s\n %(event_type)s \n %(infos)s\n" % vars() |
---|
| 237 | if not infos.has_key('rpath'): |
---|
| 238 | return |
---|
| 239 | if pt == 'Student' and event_type == "workflow": |
---|
| 240 | pass |
---|
| 241 | elif mt == 'StudentApplication': |
---|
| 242 | if event_type not in ('sys_modify_object'): |
---|
| 243 | return |
---|
| 244 | print "%(pt)s\n %(event_type)s \n %(infos)s\n" % vars() |
---|
| 245 | from pdb import set_trace;set_trace() |
---|
| 246 | jamb_reg_no = getattr(object,'jamb_reg_no',None) |
---|
| 247 | if jamb_reg_no is None: |
---|
| 248 | return |
---|
| 249 | student_id = infos['rpath'].split('/')[2] |
---|
[1596] | 250 | self.fixName(student_id) |
---|
[1558] | 251 | student_entry = students_catalog(id = student_id)[0] |
---|
| 252 | if student_entry.jamb_reg_no == jamb_reg_no: |
---|
| 253 | return |
---|
| 254 | students_catalog.modifyRecord(id = student_id, |
---|
| 255 | jamb_reg_no = jamb_reg_no) |
---|
[1596] | 256 | elif mt == 'StudentPersonal': |
---|
| 257 | if event_type not in ('sys_modify_object'): |
---|
| 258 | return |
---|
| 259 | print "%(pt)s\n %(event_type)s \n %(infos)s\n" % vars() |
---|
| 260 | student_id = infos['rpath'].split('/')[2] |
---|
| 261 | self.fixName(student_id) |
---|
[1558] | 262 | |
---|
[971] | 263 | |
---|
| 264 | InitializeClass(StudentsCatalog) |
---|
| 265 | |
---|
[1146] | 266 | ###) |
---|
| 267 | |
---|
| 268 | class CoursesCatalog(WAeUPTable): ###( |
---|
| 269 | |
---|
| 270 | meta_type = 'WAeUP Courses Catalog' |
---|
| 271 | name = "students_catalog" |
---|
| 272 | key = "code" |
---|
| 273 | def __init__(self): |
---|
| 274 | WAeUPTable.__init__(self, 'courses_catalog') |
---|
| 275 | |
---|
| 276 | |
---|
| 277 | InitializeClass(CoursesCatalog) |
---|
[1151] | 278 | ###) |
---|
[1146] | 279 | |
---|
[1151] | 280 | class ReturningImport(WAeUPTable): ###( |
---|
[1146] | 281 | |
---|
[1151] | 282 | meta_type = 'Returning Import Table' |
---|
| 283 | name = "returning_import" |
---|
[1146] | 284 | key = "matric_no" |
---|
| 285 | def __init__(self): |
---|
[1151] | 286 | WAeUPTable.__init__(self, 'returning_import') |
---|
[1146] | 287 | |
---|
| 288 | |
---|
[1151] | 289 | InitializeClass(ReturningImport) |
---|
| 290 | ###) |
---|
[1146] | 291 | |
---|
| 292 | class ResultsImport(WAeUPTable): ###( |
---|
| 293 | |
---|
| 294 | meta_type = 'Results Import Table' |
---|
| 295 | name = "results_import" |
---|
| 296 | key = "key" |
---|
| 297 | def __init__(self): |
---|
| 298 | WAeUPTable.__init__(self, 'results_import') |
---|
| 299 | |
---|
| 300 | |
---|
| 301 | InitializeClass(ResultsImport) |
---|
| 302 | |
---|
| 303 | ###) |
---|
| 304 | |
---|
| 305 | class PaymentsCatalog(WAeUPTable): ###( |
---|
| 306 | |
---|
| 307 | meta_type = 'WAeUP Payments Catalog' |
---|
| 308 | name = "students_catalog" |
---|
| 309 | key = "id" |
---|
| 310 | def __init__(self): |
---|
| 311 | WAeUPTable.__init__(self, 'payments_catalog') |
---|
| 312 | |
---|
| 313 | |
---|
| 314 | InitializeClass(PaymentsCatalog) |
---|
| 315 | |
---|
| 316 | ###) |
---|
| 317 | |
---|
[414] | 318 | # BBB: |
---|
| 319 | AccomodationTable = AccommodationTable |
---|