source: WAeUP_SRP/trunk/WAeUPTables.py @ 952

Last change on this file since 952 was 952, checked in by joachim, 18 years ago

loading of cps_portlets by generic_setup

  • Property svn:keywords set to Id
File size: 5.7 KB
RevLine 
[363]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 952 2006-11-27 20:47:26Z joachim $
19
20from zope.interface import implements
21from Globals import InitializeClass
22from Products.ZCatalog.ZCatalog import ZCatalog
[780]23from AccessControl import ClassSecurityInfo
24from Products.CMFCore.permissions import ModifyPortalContent
[363]25
[780]26import DateTime
27import csv,re
28import logging
29import Globals
30p_home = Globals.package_home(globals())
31i_home = Globals.INSTANCE_HOME
32
[363]33from interfaces import IWAeUPTable
34
35class AttributeHolder(object):
36    pass
37
38def dict2ob(dict):
39    ob = AttributeHolder()
40    for key, value in dict.items():
41        setattr(ob, key, value)
42    return ob
43
[834]44
[363]45class WAeUPTable(ZCatalog):
[834]46
[363]47    implements(IWAeUPTable)
[780]48    security = ClassSecurityInfo()
[834]49
[363]50    def addRecord(self, **data):
[502]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
[834]58
[363]59    def deleteRecord(self, uid):
[635]60        #import pdb;pdb.set_trace()
[363]61        self.uncatalog_object(uid)
[834]62
[502]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})
[363]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
[780]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))
[834]102
103
[404]104class AccommodationTable(WAeUPTable):
[834]105
[404]106    meta_type = 'WAeUP Accommodation Tool'
[502]107    name = "accommodation"
108    key = "bed"
[363]109    def __init__(self):
[404]110        WAeUPTable.__init__(self, 'portal_accommodation')
[363]111
[635]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)
[834]116
[673]117        records = [r for r in self.searchResults({'bed_type' : bed_type}) if not r.student]
[686]118        #import pdb;pdb.set_trace()
[635]119        if len(records) == 0:
[834]120            return -1,"No bed of this type available"
[635]121        rec = records[0]
122        self.modifyRecord(bed=rec.bed,student=student_id)
[952]123        s_logger = logging.getLogger('hostel_reservation')
124        s_logger.info("Student %s reserved bed %s" % (student_id,rec.bed))
[635]125        return 1,rec.bed
[363]126
[834]127
[404]128InitializeClass(AccommodationTable)
[411]129
[440]130class PinTable(WAeUPTable):
[834]131
[440]132    meta_type = 'WAeUP Pin Tool'
[502]133    name = "pins"
134    key = 'pin'
[440]135    def __init__(self):
136        WAeUPTable.__init__(self, 'portal_pins')
137
138
[710]139    def searchAndSetRecord(self, uid, student_id,prefix):
[502]140        #records = self.searchResults(uid=uid)
[710]141        records = self.searchResults(student = student_id)
142        if len(records) > 0:
143            for r in records:
[834]144                if r.pin != uid and r.prefix_batch.startswith(prefix):
[710]145                    return -2
[502]146        records = self.searchResults({"%s" % self.key : uid})
147        if len(records) > 1:
148            # Can not happen, but anyway...
149            raise ValueError("More than one record with uid %s" % uid)
150        if len(records) == 0:
151            return -1
152        record = records[0]
153        if record.student == "":
154            record_data = {}
155            for field in self.schema() + self.indexes():
156                record_data[field] = getattr(record, field)
157            # Add the updated data:
[635]158            record_data['student'] = student_id
[502]159            self.catalog_object(dict2ob(record_data), uid)
160            return 1
[635]161        if record.student != student_id:
[502]162            return 0
[635]163        if record.student == student_id:
[502]164            return 2
[440]165
166InitializeClass(PinTable)
167
[414]168# BBB:
169AccomodationTable = AccommodationTable
Note: See TracBrowser for help on using the repository browser.