[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 997 2006-12-06 07:36:54Z henrik $ |
---|
| 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 | |
---|
[363] | 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) |
---|
| 80 | # Add the updated data: |
---|
| 81 | record_data.update(data) |
---|
| 82 | self.catalog_object(dict2ob(record_data), uid) |
---|
| 83 | |
---|
[780] | 84 | security.declareProtected(ModifyPortalContent,"exportAllRecords") |
---|
| 85 | def exportAllRecords(self): |
---|
| 86 | "export a WAeUPTable" |
---|
| 87 | #import pdb;pdb.set_trace() |
---|
| 88 | fields = [field for field in self.schema()] |
---|
| 89 | format = ','.join(['"%%(%s)s"' % fn for fn in fields]) |
---|
| 90 | csv = [] |
---|
| 91 | csv.append(','.join(['"%s"' % fn for fn in fields])) |
---|
| 92 | for uid in self._catalog.uids: |
---|
| 93 | records = self.searchResults({"%s" % self.key : uid}) |
---|
| 94 | if len(records) > 1: |
---|
| 95 | # Can not happen, but anyway... |
---|
| 96 | raise ValueError("More than one record with uid %s" % uid) |
---|
| 97 | if len(records) == 0: |
---|
| 98 | raise KeyError("No record for uid %s" % uid) |
---|
| 99 | rec = records[0] |
---|
| 100 | csv.append(format % rec) |
---|
| 101 | current = DateTime.DateTime().strftime("%d-%m-%y_%H_%M_%S") |
---|
| 102 | open("%s/import/%s-%s.csv" % (i_home,self.getId(),current),"w+").write('\n'.join(csv)) |
---|
[834] | 103 | |
---|
| 104 | |
---|
[404] | 105 | class AccommodationTable(WAeUPTable): |
---|
[834] | 106 | |
---|
[404] | 107 | meta_type = 'WAeUP Accommodation Tool' |
---|
[502] | 108 | name = "accommodation" |
---|
| 109 | key = "bed" |
---|
[363] | 110 | def __init__(self): |
---|
[404] | 111 | WAeUPTable.__init__(self, 'portal_accommodation') |
---|
[363] | 112 | |
---|
[635] | 113 | def searchAndReserveBed(self, student_id,bed_type): |
---|
| 114 | records = self.searchResults({'student' : student_id}) |
---|
| 115 | if len(records) > 0: |
---|
| 116 | return -1,"Student with Id %s already booked bed %s" % (student_id,records[0].bed) |
---|
[834] | 117 | |
---|
[673] | 118 | records = [r for r in self.searchResults({'bed_type' : bed_type}) if not r.student] |
---|
[686] | 119 | #import pdb;pdb.set_trace() |
---|
[635] | 120 | if len(records) == 0: |
---|
[834] | 121 | return -1,"No bed of this type available" |
---|
[635] | 122 | rec = records[0] |
---|
| 123 | self.modifyRecord(bed=rec.bed,student=student_id) |
---|
[952] | 124 | s_logger = logging.getLogger('hostel_reservation') |
---|
| 125 | s_logger.info("Student %s reserved bed %s" % (student_id,rec.bed)) |
---|
[635] | 126 | return 1,rec.bed |
---|
[363] | 127 | |
---|
[834] | 128 | |
---|
[404] | 129 | InitializeClass(AccommodationTable) |
---|
[411] | 130 | |
---|
[440] | 131 | class PinTable(WAeUPTable): |
---|
[834] | 132 | |
---|
[440] | 133 | meta_type = 'WAeUP Pin Tool' |
---|
[502] | 134 | name = "pins" |
---|
| 135 | key = 'pin' |
---|
[440] | 136 | def __init__(self): |
---|
| 137 | WAeUPTable.__init__(self, 'portal_pins') |
---|
| 138 | |
---|
| 139 | |
---|
[710] | 140 | def searchAndSetRecord(self, uid, student_id,prefix): |
---|
[502] | 141 | #records = self.searchResults(uid=uid) |
---|
[710] | 142 | records = self.searchResults(student = student_id) |
---|
[990] | 143 | #import pdb;pdb.set_trace() |
---|
[710] | 144 | if len(records) > 0: |
---|
| 145 | for r in records: |
---|
[834] | 146 | if r.pin != uid and r.prefix_batch.startswith(prefix): |
---|
[710] | 147 | return -2 |
---|
[502] | 148 | records = self.searchResults({"%s" % self.key : uid}) |
---|
| 149 | if len(records) > 1: |
---|
| 150 | # Can not happen, but anyway... |
---|
| 151 | raise ValueError("More than one record with uid %s" % uid) |
---|
| 152 | if len(records) == 0: |
---|
| 153 | return -1 |
---|
| 154 | record = records[0] |
---|
| 155 | if record.student == "": |
---|
| 156 | record_data = {} |
---|
| 157 | for field in self.schema() + self.indexes(): |
---|
| 158 | record_data[field] = getattr(record, field) |
---|
| 159 | # Add the updated data: |
---|
[635] | 160 | record_data['student'] = student_id |
---|
[502] | 161 | self.catalog_object(dict2ob(record_data), uid) |
---|
| 162 | return 1 |
---|
[990] | 163 | if record.student.upper() != student_id.upper(): |
---|
[502] | 164 | return 0 |
---|
[997] | 165 | if record.student.upper() == student_id.upper(): |
---|
[502] | 166 | return 2 |
---|
[997] | 167 | return -3 |
---|
[440] | 168 | |
---|
| 169 | InitializeClass(PinTable) |
---|
| 170 | |
---|
[966] | 171 | class PumeResultsTable(WAeUPTable): |
---|
| 172 | |
---|
| 173 | meta_type = 'WAeUP PumeResults Tool' |
---|
| 174 | name = "pumeresults" |
---|
| 175 | key = "jamb_reg_no" |
---|
| 176 | def __init__(self): |
---|
| 177 | WAeUPTable.__init__(self, 'portal_pumeresults') |
---|
| 178 | |
---|
| 179 | |
---|
| 180 | InitializeClass(PumeResultsTable) |
---|
| 181 | |
---|
[971] | 182 | class StudentsCatalog(WAeUPTable): |
---|
| 183 | |
---|
| 184 | meta_type = 'WAeUP Students Catalog' |
---|
| 185 | name = "students_catalog" |
---|
| 186 | key = "id" |
---|
| 187 | def __init__(self): |
---|
| 188 | WAeUPTable.__init__(self, 'students_catalog') |
---|
| 189 | |
---|
| 190 | |
---|
| 191 | InitializeClass(StudentsCatalog) |
---|
| 192 | |
---|
[414] | 193 | # BBB: |
---|
| 194 | AccomodationTable = AccommodationTable |
---|