1 | ## Script (Python) "uploads_index" |
---|
2 | ##bind container=container |
---|
3 | ##bind context=context |
---|
4 | ##bind namespace= |
---|
5 | ##bind script=script |
---|
6 | ##bind subpath=traverse_subpath |
---|
7 | ##parameters= |
---|
8 | ##title= |
---|
9 | ## |
---|
10 | # $Id:uploads_index.py 486 2006-09-06 10:09:39Z joachim $ |
---|
11 | """ |
---|
12 | return Info about the Uploads |
---|
13 | """ |
---|
14 | import DateTime |
---|
15 | import logging |
---|
16 | logger = logging.getLogger('Skins.upload_index') |
---|
17 | |
---|
18 | |
---|
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 | NO_KEY = '----' |
---|
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: |
---|
41 | for long_id in ids: |
---|
42 | short_id = long_id.replace('.csv','') |
---|
43 | doc = getattr(context, long_id).getContent() |
---|
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)) |
---|
46 | doc.edit(mapping = request.form[short_id]) |
---|
47 | msg, invalid_keys = doc['checkKeys']() |
---|
48 | new_keys = [] |
---|
49 | for key in invalid_keys: |
---|
50 | id_key = "%s|%s" % (short_id,key) |
---|
51 | if request.form.has_key(id_key): |
---|
52 | new_key = request.form.get(id_key)['valid_key'] |
---|
53 | if new_key != NO_KEY: |
---|
54 | new_keys += (key,new_key), |
---|
55 | if new_keys: |
---|
56 | doc.editHeadline(new_keys) |
---|
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])) |
---|
59 | request.RESPONSE.redirect(context.absolute_url()) |
---|
60 | |
---|
61 | wrong = [] |
---|
62 | imported = [] |
---|
63 | importable = [] |
---|
64 | for id,upload in context.objectItems(): |
---|
65 | add_to = wrong |
---|
66 | row = {} |
---|
67 | doc = upload.getContent() |
---|
68 | row['valid_keys'] = doc['getKeys']() |
---|
69 | row['valid_keys'][0] = NO_KEY |
---|
70 | row['id'] = id.replace('.csv','') |
---|
71 | row['title'] = doc.filename |
---|
72 | row['url'] = upload.absolute_url() |
---|
73 | row['imported'] = False |
---|
74 | row['invalid_keys'] = [] |
---|
75 | import_message = getattr(doc,'import_message','') |
---|
76 | if import_message: |
---|
77 | row['imported'] = True |
---|
78 | row['msg'] = import_message |
---|
79 | if getattr(doc,'imported_at',''): |
---|
80 | row['imported_at'] = doc.imported_at.strftime("%d/%m/%y %H:%M:%S") |
---|
81 | else: |
---|
82 | row['imported_at'] = '' |
---|
83 | add_to = imported |
---|
84 | else: |
---|
85 | row['msg'],row['invalid_keys'] = doc['checkKeys']() |
---|
86 | if not row['msg'] and not row['invalid_keys']: |
---|
87 | add_to = importable |
---|
88 | row['filename'] = doc.filename |
---|
89 | row['import_layout'] = doc.import_layout |
---|
90 | row['upload_date'] = doc.upload_date.strftime("%d/%m/%y %H:%M:%S") |
---|
91 | row['uploaded_by'] = doc.uploaded_by |
---|
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 |
---|
99 | |
---|
100 | validate = "upload" in request.keys() |
---|
101 | mode = "create" |
---|
102 | res,psm,ds = context.portal_layouts.renderLayout(layout_id = 'upload', |
---|
103 | schema_id = 'upload', |
---|
104 | layout_mode = mode, |
---|
105 | context=context, |
---|
106 | mapping=validate and request, |
---|
107 | ob= {}, |
---|
108 | commit = True, |
---|
109 | ) |
---|
110 | while True: |
---|
111 | if psm == 'invalid': |
---|
112 | psm = "Please correct your input!" |
---|
113 | # return context.uploads_form(rendered = res, |
---|
114 | # psm = "Please correct your input!", |
---|
115 | # mode = mode, |
---|
116 | # ds = ds, |
---|
117 | # info = info, |
---|
118 | # ) |
---|
119 | elif psm == 'valid': |
---|
120 | filename = ds['upload_file'].filename |
---|
121 | if filename not in context.objectIds(): |
---|
122 | break |
---|
123 | psm = "Upload object exists!" |
---|
124 | return context.uploads_form(rendered = res, |
---|
125 | psm = psm, |
---|
126 | ds = ds, |
---|
127 | mode = mode, |
---|
128 | info = info, |
---|
129 | ) |
---|
130 | |
---|
131 | context.invokeFactory('Upload',filename) |
---|
132 | upload = getattr(context,filename) |
---|
133 | d = {} |
---|
134 | d['filename'] = d['id'] = filename |
---|
135 | d['upload_date'] = DateTime.DateTime() |
---|
136 | d['import_layout'] = ds.get('import_layout','') |
---|
137 | d['uploaded_by'] = str(member) |
---|
138 | d['url'] = upload.absolute_url() |
---|
139 | upload.getContent().edit(mapping=d) |
---|
140 | logger.info('%s uploaded file %s in layout mode %s' % (member,filename,d['import_layout'])) |
---|
141 | |
---|
142 | request.RESPONSE.redirect(context.absolute_url()) |
---|
143 | |
---|