# (C) Copyright 2005 AixtraWare # Author: Joachim Schmitz # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License version 2 as published # by the Free Software Foundation. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA # 02111-1307, USA. # # $Id: WAeUPTables.py 834 2006-11-10 21:15:13Z henrik $ from zope.interface import implements from Globals import InitializeClass from Products.ZCatalog.ZCatalog import ZCatalog from AccessControl import ClassSecurityInfo from Products.CMFCore.permissions import ModifyPortalContent import DateTime import csv,re import logging import Globals p_home = Globals.package_home(globals()) i_home = Globals.INSTANCE_HOME from interfaces import IWAeUPTable class AttributeHolder(object): pass def dict2ob(dict): ob = AttributeHolder() for key, value in dict.items(): setattr(ob, key, value) return ob class WAeUPTable(ZCatalog): implements(IWAeUPTable) security = ClassSecurityInfo() def addRecord(self, **data): # The uid is the same as "bed". uid = data[self.key] res = self.searchResults({"%s" % self.key : uid}) if len(res) > 0: raise ValueError("More than one record with uid %s" % uid) self.catalog_object(dict2ob(data), uid=uid) return uid def deleteRecord(self, uid): #import pdb;pdb.set_trace() self.uncatalog_object(uid) def searchAndSetRecord(self, **data): raise NotImplemented def modifyRecord(self, **data): #records = self.searchResults(uid=uid) uid = data[self.key] records = self.searchResults({"%s" % self.key : uid}) if len(records) > 1: # Can not happen, but anyway... raise ValueError("More than one record with uid %s" % uid) if len(records) == 0: raise KeyError("No record for uid %s" % uid) record = records[0] record_data = {} for field in self.schema() + self.indexes(): record_data[field] = getattr(record, field) # Add the updated data: record_data.update(data) self.catalog_object(dict2ob(record_data), uid) security.declareProtected(ModifyPortalContent,"exportAllRecords") def exportAllRecords(self): "export a WAeUPTable" #import pdb;pdb.set_trace() fields = [field for field in self.schema()] format = ','.join(['"%%(%s)s"' % fn for fn in fields]) csv = [] csv.append(','.join(['"%s"' % fn for fn in fields])) for uid in self._catalog.uids: records = self.searchResults({"%s" % self.key : uid}) if len(records) > 1: # Can not happen, but anyway... raise ValueError("More than one record with uid %s" % uid) if len(records) == 0: raise KeyError("No record for uid %s" % uid) rec = records[0] csv.append(format % rec) current = DateTime.DateTime().strftime("%d-%m-%y_%H_%M_%S") open("%s/import/%s-%s.csv" % (i_home,self.getId(),current),"w+").write('\n'.join(csv)) class AccommodationTable(WAeUPTable): meta_type = 'WAeUP Accommodation Tool' name = "accommodation" key = "bed" def __init__(self): WAeUPTable.__init__(self, 'portal_accommodation') def searchAndReserveBed(self, student_id,bed_type): records = self.searchResults({'student' : student_id}) if len(records) > 0: return -1,"Student with Id %s already booked bed %s" % (student_id,records[0].bed) records = [r for r in self.searchResults({'bed_type' : bed_type}) if not r.student] #import pdb;pdb.set_trace() if len(records) == 0: return -1,"No bed of this type available" rec = records[0] self.modifyRecord(bed=rec.bed,student=student_id) return 1,rec.bed InitializeClass(AccommodationTable) class PinTable(WAeUPTable): meta_type = 'WAeUP Pin Tool' name = "pins" key = 'pin' def __init__(self): WAeUPTable.__init__(self, 'portal_pins') def searchAndSetRecord(self, uid, student_id,prefix): #records = self.searchResults(uid=uid) records = self.searchResults(student = student_id) if len(records) > 0: for r in records: if r.pin != uid and r.prefix_batch.startswith(prefix): return -2 records = self.searchResults({"%s" % self.key : uid}) if len(records) > 1: # Can not happen, but anyway... raise ValueError("More than one record with uid %s" % uid) if len(records) == 0: return -1 record = records[0] if record.student == "": record_data = {} for field in self.schema() + self.indexes(): record_data[field] = getattr(record, field) # Add the updated data: record_data['student'] = student_id self.catalog_object(dict2ob(record_data), uid) return 1 if record.student != student_id: return 0 if record.student == student_id: return 2 InitializeClass(PinTable) # BBB: AccomodationTable = AccommodationTable