## Script (Python) "uploads_index" ##bind container=container ##bind context=context ##bind namespace= ##bind script=script ##bind subpath=traverse_subpath ##parameters= ##title= ## # $Id:uploads_index.py 486 2006-09-06 10:09:39Z joachim $ """ return Info about the Uploads """ import DateTime import logging logger = logging.getLogger('Skins.upload_index') try: from Products.zdb import set_trace except: def set_trace(): pass request = context.REQUEST wf = context.portal_workflow mtool = context.portal_membership member = mtool.getAuthenticatedMember() path_info = request.get('PATH_INFO').split('/') info = {} info['action'] = "%s" % context.absolute_url() info['choosen_ids'] = request.get('ids',[]) info['doc'] = context.getContent() edit = request.form.has_key('edit') ids = request.get('ids',[]) if edit and ids: for long_id in ids: short_id = long_id.replace('.csv','') doc = getattr(context, long_id).getContent() import_layout = request.form.get(short_id)['import_layout'] logger.info('%s set import layout of object %s to %s' % (member,long_id,import_layout)) doc.edit(mapping = request.form[short_id]) msg, invalid_keys = doc['checkKeys']() new_keys = [] i = 0 for old_key,previous,value,err in invalid_keys: id_key = "%s|%s" % (short_id,i) if request.form.has_key(id_key): new_key = request.form.get(id_key)['valid_key'] if new_key != old_key: if new_key == doc.IGNORE: new_keys += (old_key,"%s_%s" % (doc.IGNORE,old_key),i), elif new_key != doc.NO_KEY: new_keys += (old_key,new_key,i), i += 1 if new_keys: doc.editHeadline(new_keys) for key in new_keys: logger.info('%s changed column %s heading in file %s from %s to %s' % (member, key[2], long_id, key[0], key[1])) return request.RESPONSE.redirect(context.absolute_url()) wrong = [] imported = [] importable = [] for id,upload in context.objectItems(): add_to = wrong row = {} doc = upload.getContent() row['valid_keys'] = doc['getKeys']() row['valid_keys'][0:0] = doc.NO_KEY, row['valid_keys'][1:1] = doc.IGNORE, row['id'] = id.replace('.csv','') row['title'] = doc.filename row['url'] = upload.absolute_url() row['imported'] = False row['fields'] = [] import_message = getattr(doc,'import_message','') if import_message: row['imported'] = True row['imported_by'] = getattr('doc','imported_by','') row['msg'] = import_message if getattr(doc,'import_date',''): row['import_date'] = doc.import_date else: row['import_date'] = '' add_to = imported else: row['msg'],row['fields'] = doc['checkKeys']() if not (request.has_key('goto_edit') and id in ids) and not row['msg']: add_to = importable row['filename'] = doc.filename row['import_layout'] = doc.import_layout row['uploaded_by'] = doc.uploaded_by row['upload_date'] = doc.upload_date.strftime("%d/%m/%y %H:%M:%S") row['imported_by'] = doc.imported_by add_to.append(row) wrong.sort() imported.sort() importable.sort() info['wrong'] = wrong info['imported'] = imported info['importable'] = importable info['import_allowed'] = str(member) in ('admin','joachim',) validate = "upload" in request.keys() mode = "create" res,psm,ds = context.portal_layouts.renderLayout(layout_id = 'upload', schema_id = 'upload', layout_mode = mode, context=context, mapping=validate and request, ob= {}, commit = True, ) while True: if psm == 'invalid': psm = "Please correct your input!" # return context.uploads_form(rendered = res, # psm = "Please correct your input!", # mode = mode, # ds = ds, # info = info, # ) elif psm == 'valid': filename = ds['upload_file'].filename if filename not in context.objectIds(): break psm = "Upload object exists!" logger.info('%s views upload section' % member) return context.uploads_form(rendered = res, psm = psm, ds = ds, mode = mode, info = info, ) context.invokeFactory('Upload',filename) upload = getattr(context,filename) d = {} d['filename'] = d['id'] = filename d['upload_date'] = DateTime.DateTime() d['import_layout'] = ds.get('import_layout','') d['uploaded_by'] = str(member) d['url'] = upload.absolute_url() upload.getContent().edit(mapping=d) logger.info('%s uploaded file %s with import layout %s' % (member,filename,d['import_layout'])) return request.RESPONSE.redirect(context.absolute_url())