Statistics
| Branch: | Tag: | Revision:

root / snf-astakos-app / astakos / quotaholder / models.py @ 003909e3

History | View | Annotate | Download (4.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.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
    source      =   CharField(max_length=4096, null=True)
46
    resource    =   CharField(max_length=4096, null=False)
47

    
48
    limit       =   intDecimalField()
49
    flags       =   BigIntegerField(null=False, default=0)
50

    
51
    imported_min    =   intDecimalField(default=0)
52
    imported_max    =   intDecimalField(default=0)
53

    
54
    objects     =   ForUpdateManager()
55

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

    
59

    
60
from datetime import datetime
61

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

    
65

    
66
class Commission(Model):
67

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

    
73
    objects     =   ForUpdateManager()
74

    
75
class Provision(Model):
76

    
77
    serial      =   ForeignKey( Commission,
78
                                to_field='serial',
79
                                related_name='provisions'   )
80
    holder      =   CharField(max_length=4096, db_index=True)
81
    source      =   CharField(max_length=4096, null=True)
82
    resource    =   CharField(max_length=4096, null=False)
83

    
84
    quantity    =   intDecimalField()
85

    
86
    objects     =   ForUpdateManager()
87

    
88
    def todict(self):
89
        return {'holder':   self.holder,
90
                'source':   self.source,
91
                'resource': self.resource,
92
                'quantity': self.quantity,
93
                }
94

    
95
    def holding_key(self):
96
        return (self.holder, self.source, self.resource)
97

    
98

    
99
class ProvisionLog(Model):
100

    
101
    serial              =   BigIntegerField()
102
    name                =   CharField(max_length=4096, null=True)
103
    issue_time          =   CharField(max_length=4096)
104
    log_time            =   CharField(max_length=4096)
105
    holder              =   CharField(max_length=4096)
106
    source              =   CharField(max_length=4096, null=True)
107
    resource            =   CharField(max_length=4096)
108
    limit               =   intDecimalField()
109
    imported_min        =   intDecimalField()
110
    imported_max        =   intDecimalField()
111
    delta_quantity      =   intDecimalField()
112
    reason              =   CharField(max_length=4096)
113

    
114
    objects     =   ForUpdateManager()
115

    
116

    
117
def _get(*args, **kwargs):
118
    model = args[0]
119
    args = args[1:]
120
    o = model.objects
121

    
122
    for_update = kwargs.pop('for_update', False)
123
    f = o.get_for_update if for_update else o.get
124
    return f(*args, **kwargs)
125

    
126

    
127
def _filter(*args, **kwargs):
128
    model = args[0]
129
    args = args[1:]
130
    o = model.objects
131

    
132
    for_update = kwargs.pop('for_update', False)
133
    q = o.filter(*args, **kwargs)
134
    q = q.select_for_update() if for_update else q
135
    return q
136

    
137

    
138
def db_get_holding(*args, **kwargs):
139
    return _get(Holding, *args, **kwargs)
140

    
141
def db_get_commission(*args, **kwargs):
142
    return _get(Commission, *args, **kwargs)
143

    
144
def db_filter_provision(*args, **kwargs):
145
    return _filter(Provision, *args, **kwargs)