[3292] | 1 | ## Script (Python) "uploads_index" |
---|
[3277] | 2 | ##bind container=container |
---|
| 3 | ##bind context=context |
---|
| 4 | ##bind namespace= |
---|
| 5 | ##bind script=script |
---|
| 6 | ##bind subpath=traverse_subpath |
---|
[3292] | 7 | ##parameters= |
---|
[3277] | 8 | ##title= |
---|
| 9 | ## |
---|
[3292] | 10 | # $Id:uploads_index.py 486 2006-09-06 10:09:39Z joachim $ |
---|
[3277] | 11 | """ |
---|
| 12 | return Info about the Uploads |
---|
| 13 | """ |
---|
| 14 | import DateTime |
---|
[3291] | 15 | import logging |
---|
| 16 | logger = logging.getLogger('Skins.upload_index') |
---|
| 17 | |
---|
| 18 | |
---|
[3277] | 19 | try: |
---|
| 20 | from Products.zdb import set_trace |
---|
| 21 | except: |
---|
| 22 | def set_trace(): |
---|
| 23 | pass |
---|
| 24 | request = context.REQUEST |
---|
| 25 | |
---|
| 26 | wf = context.portal_workflow |
---|
| 27 | mtool = context.portal_membership |
---|
| 28 | member = mtool.getAuthenticatedMember() |
---|
| 29 | |
---|
| 30 | path_info = request.get('PATH_INFO').split('/') |
---|
[3283] | 31 | NO_KEY = '----' |
---|
[3277] | 32 | info = {} |
---|
| 33 | info['action'] = "%s" % context.absolute_url() |
---|
| 34 | info['choosen_ids'] = request.get('ids',[]) |
---|
| 35 | info['doc'] = context.getContent() |
---|
| 36 | |
---|
| 37 | edit = request.form.has_key('edit') |
---|
| 38 | ids = request.get('ids',[]) |
---|
| 39 | #set_trace() |
---|
| 40 | if edit and ids: |
---|
[3283] | 41 | for long_id in ids: |
---|
| 42 | short_id = long_id.replace('.csv','') |
---|
| 43 | doc = getattr(context, long_id).getContent() |
---|
[3293] | 44 | import_layout = request.form.get(short_id)['import_layout'] |
---|
| 45 | logger.info('%s set import layout of object %s to %s' % (member,long_id,import_layout)) |
---|
[3283] | 46 | doc.edit(mapping = request.form[short_id]) |
---|
[3277] | 47 | msg, invalid_keys = doc['checkKeys']() |
---|
| 48 | new_keys = [] |
---|
| 49 | for key in invalid_keys: |
---|
[3283] | 50 | id_key = "%s|%s" % (short_id,key) |
---|
[3277] | 51 | if request.form.has_key(id_key): |
---|
[3283] | 52 | new_key = request.form.get(id_key)['valid_key'] |
---|
| 53 | if new_key != NO_KEY: |
---|
| 54 | new_keys += (key,new_key), |
---|
[3277] | 55 | if new_keys: |
---|
| 56 | doc.editHeadline(new_keys) |
---|
[3291] | 57 | for key in new_keys: |
---|
| 58 | logger.info('%s changed column heading in file %s from %s to %s' % (member,long_id,key[0],key[1])) |
---|
[3305] | 59 | |
---|
[3292] | 60 | wrong = [] |
---|
| 61 | imported = [] |
---|
| 62 | importable = [] |
---|
[3277] | 63 | for id,upload in context.objectItems(): |
---|
[3292] | 64 | add_to = wrong |
---|
[3277] | 65 | row = {} |
---|
| 66 | doc = upload.getContent() |
---|
| 67 | row['valid_keys'] = doc['getKeys']() |
---|
[3283] | 68 | row['valid_keys'][0] = NO_KEY |
---|
[3277] | 69 | row['id'] = id.replace('.csv','') |
---|
| 70 | row['title'] = doc.filename |
---|
| 71 | row['url'] = upload.absolute_url() |
---|
[3292] | 72 | row['imported'] = False |
---|
| 73 | row['invalid_keys'] = [] |
---|
| 74 | import_message = getattr(doc,'import_message','') |
---|
| 75 | if import_message: |
---|
| 76 | row['imported'] = True |
---|
| 77 | row['msg'] = import_message |
---|
[3308] | 78 | if getattr(doc,'import_date',''): |
---|
| 79 | row['import_date'] = doc.import_date.strftime("%d/%m/%y %H:%M:%S") |
---|
[3297] | 80 | else: |
---|
[3308] | 81 | row['import_date'] = '' |
---|
[3292] | 82 | add_to = imported |
---|
| 83 | else: |
---|
| 84 | row['msg'],row['invalid_keys'] = doc['checkKeys']() |
---|
| 85 | if not row['msg'] and not row['invalid_keys']: |
---|
| 86 | add_to = importable |
---|
[3285] | 87 | row['filename'] = doc.filename |
---|
[3277] | 88 | row['import_layout'] = doc.import_layout |
---|
| 89 | row['upload_date'] = doc.upload_date.strftime("%d/%m/%y %H:%M:%S") |
---|
| 90 | row['uploaded_by'] = doc.uploaded_by |
---|
[3313] | 91 | row['imported_by'] = doc.imported_by |
---|
[3292] | 92 | add_to.append(row) |
---|
| 93 | wrong.sort() |
---|
| 94 | imported.sort() |
---|
| 95 | importable.sort() |
---|
| 96 | info['wrong'] = wrong |
---|
| 97 | info['imported'] = imported |
---|
| 98 | info['importable'] = importable |
---|
[3308] | 99 | info['import_allowed'] = str(member) in ('admin','joachim',) |
---|
[3277] | 100 | |
---|
| 101 | validate = "upload" in request.keys() |
---|
[3293] | 102 | mode = "create" |
---|
[3277] | 103 | res,psm,ds = context.portal_layouts.renderLayout(layout_id = 'upload', |
---|
| 104 | schema_id = 'upload', |
---|
| 105 | layout_mode = mode, |
---|
| 106 | context=context, |
---|
| 107 | mapping=validate and request, |
---|
| 108 | ob= {}, |
---|
| 109 | commit = True, |
---|
| 110 | ) |
---|
[3283] | 111 | while True: |
---|
| 112 | if psm == 'invalid': |
---|
| 113 | psm = "Please correct your input!" |
---|
| 114 | # return context.uploads_form(rendered = res, |
---|
| 115 | # psm = "Please correct your input!", |
---|
| 116 | # mode = mode, |
---|
| 117 | # ds = ds, |
---|
| 118 | # info = info, |
---|
| 119 | # ) |
---|
| 120 | elif psm == 'valid': |
---|
| 121 | filename = ds['upload_file'].filename |
---|
| 122 | if filename not in context.objectIds(): |
---|
| 123 | break |
---|
[3285] | 124 | psm = "Upload object exists!" |
---|
[3307] | 125 | logger.info('%s views upload section' % member) |
---|
[3277] | 126 | return context.uploads_form(rendered = res, |
---|
| 127 | psm = psm, |
---|
| 128 | ds = ds, |
---|
| 129 | mode = mode, |
---|
| 130 | info = info, |
---|
| 131 | ) |
---|
| 132 | |
---|
[3283] | 133 | context.invokeFactory('Upload',filename) |
---|
| 134 | upload = getattr(context,filename) |
---|
| 135 | d = {} |
---|
[3285] | 136 | d['filename'] = d['id'] = filename |
---|
[3283] | 137 | d['upload_date'] = DateTime.DateTime() |
---|
| 138 | d['import_layout'] = ds.get('import_layout','') |
---|
| 139 | d['uploaded_by'] = str(member) |
---|
| 140 | d['url'] = upload.absolute_url() |
---|
| 141 | upload.getContent().edit(mapping=d) |
---|
[3301] | 142 | logger.info('%s uploaded file %s with import layout %s' % (member,filename,d['import_layout'])) |
---|
[3283] | 143 | |
---|
[3301] | 144 | return request.RESPONSE.redirect(context.absolute_url()) |
---|
[3277] | 145 | |
---|
[3305] | 146 | |
---|