[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 |
---|
| 15 | from WAeUPImport import DepartmentImport,FacultyImport,StudentImport,VerdictImport |
---|
[3316] | 16 | from WAeUPImport import NO_KEY,IGNORE |
---|
[3314] | 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 |
---|
[3277] | 58 | security.declareProtected(View,"Title") ###( |
---|
| 59 | def Title(self): |
---|
| 60 | """compose title""" |
---|
| 61 | return self.title |
---|
| 62 | ###) |
---|
| 63 | |
---|
| 64 | security.declareProtected(View,"checkKeys") ###( |
---|
| 65 | def checkKeys(self): |
---|
| 66 | """check fields in csv-headline""" |
---|
[3292] | 67 | doc = self.getContent() |
---|
| 68 | csv_path = os.path.join(storage_path,doc.filename) |
---|
[3277] | 69 | importer_name = ''.join([part.capitalize() for part in self.getContent().import_layout.split('_')]) |
---|
| 70 | importer = eval("%sImport" % importer_name)(self) |
---|
| 71 | msg = '' |
---|
| 72 | invalid_keys = [] |
---|
[3314] | 73 | keys = [] |
---|
[3277] | 74 | if not os.path.exists(csv_path): |
---|
[3292] | 75 | base,ext = os.path.splitext(csv_path) |
---|
| 76 | if os.path.exists("%(base)s.done" % vars()): |
---|
| 77 | #import pdb;pdb.set_trace() |
---|
| 78 | #msg = "imported" |
---|
| 79 | pass |
---|
| 80 | else: |
---|
| 81 | base_path = os.path.split(csv_path)[1] |
---|
| 82 | msg = 'No such file %(base_path)s' % vars() |
---|
[3277] | 83 | else: |
---|
[3314] | 84 | reader = csv.reader(open(csv_path,"rb")) |
---|
| 85 | headline = reader.next() |
---|
| 86 | values = reader.next() |
---|
[3277] | 87 | if "import_mode" not in headline: |
---|
| 88 | msg += 'import_mode must be in heading' |
---|
| 89 | invalid_keys = importer.checkHeadline(headline) |
---|
| 90 | if invalid_keys: |
---|
| 91 | msg += "invalid keys in heading" |
---|
[3316] | 92 | err,keys = importer.getHeadlineFields(headline,values) |
---|
| 93 | if err: |
---|
| 94 | msg += err |
---|
[3314] | 95 | return msg,keys |
---|
| 96 | ###) |
---|
| 97 | |
---|
[3292] | 98 | security.declareProtected(View,"getFields") ###( |
---|
[3277] | 99 | def getKeys(self): |
---|
| 100 | """return the valid keys for headline""" |
---|
| 101 | importer_name = ''.join([part.capitalize() for part in self.getContent().import_layout.split('_')]) |
---|
| 102 | importer = eval("%sImport" % importer_name)(self) |
---|
| 103 | keys = importer.schema.keys() |
---|
| 104 | keys.sort() |
---|
| 105 | return keys |
---|
[3292] | 106 | ###) |
---|
[3277] | 107 | |
---|
[3292] | 108 | security.declareProtected(View,"getUploadFileInfo") ###( |
---|
| 109 | def getUploadFileInfo(self): |
---|
| 110 | """return the valid keys for headline""" |
---|
| 111 | doc = self.getContent() |
---|
| 112 | return os.stat(os.path.join(storage_path,doc.filename)) |
---|
[3314] | 113 | ###) |
---|
| 114 | |
---|
[3277] | 115 | security.declareProtected(ModifyPortalContent,"editHeadline") ###( |
---|
| 116 | def editHeadline(self,key_pairs): |
---|
| 117 | """edit headline""" |
---|
| 118 | importer_name = ''.join([part.capitalize() for part in self.getContent().import_layout.split('_')]) |
---|
| 119 | importer = eval("%sImport" % importer_name)(self) |
---|
| 120 | csv_path = os.path.join(storage_path,self.filename) |
---|
| 121 | reader = csv.reader(open(csv_path,"rb")) |
---|
| 122 | headline = reader.next() |
---|
| 123 | records = [record for record in reader] |
---|
| 124 | #import pdb;pdb.set_trace() |
---|
[3316] | 125 | for old,new,pos in key_pairs: |
---|
[3277] | 126 | del headline[pos] |
---|
| 127 | headline.insert(pos,new) |
---|
| 128 | writer = csv.writer(open(csv_path,"w")) |
---|
| 129 | writer.writerow(headline) |
---|
| 130 | writer.writerows(records) |
---|
| 131 | |
---|
| 132 | |
---|
| 133 | InitializeClass(Upload) |
---|
| 134 | |
---|
| 135 | |
---|
| 136 | def addUpload(container, id, REQUEST=None, **kw): |
---|
| 137 | """Add a Upload.""" |
---|
| 138 | ob = Upload(id, **kw) |
---|
| 139 | return CPSBase_adder(container, ob, REQUEST=REQUEST) |
---|
| 140 | ###) |
---|