| 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 | csv_path = os.path.join(storage_path,self.filename) |
|---|
| 66 | importer_name = ''.join([part.capitalize() for part in self.getContent().import_layout.split('_')]) |
|---|
| 67 | importer = eval("%sImport" % importer_name)(self) |
|---|
| 68 | msg = '' |
|---|
| 69 | invalid_keys = [] |
|---|
| 70 | if not os.path.exists(csv_path): |
|---|
| 71 | msg = 'No such file %(csv_path)s' % vars() |
|---|
| 72 | else: |
|---|
| 73 | headline = csv.reader(open(csv_path,"rb")).next() |
|---|
| 74 | if "import_mode" not in headline: |
|---|
| 75 | msg += 'import_mode must be in heading' |
|---|
| 76 | invalid_keys = importer.checkHeadline(headline) |
|---|
| 77 | if invalid_keys: |
|---|
| 78 | msg += "invalid keys in heading" |
|---|
| 79 | return msg,invalid_keys |
|---|
| 80 | |
|---|
| 81 | security.declareProtected(View,"checkFields") ###( |
|---|
| 82 | def getKeys(self): |
|---|
| 83 | """return the valid keys for headline""" |
|---|
| 84 | importer_name = ''.join([part.capitalize() for part in self.getContent().import_layout.split('_')]) |
|---|
| 85 | importer = eval("%sImport" % importer_name)(self) |
|---|
| 86 | keys = importer.schema.keys() |
|---|
| 87 | keys.sort() |
|---|
| 88 | return keys |
|---|
| 89 | |
|---|
| 90 | security.declareProtected(ModifyPortalContent,"editHeadline") ###( |
|---|
| 91 | def editHeadline(self,key_pairs): |
|---|
| 92 | """edit headline""" |
|---|
| 93 | importer_name = ''.join([part.capitalize() for part in self.getContent().import_layout.split('_')]) |
|---|
| 94 | importer = eval("%sImport" % importer_name)(self) |
|---|
| 95 | csv_path = os.path.join(storage_path,self.filename) |
|---|
| 96 | reader = csv.reader(open(csv_path,"rb")) |
|---|
| 97 | headline = reader.next() |
|---|
| 98 | records = [record for record in reader] |
|---|
| 99 | #import pdb;pdb.set_trace() |
|---|
| 100 | for old,new in key_pairs: |
|---|
| 101 | pos = headline.index(old) |
|---|
| 102 | del headline[pos] |
|---|
| 103 | headline.insert(pos,new) |
|---|
| 104 | writer = csv.writer(open(csv_path,"w")) |
|---|
| 105 | writer.writerow(headline) |
|---|
| 106 | writer.writerows(records) |
|---|
| 107 | |
|---|
| 108 | |
|---|
| 109 | InitializeClass(Upload) |
|---|
| 110 | |
|---|
| 111 | |
|---|
| 112 | def addUpload(container, id, REQUEST=None, **kw): |
|---|
| 113 | """Add a Upload.""" |
|---|
| 114 | ob = Upload(id, **kw) |
|---|
| 115 | return CPSBase_adder(container, ob, REQUEST=REQUEST) |
|---|
| 116 | ###) |
|---|