Revision 04dcc30e snf-astakos-app/astakos/quotaholder/callpoint.py

b/snf-astakos-app/astakos/quotaholder/callpoint.py
306 306

  
307 307
                # Source
308 308
                try:
309
                    h = db_get_holding(holder=holder, resource=resource,
310
                                       for_update=True)
309
                    h = (db_get_holding(holder=holder, resource=resource,
310
                                        for_update=True)
311
                         if holder is not None
312
                         else None)
311 313
                except Holding.DoesNotExist:
312 314
                    m = ("%s has no stock of %s." % (holder, resource))
313 315
                    raise NoStockError(m,
......
332 334
                                          limit=0)
333 335

  
334 336
                if quantity >= 0:
335
                    operations.prepare(Export, h, quantity)
337
                    if h is not None:
338
                        operations.prepare(Export, h, quantity)
336 339
                    operations.prepare(Import, th, quantity)
337 340

  
338 341
                else: # release
339 342
                    abs_quantity = -quantity
340 343

  
341
                    operations.prepare(Reclaim, h, abs_quantity)
344
                    if h is not None:
345
                        operations.prepare(Reclaim, h, abs_quantity)
342 346
                    operations.prepare(Release, th, abs_quantity)
343 347

  
344 348
                Provision.objects.create(serial=commission,
......
356 360
                       commission, s_holding, t_holding,
357 361
                       provision, log_time, reason):
358 362

  
359
        s_holder = s_holding.holder
360
        t_holder = t_holding.holder
363
        if s_holding is not None:
364
            s_holder = s_holding.holder
365
            s_capacity = s_holding.capacity
366
            s_imported_min = s_holding.imported_min
367
            s_imported_max = s_holding.imported_max
368
            s_stock_min = s_holding.stock_min
369
            s_stock_max = s_holding.stock_max
370
        else:
371
            s_holder = None
372
            s_capacity = None
373
            s_imported_min = None
374
            s_imported_max = None
375
            s_stock_min = None
376
            s_stock_max = None
361 377

  
362 378
        kwargs = {
363 379
            'serial':              commission.serial,
364 380
            'name':                commission.name,
365 381
            'source':              s_holder,
366
            'target':              t_holder,
382
            'target':              t_holding.holder,
367 383
            'resource':            provision.resource,
368
            'source_capacity':     s_holding.capacity,
369
            'source_imported_min': s_holding.imported_min,
370
            'source_imported_max': s_holding.imported_max,
371
            'source_stock_min':    s_holding.stock_min,
372
            'source_stock_max':    s_holding.stock_max,
384
            'source_capacity':     s_capacity,
385
            'source_imported_min': s_imported_min,
386
            'source_imported_max': s_imported_max,
387
            'source_stock_min':    s_stock_min,
388
            'source_stock_max':    s_stock_max,
373 389
            'target_capacity':     t_holding.capacity,
374 390
            'target_imported_min': t_holding.imported_min,
375 391
            'target_imported_max': t_holding.imported_max,
......
402 418
            provisions = db_filter_provision(serial=serial, for_update=True)
403 419
            for pv in provisions:
404 420
                try:
405
                    h = db_get_holding(holder=pv.holder,
406
                                       resource=pv.resource, for_update=True)
421
                    h = (db_get_holding(holder=pv.holder,
422
                                        resource=pv.resource, for_update=True)
423
                         if pv.holder is not None
424
                         else None)
407 425
                    th = db_get_holding(holder=t, resource=pv.resource,
408 426
                                        for_update=True)
409 427
                except Holding.DoesNotExist:
......
413 431
                quantity = pv.quantity
414 432

  
415 433
                if quantity >= 0:
416
                    operations.finalize(Export, h, quantity)
434
                    if h is not None:
435
                        operations.finalize(Export, h, quantity)
417 436
                    operations.finalize(Import, th, quantity)
418 437
                else: # release
419 438
                    abs_quantity = -quantity
420 439

  
421
                    operations.finalize(Reclaim, h, abs_quantity)
440
                    if h is not None:
441
                        operations.finalize(Reclaim, h, abs_quantity)
422 442
                    operations.finalize(Release, th, abs_quantity)
423 443

  
424 444
                reason = 'ACCEPT:' + reason[-121:]
......
447 467
            provisions = db_filter_provision(serial=serial, for_update=True)
448 468
            for pv in provisions:
449 469
                try:
450
                    h = db_get_holding(holder=pv.holder,
451
                                       resource=pv.resource, for_update=True)
470
                    h = (db_get_holding(holder=pv.holder,
471
                                        resource=pv.resource, for_update=True)
472
                         if pv.holder is not None
473
                         else None)
452 474
                    th = db_get_holding(holder=t, resource=pv.resource,
453 475
                                        for_update=True)
454 476
                except Holding.DoesNotExist:
......
458 480
                quantity = pv.quantity
459 481

  
460 482
                if quantity >= 0:
461
                    operations.undo(Export, h, quantity)
483
                    if h is not None:
484
                        operations.undo(Export, h, quantity)
462 485
                    operations.undo(Import, th, quantity)
463 486
                else: # release
464 487
                    abs_quantity = -quantity
465 488

  
466
                    operations.undo(Reclaim, h, abs_quantity)
489
                    if h is not None:
490
                        operations.undo(Reclaim, h, abs_quantity)
467 491
                    operations.undo(Release, th, abs_quantity)
468 492

  
469 493
                reason = 'REJECT:' + reason[-121:]

Also available in: Unified diff