root / snf-quotaholder-app / quotaholder_django / quotaholder_app / callpoint.py @ a7cdc223
History | View | Annotate | Download (27.4 kB)
1 | 3e5941c1 | Giorgos Korfiatis | # Copyright 2012 GRNET S.A. All rights reserved.
|
---|---|---|---|
2 | 3e5941c1 | Giorgos Korfiatis | #
|
3 | 3e5941c1 | Giorgos Korfiatis | # Redistribution and use in source and binary forms, with or
|
4 | 3e5941c1 | Giorgos Korfiatis | # without modification, are permitted provided that the following
|
5 | 3e5941c1 | Giorgos Korfiatis | # conditions are met:
|
6 | 3e5941c1 | Giorgos Korfiatis | #
|
7 | 3e5941c1 | Giorgos Korfiatis | # 1. Redistributions of source code must retain the above
|
8 | 3e5941c1 | Giorgos Korfiatis | # copyright notice, this list of conditions and the following
|
9 | 3e5941c1 | Giorgos Korfiatis | # disclaimer.
|
10 | 3e5941c1 | Giorgos Korfiatis | #
|
11 | 3e5941c1 | Giorgos Korfiatis | # 2. Redistributions in binary form must reproduce the above
|
12 | 3e5941c1 | Giorgos Korfiatis | # copyright notice, this list of conditions and the following
|
13 | 3e5941c1 | Giorgos Korfiatis | # disclaimer in the documentation and/or other materials
|
14 | 3e5941c1 | Giorgos Korfiatis | # provided with the distribution.
|
15 | 3e5941c1 | Giorgos Korfiatis | #
|
16 | 3e5941c1 | Giorgos Korfiatis | # THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
|
17 | 3e5941c1 | Giorgos Korfiatis | # OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
18 | 3e5941c1 | Giorgos Korfiatis | # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
19 | 3e5941c1 | Giorgos Korfiatis | # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
|
20 | 3e5941c1 | Giorgos Korfiatis | # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
21 | 3e5941c1 | Giorgos Korfiatis | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
22 | 3e5941c1 | Giorgos Korfiatis | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
23 | 3e5941c1 | Giorgos Korfiatis | # USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
24 | 3e5941c1 | Giorgos Korfiatis | # AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
25 | 3e5941c1 | Giorgos Korfiatis | # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
26 | 3e5941c1 | Giorgos Korfiatis | # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
27 | 3e5941c1 | Giorgos Korfiatis | # POSSIBILITY OF SUCH DAMAGE.
|
28 | 3e5941c1 | Giorgos Korfiatis | #
|
29 | 3e5941c1 | Giorgos Korfiatis | # The views and conclusions contained in the software and
|
30 | 3e5941c1 | Giorgos Korfiatis | # documentation are those of the authors and should not be
|
31 | 3e5941c1 | Giorgos Korfiatis | # interpreted as representing official policies, either expressed
|
32 | 3e5941c1 | Giorgos Korfiatis | # or implied, of GRNET S.A.
|
33 | 3e5941c1 | Giorgos Korfiatis | |
34 | 833abd3c | Giorgos Korfiatis | from synnefo.lib.quotaholder.api import ( |
35 | 833abd3c | Giorgos Korfiatis | QuotaholderAPI, |
36 | fac1de87 | Georgios D. Tsoukalas | InvalidKeyError, NoEntityError, |
37 | 3a7732f5 | Georgios D. Tsoukalas | NoQuantityError, NoCapacityError, |
38 | 6afede3f | Giorgos Korfiatis | ExportLimitError, ImportLimitError, |
39 | 6afede3f | Giorgos Korfiatis | DuplicateError) |
40 | fac1de87 | Georgios D. Tsoukalas | |
41 | f54beeea | Giorgos Korfiatis | from synnefo.lib.commissioning import \ |
42 | f54beeea | Giorgos Korfiatis | Callpoint, CorruptedError, InvalidDataError |
43 | f54beeea | Giorgos Korfiatis | from synnefo.lib.commissioning.utils.newname import newname |
44 | f54beeea | Giorgos Korfiatis | |
45 | 7fcfeb5d | Giorgos Korfiatis | from django.db.models import Q |
46 | 0a24e225 | Sofia Papagiannaki | from django.db import transaction, IntegrityError |
47 | f590f930 | Georgios D. Tsoukalas | from .models import (Holder, Entity, Policy, Holding, |
48 | fe6c61fb | root | Commission, Provision, ProvisionLog, now) |
49 | fac1de87 | Georgios D. Tsoukalas | |
50 | fac1de87 | Georgios D. Tsoukalas | |
51 | fac1de87 | Georgios D. Tsoukalas | class QuotaholderDjangoDBCallpoint(Callpoint): |
52 | fac1de87 | Georgios D. Tsoukalas | |
53 | fac1de87 | Georgios D. Tsoukalas | api_spec = QuotaholderAPI() |
54 | fac1de87 | Georgios D. Tsoukalas | |
55 | fac1de87 | Georgios D. Tsoukalas | http_exc_lookup = { |
56 | fac1de87 | Georgios D. Tsoukalas | CorruptedError: 550,
|
57 | fac1de87 | Georgios D. Tsoukalas | InvalidDataError: 400,
|
58 | fac1de87 | Georgios D. Tsoukalas | InvalidKeyError: 401,
|
59 | fac1de87 | Georgios D. Tsoukalas | NoEntityError: 404,
|
60 | fac1de87 | Georgios D. Tsoukalas | NoQuantityError: 413,
|
61 | fac1de87 | Georgios D. Tsoukalas | NoCapacityError: 413,
|
62 | fac1de87 | Georgios D. Tsoukalas | } |
63 | fac1de87 | Georgios D. Tsoukalas | |
64 | fac1de87 | Georgios D. Tsoukalas | def init_connection(self, connection): |
65 | fac1de87 | Georgios D. Tsoukalas | if connection is not None: |
66 | fac1de87 | Georgios D. Tsoukalas | raise ValueError("Cannot specify connection args with %s" % |
67 | fac1de87 | Georgios D. Tsoukalas | type(self).__name__) |
68 | fac1de87 | Georgios D. Tsoukalas | pass
|
69 | fac1de87 | Georgios D. Tsoukalas | |
70 | fac1de87 | Georgios D. Tsoukalas | def commit(self): |
71 | fac1de87 | Georgios D. Tsoukalas | transaction.commit() |
72 | fac1de87 | Georgios D. Tsoukalas | |
73 | fac1de87 | Georgios D. Tsoukalas | def rollback(self): |
74 | fac1de87 | Georgios D. Tsoukalas | transaction.rollback() |
75 | fac1de87 | Georgios D. Tsoukalas | |
76 | fac1de87 | Georgios D. Tsoukalas | def do_make_call(self, call_name, data): |
77 | fac1de87 | Georgios D. Tsoukalas | call_fn = getattr(self, call_name, None) |
78 | fac1de87 | Georgios D. Tsoukalas | if not call_fn: |
79 | fac1de87 | Georgios D. Tsoukalas | m = "cannot find call '%s'" % (call_name,)
|
80 | fac1de87 | Georgios D. Tsoukalas | raise CorruptedError(m)
|
81 | fac1de87 | Georgios D. Tsoukalas | |
82 | fac1de87 | Georgios D. Tsoukalas | return call_fn(**data)
|
83 | fac1de87 | Georgios D. Tsoukalas | |
84 | fac1de87 | Georgios D. Tsoukalas | def create_entity(self, context={}, create_entity=()): |
85 | fac1de87 | Georgios D. Tsoukalas | rejected = [] |
86 | fac1de87 | Georgios D. Tsoukalas | append = rejected.append |
87 | fac1de87 | Georgios D. Tsoukalas | |
88 | d793e5c4 | Giorgos Korfiatis | for idx, (entity, owner, key, ownerkey) in enumerate(create_entity): |
89 | fac1de87 | Georgios D. Tsoukalas | try:
|
90 | fac1de87 | Georgios D. Tsoukalas | owner = Entity.objects.get(entity=owner, key=ownerkey) |
91 | fac1de87 | Georgios D. Tsoukalas | except Entity.DoesNotExist:
|
92 | d793e5c4 | Giorgos Korfiatis | append(idx) |
93 | 635c115c | Sofia Papagiannaki | continue
|
94 | fac1de87 | Georgios D. Tsoukalas | |
95 | 0a24e225 | Sofia Papagiannaki | try:
|
96 | d793e5c4 | Giorgos Korfiatis | e = Entity.objects.get(entity=entity) |
97 | d793e5c4 | Giorgos Korfiatis | append(idx) |
98 | a3959e74 | Georgios D. Tsoukalas | except Entity.DoesNotExist:
|
99 | a2db0eb5 | root | e = Entity.objects.create(entity=entity, |
100 | a2db0eb5 | root | owner=owner, |
101 | a2db0eb5 | root | key=key) |
102 | fac1de87 | Georgios D. Tsoukalas | return rejected
|
103 | fac1de87 | Georgios D. Tsoukalas | |
104 | 1dedc367 | Georgios D. Tsoukalas | def set_entity_key(self, context={}, set_entity_key=()): |
105 | 1dedc367 | Georgios D. Tsoukalas | rejected = [] |
106 | 1dedc367 | Georgios D. Tsoukalas | append = rejected.append |
107 | 1dedc367 | Georgios D. Tsoukalas | |
108 | 1dedc367 | Georgios D. Tsoukalas | for entity, key, newkey in set_entity_key: |
109 | 1dedc367 | Georgios D. Tsoukalas | try:
|
110 | 1dedc367 | Georgios D. Tsoukalas | e = Entity.objects.get(entity=entity, key=key) |
111 | 1dedc367 | Georgios D. Tsoukalas | except Entity.DoesNotExist:
|
112 | 1dedc367 | Georgios D. Tsoukalas | append(entity) |
113 | 2546e0cc | Georgios D. Tsoukalas | continue
|
114 | 1dedc367 | Georgios D. Tsoukalas | |
115 | 1dedc367 | Georgios D. Tsoukalas | e.key = newkey |
116 | 1dedc367 | Georgios D. Tsoukalas | e.save() |
117 | 1dedc367 | Georgios D. Tsoukalas | |
118 | 1dedc367 | Georgios D. Tsoukalas | return rejected
|
119 | 1dedc367 | Georgios D. Tsoukalas | |
120 | fac1de87 | Georgios D. Tsoukalas | def list_entities(self, context={}, entity=None, key=None): |
121 | fac1de87 | Georgios D. Tsoukalas | try:
|
122 | fac1de87 | Georgios D. Tsoukalas | e = Entity.objects.get(entity=entity, key=key) |
123 | fac1de87 | Georgios D. Tsoukalas | except Entity.DoesNotExist:
|
124 | fac1de87 | Georgios D. Tsoukalas | m = "Entity '%s' does not exist" % (entity,)
|
125 | fac1de87 | Georgios D. Tsoukalas | raise NoEntityError(m)
|
126 | fac1de87 | Georgios D. Tsoukalas | |
127 | fac1de87 | Georgios D. Tsoukalas | children = e.entities.all() |
128 | fac1de87 | Georgios D. Tsoukalas | entities = [e.entity for e in children] |
129 | fac1de87 | Georgios D. Tsoukalas | return entities
|
130 | fac1de87 | Georgios D. Tsoukalas | |
131 | fac1de87 | Georgios D. Tsoukalas | def get_entity(self, context={}, get_entity=()): |
132 | fac1de87 | Georgios D. Tsoukalas | entities = [] |
133 | fac1de87 | Georgios D. Tsoukalas | append = entities.append |
134 | fac1de87 | Georgios D. Tsoukalas | |
135 | fac1de87 | Georgios D. Tsoukalas | for entity, key in get_entity: |
136 | fac1de87 | Georgios D. Tsoukalas | try:
|
137 | d69d04e7 | Giorgos Korfiatis | e = Entity.objects.get(entity=entity, key=key) |
138 | fac1de87 | Georgios D. Tsoukalas | except Entity.DoesNotExist:
|
139 | fac1de87 | Georgios D. Tsoukalas | continue
|
140 | fac1de87 | Georgios D. Tsoukalas | |
141 | d69d04e7 | Giorgos Korfiatis | append((entity, e.owner.entity)) |
142 | fac1de87 | Georgios D. Tsoukalas | |
143 | fac1de87 | Georgios D. Tsoukalas | return entities
|
144 | fac1de87 | Georgios D. Tsoukalas | |
145 | fac1de87 | Georgios D. Tsoukalas | def get_limits(self, context={}, get_limits=()): |
146 | fac1de87 | Georgios D. Tsoukalas | limits = [] |
147 | fac1de87 | Georgios D. Tsoukalas | append = limits.append |
148 | fac1de87 | Georgios D. Tsoukalas | |
149 | 3f84d70e | Christos KK Loverdos | for policy in get_limits: |
150 | fac1de87 | Georgios D. Tsoukalas | try:
|
151 | a116d82a | Giorgos Korfiatis | p = Policy.objects.get(policy=policy) |
152 | fac1de87 | Georgios D. Tsoukalas | except Policy.DoesNotExist:
|
153 | fac1de87 | Georgios D. Tsoukalas | continue
|
154 | fac1de87 | Georgios D. Tsoukalas | |
155 | a116d82a | Giorgos Korfiatis | append((policy, p.quantity, p.capacity, |
156 | a116d82a | Giorgos Korfiatis | p.import_limit, p.export_limit)) |
157 | fac1de87 | Georgios D. Tsoukalas | |
158 | fac1de87 | Georgios D. Tsoukalas | return limits
|
159 | fac1de87 | Georgios D. Tsoukalas | |
160 | fac1de87 | Georgios D. Tsoukalas | def set_limits(self, context={}, set_limits=()): |
161 | fac1de87 | Georgios D. Tsoukalas | |
162 | fac1de87 | Georgios D. Tsoukalas | for ( policy, quantity, capacity,
|
163 | fac1de87 | Georgios D. Tsoukalas | import_limit, export_limit ) in set_limits:
|
164 | fac1de87 | Georgios D. Tsoukalas | |
165 | 72c4abd2 | root | try:
|
166 | 72c4abd2 | root | policy = Policy.objects.get(policy=policy) |
167 | 72c4abd2 | root | except Policy.DoesNotExist:
|
168 | 72c4abd2 | root | Policy.objects.create( policy=policy, |
169 | 72c4abd2 | root | quantity=quantity, |
170 | 72c4abd2 | root | capacity=capacity, |
171 | 72c4abd2 | root | import_limit=import_limit, |
172 | 72c4abd2 | root | export_limit=export_limit ) |
173 | 72c4abd2 | root | else:
|
174 | 72c4abd2 | root | policy.quantity = quantity |
175 | 72c4abd2 | root | policy.capacity = capacity |
176 | 72c4abd2 | root | policy.export_limit = export_limit |
177 | 72c4abd2 | root | policy.import_limit = import_limit |
178 | 72c4abd2 | root | policy.save() |
179 | fac1de87 | Georgios D. Tsoukalas | |
180 | fac1de87 | Georgios D. Tsoukalas | return ()
|
181 | fac1de87 | Georgios D. Tsoukalas | |
182 | fac1de87 | Georgios D. Tsoukalas | def get_holding(self, context={}, get_holding=()): |
183 | fac1de87 | Georgios D. Tsoukalas | holdings = [] |
184 | fac1de87 | Georgios D. Tsoukalas | append = holdings.append |
185 | fac1de87 | Georgios D. Tsoukalas | |
186 | fac1de87 | Georgios D. Tsoukalas | for entity, resource, key in get_holding: |
187 | fac1de87 | Georgios D. Tsoukalas | try:
|
188 | fac1de87 | Georgios D. Tsoukalas | h = Holding.objects.get(entity=entity, resource=resource) |
189 | fac1de87 | Georgios D. Tsoukalas | except Holding.DoesNotExist:
|
190 | fac1de87 | Georgios D. Tsoukalas | continue
|
191 | fac1de87 | Georgios D. Tsoukalas | |
192 | fac1de87 | Georgios D. Tsoukalas | if h.entity.key != key:
|
193 | fac1de87 | Georgios D. Tsoukalas | continue
|
194 | fac1de87 | Georgios D. Tsoukalas | |
195 | 091494d7 | Giorgos Korfiatis | append((h.entity.entity, h.resource, h.policy.policy, |
196 | b435047b | root | h.imported, h.exported, |
197 | d6662f65 | root | h.returned, h.released, h.flags)) |
198 | fac1de87 | Georgios D. Tsoukalas | |
199 | fac1de87 | Georgios D. Tsoukalas | return holdings
|
200 | fac1de87 | Georgios D. Tsoukalas | |
201 | 8dc7c13b | Giorgos Korfiatis | def _set_holding(self, entity, resource, policy, flags): |
202 | 8dc7c13b | Giorgos Korfiatis | try:
|
203 | 8dc7c13b | Giorgos Korfiatis | h = Holding.objects.get(entity=entity, resource=resource) |
204 | 8dc7c13b | Giorgos Korfiatis | h.policy = p |
205 | 8dc7c13b | Giorgos Korfiatis | h.flags = flags |
206 | 8dc7c13b | Giorgos Korfiatis | h.save() |
207 | 8dc7c13b | Giorgos Korfiatis | except Holding.DoesNotExist:
|
208 | 8dc7c13b | Giorgos Korfiatis | h = Holding.objects.create( entity=e, resource=resource, |
209 | 8dc7c13b | Giorgos Korfiatis | policy=p, flags=flags ) |
210 | 8dc7c13b | Giorgos Korfiatis | return h
|
211 | 8dc7c13b | Giorgos Korfiatis | |
212 | fac1de87 | Georgios D. Tsoukalas | def set_holding(self, context={}, set_holding=()): |
213 | fac1de87 | Georgios D. Tsoukalas | rejected = [] |
214 | fac1de87 | Georgios D. Tsoukalas | append = rejected.append |
215 | fac1de87 | Georgios D. Tsoukalas | |
216 | fac1de87 | Georgios D. Tsoukalas | for entity, resource, key, policy, flags in set_holding: |
217 | fac1de87 | Georgios D. Tsoukalas | try:
|
218 | fac1de87 | Georgios D. Tsoukalas | e = Entity.objects.get(entity=entity, key=key) |
219 | fac1de87 | Georgios D. Tsoukalas | except Entity.DoesNotExist:
|
220 | fac1de87 | Georgios D. Tsoukalas | append((entity, resource, policy)) |
221 | fac1de87 | Georgios D. Tsoukalas | continue
|
222 | fac1de87 | Georgios D. Tsoukalas | |
223 | fac1de87 | Georgios D. Tsoukalas | if e.key != key:
|
224 | fac1de87 | Georgios D. Tsoukalas | append((entity, resource, policy)) |
225 | fac1de87 | Georgios D. Tsoukalas | continue
|
226 | fac1de87 | Georgios D. Tsoukalas | |
227 | fac1de87 | Georgios D. Tsoukalas | try:
|
228 | fac1de87 | Georgios D. Tsoukalas | p = Policy.objects.get(policy=policy) |
229 | fac1de87 | Georgios D. Tsoukalas | except Policy.DoesNotExist:
|
230 | fac1de87 | Georgios D. Tsoukalas | append((entity, resource, policy)) |
231 | fac1de87 | Georgios D. Tsoukalas | continue
|
232 | fac1de87 | Georgios D. Tsoukalas | |
233 | fac1de87 | Georgios D. Tsoukalas | try:
|
234 | fac1de87 | Georgios D. Tsoukalas | h = Holding.objects.get(entity=entity, resource=resource) |
235 | fac1de87 | Georgios D. Tsoukalas | h.policy = p |
236 | fac1de87 | Georgios D. Tsoukalas | h.flags = flags |
237 | fac1de87 | Georgios D. Tsoukalas | h.save() |
238 | fac1de87 | Georgios D. Tsoukalas | except Holding.DoesNotExist:
|
239 | c2260987 | Giorgos Korfiatis | h = Holding.objects.create( entity=e, resource=resource, |
240 | c2260987 | Giorgos Korfiatis | policy=p, flags=flags ) |
241 | fac1de87 | Georgios D. Tsoukalas | |
242 | fac1de87 | Georgios D. Tsoukalas | return rejected
|
243 | fac1de87 | Georgios D. Tsoukalas | |
244 | a7cdc223 | Giorgos Korfiatis | def _init_holding(self, entity, resource, policy, |
245 | 5b522bc3 | Giorgos Korfiatis | imported, exported, returned, released, |
246 | 5b522bc3 | Giorgos Korfiatis | flags): |
247 | 5b522bc3 | Giorgos Korfiatis | try:
|
248 | 5b522bc3 | Giorgos Korfiatis | h = Holding.objects.get(entity=entity, resource=resource) |
249 | 5b522bc3 | Giorgos Korfiatis | except Holding.DoesNotExist:
|
250 | 5b522bc3 | Giorgos Korfiatis | h = Holding(entity=entity, resource=resource) |
251 | 5b522bc3 | Giorgos Korfiatis | |
252 | 5b522bc3 | Giorgos Korfiatis | h.policy = policy |
253 | 5b522bc3 | Giorgos Korfiatis | h.flags = flags |
254 | 5b522bc3 | Giorgos Korfiatis | h.imported=imported |
255 | 5b522bc3 | Giorgos Korfiatis | h.importing=imported |
256 | 5b522bc3 | Giorgos Korfiatis | h.exported=exported |
257 | 5b522bc3 | Giorgos Korfiatis | h.exporting=exported |
258 | 5b522bc3 | Giorgos Korfiatis | h.returned=returned |
259 | 5b522bc3 | Giorgos Korfiatis | h.returning=returned |
260 | 5b522bc3 | Giorgos Korfiatis | h.released=released |
261 | 5b522bc3 | Giorgos Korfiatis | h.releasing=released |
262 | 5b522bc3 | Giorgos Korfiatis | h.save() |
263 | 5b522bc3 | Giorgos Korfiatis | |
264 | a7cdc223 | Giorgos Korfiatis | def init_holding(self, context={}, init_holding=()): |
265 | 5b522bc3 | Giorgos Korfiatis | rejected = [] |
266 | 5b522bc3 | Giorgos Korfiatis | append = rejected.append |
267 | 5b522bc3 | Giorgos Korfiatis | |
268 | a7cdc223 | Giorgos Korfiatis | for idx, sfh in enumerate(init_holding): |
269 | 5b522bc3 | Giorgos Korfiatis | (entity, resource, key, policy, |
270 | 5b522bc3 | Giorgos Korfiatis | imported, exported, returned, released, |
271 | 5b522bc3 | Giorgos Korfiatis | flags) = sfh |
272 | 5b522bc3 | Giorgos Korfiatis | try:
|
273 | 5b522bc3 | Giorgos Korfiatis | e = Entity.objects.get(entity=entity, key=key) |
274 | 5b522bc3 | Giorgos Korfiatis | except Entity.DoesNotExist:
|
275 | 5b522bc3 | Giorgos Korfiatis | append(idx) |
276 | 5b522bc3 | Giorgos Korfiatis | continue
|
277 | 5b522bc3 | Giorgos Korfiatis | |
278 | 5b522bc3 | Giorgos Korfiatis | if e.key != key:
|
279 | 5b522bc3 | Giorgos Korfiatis | append(idx) |
280 | 5b522bc3 | Giorgos Korfiatis | continue
|
281 | 5b522bc3 | Giorgos Korfiatis | |
282 | 5b522bc3 | Giorgos Korfiatis | try:
|
283 | 5b522bc3 | Giorgos Korfiatis | p = Policy.objects.get(policy=policy) |
284 | 5b522bc3 | Giorgos Korfiatis | except Policy.DoesNotExist:
|
285 | 5b522bc3 | Giorgos Korfiatis | append(idx) |
286 | 5b522bc3 | Giorgos Korfiatis | continue
|
287 | 5b522bc3 | Giorgos Korfiatis | |
288 | a7cdc223 | Giorgos Korfiatis | self._init_holding(e, resource, p,
|
289 | 5b522bc3 | Giorgos Korfiatis | imported, exported, |
290 | 5b522bc3 | Giorgos Korfiatis | returned, released, |
291 | 5b522bc3 | Giorgos Korfiatis | flags) |
292 | 5b522bc3 | Giorgos Korfiatis | return rejected
|
293 | 5b522bc3 | Giorgos Korfiatis | |
294 | 8dc7c13b | Giorgos Korfiatis | def _check_pending(self, entity=None): |
295 | 8dc7c13b | Giorgos Korfiatis | serials = [] |
296 | 8dc7c13b | Giorgos Korfiatis | try:
|
297 | 8dc7c13b | Giorgos Korfiatis | cs = Commission.objects.filter(entity=entity) |
298 | 8dc7c13b | Giorgos Korfiatis | serials = [c.serial for c in cs] |
299 | 8dc7c13b | Giorgos Korfiatis | except Commission.DoesNotExist:
|
300 | 8dc7c13b | Giorgos Korfiatis | pass
|
301 | 8dc7c13b | Giorgos Korfiatis | |
302 | 8dc7c13b | Giorgos Korfiatis | try:
|
303 | 8dc7c13b | Giorgos Korfiatis | ps = Provision.objects.filter(entity=entity) |
304 | 8dc7c13b | Giorgos Korfiatis | serials += [p.serial.serial for p in ps] |
305 | 8dc7c13b | Giorgos Korfiatis | except Provision.DoesNotExist:
|
306 | 8dc7c13b | Giorgos Korfiatis | pass
|
307 | 8dc7c13b | Giorgos Korfiatis | print 'PENDING are', serials |
308 | 8dc7c13b | Giorgos Korfiatis | return serials
|
309 | 8dc7c13b | Giorgos Korfiatis | |
310 | 8dc7c13b | Giorgos Korfiatis | def _actual_quantity(self, holding): |
311 | 8dc7c13b | Giorgos Korfiatis | hp = holding.policy |
312 | 8dc7c13b | Giorgos Korfiatis | return hp.quantity + (holding.imported + holding.returned -
|
313 | 8dc7c13b | Giorgos Korfiatis | holding.exported - holding.released) |
314 | 8dc7c13b | Giorgos Korfiatis | |
315 | 8dc7c13b | Giorgos Korfiatis | def _new_policy_name(self): |
316 | 8dc7c13b | Giorgos Korfiatis | return newname('policy_') |
317 | 8dc7c13b | Giorgos Korfiatis | |
318 | 8dc7c13b | Giorgos Korfiatis | def _increase_resource(self, entity, resource, amount): |
319 | 8dc7c13b | Giorgos Korfiatis | try:
|
320 | 8dc7c13b | Giorgos Korfiatis | h = Holding.objects.get(entity=entity, resource=resource) |
321 | 8dc7c13b | Giorgos Korfiatis | except Holding.DoesNotExist:
|
322 | 8dc7c13b | Giorgos Korfiatis | h = Holding(entity=entity, resource=resource) |
323 | 8dc7c13b | Giorgos Korfiatis | p = Policy.objects.create(policy=self._new_policy_name(),
|
324 | 8dc7c13b | Giorgos Korfiatis | quantity=0)
|
325 | 8dc7c13b | Giorgos Korfiatis | h.policy = p |
326 | 8dc7c13b | Giorgos Korfiatis | h.imported += amount |
327 | 8dc7c13b | Giorgos Korfiatis | h.save() |
328 | 8dc7c13b | Giorgos Korfiatis | |
329 | 8dc7c13b | Giorgos Korfiatis | def release_holding(self, context={}, release_holding=()): |
330 | 8dc7c13b | Giorgos Korfiatis | rejected = [] |
331 | 8dc7c13b | Giorgos Korfiatis | append = rejected.append |
332 | 8dc7c13b | Giorgos Korfiatis | |
333 | 8dc7c13b | Giorgos Korfiatis | for idx, (entity, resource, key) in enumerate(release_holding): |
334 | 8dc7c13b | Giorgos Korfiatis | try:
|
335 | 8dc7c13b | Giorgos Korfiatis | h = Holding.objects.get(entity=entity, resource=resource) |
336 | 8dc7c13b | Giorgos Korfiatis | except Holding.DoesNotExist:
|
337 | 8dc7c13b | Giorgos Korfiatis | append(idx) |
338 | 8dc7c13b | Giorgos Korfiatis | continue
|
339 | 8dc7c13b | Giorgos Korfiatis | |
340 | 8dc7c13b | Giorgos Korfiatis | if h.entity.key != key:
|
341 | 8dc7c13b | Giorgos Korfiatis | append(idx) |
342 | 8dc7c13b | Giorgos Korfiatis | continue
|
343 | 8dc7c13b | Giorgos Korfiatis | |
344 | 8dc7c13b | Giorgos Korfiatis | if self._check_pending(entity): |
345 | 8dc7c13b | Giorgos Korfiatis | append(idx) |
346 | 8dc7c13b | Giorgos Korfiatis | continue
|
347 | 8dc7c13b | Giorgos Korfiatis | |
348 | 8dc7c13b | Giorgos Korfiatis | q = self._actual_quantity(h)
|
349 | 8dc7c13b | Giorgos Korfiatis | owner = h.entity.owner |
350 | 8dc7c13b | Giorgos Korfiatis | self._increase_resource(owner, resource, q)
|
351 | 8dc7c13b | Giorgos Korfiatis | h.delete() |
352 | 8dc7c13b | Giorgos Korfiatis | |
353 | 8dc7c13b | Giorgos Korfiatis | return rejected
|
354 | 8dc7c13b | Giorgos Korfiatis | |
355 | fac1de87 | Georgios D. Tsoukalas | def list_resources(self, context={}, entity=None, key=None): |
356 | fac1de87 | Georgios D. Tsoukalas | try:
|
357 | 72c4abd2 | root | e = Entity.objects.get(entity=entity) |
358 | fac1de87 | Georgios D. Tsoukalas | except Entity.DoesNotExist:
|
359 | fac1de87 | Georgios D. Tsoukalas | m = "No such entity '%s'" % (entity,)
|
360 | fac1de87 | Georgios D. Tsoukalas | raise NoEntityError(m)
|
361 | fac1de87 | Georgios D. Tsoukalas | |
362 | fac1de87 | Georgios D. Tsoukalas | if e.key != key:
|
363 | fac1de87 | Georgios D. Tsoukalas | m = "Invalid key for entity '%s'" % (entity,)
|
364 | fac1de87 | Georgios D. Tsoukalas | raise InvalidKeyError(m)
|
365 | fac1de87 | Georgios D. Tsoukalas | |
366 | c8ef1fdb | Sofia Papagiannaki | holdings = e.holding_set.filter(entity=entity) |
367 | fac1de87 | Georgios D. Tsoukalas | resources = [h.resource for h in holdings] |
368 | fac1de87 | Georgios D. Tsoukalas | return resources
|
369 | fac1de87 | Georgios D. Tsoukalas | |
370 | fac1de87 | Georgios D. Tsoukalas | def get_quota(self, context={}, get_quota=()): |
371 | fac1de87 | Georgios D. Tsoukalas | quotas = [] |
372 | fac1de87 | Georgios D. Tsoukalas | append = quotas.append |
373 | fac1de87 | Georgios D. Tsoukalas | |
374 | fac1de87 | Georgios D. Tsoukalas | for entity, resource, key in get_quota: |
375 | fac1de87 | Georgios D. Tsoukalas | try:
|
376 | fac1de87 | Georgios D. Tsoukalas | h = Holding.objects.get(entity=entity, resource=resource) |
377 | fac1de87 | Georgios D. Tsoukalas | except Holding.DoesNotExist:
|
378 | fac1de87 | Georgios D. Tsoukalas | continue
|
379 | fac1de87 | Georgios D. Tsoukalas | |
380 | fac1de87 | Georgios D. Tsoukalas | if h.entity.key != key:
|
381 | fac1de87 | Georgios D. Tsoukalas | continue
|
382 | fac1de87 | Georgios D. Tsoukalas | |
383 | fac1de87 | Georgios D. Tsoukalas | p = h.policy |
384 | fac1de87 | Georgios D. Tsoukalas | |
385 | fac1de87 | Georgios D. Tsoukalas | append((h.entity.entity, h.resource, p.quantity, p.capacity, |
386 | fac1de87 | Georgios D. Tsoukalas | p.import_limit, p.export_limit, |
387 | b435047b | root | h.imported, h.exported, |
388 | d6662f65 | root | h.returned, h.released, |
389 | b435047b | root | h.flags)) |
390 | fac1de87 | Georgios D. Tsoukalas | |
391 | fac1de87 | Georgios D. Tsoukalas | return quotas
|
392 | fac1de87 | Georgios D. Tsoukalas | |
393 | fac1de87 | Georgios D. Tsoukalas | def set_quota(self, context={}, set_quota=()): |
394 | fac1de87 | Georgios D. Tsoukalas | rejected = [] |
395 | fac1de87 | Georgios D. Tsoukalas | append = rejected.append |
396 | fac1de87 | Georgios D. Tsoukalas | |
397 | fac1de87 | Georgios D. Tsoukalas | for ( entity, resource, key,
|
398 | fac1de87 | Georgios D. Tsoukalas | quantity, capacity, |
399 | fac1de87 | Georgios D. Tsoukalas | import_limit, export_limit, flags ) in set_quota:
|
400 | fac1de87 | Georgios D. Tsoukalas | |
401 | eec92c03 | Georgios D. Tsoukalas | p = None
|
402 | eec92c03 | Georgios D. Tsoukalas | |
403 | fac1de87 | Georgios D. Tsoukalas | try:
|
404 | fac1de87 | Georgios D. Tsoukalas | h = Holding.objects.get(entity=entity, resource=resource) |
405 | fac1de87 | Georgios D. Tsoukalas | if h.entity.key != key:
|
406 | fac1de87 | Georgios D. Tsoukalas | append((entity, resource)) |
407 | fac1de87 | Georgios D. Tsoukalas | continue
|
408 | eec92c03 | Georgios D. Tsoukalas | p = h.policy |
409 | eec92c03 | Georgios D. Tsoukalas | |
410 | fac1de87 | Georgios D. Tsoukalas | except Holding.DoesNotExist:
|
411 | eec92c03 | Georgios D. Tsoukalas | try:
|
412 | eec92c03 | Georgios D. Tsoukalas | e = Entity.objects.get(entity=entity) |
413 | eec92c03 | Georgios D. Tsoukalas | except Entity.DoesNotExist:
|
414 | eec92c03 | Georgios D. Tsoukalas | append((entity, resource)) |
415 | eec92c03 | Georgios D. Tsoukalas | continue
|
416 | fac1de87 | Georgios D. Tsoukalas | |
417 | eec92c03 | Georgios D. Tsoukalas | if e.key != key:
|
418 | eec92c03 | Georgios D. Tsoukalas | append((entity, resource)) |
419 | eec92c03 | Georgios D. Tsoukalas | continue
|
420 | eec92c03 | Georgios D. Tsoukalas | |
421 | eec92c03 | Georgios D. Tsoukalas | h = None
|
422 | eec92c03 | Georgios D. Tsoukalas | |
423 | eec92c03 | Georgios D. Tsoukalas | policy = newname('policy_')
|
424 | eec92c03 | Georgios D. Tsoukalas | newp = Policy ( |
425 | fac1de87 | Georgios D. Tsoukalas | policy=policy, |
426 | fac1de87 | Georgios D. Tsoukalas | quantity=quantity, |
427 | fac1de87 | Georgios D. Tsoukalas | capacity=capacity, |
428 | fac1de87 | Georgios D. Tsoukalas | import_limit=import_limit, |
429 | fac1de87 | Georgios D. Tsoukalas | export_limit=export_limit |
430 | fac1de87 | Georgios D. Tsoukalas | ) |
431 | fac1de87 | Georgios D. Tsoukalas | |
432 | eec92c03 | Georgios D. Tsoukalas | if h is None: |
433 | 443fa939 | Sofia Papagiannaki | h = Holding(entity=e, resource=resource, |
434 | eec92c03 | Georgios D. Tsoukalas | policy=newp, flags=flags) |
435 | eec92c03 | Georgios D. Tsoukalas | else:
|
436 | eec92c03 | Georgios D. Tsoukalas | h.policy = newp |
437 | eec92c03 | Georgios D. Tsoukalas | h.flags = flags |
438 | eec92c03 | Georgios D. Tsoukalas | |
439 | fac1de87 | Georgios D. Tsoukalas | h.save() |
440 | eec92c03 | Georgios D. Tsoukalas | newp.save() |
441 | fac1de87 | Georgios D. Tsoukalas | |
442 | c8ef1fdb | Sofia Papagiannaki | if p is not None and p.holding_set.count() == 0: |
443 | fac1de87 | Georgios D. Tsoukalas | p.delete() |
444 | fac1de87 | Georgios D. Tsoukalas | |
445 | fac1de87 | Georgios D. Tsoukalas | return rejected
|
446 | fac1de87 | Georgios D. Tsoukalas | |
447 | f590f930 | Georgios D. Tsoukalas | def issue_commission(self, context = {}, |
448 | f590f930 | Georgios D. Tsoukalas | clientkey = None,
|
449 | f590f930 | Georgios D. Tsoukalas | target = None,
|
450 | f590f930 | Georgios D. Tsoukalas | key = None,
|
451 | d6662f65 | root | name = None,
|
452 | f590f930 | Georgios D. Tsoukalas | provisions = () ): |
453 | fac1de87 | Georgios D. Tsoukalas | |
454 | fac1de87 | Georgios D. Tsoukalas | try:
|
455 | fac1de87 | Georgios D. Tsoukalas | t = Entity.objects.get(entity=target) |
456 | fac1de87 | Georgios D. Tsoukalas | except Entity.DoesNotExist:
|
457 | 7b1891cc | Giorgos Korfiatis | m = "No target entity '%s'" % (target,)
|
458 | 7b1891cc | Giorgos Korfiatis | raise NoEntityError(m)
|
459 | fac1de87 | Georgios D. Tsoukalas | else:
|
460 | fac1de87 | Georgios D. Tsoukalas | if t.key != key:
|
461 | fac1de87 | Georgios D. Tsoukalas | m = "Invalid key for target entity '%s'" % (target,)
|
462 | fac1de87 | Georgios D. Tsoukalas | raise InvalidKeyError(m)
|
463 | fac1de87 | Georgios D. Tsoukalas | |
464 | c3a709ad | root | create = Commission.objects.create |
465 | d6662f65 | root | commission = create(entity_id=target, clientkey=clientkey, name=name) |
466 | fac1de87 | Georgios D. Tsoukalas | serial = commission.serial |
467 | fac1de87 | Georgios D. Tsoukalas | |
468 | 6afede3f | Giorgos Korfiatis | checked = [] |
469 | fac1de87 | Georgios D. Tsoukalas | for entity, resource, quantity in provisions: |
470 | 6afede3f | Giorgos Korfiatis | |
471 | 6afede3f | Giorgos Korfiatis | ent_res = entity, resource |
472 | 6afede3f | Giorgos Korfiatis | if ent_res in checked: |
473 | 6afede3f | Giorgos Korfiatis | m = "Duplicate provision for %s.%s" % ent_res
|
474 | 6afede3f | Giorgos Korfiatis | raise DuplicateError(m)
|
475 | 6afede3f | Giorgos Korfiatis | checked.append(ent_res) |
476 | 6afede3f | Giorgos Korfiatis | |
477 | 58c077a9 | Giorgos Korfiatis | try:
|
478 | 58c077a9 | Giorgos Korfiatis | e = Entity.objects.get(entity=entity) |
479 | 58c077a9 | Giorgos Korfiatis | except Entity.DoesNotExist:
|
480 | 58c077a9 | Giorgos Korfiatis | m = "No source entity '%s'" % (entity,)
|
481 | 58c077a9 | Giorgos Korfiatis | raise NoEntityError(m)
|
482 | 58c077a9 | Giorgos Korfiatis | |
483 | c3a709ad | root | release = 0
|
484 | c3a709ad | root | if quantity < 0: |
485 | c3a709ad | root | release = 1
|
486 | c3a709ad | root | |
487 | fac1de87 | Georgios D. Tsoukalas | try:
|
488 | fac1de87 | Georgios D. Tsoukalas | h = Holding.objects.get(entity=entity, resource=resource) |
489 | fac1de87 | Georgios D. Tsoukalas | except Holding.DoesNotExist:
|
490 | fac1de87 | Georgios D. Tsoukalas | m = ("There is not enough quantity "
|
491 | fac1de87 | Georgios D. Tsoukalas | "to allocate from in %s.%s" % (entity, resource))
|
492 | fac1de87 | Georgios D. Tsoukalas | raise NoQuantityError(m)
|
493 | fac1de87 | Georgios D. Tsoukalas | |
494 | fac1de87 | Georgios D. Tsoukalas | hp = h.policy |
495 | fac1de87 | Georgios D. Tsoukalas | |
496 | 3a7732f5 | Georgios D. Tsoukalas | if (hp.export_limit is not None and |
497 | 3a7732f5 | Georgios D. Tsoukalas | h.exporting + quantity > hp.export_limit): |
498 | 3a7732f5 | Georgios D. Tsoukalas | m = ("Export limit reached for %s.%s" % (entity, resource))
|
499 | 3a7732f5 | Georgios D. Tsoukalas | raise ExportLimitError(m)
|
500 | 3a7732f5 | Georgios D. Tsoukalas | |
501 | 17d5c7a1 | root | if hp.quantity is not None: |
502 | d6662f65 | root | available = (+ hp.quantity + h.imported + h.returned |
503 | 17d5c7a1 | root | - h.exporting - h.releasing) |
504 | f590f930 | Georgios D. Tsoukalas | |
505 | 17d5c7a1 | root | if available - quantity < 0: |
506 | 17d5c7a1 | root | m = ("There is not enough quantity "
|
507 | 17d5c7a1 | root | "to allocate from in %s.%s" % (entity, resource))
|
508 | 17d5c7a1 | root | raise NoQuantityError(m)
|
509 | fac1de87 | Georgios D. Tsoukalas | |
510 | fac1de87 | Georgios D. Tsoukalas | try:
|
511 | fac1de87 | Georgios D. Tsoukalas | th = Holding.objects.get(entity=target, resource=resource) |
512 | fac1de87 | Georgios D. Tsoukalas | except Holding.DoesNotExist:
|
513 | fac1de87 | Georgios D. Tsoukalas | m = ("There is not enough capacity "
|
514 | fac1de87 | Georgios D. Tsoukalas | "to allocate into in %s.%s" % (target, resource))
|
515 | fac1de87 | Georgios D. Tsoukalas | raise NoCapacityError(m)
|
516 | fac1de87 | Georgios D. Tsoukalas | |
517 | fac1de87 | Georgios D. Tsoukalas | tp = th.policy |
518 | fac1de87 | Georgios D. Tsoukalas | |
519 | 3a7732f5 | Georgios D. Tsoukalas | if (tp.import_limit is not None and |
520 | 3a7732f5 | Georgios D. Tsoukalas | th.importing + quantity > tp.import_limit): |
521 | 3a7732f5 | Georgios D. Tsoukalas | m = ("Import limit reached for %s.%s" % (target, resource))
|
522 | 3a7732f5 | Georgios D. Tsoukalas | raise ImportLimitError(m)
|
523 | 3a7732f5 | Georgios D. Tsoukalas | |
524 | 17d5c7a1 | root | if tp.capacity is not None: |
525 | 17d5c7a1 | root | capacity = (+ tp.capacity + th.exported + th.released |
526 | d6662f65 | root | - th.importing - th.returning) |
527 | fac1de87 | Georgios D. Tsoukalas | |
528 | 17d5c7a1 | root | if capacity - quantity < 0: |
529 | 17d5c7a1 | root | m = ("There is not enough capacity "
|
530 | 17d5c7a1 | root | "to allocate into in %s.%s" % (target, resource))
|
531 | 17d5c7a1 | root | raise NoCapacityError(m)
|
532 | fac1de87 | Georgios D. Tsoukalas | |
533 | d6662f65 | root | Provision.objects.create( serial = commission, |
534 | 58c077a9 | Giorgos Korfiatis | entity = e, |
535 | d6662f65 | root | resource = resource, |
536 | d6662f65 | root | quantity = quantity ) |
537 | f590f930 | Georgios D. Tsoukalas | if release:
|
538 | d6662f65 | root | h.returning -= quantity |
539 | f590f930 | Georgios D. Tsoukalas | th.releasing -= quantity |
540 | f590f930 | Georgios D. Tsoukalas | else:
|
541 | f590f930 | Georgios D. Tsoukalas | h.exporting += quantity |
542 | f590f930 | Georgios D. Tsoukalas | th.importing += quantity |
543 | fac1de87 | Georgios D. Tsoukalas | |
544 | fac1de87 | Georgios D. Tsoukalas | h.save() |
545 | fac1de87 | Georgios D. Tsoukalas | th.save() |
546 | fac1de87 | Georgios D. Tsoukalas | |
547 | fac1de87 | Georgios D. Tsoukalas | return serial
|
548 | fac1de87 | Georgios D. Tsoukalas | |
549 | b435047b | root | def _log_provision(self, commission, s_holding, t_holding, |
550 | 1dedc367 | Georgios D. Tsoukalas | provision, log_time, reason): |
551 | 1dedc367 | Georgios D. Tsoukalas | |
552 | d6662f65 | root | s_entity = s_holding.entity |
553 | d6662f65 | root | s_policy = s_holding.policy |
554 | d6662f65 | root | t_entity = t_holding.entity |
555 | d6662f65 | root | t_policy = t_holding.policy |
556 | 1dedc367 | Georgios D. Tsoukalas | |
557 | 1dedc367 | Georgios D. Tsoukalas | ProvisionLog.objects.create( |
558 | b435047b | root | serial = commission.serial, |
559 | 0360f54b | root | name = commission.name, |
560 | d6662f65 | root | source = s_entity.entity, |
561 | d6662f65 | root | target = t_entity.entity, |
562 | 1dedc367 | Georgios D. Tsoukalas | resource = provision.resource, |
563 | d6662f65 | root | source_quantity = s_policy.quantity, |
564 | d6662f65 | root | source_capacity = s_policy.capacity, |
565 | d6662f65 | root | source_import_limit = s_policy.import_limit, |
566 | d6662f65 | root | source_export_limit = s_policy.export_limit, |
567 | d6662f65 | root | source_imported = s_holding.imported, |
568 | d6662f65 | root | source_exported = s_holding.exported, |
569 | d6662f65 | root | source_returned = s_holding.returned, |
570 | d6662f65 | root | source_released = s_holding.released, |
571 | d6662f65 | root | target_quantity = t_policy.quantity, |
572 | d6662f65 | root | target_capacity = t_policy.capacity, |
573 | d6662f65 | root | target_import_limit = t_policy.import_limit, |
574 | d6662f65 | root | target_export_limit = t_policy.export_limit, |
575 | d6662f65 | root | target_imported = t_holding.imported, |
576 | d6662f65 | root | target_exported = t_holding.exported, |
577 | d6662f65 | root | target_returned = t_holding.returned, |
578 | d6662f65 | root | target_released = t_holding.released, |
579 | 1dedc367 | Georgios D. Tsoukalas | delta_quantity = provision.quantity, |
580 | b435047b | root | issue_time = commission.issue_time, |
581 | 1dedc367 | Georgios D. Tsoukalas | log_time = log_time, |
582 | 1dedc367 | Georgios D. Tsoukalas | reason = reason) |
583 | 1dedc367 | Georgios D. Tsoukalas | |
584 | f590f930 | Georgios D. Tsoukalas | def accept_commission(self, context={}, clientkey=None, |
585 | fe6c61fb | root | serials=(), reason=''):
|
586 | b435047b | root | log_time = now() |
587 | b435047b | root | |
588 | fe6c61fb | root | for serial in serials: |
589 | fe6c61fb | root | try:
|
590 | fe6c61fb | root | c = Commission.objects.get(clientkey=clientkey, serial=serial) |
591 | fe6c61fb | root | except Commission.DoesNotExist:
|
592 | fe6c61fb | root | return
|
593 | fac1de87 | Georgios D. Tsoukalas | |
594 | fe6c61fb | root | t = c.entity |
595 | fac1de87 | Georgios D. Tsoukalas | |
596 | fe6c61fb | root | provisions = Provision.objects.filter(serial=serial) |
597 | fe6c61fb | root | for pv in provisions: |
598 | fe6c61fb | root | try:
|
599 | fe6c61fb | root | h = Holding.objects.get(entity=pv.entity.entity, |
600 | fe6c61fb | root | resource=pv.resource ) |
601 | fe6c61fb | root | th = Holding.objects.get(entity=t, resource=pv.resource) |
602 | fe6c61fb | root | except Holding.DoesNotExist:
|
603 | fe6c61fb | root | m = "Corrupted provision"
|
604 | fe6c61fb | root | raise CorruptedError(m)
|
605 | fac1de87 | Georgios D. Tsoukalas | |
606 | fe6c61fb | root | quantity = pv.quantity |
607 | fe6c61fb | root | release = 0
|
608 | fe6c61fb | root | if quantity < 0: |
609 | fe6c61fb | root | release = 1
|
610 | f590f930 | Georgios D. Tsoukalas | |
611 | fe6c61fb | root | if release:
|
612 | d6662f65 | root | h.returned -= quantity |
613 | fe6c61fb | root | th.released -= quantity |
614 | fe6c61fb | root | else:
|
615 | fe6c61fb | root | h.exported += quantity |
616 | fe6c61fb | root | th.imported += quantity |
617 | f590f930 | Georgios D. Tsoukalas | |
618 | fe6c61fb | root | reason = 'ACCEPT:' + reason[-121:] |
619 | b435047b | root | self._log_provision(c, h, th, pv, log_time, reason)
|
620 | fe6c61fb | root | h.save() |
621 | fe6c61fb | root | th.save() |
622 | fe6c61fb | root | pv.delete() |
623 | 7661c43c | Christos Stavrakakis | c.delete() |
624 | fac1de87 | Georgios D. Tsoukalas | |
625 | fac1de87 | Georgios D. Tsoukalas | return
|
626 | fac1de87 | Georgios D. Tsoukalas | |
627 | f590f930 | Georgios D. Tsoukalas | def reject_commission(self, context={}, clientkey=None, |
628 | fe6c61fb | root | serials=(), reason=''):
|
629 | b435047b | root | log_time = now() |
630 | b435047b | root | |
631 | fe6c61fb | root | for serial in serials: |
632 | fe6c61fb | root | try:
|
633 | fe6c61fb | root | c = Commission.objects.get(clientkey=clientkey, serial=serial) |
634 | fe6c61fb | root | except Commission.DoesNotExist:
|
635 | fe6c61fb | root | return
|
636 | fac1de87 | Georgios D. Tsoukalas | |
637 | fe6c61fb | root | t = c.entity |
638 | fac1de87 | Georgios D. Tsoukalas | |
639 | fe6c61fb | root | provisions = Provision.objects.filter(serial=serial) |
640 | fe6c61fb | root | for pv in provisions: |
641 | fe6c61fb | root | try:
|
642 | fe6c61fb | root | h = Holding.objects.get(entity=pv.entity.entity, |
643 | fe6c61fb | root | resource=pv.resource) |
644 | fe6c61fb | root | th = Holding.objects.get(entity=t, resource=pv.resource) |
645 | fe6c61fb | root | except Holding.DoesNotExist:
|
646 | fe6c61fb | root | m = "Corrupted provision"
|
647 | fe6c61fb | root | raise CorruptedError(m)
|
648 | fac1de87 | Georgios D. Tsoukalas | |
649 | fe6c61fb | root | quantity = pv.quantity |
650 | fe6c61fb | root | release = 0
|
651 | fe6c61fb | root | if quantity < 0: |
652 | fe6c61fb | root | release = 1
|
653 | f590f930 | Georgios D. Tsoukalas | |
654 | fe6c61fb | root | if release:
|
655 | d6662f65 | root | h.returning += quantity |
656 | fe6c61fb | root | th.releasing += quantity |
657 | fe6c61fb | root | else:
|
658 | fe6c61fb | root | h.exporting -= quantity |
659 | fe6c61fb | root | th.importing -= quantity |
660 | fe6c61fb | root | |
661 | fe6c61fb | root | reason = 'REJECT:' + reason[-121:] |
662 | b435047b | root | self._log_provision(c, h, th, pv, log_time, reason)
|
663 | fe6c61fb | root | h.save() |
664 | fe6c61fb | root | th.save() |
665 | fe6c61fb | root | pv.delete() |
666 | 7661c43c | Christos Stavrakakis | c.delete() |
667 | fac1de87 | Georgios D. Tsoukalas | |
668 | fac1de87 | Georgios D. Tsoukalas | return
|
669 | fac1de87 | Georgios D. Tsoukalas | |
670 | fac1de87 | Georgios D. Tsoukalas | def get_pending_commissions(self, context={}, clientkey=None): |
671 | 4d516a3b | Christos Stavrakakis | pending = Commission.objects.filter(clientkey=clientkey)\ |
672 | 4d516a3b | Christos Stavrakakis | .values_list('serial', flat=True) |
673 | fac1de87 | Georgios D. Tsoukalas | return pending
|
674 | fac1de87 | Georgios D. Tsoukalas | |
675 | fac1de87 | Georgios D. Tsoukalas | def resolve_pending_commissions(self, context={}, clientkey=None, |
676 | fac1de87 | Georgios D. Tsoukalas | max_serial=None, accept_set=() ):
|
677 | fac1de87 | Georgios D. Tsoukalas | accept_set = set(accept_set)
|
678 | ddf2e911 | Giorgos Korfiatis | pending = self.get_pending_commissions(context=context, clientkey=clientkey)
|
679 | fac1de87 | Georgios D. Tsoukalas | pending = sorted(pending)
|
680 | fac1de87 | Georgios D. Tsoukalas | |
681 | fac1de87 | Georgios D. Tsoukalas | accept = self.accept_commission
|
682 | fac1de87 | Georgios D. Tsoukalas | reject = self.reject_commission
|
683 | fac1de87 | Georgios D. Tsoukalas | |
684 | fac1de87 | Georgios D. Tsoukalas | for serial in pending: |
685 | fac1de87 | Georgios D. Tsoukalas | if serial > max_serial:
|
686 | fac1de87 | Georgios D. Tsoukalas | break
|
687 | fac1de87 | Georgios D. Tsoukalas | |
688 | fac1de87 | Georgios D. Tsoukalas | if serial in accept_set: |
689 | ddf2e911 | Giorgos Korfiatis | accept(context=context, clientkey=clientkey, serials=[serial]) |
690 | fac1de87 | Georgios D. Tsoukalas | else:
|
691 | ddf2e911 | Giorgos Korfiatis | reject(context=context, clientkey=clientkey, serials=[serial]) |
692 | fac1de87 | Georgios D. Tsoukalas | |
693 | fac1de87 | Georgios D. Tsoukalas | return
|
694 | fac1de87 | Georgios D. Tsoukalas | |
695 | fac1de87 | Georgios D. Tsoukalas | def release_entity(self, context={}, release_entity=()): |
696 | fac1de87 | Georgios D. Tsoukalas | rejected = [] |
697 | fac1de87 | Georgios D. Tsoukalas | append = rejected.append |
698 | fac1de87 | Georgios D. Tsoukalas | for entity, key in release_entity: |
699 | fac1de87 | Georgios D. Tsoukalas | try:
|
700 | fac1de87 | Georgios D. Tsoukalas | e = Entity.objects.get(entity=entity, key=key) |
701 | fac1de87 | Georgios D. Tsoukalas | except Entity.DoesNotExist:
|
702 | fac1de87 | Georgios D. Tsoukalas | append(entity) |
703 | fac1de87 | Georgios D. Tsoukalas | continue
|
704 | fac1de87 | Georgios D. Tsoukalas | |
705 | fac1de87 | Georgios D. Tsoukalas | if e.entities.count() != 0: |
706 | fac1de87 | Georgios D. Tsoukalas | append(entity) |
707 | fac1de87 | Georgios D. Tsoukalas | continue
|
708 | fac1de87 | Georgios D. Tsoukalas | |
709 | 492a783b | Georgios D. Tsoukalas | if e.holding_set.count() != 0: |
710 | fac1de87 | Georgios D. Tsoukalas | append(entity) |
711 | fac1de87 | Georgios D. Tsoukalas | continue
|
712 | fac1de87 | Georgios D. Tsoukalas | |
713 | fac1de87 | Georgios D. Tsoukalas | e.delete() |
714 | fac1de87 | Georgios D. Tsoukalas | |
715 | fac1de87 | Georgios D. Tsoukalas | return rejected
|
716 | fac1de87 | Georgios D. Tsoukalas | |
717 | 0360f54b | root | def get_timeline(self, context={}, after="", before="Z", get_timeline=()): |
718 | f590f930 | Georgios D. Tsoukalas | entity_set = set()
|
719 | 0360f54b | root | e_add = entity_set.add |
720 | 0360f54b | root | resource_set = set()
|
721 | 0360f54b | root | r_add = resource_set.add |
722 | f590f930 | Georgios D. Tsoukalas | |
723 | 0360f54b | root | for entity, resource, key in get_timeline: |
724 | 0360f54b | root | if entity not in entity_set: |
725 | 0360f54b | root | try:
|
726 | 0360f54b | root | e = Entity.objects.get(entity=entity, key=key) |
727 | 0360f54b | root | e_add(entity) |
728 | 0360f54b | root | except Entity.DoesNotExist:
|
729 | 0360f54b | root | continue
|
730 | 0360f54b | root | |
731 | 0360f54b | root | r_add((entity, resource)) |
732 | f590f930 | Georgios D. Tsoukalas | |
733 | f590f930 | Georgios D. Tsoukalas | chunk_size = 65536
|
734 | f590f930 | Georgios D. Tsoukalas | nr = 0
|
735 | f590f930 | Georgios D. Tsoukalas | timeline = [] |
736 | 0360f54b | root | append = timeline.append |
737 | 1dedc367 | Georgios D. Tsoukalas | filterlogs = ProvisionLog.objects.filter |
738 | 1dedc367 | Georgios D. Tsoukalas | if entity_set:
|
739 | f590f930 | Georgios D. Tsoukalas | q_entity = Q(source__in = entity_set) | Q(target__in = entity_set) |
740 | 1dedc367 | Georgios D. Tsoukalas | else:
|
741 | 1dedc367 | Georgios D. Tsoukalas | q_entity = Q() |
742 | 1dedc367 | Georgios D. Tsoukalas | |
743 | 1dedc367 | Georgios D. Tsoukalas | while 1: |
744 | 1dedc367 | Georgios D. Tsoukalas | logs = filterlogs( q_entity, |
745 | 1dedc367 | Georgios D. Tsoukalas | issue_time__gt = after, |
746 | f590f930 | Georgios D. Tsoukalas | issue_time__lte = before, |
747 | 1dedc367 | Georgios D. Tsoukalas | reason__startswith = 'ACCEPT:' )
|
748 | f590f930 | Georgios D. Tsoukalas | |
749 | f590f930 | Georgios D. Tsoukalas | logs = logs.order_by('issue_time')
|
750 | d6662f65 | root | #logs = logs.values()
|
751 | f590f930 | Georgios D. Tsoukalas | logs = logs[:chunk_size] |
752 | f590f930 | Georgios D. Tsoukalas | nr += len(logs)
|
753 | f590f930 | Georgios D. Tsoukalas | if not logs: |
754 | f590f930 | Georgios D. Tsoukalas | break
|
755 | 0360f54b | root | for g in logs: |
756 | 0360f54b | root | if ((g.source, g.resource) not in resource_set |
757 | 0360f54b | root | or (g.target, g.resource) not in resource_set): |
758 | 0360f54b | root | continue
|
759 | 0360f54b | root | |
760 | d6662f65 | root | o = { |
761 | d6662f65 | root | 'serial' : g.serial,
|
762 | d6662f65 | root | 'source' : g.source,
|
763 | d6662f65 | root | 'target' : g.target,
|
764 | d6662f65 | root | 'resource' : g.resource,
|
765 | d6662f65 | root | 'name' : g.name,
|
766 | d6662f65 | root | 'quantity' : g.delta_quantity,
|
767 | d6662f65 | root | 'source_allocated' : g.source_allocated(),
|
768 | d6662f65 | root | 'source_allocated_through' : g.source_allocated_through(),
|
769 | d6662f65 | root | 'source_inbound' : g.source_inbound(),
|
770 | d6662f65 | root | 'source_inbound_through' : g.source_inbound_through(),
|
771 | d6662f65 | root | 'source_outbound' : g.source_outbound(),
|
772 | b82eb8c4 | root | 'source_outbound_through' : g.source_outbound_through(),
|
773 | d6662f65 | root | 'target_allocated' : g.target_allocated(),
|
774 | d6662f65 | root | 'target_allocated_through' : g.target_allocated_through(),
|
775 | d6662f65 | root | 'target_inbound' : g.target_inbound(),
|
776 | d6662f65 | root | 'target_inbound_through' : g.target_inbound_through(),
|
777 | d6662f65 | root | 'target_outbound' : g.target_outbound(),
|
778 | d6662f65 | root | 'target_outbound_through' : g.target_outbound_through(),
|
779 | d6662f65 | root | 'issue_time' : g.issue_time,
|
780 | d6662f65 | root | 'log_time' : g.log_time,
|
781 | d6662f65 | root | 'reason' : g.reason,
|
782 | d6662f65 | root | } |
783 | d36dcd3d | Christos KK Loverdos | |
784 | 0360f54b | root | append(o) |
785 | d6662f65 | root | |
786 | d6662f65 | root | after = g.issue_time |
787 | f590f930 | Georgios D. Tsoukalas | if after >= before:
|
788 | f590f930 | Georgios D. Tsoukalas | break
|
789 | f590f930 | Georgios D. Tsoukalas | |
790 | f590f930 | Georgios D. Tsoukalas | return timeline
|
791 | f590f930 | Georgios D. Tsoukalas | |
792 | f590f930 | Georgios D. Tsoukalas | |
793 | fac1de87 | Georgios D. Tsoukalas | API_Callpoint = QuotaholderDjangoDBCallpoint |