1 | #-*- mode: python; mode: fold -*- |
---|
2 | from Globals import InitializeClass |
---|
3 | from AccessControl import ClassSecurityInfo |
---|
4 | |
---|
5 | from Products.CMFCore.utils import UniqueObject, getToolByName |
---|
6 | from Products.CMFCore.permissions import View |
---|
7 | from Products.CMFCore.permissions import ModifyPortalContent |
---|
8 | from Products.CPSCore.CPSBase import CPSBase_adder, CPSBaseFolder |
---|
9 | #from Products.CPSCore.CPSBase import CPSBaseDocument as BaseDocument |
---|
10 | from Products.CPSDocument.CPSDocument import CPSDocument |
---|
11 | from Products.CPSCore.CPSBase import CPSBaseBTreeFolder as BaseBTreeFolder |
---|
12 | from Products.WAeUP_SRP.WAeUPTables import AccommodationTable,NOT_OCCUPIED |
---|
13 | from WAeUPImport import ApplicationImport,CertificateImport,CertificateCourseImport |
---|
14 | from WAeUPImport import CourseImport,CourseResultImport,PaymentImport |
---|
15 | from WAeUPImport import DepartmentImport,FacultyImport,StudentImport,VerdictImport,StudentStudyLevelImport |
---|
16 | from WAeUPImport import NO_KEY,IGNORE |
---|
17 | |
---|
18 | import logging |
---|
19 | import csv,re,os |
---|
20 | import Globals |
---|
21 | import DateTime |
---|
22 | import re |
---|
23 | p_home = Globals.package_home(globals()) |
---|
24 | i_home = Globals.INSTANCE_HOME |
---|
25 | storage_path = "%s/%s" % (i_home,'import') |
---|
26 | |
---|
27 | class 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 | |
---|
41 | InitializeClass(UploadsFolder) |
---|
42 | |
---|
43 | def 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 | |
---|
49 | class 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 | |
---|
174 | InitializeClass(Upload) |
---|
175 | |
---|
176 | |
---|
177 | def addUpload(container, id, REQUEST=None, **kw): |
---|
178 | """Add a Upload.""" |
---|
179 | ob = Upload(id, **kw) |
---|
180 | return CPSBase_adder(container, ob, REQUEST=REQUEST) |
---|
181 | ###) |
---|