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