Changeset 3277 for WAeUP_SRP/base
- Timestamp:
- 7 Mar 2008, 08:12:02 (17 years ago)
- Location:
- WAeUP_SRP/base
- Files:
-
- 5 added
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
WAeUP_SRP/base/WAeUPImport.py
r3268 r3277 26 26 #from Globals import DTMLFile 27 27 #from Globals import InitializeClass 28 #from OFS.SimpleItem import SimpleItem28 from OFS.SimpleItem import SimpleItem 29 29 from zExceptions import BadRequest 30 30 … … 33 33 #from Products.CPSSchemas.DataModel import DataModel 34 34 #from Products.CPSSchemas.StorageAdapter import MappingStorageAdapter 35 #from Products.CMFCore.ActionProviderBase import ActionProviderBase35 from Products.CMFCore.ActionProviderBase import ActionProviderBase 36 36 #from Products.CMFCore.permissions import View 37 37 #from Products.ZCatalog.ZCatalog import ZCatalog 38 38 #from Products.CMFCore.permissions import ModifyPortalContent 39 39 #from Products.CMFCore.permissions import ManagePortal 40 #from Products.CMFCore.utils import UniqueObject40 from Products.CMFCore.utils import UniqueObject 41 41 #from Products.CMFCore.URLTool import URLTool 42 42 from Products.CMFCore.utils import getToolByName … … 51 51 from utils import makeDigest 52 52 53 class WAeUPImport : ###(53 class WAeUPImport(UniqueObject, SimpleItem, ActionProviderBase): ###( 54 54 """ WAeUPImport """ 55 55 required_modes = ('create',) 56 56 57 def __init__(self,waeup_tool): 57 def __init__(self,waeup_tool): ###( 58 self.students_folder = waeup_tool.portal_url.getPortalObject().campus.students 58 59 self.member = member = waeup_tool.portal_membership.getAuthenticatedMember() 59 60 self.import_date = DateTime.DateTime().strftime("%d/%m/%y %H:%M:%S") 60 61 self.current = DateTime.DateTime().strftime("%d-%m-%y_%H_%M_%S") 61 62 self.waeup_tool = waeup_tool 62 self.students_folder = waeup_tool.portal_url.getPortalObject().campus.students63 63 self.academics_folder = waeup_tool.portal_url.getPortalObject().campus.academics 64 64 self.schema_tool = getToolByName(waeup_tool, 'portal_schemas') … … 115 115 break 116 116 self.init_errors = ','.join(errors) 117 118 def makeIdLists(self): 117 ###) 118 119 def makeIdLists(self): ###( 119 120 pending_digests = [] 120 121 pending = [] … … 134 135 #copy2(self.pending_path,self.pending_backup) 135 136 return pending, pending_digests 137 ###) 138 139 def checkHeadline(self,headline): 140 """ check the headline of an csv.file """ 141 import_keys = [k.strip() for k in headline if not (k.strip().startswith('ignore') 142 or k.strip() in self.info.keys())] 143 diff2schema = set(import_keys).difference(set(self.schema.keys())) 144 diff2layout = set(import_keys).difference(set(self.layout.keys())) 145 #if diff2schema and diff2schema != set(['id',]): 146 if diff2schema: 147 return list(diff2schema) 148 return [] 136 149 ###) 150 137 151 138 152 class ApplicationImport(WAeUPImport):###( -
WAeUP_SRP/base/WAeUPTool.py
r3272 r3277 1467 1467 if not pending_only: 1468 1468 info['imported_from'] = import_source_fn 1469 attrs= csv.reader(open(import_source_path,"rb")).next()1470 if "import_mode" not in attrs:1469 headline = csv.reader(open(import_source_path,"rb")).next() 1470 if "import_mode" not in headline: 1471 1471 msg = 'import_mode must be in heading' 1472 1472 return msg 1473 1474 import_keys = [k.strip() for k in attrs if not (k.strip().startswith('ignore') 1475 or k.strip() in info.keys())] 1476 diff2schema = set(import_keys).difference(set(importer.schema.keys())) 1477 diff2layout = set(import_keys).difference(set(importer.layout.keys())) 1478 if diff2schema and diff2schema != set(['id',]): 1479 msg = 'not ignorable key(s): "%s" found in heading' % ", ".join(diff2schema) 1480 return msg 1473 invalid_keys = importer.checkHeadline(headline) 1474 if invalid_keys: 1475 return 'not ignorable key(s): "%s" found in heading' % ", ".join(invalid_keys) 1476 1477 # import_keys = [k.strip() for k in headline if not (k.strip().startswith('ignore') 1478 # or k.strip() in info.keys())] 1479 # diff2schema = set(import_keys).difference(set(importer.schema.keys())) 1480 # diff2layout = set(import_keys).difference(set(importer.layout.keys())) 1481 # if diff2schema and diff2schema != set(['id',]): 1482 # msg = 'not ignorable key(s): "%s" found in heading' % ", ".join(diff2schema) 1483 # return msg 1481 1484 # 1482 1485 # start importing … … 1495 1498 count = 0 1496 1499 imported = [] 1497 old_commit_count = 01500 old_commit_count = -1 1498 1501 error_count = imported_count = 0 1499 1502 already_in = 0 … … 1581 1584 logger.info(' '.join(log_list)) 1582 1585 finished = count > total_to_import - 1 1583 must_commit = False 1584 if len(imported) != old_commit_count: 1585 if not len(imported) % commit_after: 1586 must_commit = True 1587 old_commit_count = len(imported) 1586 must_commit = (imported_count != old_commit_count) and (not imported_count % commit_after) 1587 if must_commit: 1588 old_commit_count = imported_count 1588 1589 1589 1590 if must_commit or finished: -
WAeUP_SRP/base/Widgets.py
r3265 r3277 372 372 v = '' 373 373 sessions = self._getSessions() 374 #import pdb;pdb.set_trace() 374 375 if not sessions.has_key(v): 375 376 datastructure.setError(widget_id, "'%s' not a valid session key" % v) … … 437 438 return 0 438 439 439 if not value:440 v = value = '100'440 # if not value: 441 # v = value = '100' 441 442 #import pdb;pdb.set_trace() 442 443 levels = self._getLevels() … … 1920 1921 """Get the file info from the datastructure.""" 1921 1922 widget_id = self.getWidgetId() 1923 info = None 1922 1924 if datastructure.has_key(widget_id): 1923 1925 fileupload = datastructure[widget_id] … … 2148 2150 ###) 2149 2151 2152 class DiscFileWidget(CPSFileWidget): ###( 2153 """File widget with filesystem storage.""" 2154 meta_type = 'Disc File Widget' 2155 _properties = CPSFileWidget._properties +\ 2156 ( 2157 {'id': 'path', 'type': 'string', 'mode': 'w', 2158 'label': 'Relative Path'}, 2159 ) 2160 path = "import" 2161 storage_path = "%s/%s" % (i_home,path) 2162 2163 def getStoredFileInfo(self,datastructure): ###( 2164 datamodel = datastructure.getDataModel() 2165 field_id = self.fields[0] 2166 widget_id = self.getWidgetId() 2167 file_name = datamodel[field_id] 2168 info = {} 2169 registry = getToolByName(self, 'mimetypes_registry') 2170 info['mimetype'] = registry.lookupExtension('file.csv') 2171 info['empty_file'] = True 2172 info['size'] = 0 2173 info['last_modified'] = '' 2174 file_path = os.path.join(self.storage_path, file_name) 2175 if os.path.exists(file_path): 2176 info['empty_file'] = False 2177 info['file_name'] = file_name 2178 info['content_url'] = os.path.join(self.portal_url(), 2179 "viewfile", 2180 file_name, 2181 ) 2182 info['current_filename'] = file_name 2183 # get the mimetype 2184 mimetype = (registry.lookupExtension(file_name.lower()) or 2185 registry.lookupExtension('file.bin')) 2186 info['mimetype'] = mimetype 2187 info['file_path'] = file_path 2188 else: 2189 info['file_name'] = '' 2190 info['content_url'] = '' 2191 info['current_filename'] = '' 2192 info['file_path'] = '' 2193 return info 2194 ###) 2195 2196 def prepare(self, datastructure, **kw): ###( 2197 """Prepare datastructure from datamodel.""" 2198 datamodel = datastructure.getDataModel() 2199 #import pdb;pdb.set_trace() 2200 widget_id = self.getWidgetId() 2201 file_name = datamodel[self.fields[0]] 2202 datastructure[widget_id] = file_name 2203 datastructure[widget_id + '_filename'] = file_name 2204 datastructure[widget_id+'_choice'] = "keep" 2205 ###) 2206 2207 def validate(self, datastructure, **kw): ###( 2208 """Update datamodel from user data in datastructure. 2209 """ 2210 logger = logging.getLogger('Widgets.DiscFileWidget.validate') 2211 datamodel = datastructure.getDataModel() 2212 field_id = self.fields[0] 2213 widget_id = self.getWidgetId() 2214 store = False 2215 fileupload = None 2216 mimetype = None 2217 old_file = datamodel[field_id] 2218 choice = datastructure[widget_id+'_choice'] 2219 fileupload = datastructure[widget_id] 2220 is_upload = isinstance(fileupload, FileUpload) 2221 if not is_upload and not datamodel[field_id]: 2222 if self.is_required: 2223 return self.validateError('File upload required', {}, 2224 datastructure) 2225 if choice == 'delete': 2226 if self.is_required: 2227 return self.validateError('cpsschemas_err_required', {}, 2228 datastructure) 2229 info = self.getStoredFileInfo(datastructure) 2230 # Remove the file in the filesystem 2231 if info: 2232 os.remove(info['file_path']) 2233 datamodel[field_id] = None 2234 elif choice == 'keep': 2235 fileupload = datastructure[widget_id] 2236 if isinstance(fileupload, PersistableFileUpload): 2237 # Keeping something from the session means we 2238 # actually want to store it. 2239 store = True 2240 # else: 2241 # # Nothing to change, don't pollute datastructure 2242 # # with something costly already stored, which therefore 2243 # # doesn't need to be kept in the session. 2244 # self.unprepare(datastructure) 2245 elif choice == 'change' and is_upload: 2246 if not fileupload: 2247 return self.validateError('cpsschemas_err_file_empty', {}, 2248 datastructure) 2249 if not isinstance(fileupload, FileUpload): 2250 return self.validateError('cpsschemas_err_file', {}, 2251 datastructure) 2252 fileupload.seek(0, 2) # end of file 2253 size = fileupload.tell() 2254 if not size: 2255 return self.validateError('cpsschemas_err_file_empty', {}, 2256 datastructure) 2257 if self.size_max and size > self.size_max: 2258 max_size_str = self.getHumanReadableSize(self.size_max) 2259 err = 'This file is too big, the allowed max size is ${max_size}' 2260 member_id = str(self.portal_membership.getAuthenticatedMember()) 2261 logger.info('%s tried to upload picture with size %dk' %(member_id,int(size)/1000) ) 2262 err_mapping = {'max_size': max_size_str} 2263 return self.validateError(err, err_mapping, datastructure) 2264 store = True 2265 2266 2267 # Find filename 2268 if is_upload and store: 2269 filename = cookId('', '', fileupload)[0].strip() 2270 base,ext = os.path.splitext(filename) 2271 if ext not in ("csv",".csv"): 2272 return self.validateError('only csv files allowed', {}, 2273 datastructure) 2274 datamodel[field_id] = filename 2275 registry = getToolByName(self, 'mimetypes_registry') 2276 mimetype = registry.lookupExtension(filename.lower()) 2277 if mimetype is not None: 2278 mimetype = str(mimetype) # normalize 2279 file = self.makeFile(filename, fileupload, datastructure) 2280 # Fixup mimetype 2281 if mimetype and file.content_type != mimetype: 2282 file.content_type = mimetype 2283 file_path = os.path.join(self.storage_path,filename) 2284 #import pdb; pdb.set_trace() 2285 data = open(file_path,"w") 2286 data.write(str(file.data)) 2287 data.close() 2288 return True 2289 ###) 2290 2291 def render(self, mode, datastructure, **kw): ###( 2292 render_method = 'widget_uploadfile_render' 2293 meth = getattr(self, render_method, None) 2294 #import pdb;pdb.set_trace() 2295 if meth is None: 2296 raise RuntimeError("Unknown Render Method %s for widget type %s" 2297 % (render_method, self.getId())) 2298 file_info = self.getStoredFileInfo(datastructure) 2299 2300 return meth(mode=mode, datastructure=datastructure, **file_info) 2301 ###) 2302 2303 InitializeClass(DiscFileWidget) 2304 2305 widgetRegistry.register(DiscFileWidget) 2306 ###) 2307 2150 2308 ########### 2151 2309 -
WAeUP_SRP/base/__init__.py
r2914 r3277 88 88 ) 89 89 ), 90 ("Upload", 91 ("UploadsFolder", 92 "Upload", 93 ) 94 ), 90 95 ) 91 96 -
WAeUP_SRP/base/exportimport.py
r3006 r3277 79 79 students = getattr(campus,'students',None) 80 80 accommodation = getattr(campus,'accommodation',None) 81 upload = getattr(campus,'upload',None) 81 82 campus.manage_setLocalGroupRoles(groupid='CampusOfficers', 82 83 roles=('SectionOfficer',)) … … 103 104 roles=('SectionReader',)) 104 105 accommodation.manage_setLocalGroupRoles(groupid='AccommodationOfficers', 106 roles=('SectionOfficer',)) 107 if upload is not None: 108 upload.manage_setLocalGroupRoles(groupid='CampusOfficers', 105 109 roles=('SectionOfficer',)) 106 110 members = getattr(site.portal_directories,'members')
Note: See TracChangeset for help on using the changeset viewer.