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