Revision daa780fa snf-astakos-app/astakos/quotaholder_app/callpoint.py

b/snf-astakos-app/astakos/quotaholder_app/callpoint.py
38 38
    NoCommissionError,
39 39
    CorruptedError,
40 40
    NoHoldingError,
41
    DuplicateError)
41
)
42 42

  
43 43
from astakos.quotaholder_app.commission import (
44 44
    Import, Release, Operations, finalize, undo)
......
139 139
    Holding.objects.bulk_create(new_holdings.values())
140 140

  
141 141

  
142
def _merge_same_keys(provisions):
143
    prov_dict = _partition_by(lambda t: t[0], provisions, lambda t: t[1])
144
    tuples = []
145
    for key, values in prov_dict.iteritems():
146
        tuples.append((key, sum(values)))
147
    return tuples
148

  
149

  
142 150
def issue_commission(clientkey, provisions, name="", force=False):
143 151
    operations = Operations()
144 152
    provisions_to_create = []
145 153

  
154
    provisions = _merge_same_keys(provisions)
146 155
    keys = [key for (key, value) in provisions]
147 156
    holdings = _get_holdings_for_update(keys)
148 157
    try:
149
        checked = []
150 158
        for key, quantity in provisions:
151
            if key in checked:
152
                m = "Duplicate provision for %s" % str(key)
153
                provision = _mkProvision(key, quantity)
154
                raise DuplicateError(m,
155
                                     provision=provision)
156
            checked.append(key)
157

  
158 159
            # Target
159 160
            try:
160 161
                th = holdings[key]
......
221 222
    return commissions
222 223

  
223 224

  
224
def _partition_by(f, l):
225
def _partition_by(f, l, convert=None):
226
    if convert is None:
227
        convert = lambda x: x
225 228
    d = {}
226 229
    for x in l:
227 230
        group = f(x)
228 231
        group_l = d.get(group, [])
229
        group_l.append(x)
232
        group_l.append(convert(x))
230 233
        d[group] = group_l
231 234
    return d
232 235

  

Also available in: Unified diff