Revision 79e3da8a snf-astakos-app/astakos/quotaholder/commission.py

b/snf-astakos-app/astakos/quotaholder/commission.py
31 31
# interpreted as representing official policies, either expressed
32 32
# or implied, of GRNET S.A.
33 33

  
34
from astakos.quotaholder.exception import (
35
    NoCapacityError, NonImportedError)
34
from astakos.quotaholder.exception import NoCapacityError, NoQuantityError
36 35

  
37 36

  
38 37
class Operation(object):
39 38

  
40 39
    @staticmethod
41 40
    def assertions(holding):
42
        assert(0 <= holding.imported_min)
43 41
        assert(holding.imported_min <= holding.imported_max)
44 42

  
45 43
    @classmethod
......
49 47
    @classmethod
50 48
    def prepare(cls, holding, quantity, check=True):
51 49
        cls.assertions(holding)
52
        cls._prepare(holding, quantity, check=True)
50
        cls._prepare(holding, quantity, check)
53 51

  
54 52
    @classmethod
55 53
    def _finalize(cls, holding, quantity):
......
69 67
        # Assertions do not hold when reverting
70 68
        cls._prepare(holding, -quantity, check=False)
71 69

  
70
    @classmethod
71
    def provision(cls, holding, quantity, importing=True):
72
        return {'holder': holding.holder,
73
                'source': holding.source,
74
                'resource': holding.resource,
75
                'quantity': quantity if importing else -quantity,
76
                }
77

  
72 78

  
73 79
class Import(Operation):
74 80

  
......
82 88
            holder = holding.holder
83 89
            resource = holding.resource
84 90
            m = ("%s has not enough capacity of %s." % (holder, resource))
91
            provision = cls.provision(holding, quantity, importing=True)
85 92
            raise NoCapacityError(m,
86
                                  holder=holder,
87
                                  resource=resource,
88
                                  requested=quantity,
89
                                  current=imported_max,
90
                                  limit=limit)
93
                                  provision=provision,
94
                                  available=limit-imported_max)
91 95

  
92 96
        holding.imported_max = new_imported_max
93 97
        holding.save()
......
110 114
            resource = holding.resource
111 115
            m = ("%s attempts to release more %s than it contains." %
112 116
                 (holder, resource))
113
            raise NonImportedError(m,
114
                                   holder=holder,
115
                                   resource=resource,
116
                                   requested=quantity,
117
                                   limit=imported_min)
117
            provision = cls.provision(holding, quantity, importing=False)
118
            raise NoQuantityError(m,
119
                                  provision=provision,
120
                                  available=imported_min)
118 121

  
119 122
        holding.imported_min = new_imported_min
120 123
        holding.save()
......
129 132
    def __init__(self):
130 133
        self.operations = []
131 134

  
132
    def prepare(self, operation, holding, quantity):
133
        operation.prepare(holding, quantity)
135
    def prepare(self, operation, holding, quantity, force):
136
        check = not force
137
        operation.prepare(holding, quantity, check)
134 138
        self.operations.append((operation, holding, quantity))
135 139

  
136 140
    def finalize(self, operation, holding, quantity):

Also available in: Unified diff