[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('/') |
---|
| 31 | info = {} |
---|
| 32 | info['action'] = "%s" % context.absolute_url() |
---|
| 33 | info['choosen_ids'] = request.get('ids',[]) |
---|
| 34 | info['doc'] = context.getContent() |
---|
| 35 | |
---|
| 36 | edit = request.form.has_key('edit') |
---|
| 37 | ids = request.get('ids',[]) |
---|
| 38 | if edit and ids: |
---|
[3283] | 39 | for long_id in ids: |
---|
| 40 | short_id = long_id.replace('.csv','') |
---|
| 41 | doc = getattr(context, long_id).getContent() |
---|
[3293] | 42 | import_layout = request.form.get(short_id)['import_layout'] |
---|
| 43 | logger.info('%s set import layout of object %s to %s' % (member,long_id,import_layout)) |
---|
[3283] | 44 | doc.edit(mapping = request.form[short_id]) |
---|
[3277] | 45 | msg, invalid_keys = doc['checkKeys']() |
---|
| 46 | new_keys = [] |
---|
[3316] | 47 | i = 0 |
---|
| 48 | for old_key,previous,value,err in invalid_keys: |
---|
| 49 | id_key = "%s|%s" % (short_id,i) |
---|
[3277] | 50 | if request.form.has_key(id_key): |
---|
[3283] | 51 | new_key = request.form.get(id_key)['valid_key'] |
---|
[3316] | 52 | if new_key != old_key: |
---|
| 53 | if new_key == doc.IGNORE: |
---|
| 54 | new_keys += (old_key,"%s_%s" % (doc.IGNORE,old_key),i), |
---|
| 55 | elif new_key != doc.NO_KEY: |
---|
| 56 | new_keys += (old_key,new_key,i), |
---|
| 57 | i += 1 |
---|
[3277] | 58 | if new_keys: |
---|
| 59 | doc.editHeadline(new_keys) |
---|
[3291] | 60 | for key in new_keys: |
---|
[3316] | 61 | logger.info('%s changed column %s heading in file %s from %s to %s' % (member, |
---|
| 62 | key[2], |
---|
| 63 | long_id, |
---|
| 64 | key[0], |
---|
| 65 | key[1])) |
---|
[3314] | 66 | return request.RESPONSE.redirect(context.absolute_url()) |
---|
[3305] | 67 | |
---|
[3292] | 68 | wrong = [] |
---|
| 69 | imported = [] |
---|
| 70 | importable = [] |
---|
[3277] | 71 | for id,upload in context.objectItems(): |
---|
[3292] | 72 | add_to = wrong |
---|
[3277] | 73 | row = {} |
---|
| 74 | doc = upload.getContent() |
---|
| 75 | row['valid_keys'] = doc['getKeys']() |
---|
[3314] | 76 | row['valid_keys'][0:0] = doc.NO_KEY, |
---|
[3316] | 77 | row['valid_keys'][1:1] = doc.IGNORE, |
---|
[3277] | 78 | row['id'] = id.replace('.csv','') |
---|
| 79 | row['title'] = doc.filename |
---|
| 80 | row['url'] = upload.absolute_url() |
---|
[3292] | 81 | row['imported'] = False |
---|
[3316] | 82 | row['fields'] = [] |
---|
[3292] | 83 | import_message = getattr(doc,'import_message','') |
---|
| 84 | if import_message: |
---|
| 85 | row['imported'] = True |
---|
[3314] | 86 | row['imported_by'] = getattr('doc','imported_by','') |
---|
[3292] | 87 | row['msg'] = import_message |
---|
[3308] | 88 | if getattr(doc,'import_date',''): |
---|
[3327] | 89 | row['import_date'] = doc.import_date.strftime("%d/%m/%y %H:%M:%S") |
---|
[3403] | 90 | row['import_date_for_sort'] = doc.import_date.strftime("%y/%m/%d %H:%M:%S") |
---|
[3297] | 91 | else: |
---|
[3308] | 92 | row['import_date'] = '' |
---|
[3292] | 93 | add_to = imported |
---|
[3327] | 94 | |
---|
[3292] | 95 | else: |
---|
[3316] | 96 | row['msg'],row['fields'] = doc['checkKeys']() |
---|
[3314] | 97 | if not (request.has_key('goto_edit') and id in ids) and not row['msg']: |
---|
[3292] | 98 | add_to = importable |
---|
[3285] | 99 | row['filename'] = doc.filename |
---|
[3277] | 100 | row['import_layout'] = doc.import_layout |
---|
[3314] | 101 | row['uploaded_by'] = doc.uploaded_by |
---|
[3403] | 102 | row['upload_date_for_sort'] = doc.upload_date.strftime("%y/%m/%d %H:%M:%S") |
---|
[3277] | 103 | row['upload_date'] = doc.upload_date.strftime("%d/%m/%y %H:%M:%S") |
---|
[3313] | 104 | row['imported_by'] = doc.imported_by |
---|
[3292] | 105 | add_to.append(row) |
---|
[3403] | 106 | wrong.sort(cmp,key=lambda d:d['upload_date_for_sort'],reverse=1) |
---|
| 107 | importable.sort(cmp,key=lambda d:d['upload_date_for_sort'],reverse=1) |
---|
| 108 | imported.sort(cmp,key=lambda d:d['import_date_for_sort'],reverse=1) |
---|
[3292] | 109 | info['wrong'] = wrong |
---|
| 110 | info['imported'] = imported |
---|
| 111 | info['importable'] = importable |
---|
[5596] | 112 | info['import_allowed'] = str(member) in ('admin','isouaba',) |
---|
[3277] | 113 | |
---|
| 114 | validate = "upload" in request.keys() |
---|
[3293] | 115 | mode = "create" |
---|
[3277] | 116 | res,psm,ds = context.portal_layouts.renderLayout(layout_id = 'upload', |
---|
| 117 | schema_id = 'upload', |
---|
| 118 | layout_mode = mode, |
---|
| 119 | context=context, |
---|
| 120 | mapping=validate and request, |
---|
| 121 | ob= {}, |
---|
| 122 | commit = True, |
---|
| 123 | ) |
---|
[3283] | 124 | while True: |
---|
| 125 | if psm == 'invalid': |
---|
| 126 | psm = "Please correct your input!" |
---|
| 127 | # return context.uploads_form(rendered = res, |
---|
| 128 | # psm = "Please correct your input!", |
---|
| 129 | # mode = mode, |
---|
| 130 | # ds = ds, |
---|
| 131 | # info = info, |
---|
| 132 | # ) |
---|
| 133 | elif psm == 'valid': |
---|
| 134 | filename = ds['upload_file'].filename |
---|
[3332] | 135 | if filename.find('\\') > -1: |
---|
| 136 | filename = filename.split('\\')[-1] |
---|
[3283] | 137 | if filename not in context.objectIds(): |
---|
| 138 | break |
---|
[3285] | 139 | psm = "Upload object exists!" |
---|
[3307] | 140 | logger.info('%s views upload section' % member) |
---|
[3277] | 141 | return context.uploads_form(rendered = res, |
---|
| 142 | psm = psm, |
---|
| 143 | ds = ds, |
---|
| 144 | mode = mode, |
---|
| 145 | info = info, |
---|
| 146 | ) |
---|
| 147 | |
---|
[3283] | 148 | context.invokeFactory('Upload',filename) |
---|
[3332] | 149 | #set_trace() |
---|
[3283] | 150 | upload = getattr(context,filename) |
---|
| 151 | d = {} |
---|
[3285] | 152 | d['filename'] = d['id'] = filename |
---|
[3283] | 153 | d['upload_date'] = DateTime.DateTime() |
---|
| 154 | d['import_layout'] = ds.get('import_layout','') |
---|
| 155 | d['uploaded_by'] = str(member) |
---|
| 156 | d['url'] = upload.absolute_url() |
---|
| 157 | upload.getContent().edit(mapping=d) |
---|
[3301] | 158 | logger.info('%s uploaded file %s with import layout %s' % (member,filename,d['import_layout'])) |
---|
[3283] | 159 | |
---|
[3301] | 160 | return request.RESPONSE.redirect(context.absolute_url()) |
---|
[3277] | 161 | |
---|
[3305] | 162 | |
---|