source: WAeUP_SRP/trunk/Upload.py @ 6298

Last change on this file since 6298 was 4819, checked in by Henrik Bettermann, 15 years ago

fix typo (section titles should be better singular than plural)

  • Property svn:keywords set to Id
File size: 6.1 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,"getData") ###(
126    def getData(self):
127        """return data"""
128        data_path = "%s/import/%s" % (i_home,self.filename)
129        data = open(data_path).read()
130        return self.filename,data       
131
132    security.declareProtected(ModifyPortalContent,"getErrors") ###(
133    def getErrors(self):
134        """return import_errors"""
135        import tempfile
136        importer = self.getImporter()
137        lines = []
138        filename = self.getContent().filename
139        data = open(importer.pending_path)
140        reader = csv.reader(data)
141        headline = reader.next()
142        reader = csv.DictReader(data,headline)
143        # imported_from_pos = headline.index('imported_from')
144        # imported_line_nr_pos = headline.index('imported_line_nr')
145        out_file = tempfile.TemporaryFile()
146        writer = csv.DictWriter(out_file,
147                                importer.info.keys(),
148                                extrasaction='ignore')
149        lines += dict([(k,k) for k in importer.info.keys()]),
150        #import pdb;pdb.set_trace()
151        for item in reader:
152            if item["imported_from"] == filename:
153                lines += item,
154        writer.writerows(lines)
155        out_file.seek(0)
156        return out_file.read()
157
158    security.declareProtected(ModifyPortalContent,"editHeadline") ###(
159    def editHeadline(self,key_pairs):
160        """edit headline"""
161        csv_path = os.path.join(storage_path,self.filename)
162        reader = csv.reader(open(csv_path,"rb"))
163        headline = reader.next()
164        records = [record for record in reader]
165        #import pdb;pdb.set_trace()
166        for old,new,pos in key_pairs:
167            del headline[pos]
168            headline.insert(pos,new)
169        writer = csv.writer(open(csv_path,"w"))
170        writer.writerow(headline)
171        writer.writerows(records)
172
173
174InitializeClass(Upload)
175
176
177def addUpload(container, id, REQUEST=None, **kw):
178    """Add a Upload."""
179    ob = Upload(id, **kw)
180    return CPSBase_adder(container, ob, REQUEST=REQUEST)
181###)
Note: See TracBrowser for help on using the repository browser.