source: WAeUP_SRP/base/skins/waeup_upload/uploads_index.py @ 3316

Last change on this file since 3316 was 3316, checked in by joachim, 17 years ago

allow ignore,
check for duplicates

  • Property svn:keywords set to Id
File size: 5.5 KB
Line 
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"""
12return Info about the Uploads
13"""
14import DateTime
15import logging
16logger = logging.getLogger('Skins.upload_index')
17
18
19try:
20    from Products.zdb import set_trace
21except:
22    def set_trace():
23        pass
24request = context.REQUEST
25
26wf = context.portal_workflow
27mtool = context.portal_membership
28member = mtool.getAuthenticatedMember()
29
30path_info = request.get('PATH_INFO').split('/')
31info = {}
32info['action'] = "%s" % context.absolute_url()
33info['choosen_ids'] = request.get('ids',[])
34info['doc'] = context.getContent()
35
36edit = request.form.has_key('edit')
37ids = request.get('ids',[])
38if edit and ids:
39    for long_id in ids:
40        short_id = long_id.replace('.csv','')
41        doc = getattr(context, long_id).getContent()
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))
44        doc.edit(mapping = request.form[short_id])
45        msg, invalid_keys = doc['checkKeys']()
46        new_keys = []
47        i = 0
48        for old_key,previous,value,err in invalid_keys:
49            id_key = "%s|%s" % (short_id,i)
50            if request.form.has_key(id_key):
51                new_key = request.form.get(id_key)['valid_key']
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
58        if new_keys:
59            doc.editHeadline(new_keys)
60            for key in new_keys:
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]))
66    return request.RESPONSE.redirect(context.absolute_url())
67
68wrong = []
69imported = []
70importable = []
71for id,upload in context.objectItems():
72    add_to = wrong
73    row = {}
74    doc = upload.getContent()
75    row['valid_keys'] = doc['getKeys']()
76    row['valid_keys'][0:0] = doc.NO_KEY,
77    row['valid_keys'][1:1] = doc.IGNORE,
78    row['id'] = id.replace('.csv','')
79    row['title'] = doc.filename
80    row['url'] = upload.absolute_url()
81    row['imported'] = False
82    row['fields'] = []
83    import_message = getattr(doc,'import_message','')
84    if import_message:
85        row['imported'] = True
86        row['imported_by'] = getattr('doc','imported_by','')
87        row['msg'] = import_message
88        if getattr(doc,'import_date',''):
89            row['import_date'] = doc.import_date.strftime("%d/%m/%y %H:%M:%S")
90        else:
91            row['import_date'] = ''
92        add_to = imported
93    else:
94        row['msg'],row['fields'] = doc['checkKeys']()
95        if not (request.has_key('goto_edit') and id in ids) and not row['msg']:
96            add_to = importable
97    row['filename'] = doc.filename
98    row['import_layout'] = doc.import_layout
99    row['uploaded_by'] =  doc.uploaded_by
100    row['upload_date'] =  doc.upload_date.strftime("%d/%m/%y %H:%M:%S")
101    row['imported_by'] = doc.imported_by
102    add_to.append(row)
103wrong.sort()
104imported.sort()
105importable.sort()
106info['wrong'] = wrong
107info['imported'] = imported
108info['importable'] = importable
109info['import_allowed'] = str(member) in ('admin','joachim',)
110
111validate = "upload" in request.keys()
112mode = "create"
113res,psm,ds = context.portal_layouts.renderLayout(layout_id = 'upload',
114                             schema_id = 'upload',
115                             layout_mode = mode,
116                             context=context,
117                             mapping=validate and request,
118                             ob= {},
119                             commit = True,
120                            )
121while True:
122    if psm == 'invalid':
123        psm = "Please correct your input!"
124    # return context.uploads_form(rendered = res,
125    #                              psm = "Please correct your input!",
126    #                              mode = mode,
127    #                              ds = ds,
128    #                              info = info,
129    #                             )
130    elif psm == 'valid':
131        filename = ds['upload_file'].filename
132        if filename not in context.objectIds():
133            break
134        psm = "Upload object exists!"
135    logger.info('%s views upload section' % member)
136    return context.uploads_form(rendered = res,
137                                        psm = psm,
138                                        ds = ds,
139                                        mode = mode,
140                                        info = info,
141                                       )
142
143context.invokeFactory('Upload',filename)
144upload = getattr(context,filename)
145d = {}
146d['filename'] = d['id'] = filename
147d['upload_date'] =  DateTime.DateTime()
148d['import_layout'] = ds.get('import_layout','')
149d['uploaded_by'] = str(member)
150d['url'] = upload.absolute_url()
151upload.getContent().edit(mapping=d)
152logger.info('%s uploaded file %s with import layout %s' % (member,filename,d['import_layout']))
153
154return request.RESPONSE.redirect(context.absolute_url())
155
156
Note: See TracBrowser for help on using the repository browser.