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