Statistics
| Branch: | Tag: | Revision:

root / snf-astakos-app / astakos / quotaholder / models.py @ 3adbfafa

History | View | Annotate | Download (6.6 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

    
48
    objects     =   ForUpdateManager()
49

    
50
class Holding(Model):
51

    
52
    holder      =   CharField(max_length=4096, db_index=True)
53
    resource    =   CharField(max_length=4096, null=False)
54

    
55
    policy      =   ForeignKey(Policy, to_field='policy')
56
    flags       =   BigIntegerField(null=False, default=0)
57

    
58
    imported    =   intDecimalField(default=0)
59
    importing   =   intDecimalField(default=0)
60
    exported    =   intDecimalField(default=0)
61
    exporting   =   intDecimalField(default=0)
62
    returned    =   intDecimalField(default=0)
63
    returning   =   intDecimalField(default=0)
64
    released    =   intDecimalField(default=0)
65
    releasing   =   intDecimalField(default=0)
66

    
67
    objects     =   ForUpdateManager()
68

    
69
    class Meta:
70
        unique_together = (('holder', 'resource'),)
71

    
72

    
73
from datetime import datetime
74

    
75
def now():
76
    return datetime.utcnow().strftime('%Y-%m-%dT%H:%M:%S.%f')[:24]
77

    
78

    
79
class Commission(Model):
80

    
81
    serial      =   AutoField(primary_key=True)
82
    holder      =   CharField(max_length=4096, db_index=True)
83
    name        =   CharField(max_length=4096, null=True)
84
    clientkey   =   CharField(max_length=4096, null=False)
85
    issue_time  =   CharField(max_length=24, default=now)
86

    
87
    objects     =   ForUpdateManager()
88

    
89
class Provision(Model):
90

    
91
    serial      =   ForeignKey( Commission,
92
                                to_field='serial',
93
                                related_name='provisions'   )
94

    
95
    holder      =   CharField(max_length=4096, db_index=True)
96
    resource    =   CharField(max_length=4096, null=False)
97
    quantity    =   intDecimalField()
98

    
99
    objects     =   ForUpdateManager()
100

    
101
class ProvisionLog(Model):
102

    
103
    serial              =   BigIntegerField()
104
    source              =   CharField(max_length=4096)
105
    target              =   CharField(max_length=4096)
106
    name                =   CharField(max_length=4096)
107
    issue_time          =   CharField(max_length=4096)
108
    log_time            =   CharField(max_length=4096)
109
    resource            =   CharField(max_length=4096)
110
    source_quantity     =   intDecimalField()
111
    source_capacity     =   intDecimalField()
112
    source_imported     =   intDecimalField()
113
    source_exported     =   intDecimalField()
114
    source_returned     =   intDecimalField()
115
    source_released     =   intDecimalField()
116
    target_quantity     =   intDecimalField()
117
    target_capacity     =   intDecimalField()
118
    target_imported     =   intDecimalField()
119
    target_exported     =   intDecimalField()
120
    target_returned     =   intDecimalField()
121
    target_released     =   intDecimalField()
122
    delta_quantity      =   intDecimalField()
123
    reason              =   CharField(max_length=4096)
124

    
125
    objects     =   ForUpdateManager()
126

    
127
    def source_allocated_through(self):
128
        return self.source_imported - self.source_released
129

    
130
    def source_allocated(self):
131
        return (+ self.source_allocated_through()
132
                - self.source_exported
133
                + self.source_returned)
134

    
135
    def source_inbound_through(self):
136
        return self.source_imported
137

    
138
    def source_inbound(self):
139
        return self.source_inbound_through() + self.source_returned
140

    
141
    def source_outbound_through(self):
142
        return self.source_released
143

    
144
    def source_outbound(self):
145
        return self.source_outbound_through() + self.source_exported
146

    
147
    def target_allocated_through(self):
148
        return self.target_imported - self.target_released
149

    
150
    def target_allocated(self):
151
        return (+ self.target_allocated_through()
152
                - self.target_exported
153
                + self.target_returned)
154

    
155
    def target_inbound_through(self):
156
        return self.target_imported
157

    
158
    def target_inbound(self):
159
        return self.target_inbound_through() + self.target_returned
160

    
161
    def target_outbound_through(self):
162
        return self.target_released
163

    
164
    def target_outbound(self):
165
        return self.target_outbound_through() + self.target_exported
166

    
167

    
168
def _get(*args, **kwargs):
169
    model = args[0]
170
    args = args[1:]
171
    o = model.objects
172

    
173
    for_update = kwargs.pop('for_update', False)
174
    f = o.get_for_update if for_update else o.get
175
    return f(*args, **kwargs)
176

    
177

    
178
def _filter(*args, **kwargs):
179
    model = args[0]
180
    args = args[1:]
181
    o = model.objects
182

    
183
    for_update = kwargs.pop('for_update', False)
184
    q = o.filter(*args, **kwargs)
185
    q = q.select_for_update() if for_update else q
186
    return q
187

    
188

    
189
def db_get_holding(*args, **kwargs):
190
    return _get(Holding, *args, **kwargs)
191

    
192
def db_get_policy(*args, **kwargs):
193
    return _get(Policy, *args, **kwargs)
194

    
195
def db_get_commission(*args, **kwargs):
196
    return _get(Commission, *args, **kwargs)
197

    
198
def db_filter_provision(*args, **kwargs):
199
    return _filter(Provision, *args, **kwargs)