#-*- mode: python; mode: fold -*- from Globals import InitializeClass from AccessControl import ClassSecurityInfo from Products.CMFCore.utils import UniqueObject, getToolByName from Products.CMFCore.permissions import View from Products.CMFCore.permissions import ModifyPortalContent from Products.CPSCore.CPSBase import CPSBase_adder, CPSBaseFolder #from Products.CPSCore.CPSBase import CPSBaseDocument as BaseDocument from Products.CPSDocument.CPSDocument import CPSDocument from Products.CPSCore.CPSBase import CPSBaseBTreeFolder as BaseBTreeFolder from Products.WAeUP_SRP.WAeUPTables import AccommodationTable,NOT_OCCUPIED from WAeUPImport import ApplicationImport,CertificateImport,CertificateCourseImport from WAeUPImport import CourseImport,CourseResultImport from WAeUPImport import DepartmentImport,FacultyImport,StudentImport,VerdictImport import logging import csv,re,os import Globals import DateTime import re p_home = Globals.package_home(globals()) i_home = Globals.INSTANCE_HOME storage_path = "%s/%s" % (i_home,'import') class UploadsFolder(CPSDocument): ###( """ WAeUP UploadsFolder containing Uploads """ meta_type = 'UploadsFolder' portal_type = meta_type security = ClassSecurityInfo() security.declareProtected(View,"Title") def Title(self): """compose title""" return "Upload Section" InitializeClass(UploadsFolder) def addUploadsFolder(container, id, REQUEST=None, **kw): """Add a UploadFolder.""" ob = UploadsFolder(id, **kw) return CPSBase_adder(container, ob, REQUEST=REQUEST) ###) class Upload(CPSDocument): ###( """ WAeUP Upload """ meta_type = 'Upload' portal_type = meta_type security = ClassSecurityInfo() security.declareProtected(View,"Title") ###( def Title(self): """compose title""" return self.title ###) security.declareProtected(View,"checkKeys") ###( def checkKeys(self): """check fields in csv-headline""" csv_path = os.path.join(storage_path,self.filename) importer_name = ''.join([part.capitalize() for part in self.getContent().import_layout.split('_')]) importer = eval("%sImport" % importer_name)(self) msg = '' invalid_keys = [] if not os.path.exists(csv_path): msg = 'No such file %(csv_path)s' % vars() else: headline = csv.reader(open(csv_path,"rb")).next() if "import_mode" not in headline: msg += 'import_mode must be in heading' invalid_keys = importer.checkHeadline(headline) if invalid_keys: msg += "invalid keys in heading" return msg,invalid_keys security.declareProtected(View,"checkFields") ###( def getKeys(self): """return the valid keys for headline""" importer_name = ''.join([part.capitalize() for part in self.getContent().import_layout.split('_')]) importer = eval("%sImport" % importer_name)(self) keys = importer.schema.keys() keys.sort() return keys security.declareProtected(ModifyPortalContent,"editHeadline") ###( def editHeadline(self,key_pairs): """edit headline""" importer_name = ''.join([part.capitalize() for part in self.getContent().import_layout.split('_')]) importer = eval("%sImport" % importer_name)(self) csv_path = os.path.join(storage_path,self.filename) reader = csv.reader(open(csv_path,"rb")) headline = reader.next() records = [record for record in reader] #import pdb;pdb.set_trace() for old,new in key_pairs: pos = headline.index(old) del headline[pos] headline.insert(pos,new) writer = csv.writer(open(csv_path,"w")) writer.writerow(headline) writer.writerows(records) InitializeClass(Upload) def addUpload(container, id, REQUEST=None, **kw): """Add a Upload.""" ob = Upload(id, **kw) return CPSBase_adder(container, ob, REQUEST=REQUEST) ###)