Statistics
| Branch: | Tag: | Revision:

root / snf-astakos-app / astakos / quotaholder_app / commission.py @ 3715f954

History | View | Annotate | Download (5 kB)

1 d03796c2 Giorgos Korfiatis
# Copyright 2013 GRNET S.A. All rights reserved.
2 d03796c2 Giorgos Korfiatis
#
3 d03796c2 Giorgos Korfiatis
# Redistribution and use in source and binary forms, with or
4 d03796c2 Giorgos Korfiatis
# without modification, are permitted provided that the following
5 d03796c2 Giorgos Korfiatis
# conditions are met:
6 d03796c2 Giorgos Korfiatis
#
7 d03796c2 Giorgos Korfiatis
#   1. Redistributions of source code must retain the above
8 d03796c2 Giorgos Korfiatis
#      copyright notice, this list of conditions and the following
9 d03796c2 Giorgos Korfiatis
#      disclaimer.
10 d03796c2 Giorgos Korfiatis
#
11 d03796c2 Giorgos Korfiatis
#   2. Redistributions in binary form must reproduce the above
12 d03796c2 Giorgos Korfiatis
#      copyright notice, this list of conditions and the following
13 d03796c2 Giorgos Korfiatis
#      disclaimer in the documentation and/or other materials
14 d03796c2 Giorgos Korfiatis
#      provided with the distribution.
15 d03796c2 Giorgos Korfiatis
#
16 d03796c2 Giorgos Korfiatis
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17 d03796c2 Giorgos Korfiatis
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 d03796c2 Giorgos Korfiatis
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 d03796c2 Giorgos Korfiatis
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20 d03796c2 Giorgos Korfiatis
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 d03796c2 Giorgos Korfiatis
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 d03796c2 Giorgos Korfiatis
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23 d03796c2 Giorgos Korfiatis
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24 d03796c2 Giorgos Korfiatis
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 d03796c2 Giorgos Korfiatis
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26 d03796c2 Giorgos Korfiatis
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 d03796c2 Giorgos Korfiatis
# POSSIBILITY OF SUCH DAMAGE.
28 d03796c2 Giorgos Korfiatis
#
29 d03796c2 Giorgos Korfiatis
# The views and conclusions contained in the software and
30 d03796c2 Giorgos Korfiatis
# documentation are those of the authors and should not be
31 d03796c2 Giorgos Korfiatis
# interpreted as representing official policies, either expressed
32 d03796c2 Giorgos Korfiatis
# or implied, of GRNET S.A.
33 d03796c2 Giorgos Korfiatis
34 ec5e00aa Giorgos Korfiatis
from astakos.quotaholder_app.exception import NoCapacityError, NoQuantityError
35 d03796c2 Giorgos Korfiatis
36 d03796c2 Giorgos Korfiatis
37 d03796c2 Giorgos Korfiatis
class Operation(object):
38 d03796c2 Giorgos Korfiatis
39 d03796c2 Giorgos Korfiatis
    @staticmethod
40 d03796c2 Giorgos Korfiatis
    def assertions(holding):
41 68b991bc Giorgos Korfiatis
        assert(holding.usage_min <= holding.usage_max)
42 d03796c2 Giorgos Korfiatis
43 d03796c2 Giorgos Korfiatis
    @classmethod
44 d03796c2 Giorgos Korfiatis
    def _prepare(cls, holding, quantity, check=True):
45 d03796c2 Giorgos Korfiatis
        raise NotImplementedError
46 d03796c2 Giorgos Korfiatis
47 d03796c2 Giorgos Korfiatis
    @classmethod
48 d03796c2 Giorgos Korfiatis
    def prepare(cls, holding, quantity, check=True):
49 d03796c2 Giorgos Korfiatis
        cls.assertions(holding)
50 79e3da8a Giorgos Korfiatis
        cls._prepare(holding, quantity, check)
51 d03796c2 Giorgos Korfiatis
52 d03796c2 Giorgos Korfiatis
    @classmethod
53 d03796c2 Giorgos Korfiatis
    def _finalize(cls, holding, quantity):
54 d03796c2 Giorgos Korfiatis
        raise NotImplementedError
55 d03796c2 Giorgos Korfiatis
56 d03796c2 Giorgos Korfiatis
    @classmethod
57 d03796c2 Giorgos Korfiatis
    def finalize(cls, holding, quantity):
58 d03796c2 Giorgos Korfiatis
        cls.assertions(holding)
59 d03796c2 Giorgos Korfiatis
        cls._finalize(holding, quantity)
60 d03796c2 Giorgos Korfiatis
61 d03796c2 Giorgos Korfiatis
    @classmethod
62 d03796c2 Giorgos Korfiatis
    def undo(cls, holding, quantity):
63 d03796c2 Giorgos Korfiatis
        cls.prepare(holding, -quantity, check=False)
64 d03796c2 Giorgos Korfiatis
65 d03796c2 Giorgos Korfiatis
    @classmethod
66 d03796c2 Giorgos Korfiatis
    def revert(cls, holding, quantity):
67 d03796c2 Giorgos Korfiatis
        # Assertions do not hold when reverting
68 d03796c2 Giorgos Korfiatis
        cls._prepare(holding, -quantity, check=False)
69 d03796c2 Giorgos Korfiatis
70 79e3da8a Giorgos Korfiatis
    @classmethod
71 79e3da8a Giorgos Korfiatis
    def provision(cls, holding, quantity, importing=True):
72 79e3da8a Giorgos Korfiatis
        return {'holder': holding.holder,
73 79e3da8a Giorgos Korfiatis
                'source': holding.source,
74 79e3da8a Giorgos Korfiatis
                'resource': holding.resource,
75 79e3da8a Giorgos Korfiatis
                'quantity': quantity if importing else -quantity,
76 79e3da8a Giorgos Korfiatis
                }
77 79e3da8a Giorgos Korfiatis
78 d03796c2 Giorgos Korfiatis
79 d03796c2 Giorgos Korfiatis
class Import(Operation):
80 d03796c2 Giorgos Korfiatis
81 d03796c2 Giorgos Korfiatis
    @classmethod
82 d03796c2 Giorgos Korfiatis
    def _prepare(cls, holding, quantity, check=True):
83 68b991bc Giorgos Korfiatis
        usage_max = holding.usage_max
84 68b991bc Giorgos Korfiatis
        new_usage_max = usage_max + quantity
85 d03796c2 Giorgos Korfiatis
86 b78f2b29 Giorgos Korfiatis
        limit = holding.limit
87 68b991bc Giorgos Korfiatis
        if check and new_usage_max > limit:
88 d03796c2 Giorgos Korfiatis
            holder = holding.holder
89 d03796c2 Giorgos Korfiatis
            resource = holding.resource
90 d03796c2 Giorgos Korfiatis
            m = ("%s has not enough capacity of %s." % (holder, resource))
91 79e3da8a Giorgos Korfiatis
            provision = cls.provision(holding, quantity, importing=True)
92 d03796c2 Giorgos Korfiatis
            raise NoCapacityError(m,
93 79e3da8a Giorgos Korfiatis
                                  provision=provision,
94 e311d555 Giorgos Korfiatis
                                  limit=limit,
95 68b991bc Giorgos Korfiatis
                                  usage=usage_max)
96 d03796c2 Giorgos Korfiatis
97 68b991bc Giorgos Korfiatis
        holding.usage_max = new_usage_max
98 d03796c2 Giorgos Korfiatis
        holding.save()
99 d03796c2 Giorgos Korfiatis
100 d03796c2 Giorgos Korfiatis
    @classmethod
101 d03796c2 Giorgos Korfiatis
    def _finalize(cls, holding, quantity):
102 68b991bc Giorgos Korfiatis
        holding.usage_min += quantity
103 d03796c2 Giorgos Korfiatis
        holding.save()
104 d03796c2 Giorgos Korfiatis
105 d03796c2 Giorgos Korfiatis
106 d03796c2 Giorgos Korfiatis
class Release(Operation):
107 d03796c2 Giorgos Korfiatis
108 d03796c2 Giorgos Korfiatis
    @classmethod
109 d03796c2 Giorgos Korfiatis
    def _prepare(cls, holding, quantity, check=True):
110 68b991bc Giorgos Korfiatis
        usage_min = holding.usage_min
111 68b991bc Giorgos Korfiatis
        new_usage_min = usage_min - quantity
112 d03796c2 Giorgos Korfiatis
113 68b991bc Giorgos Korfiatis
        if check and new_usage_min < 0:
114 d03796c2 Giorgos Korfiatis
            holder = holding.holder
115 d03796c2 Giorgos Korfiatis
            resource = holding.resource
116 d03796c2 Giorgos Korfiatis
            m = ("%s attempts to release more %s than it contains." %
117 d03796c2 Giorgos Korfiatis
                 (holder, resource))
118 79e3da8a Giorgos Korfiatis
            provision = cls.provision(holding, quantity, importing=False)
119 79e3da8a Giorgos Korfiatis
            raise NoQuantityError(m,
120 79e3da8a Giorgos Korfiatis
                                  provision=provision,
121 3a8e56bc Giorgos Korfiatis
                                  limit=0,
122 3a8e56bc Giorgos Korfiatis
                                  usage=usage_min)
123 d03796c2 Giorgos Korfiatis
124 68b991bc Giorgos Korfiatis
        holding.usage_min = new_usage_min
125 d03796c2 Giorgos Korfiatis
        holding.save()
126 d03796c2 Giorgos Korfiatis
127 d03796c2 Giorgos Korfiatis
    @classmethod
128 d03796c2 Giorgos Korfiatis
    def _finalize(cls, holding, quantity):
129 68b991bc Giorgos Korfiatis
        holding.usage_max -= quantity
130 d03796c2 Giorgos Korfiatis
        holding.save()
131 d03796c2 Giorgos Korfiatis
132 d03796c2 Giorgos Korfiatis
133 d03796c2 Giorgos Korfiatis
class Operations(object):
134 d03796c2 Giorgos Korfiatis
    def __init__(self):
135 d03796c2 Giorgos Korfiatis
        self.operations = []
136 d03796c2 Giorgos Korfiatis
137 79e3da8a Giorgos Korfiatis
    def prepare(self, operation, holding, quantity, force):
138 79e3da8a Giorgos Korfiatis
        check = not force
139 79e3da8a Giorgos Korfiatis
        operation.prepare(holding, quantity, check)
140 d03796c2 Giorgos Korfiatis
        self.operations.append((operation, holding, quantity))
141 d03796c2 Giorgos Korfiatis
142 d03796c2 Giorgos Korfiatis
    def revert(self):
143 d03796c2 Giorgos Korfiatis
        for (operation, holding, quantity) in self.operations:
144 d03796c2 Giorgos Korfiatis
            operation.revert(holding, quantity)
145 6c0f4562 Giorgos Korfiatis
146 6c0f4562 Giorgos Korfiatis
147 6c0f4562 Giorgos Korfiatis
def finalize(operation, holding, quantity):
148 6c0f4562 Giorgos Korfiatis
    operation.finalize(holding, quantity)
149 6c0f4562 Giorgos Korfiatis
150 6c0f4562 Giorgos Korfiatis
151 6c0f4562 Giorgos Korfiatis
def undo(operation, holding, quantity):
152 6c0f4562 Giorgos Korfiatis
    operation.undo(holding, quantity)