#-*- 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 from WAeUPImport import NO_KEY,IGNORE 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() NO_KEY = NO_KEY IGNORE = IGNORE security.declareProtected(View,"Title") ###( def Title(self): """compose title""" return self.title ###) security.declareProtected(View,"checkKeys") ###( def checkKeys(self): """check fields in csv-headline""" doc = self.getContent() csv_path = os.path.join(storage_path,doc.filename) importer_name = ''.join([part.capitalize() for part in self.getContent().import_layout.split('_')]) importer = eval("%sImport" % importer_name)(self) msg = '' invalid_keys = [] keys = [] if not os.path.exists(csv_path): base,ext = os.path.splitext(csv_path) if os.path.exists("%(base)s.done" % vars()): #import pdb;pdb.set_trace() #msg = "imported" pass else: base_path = os.path.split(csv_path)[1] msg = 'No such file %(base_path)s' % vars() else: reader = csv.reader(open(csv_path,"rb")) headline = reader.next() values = reader.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" err,keys = importer.getHeadlineFields(headline,values) if err: msg += err return msg,keys ###) security.declareProtected(View,"getFields") ###( 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(View,"getUploadFileInfo") ###( def getUploadFileInfo(self): """return the valid keys for headline""" doc = self.getContent() return os.stat(os.path.join(storage_path,doc.filename)) ###) 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,pos in key_pairs: 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) ###)