Merge lp:~openerp-dev/openobject-addons/6.0-bug-750508-xrg into lp:openobject-addons/6.0

Proposed by xrg
Status: Needs review
Proposed branch: lp:~openerp-dev/openobject-addons/6.0-bug-750508-xrg
Merge into: lp:openobject-addons/6.0
Diff against target: 75 lines (+44/-1)
1 file modified
mail_gateway/mail_gateway.py (+44/-1)
To merge this branch: bzr merge lp:~openerp-dev/openobject-addons/6.0-bug-750508-xrg
Reviewer Review Type Date Requested Status
OpenERP Core Team Pending
Review via email: mp+58817@code.launchpad.net
To post a comment you must log in.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'mail_gateway/mail_gateway.py'
2--- mail_gateway/mail_gateway.py 2011-03-28 14:51:27 +0000
3+++ mail_gateway/mail_gateway.py 2011-04-24 07:06:33 +0000
4@@ -29,11 +29,20 @@
5 import base64
6 import re
7 from tools.translate import _
8+import random
9 import logging
10 import xmlrpclib
11
12 _logger = logging.getLogger('mailgate')
13
14+def mkpasswd(nlen):
15+ ret = ''
16+ rnd = random.SystemRandom()
17+ crange = string.ascii_letters + string.digits + '_-.'
18+ for c in range(nlen):
19+ ret += rnd.choice(crange)
20+ return ret
21+
22 class mailgate_thread(osv.osv):
23 '''
24 Mailgateway Thread
25@@ -112,6 +121,26 @@
26 if attach is None:
27 attach = []
28
29+ def fmt_date_suffix(have_time=False):
30+ if not edate:
31+ edate = time.time()
32+ if have_time:
33+ return time.strftime('-%Y%m%d-%H%M%S', edate)
34+ else:
35+ return time.strftime('-%Y%m%d', edate)
36+
37+ def random_suffix(*arg):
38+ return '-' + mkpasswd(6)
39+
40+ avail_alt_names = [ (None, ''), # the simple name
41+ (fmt_date_suffix, False),
42+ (fmt_date_suffix, True),
43+ (None, '-2'),
44+ (random_suffix, None),
45+ (random_suffix, None), # second attempt with random
46+ ]
47+
48+ edate = None
49 if email_date:
50 edate = parsedate(email_date)
51 if edate is not None:
52@@ -127,8 +156,22 @@
53
54 for case in cases:
55 attachments = []
56+
57 for att in attach:
58- attachments.append(att_obj.create(cr, uid, {'res_model':case._name,'res_id':case.id,'name': att[0], 'datas': base64.encodestring(att[1])}))
59+ alt_names = avail_alt_names[:] # make a copy
60+ att_name = att[0].replace('/','_')
61+ while att_obj.search(cr, uid, [('res_model', '=', case._name),
62+ ('res_id','=',case.id),('name', '=', att_name)], context=context):
63+ if not alt_names:
64+ raise ValueError("Cannot find a valid name to save attachment %s as." % att[0])
65+ fn, arg = alt.names.pop(0)
66+ if fn:
67+ att_name = att[0] + fn(arg)
68+ else:
69+ att_name = att[0] + arg
70+ attachments.append(att_obj.create(cr, uid,
71+ {'res_model':case._name,'res_id':case.id,
72+ 'name': att[0], 'datas': base64.encodestring(att[1])}))
73
74 partner_id = hasattr(case, 'partner_id') and (case.partner_id and case.partner_id.id or False) or False
75 if not partner_id and case._name == 'res.partner':