source: WAeUP_SRP/trunk/WAeUPTables.py @ 962

Last change on this file since 962 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
Line 
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
23from AccessControl import ClassSecurityInfo
24from Products.CMFCore.permissions import ModifyPortalContent
25
26import DateTime
27import csv,re
28import logging
29import Globals
30p_home = Globals.package_home(globals())
31i_home = Globals.INSTANCE_HOME
32
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
44
45class 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
104class 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        s_logger = logging.getLogger('hostel_reservation')
124        s_logger.info("Student %s reserved bed %s" % (student_id,rec.bed))
125        return 1,rec.bed
126
127
128InitializeClass(AccommodationTable)
129
130class PinTable(WAeUPTable):
131
132    meta_type = 'WAeUP Pin Tool'
133    name = "pins"
134    key = 'pin'
135    def __init__(self):
136        WAeUPTable.__init__(self, 'portal_pins')
137
138
139    def searchAndSetRecord(self, uid, student_id,prefix):
140        #records = self.searchResults(uid=uid)
141        records = self.searchResults(student = student_id)
142        if len(records) > 0:
143            for r in records:
144                if r.pin != uid and r.prefix_batch.startswith(prefix):
145                    return -2
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:
158            record_data['student'] = student_id
159            self.catalog_object(dict2ob(record_data), uid)
160            return 1
161        if record.student != student_id:
162            return 0
163        if record.student == student_id:
164            return 2
165
166InitializeClass(PinTable)
167
168# BBB:
169AccomodationTable = AccommodationTable
Note: See TracBrowser for help on using the repository browser.