Statistics
| Branch: | Tag: | Revision:

root / snf-astakos-app / astakos / quotaholder / models.py @ 04dcc30e

History | View | Annotate | Download (4.9 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 Holding(Model):
43

    
44
    holder      =   CharField(max_length=4096, db_index=True)
45
    resource    =   CharField(max_length=4096, null=False)
46

    
47
    capacity    =   intDecimalField()
48
    flags       =   BigIntegerField(null=False, default=0)
49

    
50
    imported_min    =   intDecimalField(default=0)
51
    imported_max    =   intDecimalField(default=0)
52
    stock_min       =   intDecimalField(default=0)
53
    stock_max       =   intDecimalField(default=0)
54

    
55
    objects     =   ForUpdateManager()
56

    
57
    class Meta:
58
        unique_together = (('holder', 'resource'),)
59

    
60

    
61
from datetime import datetime
62

    
63
def now():
64
    return datetime.utcnow().strftime('%Y-%m-%dT%H:%M:%S.%f')[:24]
65

    
66

    
67
class Commission(Model):
68

    
69
    serial      =   AutoField(primary_key=True)
70
    holder      =   CharField(max_length=4096, db_index=True)
71
    name        =   CharField(max_length=4096, null=True)
72
    clientkey   =   CharField(max_length=4096, null=False)
73
    issue_time  =   CharField(max_length=24, default=now)
74

    
75
    objects     =   ForUpdateManager()
76

    
77
class Provision(Model):
78

    
79
    serial      =   ForeignKey( Commission,
80
                                to_field='serial',
81
                                related_name='provisions'   )
82

    
83
    holder      =   CharField(max_length=4096, db_index=True, null=True)
84
    resource    =   CharField(max_length=4096, null=False)
85
    quantity    =   intDecimalField()
86

    
87
    objects     =   ForUpdateManager()
88

    
89
class ProvisionLog(Model):
90

    
91
    serial              =   BigIntegerField()
92
    source              =   CharField(max_length=4096, null=True)
93
    target              =   CharField(max_length=4096)
94
    name                =   CharField(max_length=4096)
95
    issue_time          =   CharField(max_length=4096)
96
    log_time            =   CharField(max_length=4096)
97
    resource            =   CharField(max_length=4096)
98
    source_capacity     =   intDecimalField(null=True)
99
    source_imported_min =   intDecimalField(null=True)
100
    source_imported_max =   intDecimalField(null=True)
101
    source_stock_min    =   intDecimalField(null=True)
102
    source_stock_max    =   intDecimalField(null=True)
103
    target_capacity     =   intDecimalField()
104
    target_imported_min =   intDecimalField()
105
    target_imported_max =   intDecimalField()
106
    target_stock_min    =   intDecimalField()
107
    target_stock_max    =   intDecimalField()
108
    delta_quantity      =   intDecimalField()
109
    reason              =   CharField(max_length=4096)
110

    
111
    objects     =   ForUpdateManager()
112

    
113

    
114
def _get(*args, **kwargs):
115
    model = args[0]
116
    args = args[1:]
117
    o = model.objects
118

    
119
    for_update = kwargs.pop('for_update', False)
120
    f = o.get_for_update if for_update else o.get
121
    return f(*args, **kwargs)
122

    
123

    
124
def _filter(*args, **kwargs):
125
    model = args[0]
126
    args = args[1:]
127
    o = model.objects
128

    
129
    for_update = kwargs.pop('for_update', False)
130
    q = o.filter(*args, **kwargs)
131
    q = q.select_for_update() if for_update else q
132
    return q
133

    
134

    
135
def db_get_holding(*args, **kwargs):
136
    return _get(Holding, *args, **kwargs)
137

    
138
def db_get_commission(*args, **kwargs):
139
    return _get(Commission, *args, **kwargs)
140

    
141
def db_filter_provision(*args, **kwargs):
142
    return _filter(Provision, *args, **kwargs)