root / snf-quotaholder-app / quotaholder_django / quotaholder_app / models.py @ 140513a9
History | View | Annotate | Download (6.6 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 |
|
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 |
|
49 |
class Entity(Model): |
50 |
|
51 |
entity = CharField(max_length=4096, primary_key=True) |
52 |
owner = ForeignKey('self', to_field='entity', |
53 |
related_name='entities')
|
54 |
key = CharField(max_length=4096, null=False) |
55 |
|
56 |
|
57 |
class Policy(Model): |
58 |
|
59 |
policy = CharField(max_length=4096, primary_key=True) |
60 |
quantity = BigIntegerField(null=True, default=None) |
61 |
capacity = BigIntegerField(null=True, default=None) |
62 |
import_limit = BigIntegerField(null=True, default=None) |
63 |
export_limit = BigIntegerField(null=True, default=None) |
64 |
|
65 |
|
66 |
class Holding(Model): |
67 |
|
68 |
entity = ForeignKey(Entity, to_field='entity')
|
69 |
resource = CharField(max_length=4096, null=False) |
70 |
|
71 |
policy = ForeignKey(Policy, to_field='policy')
|
72 |
flags = BigIntegerField(null=False, default=0) |
73 |
|
74 |
imported = BigIntegerField(null=False, default=0) |
75 |
importing = BigIntegerField(null=False, default=0) |
76 |
exported = BigIntegerField(null=False, default=0) |
77 |
exporting = BigIntegerField(null=False, default=0) |
78 |
returned = BigIntegerField(null=False, default=0) |
79 |
returning = BigIntegerField(null=False, default=0) |
80 |
released = BigIntegerField(null=False, default=0) |
81 |
releasing = BigIntegerField(null=False, default=0) |
82 |
|
83 |
class Meta: |
84 |
unique_together = (('entity', 'resource'),) |
85 |
|
86 |
|
87 |
from datetime import datetime |
88 |
|
89 |
def now(): |
90 |
return datetime.utcnow().strftime('%Y-%m-%dT%H:%M:%S.%f')[:24] |
91 |
|
92 |
|
93 |
class Commission(Model): |
94 |
|
95 |
serial = AutoField(primary_key=True)
|
96 |
entity = ForeignKey(Entity, to_field='entity')
|
97 |
name = CharField(max_length=4096, null=True) |
98 |
clientkey = CharField(max_length=4096, null=False) |
99 |
issue_time = CharField(max_length=24, default=now)
|
100 |
|
101 |
|
102 |
class Provision(Model): |
103 |
|
104 |
serial = ForeignKey( Commission, |
105 |
to_field='serial',
|
106 |
related_name='provisions' )
|
107 |
|
108 |
entity = ForeignKey(Entity, to_field='entity')
|
109 |
resource = CharField(max_length=4096, null=False) |
110 |
quantity = BigIntegerField(null=False)
|
111 |
|
112 |
|
113 |
class ProvisionLog(Model): |
114 |
|
115 |
serial = BigIntegerField() |
116 |
source = CharField(max_length=4096)
|
117 |
target = CharField(max_length=4096)
|
118 |
name = CharField(max_length=4096)
|
119 |
issue_time = CharField(max_length=4096)
|
120 |
log_time = CharField(max_length=4096)
|
121 |
resource = CharField(max_length=4096)
|
122 |
source_quantity = BigIntegerField(null=True)
|
123 |
source_capacity = BigIntegerField(null=True)
|
124 |
source_import_limit = BigIntegerField(null=True)
|
125 |
source_export_limit = BigIntegerField(null=True)
|
126 |
source_imported = BigIntegerField(null=False)
|
127 |
source_exported = BigIntegerField(null=False)
|
128 |
source_returned = BigIntegerField(null=False)
|
129 |
source_released = BigIntegerField(null=False)
|
130 |
target_quantity = BigIntegerField(null=True)
|
131 |
target_capacity = BigIntegerField(null=True)
|
132 |
target_import_limit = BigIntegerField(null=True)
|
133 |
target_export_limit = BigIntegerField(null=True)
|
134 |
target_imported = BigIntegerField(null=False)
|
135 |
target_exported = BigIntegerField(null=False)
|
136 |
target_returned = BigIntegerField(null=False)
|
137 |
target_released = BigIntegerField(null=False)
|
138 |
delta_quantity = BigIntegerField(null=False)
|
139 |
reason = CharField(max_length=4096)
|
140 |
|
141 |
|
142 |
def source_allocated_through(self): |
143 |
return self.source_imported - self.source_released |
144 |
|
145 |
def source_allocated(self): |
146 |
return (+ self.source_allocated_through() |
147 |
- self.source_exported
|
148 |
+ self.source_returned)
|
149 |
|
150 |
def source_inbound_through(self): |
151 |
return self.source_imported |
152 |
|
153 |
def source_inbound(self): |
154 |
return self.source_inbound_through() + self.source_returned |
155 |
|
156 |
def source_outbound_through(self): |
157 |
return self.source_released |
158 |
|
159 |
def source_outbound(self): |
160 |
return self.source_outbound_through() + self.source_exported |
161 |
|
162 |
def target_allocated_through(self): |
163 |
return self.target_imported - self.target_released |
164 |
|
165 |
def target_allocated(self): |
166 |
return (+ self.target_allocated_through() |
167 |
- self.target_exported
|
168 |
+ self.target_returned)
|
169 |
|
170 |
def target_inbound_through(self): |
171 |
return self.target_imported |
172 |
|
173 |
def target_inbound(self): |
174 |
return self.target_inbound_through() + self.target_returned |
175 |
|
176 |
def target_outbound_through(self): |
177 |
return self.target_released |
178 |
|
179 |
def target_outbound(self): |
180 |
return self.target_outbound_through() + self.target_exported |
181 |
|