source: WAeUP_SRP/trunk/Upload.py @ 4353

Last change on this file since 4353 was 3988, checked in by Henrik Bettermann, 16 years ago

implement payment import

PatchExtendedWidgetsCPSDateTimeWidget.py: enable import of long DateTime? strings which are exported by csv writer

  • Property svn:keywords set to Id
File size: 5.8 KB
Line 
1#-*- mode: python; mode: fold -*-
2from Globals import InitializeClass
3from AccessControl import ClassSecurityInfo
4
5from Products.CMFCore.utils import UniqueObject, getToolByName
6from Products.CMFCore.permissions import View
7from Products.CMFCore.permissions import ModifyPortalContent
8from Products.CPSCore.CPSBase import CPSBase_adder, CPSBaseFolder
9#from Products.CPSCore.CPSBase import CPSBaseDocument as BaseDocument
10from Products.CPSDocument.CPSDocument import CPSDocument
11from Products.CPSCore.CPSBase import CPSBaseBTreeFolder as BaseBTreeFolder
12from Products.WAeUP_SRP.WAeUPTables import AccommodationTable,NOT_OCCUPIED
13from WAeUPImport import ApplicationImport,CertificateImport,CertificateCourseImport
14from WAeUPImport import CourseImport,CourseResultImport,PaymentImport
15from WAeUPImport import DepartmentImport,FacultyImport,StudentImport,VerdictImport,StudentStudyLevelImport
16from WAeUPImport import NO_KEY,IGNORE
17
18import logging
19import csv,re,os
20import Globals
21import DateTime
22import re
23p_home = Globals.package_home(globals())
24i_home = Globals.INSTANCE_HOME
25storage_path = "%s/%s" % (i_home,'import')
26
27class UploadsFolder(CPSDocument): ###(
28    """
29    WAeUP UploadsFolder containing Uploads
30    """
31    meta_type = 'UploadsFolder'
32    portal_type = meta_type
33    security = ClassSecurityInfo()
34
35    security.declareProtected(View,"Title")
36    def Title(self):
37        """compose title"""
38        return "Upload Section"
39
40
41InitializeClass(UploadsFolder)
42
43def addUploadsFolder(container, id, REQUEST=None, **kw):
44    """Add a UploadFolder."""
45    ob = UploadsFolder(id, **kw)
46    return CPSBase_adder(container, ob, REQUEST=REQUEST)
47###)
48
49class Upload(CPSDocument): ###(
50    """
51    WAeUP Upload
52    """
53    meta_type = 'Upload'
54    portal_type = meta_type
55    security = ClassSecurityInfo()
56    NO_KEY = NO_KEY
57    IGNORE = IGNORE
58    def getImporter(self):
59        importer_name = ''.join([part.capitalize() for part in self.getContent().import_layout.split('_')])
60        importer = eval("%sImport" % importer_name)(self)
61        return importer
62
63    security.declareProtected(View,"Title") ###(
64    def Title(self):
65        """compose title"""
66        return self.title
67    ###)
68
69    security.declareProtected(View,"checkKeys") ###(
70    def checkKeys(self):
71        """check fields in csv-headline"""
72        doc = self.getContent()
73        csv_path = os.path.join(storage_path,doc.filename)
74        importer = self.getImporter()
75        msg = ''
76        invalid_keys = []
77        keys = []
78        if not os.path.exists(csv_path):
79            base,ext = os.path.splitext(csv_path)
80            if os.path.exists("%(base)s.done" % vars()):
81            #import pdb;pdb.set_trace()
82                #msg = "imported"
83                pass
84            else:
85                base_path = os.path.split(csv_path)[1]
86                msg = '+ no such file %(base_path)s +' % vars()
87        else:
88            reader = csv.reader(open(csv_path,"rb"))
89            headline = reader.next()
90            values = reader.next()
91            if "import_mode" not in headline:
92                msg += '+ import_mode must be in heading +'
93            invalid_keys = importer.checkHeadline(headline)
94            if invalid_keys:
95                msg += "+ invalid keys in heading +"
96            err,keys = importer.getHeadlineFields(headline,values)
97            if err:
98                msg += err
99        return msg,keys
100    ###)
101
102    security.declareProtected(View,"getKeys") ###(
103    def getKeys(self):
104        """return the valid keys for headline"""
105        importer = self.getImporter()
106        keys = importer.schema.keys()
107        keys.sort()
108        return keys
109    ###)
110
111    security.declareProtected(View,"getUploadFileInfo") ###(
112    def getUploadFileInfo(self):
113        """return the valid keys for headline"""
114        doc = self.getContent()
115        return os.stat(os.path.join(storage_path,doc.filename))
116    ###)
117
118    security.declareProtected(ModifyPortalContent,"getPending") ###(
119    def getPending(self):
120        """return pending-data"""
121        importer = self.getImporter()
122        data = open(importer.pending_path).read()
123        return importer.pending_fn,data
124
125    security.declareProtected(ModifyPortalContent,"getPending") ###(
126    def getErrors(self):
127        """return import_errors"""
128        import tempfile
129        importer = self.getImporter()
130        lines = []
131        filename = self.getContent().filename
132        data = open(importer.pending_path)
133        reader = csv.reader(data)
134        headline = reader.next()
135        reader = csv.DictReader(data,headline)
136        # imported_from_pos = headline.index('imported_from')
137        # imported_line_nr_pos = headline.index('imported_line_nr')
138        out_file = tempfile.TemporaryFile()
139        writer = csv.DictWriter(out_file,
140                                importer.info.keys(),
141                                extrasaction='ignore')
142        lines += dict([(k,k) for k in importer.info.keys()]),
143        #import pdb;pdb.set_trace()
144        for item in reader:
145            if item["imported_from"] == filename:
146                lines += item,
147        writer.writerows(lines)
148        out_file.seek(0)
149        return out_file.read()
150
151    security.declareProtected(ModifyPortalContent,"editHeadline") ###(
152    def editHeadline(self,key_pairs):
153        """edit headline"""
154        csv_path = os.path.join(storage_path,self.filename)
155        reader = csv.reader(open(csv_path,"rb"))
156        headline = reader.next()
157        records = [record for record in reader]
158        #import pdb;pdb.set_trace()
159        for old,new,pos in key_pairs:
160            del headline[pos]
161            headline.insert(pos,new)
162        writer = csv.writer(open(csv_path,"w"))
163        writer.writerow(headline)
164        writer.writerows(records)
165
166
167InitializeClass(Upload)
168
169
170def addUpload(container, id, REQUEST=None, **kw):
171    """Add a Upload."""
172    ob = Upload(id, **kw)
173    return CPSBase_adder(container, ob, REQUEST=REQUEST)
174###)
Note: See TracBrowser for help on using the repository browser.