| 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 780 2006-11-02 10:56:46Z joachim $ |
|---|
| 19 | |
|---|
| 20 | from zope.interface import implements |
|---|
| 21 | from Globals import InitializeClass |
|---|
| 22 | from Products.ZCatalog.ZCatalog import ZCatalog |
|---|
| 23 | from AccessControl import ClassSecurityInfo |
|---|
| 24 | from Products.CMFCore.permissions import ModifyPortalContent |
|---|
| 25 | |
|---|
| 26 | import DateTime |
|---|
| 27 | import csv,re |
|---|
| 28 | import logging |
|---|
| 29 | import Globals |
|---|
| 30 | p_home = Globals.package_home(globals()) |
|---|
| 31 | i_home = Globals.INSTANCE_HOME |
|---|
| 32 | |
|---|
| 33 | from interfaces import IWAeUPTable |
|---|
| 34 | |
|---|
| 35 | class AttributeHolder(object): |
|---|
| 36 | pass |
|---|
| 37 | |
|---|
| 38 | def dict2ob(dict): |
|---|
| 39 | ob = AttributeHolder() |
|---|
| 40 | for key, value in dict.items(): |
|---|
| 41 | setattr(ob, key, value) |
|---|
| 42 | return ob |
|---|
| 43 | |
|---|
| 44 | |
|---|
| 45 | class WAeUPTable(ZCatalog): |
|---|
| 46 | |
|---|
| 47 | implements(IWAeUPTable) |
|---|
| 48 | security = ClassSecurityInfo() |
|---|
| 49 | |
|---|
| 50 | def addRecord(self, **data): |
|---|
| 51 | # The uid is the same as "bed". |
|---|
| 52 | uid = data[self.key] |
|---|
| 53 | res = self.searchResults({"%s" % self.key : uid}) |
|---|
| 54 | if len(res) > 0: |
|---|
| 55 | raise ValueError("More than one record with uid %s" % uid) |
|---|
| 56 | self.catalog_object(dict2ob(data), uid=uid) |
|---|
| 57 | return uid |
|---|
| 58 | |
|---|
| 59 | def deleteRecord(self, uid): |
|---|
| 60 | #import pdb;pdb.set_trace() |
|---|
| 61 | self.uncatalog_object(uid) |
|---|
| 62 | |
|---|
| 63 | def searchAndSetRecord(self, **data): |
|---|
| 64 | raise NotImplemented |
|---|
| 65 | |
|---|
| 66 | def modifyRecord(self, **data): |
|---|
| 67 | #records = self.searchResults(uid=uid) |
|---|
| 68 | uid = data[self.key] |
|---|
| 69 | records = self.searchResults({"%s" % self.key : uid}) |
|---|
| 70 | if len(records) > 1: |
|---|
| 71 | # Can not happen, but anyway... |
|---|
| 72 | raise ValueError("More than one record with uid %s" % uid) |
|---|
| 73 | if len(records) == 0: |
|---|
| 74 | raise KeyError("No record for uid %s" % uid) |
|---|
| 75 | record = records[0] |
|---|
| 76 | record_data = {} |
|---|
| 77 | for field in self.schema() + self.indexes(): |
|---|
| 78 | record_data[field] = getattr(record, field) |
|---|
| 79 | # Add the updated data: |
|---|
| 80 | record_data.update(data) |
|---|
| 81 | self.catalog_object(dict2ob(record_data), uid) |
|---|
| 82 | |
|---|
| 83 | security.declareProtected(ModifyPortalContent,"exportAllRecords") |
|---|
| 84 | def exportAllRecords(self): |
|---|
| 85 | "export a WAeUPTable" |
|---|
| 86 | #import pdb;pdb.set_trace() |
|---|
| 87 | fields = [field for field in self.schema()] |
|---|
| 88 | format = ','.join(['"%%(%s)s"' % fn for fn in fields]) |
|---|
| 89 | csv = [] |
|---|
| 90 | csv.append(','.join(['"%s"' % fn for fn in fields])) |
|---|
| 91 | for uid in self._catalog.uids: |
|---|
| 92 | records = self.searchResults({"%s" % self.key : uid}) |
|---|
| 93 | if len(records) > 1: |
|---|
| 94 | # Can not happen, but anyway... |
|---|
| 95 | raise ValueError("More than one record with uid %s" % uid) |
|---|
| 96 | if len(records) == 0: |
|---|
| 97 | raise KeyError("No record for uid %s" % uid) |
|---|
| 98 | rec = records[0] |
|---|
| 99 | csv.append(format % rec) |
|---|
| 100 | current = DateTime.DateTime().strftime("%d-%m-%y_%H_%M_%S") |
|---|
| 101 | open("%s/import/%s-%s.csv" % (i_home,self.getId(),current),"w+").write('\n'.join(csv)) |
|---|
| 102 | |
|---|
| 103 | |
|---|
| 104 | class AccommodationTable(WAeUPTable): |
|---|
| 105 | |
|---|
| 106 | meta_type = 'WAeUP Accommodation Tool' |
|---|
| 107 | name = "accommodation" |
|---|
| 108 | key = "bed" |
|---|
| 109 | def __init__(self): |
|---|
| 110 | WAeUPTable.__init__(self, 'portal_accommodation') |
|---|
| 111 | |
|---|
| 112 | def searchAndReserveBed(self, student_id,bed_type): |
|---|
| 113 | records = self.searchResults({'student' : student_id}) |
|---|
| 114 | if len(records) > 0: |
|---|
| 115 | return -1,"Student with Id %s already booked bed %s" % (student_id,records[0].bed) |
|---|
| 116 | |
|---|
| 117 | records = [r for r in self.searchResults({'bed_type' : bed_type}) if not r.student] |
|---|
| 118 | #import pdb;pdb.set_trace() |
|---|
| 119 | if len(records) == 0: |
|---|
| 120 | return -1,"no bed of this type available" |
|---|
| 121 | rec = records[0] |
|---|
| 122 | self.modifyRecord(bed=rec.bed,student=student_id) |
|---|
| 123 | return 1,rec.bed |
|---|
| 124 | |
|---|
| 125 | |
|---|
| 126 | InitializeClass(AccommodationTable) |
|---|
| 127 | |
|---|
| 128 | class PinTable(WAeUPTable): |
|---|
| 129 | |
|---|
| 130 | meta_type = 'WAeUP Pin Tool' |
|---|
| 131 | name = "pins" |
|---|
| 132 | key = 'pin' |
|---|
| 133 | def __init__(self): |
|---|
| 134 | WAeUPTable.__init__(self, 'portal_pins') |
|---|
| 135 | |
|---|
| 136 | |
|---|
| 137 | def searchAndSetRecord(self, uid, student_id,prefix): |
|---|
| 138 | #records = self.searchResults(uid=uid) |
|---|
| 139 | records = self.searchResults(student = student_id) |
|---|
| 140 | if len(records) > 0: |
|---|
| 141 | for r in records: |
|---|
| 142 | if r.pin != uid and r.prefix_batch.startswith(prefix): |
|---|
| 143 | return -2 |
|---|
| 144 | records = self.searchResults({"%s" % self.key : uid}) |
|---|
| 145 | if len(records) > 1: |
|---|
| 146 | # Can not happen, but anyway... |
|---|
| 147 | raise ValueError("More than one record with uid %s" % uid) |
|---|
| 148 | if len(records) == 0: |
|---|
| 149 | return -1 |
|---|
| 150 | record = records[0] |
|---|
| 151 | if record.student == "": |
|---|
| 152 | record_data = {} |
|---|
| 153 | for field in self.schema() + self.indexes(): |
|---|
| 154 | record_data[field] = getattr(record, field) |
|---|
| 155 | # Add the updated data: |
|---|
| 156 | record_data['student'] = student_id |
|---|
| 157 | self.catalog_object(dict2ob(record_data), uid) |
|---|
| 158 | return 1 |
|---|
| 159 | if record.student != student_id: |
|---|
| 160 | return 0 |
|---|
| 161 | if record.student == student_id: |
|---|
| 162 | return 2 |
|---|
| 163 | |
|---|
| 164 | InitializeClass(PinTable) |
|---|
| 165 | |
|---|
| 166 | # BBB: |
|---|
| 167 | AccomodationTable = AccommodationTable |
|---|