root / snf-quotaholder-app / quotaholder_django / quotaholder_app / models.py @ 5d996aea
History | View | Annotate | Download (7.8 kB)
1 |
# Copyright 2012 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 |
|
35 |
from synnefo.lib.commissioning import CorruptedError |
36 |
|
37 |
from django.db.models import (Model, BigIntegerField, CharField, |
38 |
ForeignKey, AutoField) |
39 |
from django.db import transaction |
40 |
from .managers import ForUpdateManager |
41 |
|
42 |
class Holder(Model): |
43 |
|
44 |
attribute = CharField(max_length=4096, primary_key=True) |
45 |
intval = BigIntegerField() |
46 |
strval = CharField(max_length=4096)
|
47 |
|
48 |
objects = ForUpdateManager() |
49 |
|
50 |
class Entity(Model): |
51 |
|
52 |
entity = CharField(max_length=4096, primary_key=True) |
53 |
owner = ForeignKey('self', to_field='entity', |
54 |
related_name='entities')
|
55 |
key = CharField(max_length=4096, null=False) |
56 |
|
57 |
objects = ForUpdateManager() |
58 |
|
59 |
class Policy(Model): |
60 |
|
61 |
policy = CharField(max_length=4096, primary_key=True) |
62 |
quantity = BigIntegerField(null=True, default=None) |
63 |
capacity = BigIntegerField(null=True, default=None) |
64 |
import_limit = BigIntegerField(null=True, default=None) |
65 |
export_limit = BigIntegerField(null=True, default=None) |
66 |
|
67 |
objects = ForUpdateManager() |
68 |
|
69 |
class Holding(Model): |
70 |
|
71 |
entity = ForeignKey(Entity, to_field='entity')
|
72 |
resource = CharField(max_length=4096, null=False) |
73 |
|
74 |
policy = ForeignKey(Policy, to_field='policy')
|
75 |
flags = BigIntegerField(null=False, default=0) |
76 |
|
77 |
imported = BigIntegerField(null=False, default=0) |
78 |
importing = BigIntegerField(null=False, default=0) |
79 |
exported = BigIntegerField(null=False, default=0) |
80 |
exporting = BigIntegerField(null=False, default=0) |
81 |
returned = BigIntegerField(null=False, default=0) |
82 |
returning = BigIntegerField(null=False, default=0) |
83 |
released = BigIntegerField(null=False, default=0) |
84 |
releasing = BigIntegerField(null=False, default=0) |
85 |
|
86 |
objects = ForUpdateManager() |
87 |
|
88 |
class Meta: |
89 |
unique_together = (('entity', 'resource'),) |
90 |
|
91 |
|
92 |
from datetime import datetime |
93 |
|
94 |
def now(): |
95 |
return datetime.utcnow().strftime('%Y-%m-%dT%H:%M:%S.%f')[:24] |
96 |
|
97 |
|
98 |
class Commission(Model): |
99 |
|
100 |
serial = AutoField(primary_key=True)
|
101 |
entity = ForeignKey(Entity, to_field='entity')
|
102 |
name = CharField(max_length=4096, null=True) |
103 |
clientkey = CharField(max_length=4096, null=False) |
104 |
issue_time = CharField(max_length=24, default=now)
|
105 |
|
106 |
objects = ForUpdateManager() |
107 |
|
108 |
class Provision(Model): |
109 |
|
110 |
serial = ForeignKey( Commission, |
111 |
to_field='serial',
|
112 |
related_name='provisions' )
|
113 |
|
114 |
entity = ForeignKey(Entity, to_field='entity')
|
115 |
resource = CharField(max_length=4096, null=False) |
116 |
quantity = BigIntegerField(null=False)
|
117 |
|
118 |
objects = ForUpdateManager() |
119 |
|
120 |
class ProvisionLog(Model): |
121 |
|
122 |
serial = BigIntegerField() |
123 |
source = CharField(max_length=4096)
|
124 |
target = CharField(max_length=4096)
|
125 |
name = CharField(max_length=4096)
|
126 |
issue_time = CharField(max_length=4096)
|
127 |
log_time = CharField(max_length=4096)
|
128 |
resource = CharField(max_length=4096)
|
129 |
source_quantity = BigIntegerField(null=True)
|
130 |
source_capacity = BigIntegerField(null=True)
|
131 |
source_import_limit = BigIntegerField(null=True)
|
132 |
source_export_limit = BigIntegerField(null=True)
|
133 |
source_imported = BigIntegerField(null=False)
|
134 |
source_exported = BigIntegerField(null=False)
|
135 |
source_returned = BigIntegerField(null=False)
|
136 |
source_released = BigIntegerField(null=False)
|
137 |
target_quantity = BigIntegerField(null=True)
|
138 |
target_capacity = BigIntegerField(null=True)
|
139 |
target_import_limit = BigIntegerField(null=True)
|
140 |
target_export_limit = BigIntegerField(null=True)
|
141 |
target_imported = BigIntegerField(null=False)
|
142 |
target_exported = BigIntegerField(null=False)
|
143 |
target_returned = BigIntegerField(null=False)
|
144 |
target_released = BigIntegerField(null=False)
|
145 |
delta_quantity = BigIntegerField(null=False)
|
146 |
reason = CharField(max_length=4096)
|
147 |
|
148 |
objects = ForUpdateManager() |
149 |
|
150 |
def source_allocated_through(self): |
151 |
return self.source_imported - self.source_released |
152 |
|
153 |
def source_allocated(self): |
154 |
return (+ self.source_allocated_through() |
155 |
- self.source_exported
|
156 |
+ self.source_returned)
|
157 |
|
158 |
def source_inbound_through(self): |
159 |
return self.source_imported |
160 |
|
161 |
def source_inbound(self): |
162 |
return self.source_inbound_through() + self.source_returned |
163 |
|
164 |
def source_outbound_through(self): |
165 |
return self.source_released |
166 |
|
167 |
def source_outbound(self): |
168 |
return self.source_outbound_through() + self.source_exported |
169 |
|
170 |
def target_allocated_through(self): |
171 |
return self.target_imported - self.target_released |
172 |
|
173 |
def target_allocated(self): |
174 |
return (+ self.target_allocated_through() |
175 |
- self.target_exported
|
176 |
+ self.target_returned)
|
177 |
|
178 |
def target_inbound_through(self): |
179 |
return self.target_imported |
180 |
|
181 |
def target_inbound(self): |
182 |
return self.target_inbound_through() + self.target_returned |
183 |
|
184 |
def target_outbound_through(self): |
185 |
return self.target_released |
186 |
|
187 |
def target_outbound(self): |
188 |
return self.target_outbound_through() + self.target_exported |
189 |
|
190 |
def _access(*args, **kwargs): |
191 |
method = args[0]
|
192 |
model = args[1]
|
193 |
args = args[2:]
|
194 |
o = model.objects |
195 |
try:
|
196 |
if kwargs['for_update']: |
197 |
del kwargs['for_update'] |
198 |
o = o.select_for_update() |
199 |
except KeyError: |
200 |
pass
|
201 |
f = getattr(o, method)
|
202 |
return f(*args, **kwargs)
|
203 |
|
204 |
def _get(*args, **kwargs): |
205 |
return _access('get', *args, **kwargs) |
206 |
|
207 |
def _filter(*args, **kwargs): |
208 |
return _access('filter', *args, **kwargs) |
209 |
|
210 |
def db_get_holding(*args, **kwargs): |
211 |
return _get(Holding, *args, **kwargs)
|
212 |
|
213 |
def db_get_entity(*args, **kwargs): |
214 |
return _get(Entity, *args, **kwargs)
|
215 |
|
216 |
def db_get_policy(*args, **kwargs): |
217 |
return _get(Policy, *args, **kwargs)
|
218 |
|
219 |
def db_get_commission(*args, **kwargs): |
220 |
return _get(Commission, *args, **kwargs)
|
221 |
|
222 |
def db_filter_provision(*args, **kwargs): |
223 |
return _filter(Provision, *args, **kwargs)
|