source: WAeUP_SRP/trunk/WAeUPTables.py @ 934

Last change on this file since 934 was 834, checked in by Henrik Bettermann, 18 years ago

Notification text changed

  • Property svn:keywords set to Id
File size: 5.6 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 834 2006-11-10 21:15:13Z henrik $
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)
123        return 1,rec.bed
[363]124
[834]125
[404]126InitializeClass(AccommodationTable)
[411]127
[440]128class PinTable(WAeUPTable):
[834]129
[440]130    meta_type = 'WAeUP Pin Tool'
[502]131    name = "pins"
132    key = 'pin'
[440]133    def __init__(self):
134        WAeUPTable.__init__(self, 'portal_pins')
135
136
[710]137    def searchAndSetRecord(self, uid, student_id,prefix):
[502]138        #records = self.searchResults(uid=uid)
[710]139        records = self.searchResults(student = student_id)
140        if len(records) > 0:
141            for r in records:
[834]142                if r.pin != uid and r.prefix_batch.startswith(prefix):
[710]143                    return -2
[502]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:
[635]156            record_data['student'] = student_id
[502]157            self.catalog_object(dict2ob(record_data), uid)
158            return 1
[635]159        if record.student != student_id:
[502]160            return 0
[635]161        if record.student == student_id:
[502]162            return 2
[440]163
164InitializeClass(PinTable)
165
[414]166# BBB:
167AccomodationTable = AccommodationTable
Note: See TracBrowser for help on using the repository browser.