[3277] | 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 |
---|
[3360] | 15 | from WAeUPImport import DepartmentImport,FacultyImport,StudentImport,VerdictImport,StudentStudyLevelImport |
---|
[3316] | 16 | from WAeUPImport import NO_KEY,IGNORE |
---|
[3319] | 17 | |
---|
[3277] | 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() |
---|
[3314] | 56 | NO_KEY = NO_KEY |
---|
[3316] | 57 | IGNORE = IGNORE |
---|
[3325] | 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 |
---|
[3337] | 62 | |
---|
[3277] | 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""" |
---|
[3292] | 72 | doc = self.getContent() |
---|
| 73 | csv_path = os.path.join(storage_path,doc.filename) |
---|
[3325] | 74 | importer = self.getImporter() |
---|
[3277] | 75 | msg = '' |
---|
| 76 | invalid_keys = [] |
---|
[3314] | 77 | keys = [] |
---|
[3277] | 78 | if not os.path.exists(csv_path): |
---|
[3292] | 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] |
---|
[3319] | 86 | msg = '+ no such file %(base_path)s +' % vars() |
---|
[3277] | 87 | else: |
---|
[3314] | 88 | reader = csv.reader(open(csv_path,"rb")) |
---|
| 89 | headline = reader.next() |
---|
| 90 | values = reader.next() |
---|
[3277] | 91 | if "import_mode" not in headline: |
---|
[3319] | 92 | msg += '+ import_mode must be in heading +' |
---|
[3277] | 93 | invalid_keys = importer.checkHeadline(headline) |
---|
| 94 | if invalid_keys: |
---|
[3319] | 95 | msg += "+ invalid keys in heading +" |
---|
[3316] | 96 | err,keys = importer.getHeadlineFields(headline,values) |
---|
| 97 | if err: |
---|
| 98 | msg += err |
---|
[3314] | 99 | return msg,keys |
---|
| 100 | ###) |
---|
[3319] | 101 | |
---|
[3337] | 102 | security.declareProtected(View,"getKeys") ###( |
---|
[3277] | 103 | def getKeys(self): |
---|
| 104 | """return the valid keys for headline""" |
---|
[3325] | 105 | importer = self.getImporter() |
---|
[3277] | 106 | keys = importer.schema.keys() |
---|
| 107 | keys.sort() |
---|
| 108 | return keys |
---|
[3292] | 109 | ###) |
---|
[3319] | 110 | |
---|
[3292] | 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)) |
---|
[3314] | 116 | ###) |
---|
[3319] | 117 | |
---|
[3325] | 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 | |
---|
[3277] | 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() |
---|
[3316] | 159 | for old,new,pos in key_pairs: |
---|
[3277] | 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) |
---|
[3319] | 165 | |
---|
| 166 | |
---|
[3277] | 167 | InitializeClass(Upload) |
---|
| 168 | |
---|
| 169 | |
---|
| 170 | def addUpload(container, id, REQUEST=None, **kw): |
---|
| 171 | """Add a Upload.""" |
---|
| 172 | ob = Upload(id, **kw) |
---|
| 173 | return CPSBase_adder(container, ob, REQUEST=REQUEST) |
---|
| 174 | ###) |
---|