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 |
---|
15 | from WAeUPImport import DepartmentImport,FacultyImport,StudentImport,VerdictImport |
---|
16 | import logging |
---|
17 | import csv,re,os |
---|
18 | import Globals |
---|
19 | import DateTime |
---|
20 | import re |
---|
21 | p_home = Globals.package_home(globals()) |
---|
22 | i_home = Globals.INSTANCE_HOME |
---|
23 | |
---|
24 | storage_path = "%s/%s" % (i_home,'import') |
---|
25 | |
---|
26 | class UploadsFolder(CPSDocument): ###( |
---|
27 | """ |
---|
28 | WAeUP UploadsFolder containing Uploads |
---|
29 | """ |
---|
30 | meta_type = 'UploadsFolder' |
---|
31 | portal_type = meta_type |
---|
32 | security = ClassSecurityInfo() |
---|
33 | |
---|
34 | security.declareProtected(View,"Title") |
---|
35 | def Title(self): |
---|
36 | """compose title""" |
---|
37 | return "Upload Section" |
---|
38 | |
---|
39 | |
---|
40 | InitializeClass(UploadsFolder) |
---|
41 | |
---|
42 | def addUploadsFolder(container, id, REQUEST=None, **kw): |
---|
43 | """Add a UploadFolder.""" |
---|
44 | ob = UploadsFolder(id, **kw) |
---|
45 | return CPSBase_adder(container, ob, REQUEST=REQUEST) |
---|
46 | ###) |
---|
47 | |
---|
48 | class Upload(CPSDocument): ###( |
---|
49 | """ |
---|
50 | WAeUP Upload |
---|
51 | """ |
---|
52 | meta_type = 'Upload' |
---|
53 | portal_type = meta_type |
---|
54 | security = ClassSecurityInfo() |
---|
55 | |
---|
56 | security.declareProtected(View,"Title") ###( |
---|
57 | def Title(self): |
---|
58 | """compose title""" |
---|
59 | return self.title |
---|
60 | ###) |
---|
61 | |
---|
62 | security.declareProtected(View,"checkKeys") ###( |
---|
63 | def checkKeys(self): |
---|
64 | """check fields in csv-headline""" |
---|
65 | doc = self.getContent() |
---|
66 | csv_path = os.path.join(storage_path,doc.filename) |
---|
67 | importer_name = ''.join([part.capitalize() for part in self.getContent().import_layout.split('_')]) |
---|
68 | importer = eval("%sImport" % importer_name)(self) |
---|
69 | msg = '' |
---|
70 | invalid_keys = [] |
---|
71 | if not os.path.exists(csv_path): |
---|
72 | base,ext = os.path.splitext(csv_path) |
---|
73 | if os.path.exists("%(base)s.done" % vars()): |
---|
74 | #import pdb;pdb.set_trace() |
---|
75 | #msg = "imported" |
---|
76 | pass |
---|
77 | else: |
---|
78 | base_path = os.path.split(csv_path)[1] |
---|
79 | msg = 'No such file %(base_path)s' % vars() |
---|
80 | else: |
---|
81 | headline = csv.reader(open(csv_path,"rb")).next() |
---|
82 | if "import_mode" not in headline: |
---|
83 | msg += 'import_mode must be in heading' |
---|
84 | invalid_keys = importer.checkHeadline(headline) |
---|
85 | if invalid_keys: |
---|
86 | msg += "invalid keys in heading" |
---|
87 | return msg,invalid_keys |
---|
88 | |
---|
89 | security.declareProtected(View,"getFields") ###( |
---|
90 | def getKeys(self): |
---|
91 | """return the valid keys for headline""" |
---|
92 | importer_name = ''.join([part.capitalize() for part in self.getContent().import_layout.split('_')]) |
---|
93 | importer = eval("%sImport" % importer_name)(self) |
---|
94 | keys = importer.schema.keys() |
---|
95 | keys.sort() |
---|
96 | return keys |
---|
97 | ###) |
---|
98 | |
---|
99 | security.declareProtected(View,"getUploadFileInfo") ###( |
---|
100 | def getUploadFileInfo(self): |
---|
101 | """return the valid keys for headline""" |
---|
102 | doc = self.getContent() |
---|
103 | return os.stat(os.path.join(storage_path,doc.filename)) |
---|
104 | |
---|
105 | security.declareProtected(ModifyPortalContent,"editHeadline") ###( |
---|
106 | def editHeadline(self,key_pairs): |
---|
107 | """edit headline""" |
---|
108 | importer_name = ''.join([part.capitalize() for part in self.getContent().import_layout.split('_')]) |
---|
109 | importer = eval("%sImport" % importer_name)(self) |
---|
110 | csv_path = os.path.join(storage_path,self.filename) |
---|
111 | reader = csv.reader(open(csv_path,"rb")) |
---|
112 | headline = reader.next() |
---|
113 | records = [record for record in reader] |
---|
114 | #import pdb;pdb.set_trace() |
---|
115 | for old,new in key_pairs: |
---|
116 | pos = headline.index(old) |
---|
117 | del headline[pos] |
---|
118 | headline.insert(pos,new) |
---|
119 | writer = csv.writer(open(csv_path,"w")) |
---|
120 | writer.writerow(headline) |
---|
121 | writer.writerows(records) |
---|
122 | |
---|
123 | |
---|
124 | InitializeClass(Upload) |
---|
125 | |
---|
126 | |
---|
127 | def addUpload(container, id, REQUEST=None, **kw): |
---|
128 | """Add a Upload.""" |
---|
129 | ob = Upload(id, **kw) |
---|
130 | return CPSBase_adder(container, ob, REQUEST=REQUEST) |
---|
131 | ###) |
---|