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