Statistics
| Branch: | Tag: | Revision:

root / snf-astakos-app / astakos / quotaholder / models.py @ ccdb7c02

History | View | Annotate | Download (7.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.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 synnefo.lib.db.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 _get(*args, **kwargs):
203
    model = args[0]
204
    args = args[1:]
205
    o = model.objects
206

    
207
    for_update = kwargs.pop('for_update', False)
208
    f = o.get_for_update if for_update else o.get
209
    return f(*args, **kwargs)
210

    
211

    
212
def _filter(*args, **kwargs):
213
    model = args[0]
214
    args = args[1:]
215
    o = model.objects
216

    
217
    for_update = kwargs.pop('for_update', False)
218
    q = o.filter(*args, **kwargs)
219
    q = q.select_for_update() if for_update else q
220
    return q
221

    
222

    
223
def db_get_holding(*args, **kwargs):
224
    return _get(Holding, *args, **kwargs)
225

    
226
def db_get_entity(*args, **kwargs):
227
    return _get(Entity, *args, **kwargs)
228

    
229
def db_get_policy(*args, **kwargs):
230
    return _get(Policy, *args, **kwargs)
231

    
232
def db_get_commission(*args, **kwargs):
233
    return _get(Commission, *args, **kwargs)
234

    
235
def db_get_callserial(*args, **kwargs):
236
    return _get(CallSerial, *args, **kwargs)
237

    
238
def db_filter_provision(*args, **kwargs):
239
    return _filter(Provision, *args, **kwargs)