30
30
raise Exception(_('Please install python lib xlrd'))
33
def float_or_zero(val):
34
""" Conversion function used to manage
35
empty string into float usecase"""
36
return float(val) if val else 0.0
39
33
class GenericFileParser(FileParser):
41
35
Standard parser that use a define format in csv or xls to import into a
43
37
parser, but will also be useful as it allow to import a basic flat file.
46
def __init__(self, parse_name, ftype='csv'):
50
'date': datetime.datetime,
51
'amount': float_or_zero,
52
'commission_amount': float_or_zero
54
# Order of cols does not matter but first row of the file has to be header
55
keys_to_validate = ['ref', 'label', 'date', 'amount', 'commission_amount']
56
super(GenericFileParser, self).__init__(parse_name, keys_to_validate=keys_to_validate, ftype=ftype, conversion_dict=conversion_dict)
40
def __init__(self, parse_name, ftype='csv', **kwargs):
41
super(GenericFileParser, self).__init__(parse_name, ftype=ftype, **kwargs)
59
44
def parser_for(cls, parser_name):
81
'commission_amount':value,
83
In this generic parser, the commission is given for every line, so we store it
86
return {'name': line.get('label', line.get('ref', '/')),
87
'date': line.get('date', datetime.datetime.now().date()),
88
'amount': line.get('amount', 0.0),
89
'ref': line.get('ref', '/'),
90
'label': line.get('label', ''),
91
'commission_amount': line.get('commission_amount', 0.0)}
93
def _post(self, *args, **kwargs):
95
Compute the commission from value of each line
97
res = super(GenericFileParser, self)._post(*args, **kwargs)
99
for row in self.result_row_list:
100
val += row.get('commission_amount', 0.0)
101
self.commission_global_amount = val
69
'name': line.get('label', line.get('ref', '/')),
70
'date': line.get('date', datetime.datetime.now().date()),
71
'amount': line.get('amount', 0.0),
72
'ref': line.get('ref', '/'),
73
'label': line.get('label', ''),